Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: maintain storage layout compability between ZRC20 v1 and v2 #289

Merged
merged 7 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions v2/src/zevm/GatewayZEVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ contract GatewayZEVM is
/// @param zrc20 The address of the ZRC20 token.
function withdraw(bytes memory receiver, uint256 amount, address zrc20) external nonReentrant whenNotPaused {
uint256 gasFee = _withdrawZRC20(amount, zrc20);
emit Withdrawal(msg.sender, 0, receiver, zrc20, amount, gasFee, IZRC20(zrc20).protocolFlatFee(), "");
emit Withdrawal(msg.sender, 0, receiver, zrc20, amount, gasFee, IZRC20(zrc20).PROTOCOL_FLAT_FEE(), "");
}

/// @notice Withdraw ZRC20 tokens and call a smart contract on an external chain.
Expand All @@ -137,7 +137,7 @@ contract GatewayZEVM is
whenNotPaused
{
uint256 gasFee = _withdrawZRC20(amount, zrc20);
emit Withdrawal(msg.sender, 0, receiver, zrc20, amount, gasFee, IZRC20(zrc20).protocolFlatFee(), message);
emit Withdrawal(msg.sender, 0, receiver, zrc20, amount, gasFee, IZRC20(zrc20).PROTOCOL_FLAT_FEE(), message);
}

/// @notice Withdraw ZETA tokens to an external chain.
Expand Down
40 changes: 23 additions & 17 deletions v2/src/zevm/ZRC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ contract ZRC20 is IZRC20Metadata, ZRC20Errors, ZRC20Events {
/// @notice Coin type, checkout Interfaces.sol.
CoinType public immutable COIN_TYPE;
/// @notice System contract address.
address public systemContractAddress;
/// @notice Gateway contract address.
address public gatewayAddress;
/// @dev Name is in upper case to maintain compatibility with ZRC20.sol v1
address public SYSTEM_CONTRACT_ADDRESS;
Dismissed Show dismissed Hide dismissed
/// @notice Gas limit.
uint256 public gasLimit;
/// @dev Name is in upper case to maintain compatibility with ZRC20.sol v1
uint256 public GAS_LIMIT;
Dismissed Show dismissed Hide dismissed
/// @notice Protocol flat fee.
uint256 public override protocolFlatFee;
/// @dev Name is in upper case to maintain compatibility with ZRC20.sol v1
uint256 public override PROTOCOL_FLAT_FEE;
Dismissed Show dismissed Hide dismissed

mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
Expand All @@ -44,6 +45,10 @@ contract ZRC20 is IZRC20Metadata, ZRC20Errors, ZRC20Events {
string private _symbol;
uint8 private _decimals;

/// @notice Gateway contract address.
/// @dev This variable is added at last position to maintain storage layout with ZRC20.sol v1
address public gatewayAddress;

function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
Expand Down Expand Up @@ -79,8 +84,8 @@ contract ZRC20 is IZRC20Metadata, ZRC20Errors, ZRC20Events {
_decimals = decimals_;
CHAIN_ID = chainid_;
COIN_TYPE = coinType_;
gasLimit = gasLimit_;
systemContractAddress = systemContractAddress_;
GAS_LIMIT = gasLimit_;
SYSTEM_CONTRACT_ADDRESS = systemContractAddress_;
gatewayAddress = gatewayAddress_;
}

Expand Down Expand Up @@ -232,7 +237,8 @@ contract ZRC20 is IZRC20Metadata, ZRC20Errors, ZRC20Events {
*/
function deposit(address to, uint256 amount) external override returns (bool) {
if (
msg.sender != FUNGIBLE_MODULE_ADDRESS && msg.sender != systemContractAddress && msg.sender != gatewayAddress
msg.sender != FUNGIBLE_MODULE_ADDRESS && msg.sender != SYSTEM_CONTRACT_ADDRESS
&& msg.sender != gatewayAddress
) revert InvalidSender();
_mint(to, amount);
emit Deposit(abi.encodePacked(FUNGIBLE_MODULE_ADDRESS), to, amount);
Expand All @@ -245,14 +251,14 @@ contract ZRC20 is IZRC20Metadata, ZRC20Errors, ZRC20Events {
* withdraw()
*/
function withdrawGasFee() public view override returns (address, uint256) {
address gasZRC20 = ISystem(systemContractAddress).gasCoinZRC20ByChainId(CHAIN_ID);
address gasZRC20 = ISystem(SYSTEM_CONTRACT_ADDRESS).gasCoinZRC20ByChainId(CHAIN_ID);
if (gasZRC20 == address(0)) revert ZeroGasCoin();

uint256 gasPrice = ISystem(systemContractAddress).gasPriceByChainId(CHAIN_ID);
uint256 gasPrice = ISystem(SYSTEM_CONTRACT_ADDRESS).gasPriceByChainId(CHAIN_ID);
if (gasPrice == 0) {
revert ZeroGasPrice();
}
uint256 gasFee = gasPrice * gasLimit + protocolFlatFee;
uint256 gasFee = gasPrice * GAS_LIMIT + PROTOCOL_FLAT_FEE;
return (gasZRC20, gasFee);
}

Expand All @@ -270,7 +276,7 @@ contract ZRC20 is IZRC20Metadata, ZRC20Errors, ZRC20Events {
revert GasFeeTransferFailed();
}
_burn(msg.sender, amount);
emit Withdrawal(msg.sender, to, amount, gasFee, protocolFlatFee);
emit Withdrawal(msg.sender, to, amount, gasFee, PROTOCOL_FLAT_FEE);
return true;
}

Expand All @@ -279,7 +285,7 @@ contract ZRC20 is IZRC20Metadata, ZRC20Errors, ZRC20Events {
* @param addr, new system contract address.
*/
function updateSystemContractAddress(address addr) external onlyFungible {
systemContractAddress = addr;
SYSTEM_CONTRACT_ADDRESS = addr;
emit UpdatedSystemContract(addr);
}

Expand All @@ -297,16 +303,16 @@ contract ZRC20 is IZRC20Metadata, ZRC20Errors, ZRC20Events {
* @param gasLimit_, new gas limit.
*/
function updateGasLimit(uint256 gasLimit_) external onlyFungible {
gasLimit = gasLimit_;
emit UpdatedGasLimit(gasLimit);
GAS_LIMIT = gasLimit_;
emit UpdatedGasLimit(GAS_LIMIT);
}

/**
* @dev Updates protocol flat fee. Can only be updated by the fungible module.
* @param protocolFlatFee_, new protocol flat fee.
*/
function updateProtocolFlatFee(uint256 protocolFlatFee_) external onlyFungible {
protocolFlatFee = protocolFlatFee_;
emit UpdatedProtocolFlatFee(protocolFlatFee);
PROTOCOL_FLAT_FEE = protocolFlatFee_;
emit UpdatedProtocolFlatFee(PROTOCOL_FLAT_FEE);
}
}
3 changes: 2 additions & 1 deletion v2/src/zevm/interfaces/IZRC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ interface IZRC20 {

function withdrawGasFee() external view returns (address, uint256);

function protocolFlatFee() external view returns (uint256);
/// @dev Name is in upper case to maintain compatibility with ZRC20.sol v1
function PROTOCOL_FLAT_FEE() external view returns (uint256);
Dismissed Show dismissed Hide dismissed
}

/// @title IZRC20Metadata
Expand Down
9 changes: 8 additions & 1 deletion v2/test/GatewayEVMZEVM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,14 @@ contract GatewayEVMZEVMTest is
bytes memory message = abi.encodeWithSelector(receiverEVM.receivePayable.selector, str, num, flag);
vm.expectEmit(true, true, true, true, address(gatewayZEVM));
emit Withdrawal(
ownerZEVM, 0, abi.encodePacked(receiverEVM), address(zrc20), 1_000_000, 0, zrc20.protocolFlatFee(), message
ownerZEVM,
0,
abi.encodePacked(receiverEVM),
address(zrc20),
1_000_000,
0,
zrc20.PROTOCOL_FLAT_FEE(),
message
);
vm.prank(ownerZEVM);
gatewayZEVM.withdrawAndCall(abi.encodePacked(receiverEVM), 1_000_000, address(zrc20), message);
Expand Down
6 changes: 4 additions & 2 deletions v2/test/GatewayZEVM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ contract GatewayZEVMInboundTest is Test, IGatewayZEVMEvents, IGatewayZEVMErrors
uint256 ownerBalanceBefore = zrc20.balanceOf(owner);

vm.expectEmit(true, true, true, true, address(gateway));
emit Withdrawal(owner, 0, abi.encodePacked(addr1), address(zrc20), amount, 0, zrc20.protocolFlatFee(), "");
emit Withdrawal(owner, 0, abi.encodePacked(addr1), address(zrc20), amount, 0, zrc20.PROTOCOL_FLAT_FEE(), "");
gateway.withdraw(abi.encodePacked(addr1), amount, address(zrc20));

uint256 ownerBalanceAfter = zrc20.balanceOf(owner);
Expand Down Expand Up @@ -112,7 +112,9 @@ contract GatewayZEVMInboundTest is Test, IGatewayZEVMEvents, IGatewayZEVMErrors

bytes memory message = abi.encodeWithSignature("hello(address)", addr1);
vm.expectEmit(true, true, true, true, address(gateway));
emit Withdrawal(owner, 0, abi.encodePacked(addr1), address(zrc20), amount, 0, zrc20.protocolFlatFee(), message);
emit Withdrawal(
owner, 0, abi.encodePacked(addr1), address(zrc20), amount, 0, zrc20.PROTOCOL_FLAT_FEE(), message
);
gateway.withdrawAndCall(abi.encodePacked(addr1), amount, address(zrc20), message);

uint256 ownerBalanceAfter = zrc20.balanceOf(owner);
Expand Down
6 changes: 3 additions & 3 deletions v2/test/ZRC20.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ contract ZRC20Test is Test, ZRC20Errors {
function testUpdateSystemContractAddress() public {
vm.prank(fungibleModule);
zrc20.updateSystemContractAddress(address(0x3211));
assertEq(zrc20.systemContractAddress(), address(0x3211));
assertEq(zrc20.SYSTEM_CONTRACT_ADDRESS(), address(0x3211));
}

function testUpdateSystemContractAddressFailsIfSenderIsNotFungible() public {
Expand All @@ -230,7 +230,7 @@ contract ZRC20Test is Test, ZRC20Errors {
function testUpdateGasLimit() public {
vm.prank(fungibleModule);
zrc20.updateGasLimit(10);
assertEq(10, zrc20.gasLimit());
assertEq(10, zrc20.GAS_LIMIT());
}

function testUpdateGasLimitFailsIfSenderIsNotFungible() public {
Expand All @@ -241,7 +241,7 @@ contract ZRC20Test is Test, ZRC20Errors {
function testUpdateProtocolFlatFee() public {
vm.prank(fungibleModule);
zrc20.updateProtocolFlatFee(10);
assertEq(10, zrc20.protocolFlatFee());
assertEq(10, zrc20.PROTOCOL_FLAT_FEE());
}

function testUpdateProtocolFlatFeeFailsIfSenderIsNotFungible() public {
Expand Down
Loading