Skip to content

Commit

Permalink
add overloads for withdrawAndcall functions
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Sep 12, 2024
1 parent 27e58e7 commit d05288d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 12 deletions.
81 changes: 77 additions & 4 deletions v2/contracts/zevm/GatewayZEVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ contract GatewayZEVM is
gasFee,
IZRC20(zrc20).PROTOCOL_FLAT_FEE(),
"",
IZRC20(zrc20).GAS_LIMIT(),
CallOptions({ gasLimit: IZRC20(zrc20).GAS_LIMIT(), isArbitraryCall: true }),
revertOptions
);
}
Expand Down Expand Up @@ -188,7 +188,37 @@ contract GatewayZEVM is
gasFee,
IZRC20(zrc20).PROTOCOL_FLAT_FEE(),
message,
gasLimit,
CallOptions({ gasLimit: gasLimit, isArbitraryCall: true }),
revertOptions
);
}

function withdrawAndCall(
bytes memory receiver,
uint256 amount,
address zrc20,
bytes calldata message,
CallOptions calldata callOptions,
RevertOptions calldata revertOptions
)
external
nonReentrant
whenNotPaused
{
if (receiver.length == 0) revert ZeroAddress();
if (amount == 0) revert InsufficientZRC20Amount();

uint256 gasFee = _withdrawZRC20WithGasLimit(amount, zrc20, callOptions.gasLimit);
emit Withdrawn(
msg.sender,
0,
receiver,
zrc20,
amount,
gasFee,
IZRC20(zrc20).PROTOCOL_FLAT_FEE(),
message,
callOptions,
revertOptions
);
}
Expand All @@ -211,7 +241,18 @@ contract GatewayZEVM is
if (amount == 0) revert InsufficientZetaAmount();

_transferZETA(amount, FUNGIBLE_MODULE_ADDRESS);
emit Withdrawn(msg.sender, chainId, receiver, address(zetaToken), amount, 0, 0, "", 0, revertOptions);
emit Withdrawn(
msg.sender,
chainId,
receiver,
address(zetaToken),
amount,
0,
0,
"",
CallOptions({ gasLimit: 0, isArbitraryCall: true }),
revertOptions
);
}

/// @notice Withdraw ZETA tokens and call a smart contract on an external chain.
Expand All @@ -235,7 +276,39 @@ contract GatewayZEVM is
if (amount == 0) revert InsufficientZetaAmount();

_transferZETA(amount, FUNGIBLE_MODULE_ADDRESS);
emit Withdrawn(msg.sender, chainId, receiver, address(zetaToken), amount, 0, 0, message, 0, revertOptions);
emit Withdrawn(
msg.sender,
chainId,
receiver,
address(zetaToken),
amount,
0,
0,
message,
CallOptions({ gasLimit: 0, isArbitraryCall: true }),
revertOptions
);
}

function withdrawAndCall(
bytes memory receiver,
uint256 amount,
uint256 chainId,
bytes calldata message,
CallOptions calldata callOptions,
RevertOptions calldata revertOptions
)
external
nonReentrant
whenNotPaused
{
if (receiver.length == 0) revert ZeroAddress();
if (amount == 0) revert InsufficientZetaAmount();

_transferZETA(amount, FUNGIBLE_MODULE_ADDRESS);
emit Withdrawn(
msg.sender, chainId, receiver, address(zetaToken), amount, 0, 0, message, callOptions, revertOptions
);
}

/// @notice Call a smart contract on an external chain without asset transfer.
Expand Down
4 changes: 2 additions & 2 deletions v2/contracts/zevm/interfaces/IGatewayZEVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface IGatewayZEVMEvents {
/// @param gasfee The gas fee for the withdrawal.
/// @param protocolFlatFee The protocol flat fee for the withdrawal.
/// @param message The calldata passed to the contract call.
/// @param gasLimit Gas limit.
/// @param callOptions Call options including gas limit and arbirtrary call flag.
/// @param revertOptions Revert options.
event Withdrawn(
address indexed sender,
Expand All @@ -43,7 +43,7 @@ interface IGatewayZEVMEvents {
uint256 gasfee,
uint256 protocolFlatFee,
bytes message,
uint256 gasLimit,
CallOptions callOptions,
RevertOptions revertOptions
);
}
Expand Down
11 changes: 9 additions & 2 deletions v2/test/GatewayEVMZEVM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,18 @@ contract GatewayEVMZEVMTest is
expectedGasFee,
zrc20.PROTOCOL_FLAT_FEE(),
message,
1,
CallOptions({ gasLimit: 1, isArbitraryCall: true }),
revertOptions
);
vm.prank(ownerZEVM);
gatewayZEVM.withdrawAndCall(abi.encodePacked(receiverEVM), 500_000, address(zrc20), message, 1, revertOptions);
gatewayZEVM.withdrawAndCall(
abi.encodePacked(receiverEVM),
500_000,
address(zrc20),
message,
CallOptions({ gasLimit: 1, isArbitraryCall: true }),
revertOptions
);

// Check the balance after withdrawal
uint256 balanceOfAfterWithdrawal = zrc20.balanceOf(ownerZEVM);
Expand Down
28 changes: 24 additions & 4 deletions v2/test/GatewayZEVM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ contract GatewayZEVMInboundTest is Test, IGatewayZEVMEvents, IGatewayZEVMErrors
0,
zrc20.PROTOCOL_FLAT_FEE(),
"",
0,
CallOptions({ gasLimit: 0, isArbitraryCall: true }),
revertOptions
);
gateway.withdraw(abi.encodePacked(addr1), amount, address(zrc20), revertOptions);
Expand Down Expand Up @@ -189,7 +189,7 @@ contract GatewayZEVMInboundTest is Test, IGatewayZEVMEvents, IGatewayZEVMErrors
expectedGasFee,
zrc20.PROTOCOL_FLAT_FEE(),
message,
gasLimit,
CallOptions({ gasLimit: gasLimit, isArbitraryCall: true }),
revertOptions
);
gateway.withdrawAndCall(abi.encodePacked(addr1), amount, address(zrc20), message, gasLimit, revertOptions);
Expand Down Expand Up @@ -228,7 +228,18 @@ contract GatewayZEVMInboundTest is Test, IGatewayZEVMEvents, IGatewayZEVMErrors
uint256 chainId = 1;

vm.expectEmit(true, true, true, true, address(gateway));
emit Withdrawn(owner, chainId, abi.encodePacked(addr1), address(zetaToken), amount, 0, 0, "", 0, revertOptions);
emit Withdrawn(
owner,
chainId,
abi.encodePacked(addr1),
address(zetaToken),
amount,
0,
0,
"",
CallOptions({ gasLimit: 0, isArbitraryCall: true }),
revertOptions
);
gateway.withdraw(abi.encodePacked(addr1), amount, chainId, revertOptions);

uint256 ownerBalanceAfter = zetaToken.balanceOf(owner);
Expand Down Expand Up @@ -285,7 +296,16 @@ contract GatewayZEVMInboundTest is Test, IGatewayZEVMEvents, IGatewayZEVMErrors

vm.expectEmit(true, true, true, true, address(gateway));
emit Withdrawn(
owner, chainId, abi.encodePacked(addr1), address(zetaToken), amount, 0, 0, message, 0, revertOptions
owner,
chainId,
abi.encodePacked(addr1),
address(zetaToken),
amount,
0,
0,
message,
CallOptions({ gasLimit: 0, isArbitraryCall: true }),
revertOptions
);
gateway.withdrawAndCall(abi.encodePacked(addr1), amount, chainId, message, revertOptions);

Expand Down

0 comments on commit d05288d

Please sign in to comment.