Skip to content

Commit

Permalink
nft: return extra tokens to sender, handle a case where receiver is n…
Browse files Browse the repository at this point in the history
…ot the same as sender
  • Loading branch information
fadeev committed Nov 18, 2024
1 parent 0cbaa24 commit 13f3299
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
21 changes: 14 additions & 7 deletions examples/nft/contracts/Connected.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ contract Connected is

string memory uri = tokenURI(tokenId);
_burn(tokenId);
bytes memory message = abi.encode(destination, receiver, tokenId, uri);
bytes memory message = abi.encode(
destination,
receiver,
tokenId,
uri,
msg.sender
);
if (destination == address(0)) {
gateway.call(
counterparty,
Expand All @@ -90,7 +96,7 @@ contract Connected is
address(this),
true,
address(0),
abi.encode(receiver, tokenId, uri),
abi.encode(receiver, tokenId, uri, msg.sender),
gasLimit
)
);
Expand All @@ -109,23 +115,24 @@ contract Connected is
address receiver,
uint256 tokenId,
string memory uri,
uint256 amount
) = abi.decode(message, (address, uint256, string, uint256));
uint256 amount,
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, ) = receiver.call{value: amount}("");
(bool success, ) = sender.call{value: amount}("");
if (!success) revert GasTokenTransferFailed();
}
emit TokenTransferReceived(receiver, tokenId, uri);
return "";
}

function onRevert(RevertContext calldata context) external onlyGateway {
(address sender, uint256 tokenId, string memory uri) = abi.decode(
(, uint256 tokenId, string memory uri, address sender) = abi.decode(
context.revertMessage,
(address, uint256, string)
(address, uint256, string, address)
);

_safeMint(sender, tokenId);
Expand Down
8 changes: 5 additions & 3 deletions examples/nft/contracts/Universal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,16 @@ contract Universal is
uint256 amount,
bytes calldata message
) external override onlyGateway {
revert("Method not implemented");
if (context.sender != counterparty[zrc20]) revert Unauthorized();

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

if (destination == address(0)) {
_safeMint(receiver, tokenId);
Expand All @@ -156,7 +158,7 @@ contract Universal is
abi.encodePacked(counterparty[destination]),
out - gasFee,
destination,
abi.encode(receiver, tokenId, uri, out - gasFee),
abi.encode(receiver, tokenId, uri, out - gasFee, sender),
CallOptions(gasLimit, false),
RevertOptions(address(0), false, address(0), "", 0)
);
Expand Down

0 comments on commit 13f3299

Please sign in to comment.