Skip to content

Commit

Permalink
nft: withdrawAndCall
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Nov 18, 2024
1 parent 757c438 commit 0cbaa24
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
15 changes: 11 additions & 4 deletions examples/nft/contracts/Connected.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ contract Connected is
error InvalidAddress();
error Unauthorized();
error InvalidGasLimit();
error GasTokenTransferFailed();

function setCounterparty(address contractAddress) external onlyOwner {
if (contractAddress == address(0)) revert InvalidAddress();
Expand Down Expand Up @@ -104,13 +105,19 @@ contract Connected is
) external payable onlyGateway returns (bytes4) {
if (context.sender != counterparty) revert Unauthorized();

(address receiver, uint256 tokenId, string memory uri) = abi.decode(
message,
(address, uint256, string)
);
(
address receiver,

Check notice

Code scanning / Slither

Missing zero address validation Low

uint256 tokenId,
string memory uri,
uint256 amount
) = abi.decode(message, (address, uint256, string, uint256));

_safeMint(receiver, tokenId);
_setTokenURI(tokenId, uri);
if (amount > 0) {
(bool success, ) = receiver.call{value: amount}("");
if (!success) revert GasTokenTransferFailed();
}
emit TokenTransferReceived(receiver, tokenId, uri);
return "";
}
Expand Down
11 changes: 6 additions & 5 deletions examples/nft/contracts/Universal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ 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);
bytes memory message = abi.encode(receiver, tokenId, uri, 0);

CallOptions memory callOptions = CallOptions(gasLimit, false);

Expand Down Expand Up @@ -143,19 +143,20 @@ contract Universal is
gasLimit
);

SwapHelperLib.swapExactTokensForTokens(
uint256 out = SwapHelperLib.swapExactTokensForTokens(
uniswapRouter,
zrc20,
amount,
destination,
0
);

IZRC20(destination).approve(address(gateway), gasFee);
gateway.call(
IZRC20(destination).approve(address(gateway), out);
gateway.withdrawAndCall(
abi.encodePacked(counterparty[destination]),
out - gasFee,
destination,
abi.encode(receiver, tokenId, uri),
abi.encode(receiver, tokenId, uri, out - gasFee),
CallOptions(gasLimit, false),
RevertOptions(address(0), false, address(0), "", 0)
);
Expand Down

0 comments on commit 0cbaa24

Please sign in to comment.