Skip to content

Commit

Permalink
token: return to sender
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Nov 18, 2024
1 parent 13f3299 commit 75438c3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
6 changes: 3 additions & 3 deletions examples/nft/contracts/Connected.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ contract Connected is
address receiver,
uint256 tokenId,
string memory uri,
uint256 amount,
uint256 gasAmount,
address sender

Check notice

Code scanning / Slither

Missing zero address validation Low

) = abi.decode(message, (address, uint256, string, uint256, address));

_safeMint(receiver, tokenId);
_setTokenURI(tokenId, uri);
if (amount > 0) {
(bool success, ) = sender.call{value: amount}("");
if (gasAmount > 0) {
(bool success, ) = sender.call{value: gasAmount}("");
if (!success) revert GasTokenTransferFailed();
}
emit TokenTransferReceived(receiver, tokenId, uri);
Expand Down
16 changes: 8 additions & 8 deletions examples/nft/contracts/Universal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ contract Universal is
!IZRC20(destination).transferFrom(msg.sender, address(this), gasFee)
) revert TransferFailed();
IZRC20(destination).approve(address(gateway), gasFee);
bytes memory message = abi.encode(receiver, tokenId, uri, 0);
bytes memory message = abi.encode(
receiver,
tokenId,
uri,
0,
msg.sender
);

CallOptions memory callOptions = CallOptions(gasLimit, false);

Expand Down Expand Up @@ -125,7 +131,6 @@ contract Universal is
uint256 amount,
bytes calldata message
) external override onlyGateway {
revert("Method not implemented");
if (context.sender != counterparty[zrc20]) revert Unauthorized();

(
Expand Down Expand Up @@ -162,13 +167,8 @@ contract Universal is
CallOptions(gasLimit, false),
RevertOptions(address(0), false, address(0), "", 0)
);
emit TokenTransferToDestination(
receiver,
destination,
tokenId,
uri
);
}
emit TokenTransferToDestination(receiver, destination, tokenId, uri);
}

function onRevert(RevertContext calldata context) external onlyGateway {
Expand Down
32 changes: 22 additions & 10 deletions examples/token/contracts/Connected.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ contract Connected is ERC20, Ownable2Step, Events {
error InvalidAddress();
error Unauthorized();
error InvalidGasLimit();
error GasTokenTransferFailed();

modifier onlyGateway() {
if (msg.sender != address(gateway)) revert Unauthorized();
Expand Down Expand Up @@ -51,7 +52,12 @@ contract Connected is ERC20, Ownable2Step, Events {
if (receiver == address(0)) revert InvalidAddress();
_burn(msg.sender, amount);

bytes memory message = abi.encode(destination, receiver, amount);
bytes memory message = abi.encode(
destination,
receiver,
amount,
msg.sender
);
if (destination == address(0)) {
gateway.call(
counterparty,
Expand All @@ -66,7 +72,7 @@ contract Connected is ERC20, Ownable2Step, Events {
address(this),
true,
address(0),
abi.encode(receiver, amount),
abi.encode(amount, msg.sender),
gasLimit
)
);
Expand All @@ -80,22 +86,28 @@ contract Connected is ERC20, Ownable2Step, Events {
bytes calldata message
) external payable onlyGateway returns (bytes4) {
if (context.sender != counterparty) revert Unauthorized();
(address receiver, uint256 amount) = abi.decode(
message,
(address, uint256)
);
(
address receiver,
uint256 amount,
uint256 gasAmount,
address sender

Check notice

Code scanning / Slither

Missing zero address validation Low

) = abi.decode(message, (address, uint256, uint256, address));
_mint(receiver, amount);
if (gasAmount > 0) {
(bool success, ) = sender.call{value: amount}("");
if (!success) revert GasTokenTransferFailed();
}
emit TokenTransferReceived(receiver, amount);
return "";
}

function onRevert(RevertContext calldata context) external onlyGateway {
(address sender, uint256 amount) = abi.decode(
(uint256 amount, address receiver) = abi.decode(
context.revertMessage,
(address, uint256)
(uint256, address)
);
_mint(sender, amount);
emit TokenTransferReverted(sender, amount);
_mint(receiver, amount);
emit TokenTransferReverted(receiver, amount);
}

receive() external payable {}
Expand Down
21 changes: 13 additions & 8 deletions examples/token/contracts/Universal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ contract Universal is ERC20, Ownable2Step, UniversalContract, Events {
!IZRC20(destination).transferFrom(msg.sender, address(this), gasFee)
) revert TransferFailed();
IZRC20(destination).approve(address(gateway), gasFee);
bytes memory message = abi.encode(receiver, amount);
bytes memory message = abi.encode(receiver, amount, 0, msg.sender);

CallOptions memory callOptions = CallOptions(gasLimit, false);

Expand Down Expand Up @@ -103,26 +103,31 @@ contract Universal is ERC20, Ownable2Step, UniversalContract, Events {
) external override onlyGateway {
if (keccak256(context.origin) != keccak256(counterparty[zrc20]))
revert Unauthorized();
(address destination, address receiver, uint256 tokenAmount) = abi
.decode(message, (address, address, uint256));
(
address destination,
address receiver,
uint256 tokenAmount,
address sender
) = abi.decode(message, (address, address, uint256, address));
if (destination == address(0)) {
_mint(receiver, tokenAmount);
} else {
(, uint256 gasFee) = IZRC20(destination).withdrawGasFeeWithGasLimit(
gasLimit
);
SwapHelperLib.swapExactTokensForTokens(
uint256 out = SwapHelperLib.swapExactTokensForTokens(
uniswapRouter,
zrc20,
amount,
destination,
0
);
IZRC20(destination).approve(address(gateway), gasFee);
gateway.call(
counterparty[destination],
IZRC20(destination).approve(address(gateway), out);
gateway.withdrawAndCall(
abi.encodePacked(counterparty[destination]),
out - gasFee,
destination,
abi.encode(receiver, tokenAmount),
abi.encode(receiver, tokenAmount, out - gasFee, sender),
CallOptions(gasLimit, false),
RevertOptions(address(0), false, address(0), "", 0)
);
Expand Down

0 comments on commit 75438c3

Please sign in to comment.