Skip to content

Commit

Permalink
fix: transfer and call
Browse files Browse the repository at this point in the history
  • Loading branch information
IaroslavMazur committed Jul 7, 2024
1 parent 0d5625e commit 060666f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
6 changes: 2 additions & 4 deletions src/precompiles/native-tokens/INativeTokens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ interface INativeTokens {
function mint(uint256 subID, address recipient, uint256 amount) external;
function transfer(address to, uint256 tokenID, uint256 amount) external;
function transferAndCall(
address to,
address recipientAndCallee,
uint256 tokenID,
uint256 amount,
address callee,
bytes calldata data
)
external;
Expand All @@ -24,10 +23,9 @@ interface INativeTokens {
)
external;
function transferMultipleAndCall(
address to,
address recipientAndCallee,
uint256[] calldata tokenIDs,
uint256[] calldata amounts,
address callee,
bytes calldata data
)
external;
Expand Down
16 changes: 6 additions & 10 deletions src/precompiles/native-tokens/NativeTokens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,20 @@ library NativeTokens {
/// Requirements:
/// - The calling contract must have at least `amount` tokens.
///
/// @param to The address of the recipient.
/// @param recipientAndCallee The address of the contract recipient of the tokens which will, also, be called.
/// @param tokenID The sub-identifier of the native token to transfer.
/// @param amount The quantity of native tokens to transfer.
/// @param callee The address of the contract to call after the transfer.
/// @param data The call data to pass to the `callee`.
function transferAndCall(
address to,
address recipientAndCallee,
uint256 tokenID,
uint256 amount,
address callee,
bytes calldata data
)
internal
{
// ABI encode the input parameters.
bytes memory callData = abi.encodeCall(INativeTokens.transferAndCall, (to, tokenID, amount, callee, data));
bytes memory callData = abi.encodeCall(INativeTokens.transferAndCall, (recipientAndCallee, tokenID, amount, data));

// Call the precompile, ignoring the response because the VM will panic if there's an issue.
(bool response,) = PRECOMPILE_NATIVE_TOKENS.delegatecall(callData);
Expand Down Expand Up @@ -154,23 +152,21 @@ library NativeTokens {
/// Requirements:
/// - The calling contract must have at least `amounts[i]` tokens for each token ID `tokenIDs[i]`.
///
/// @param to The address of the recipient.
/// @param recipientAndCallee The address of the contract recipient of the tokens which will, also, be called.
/// @param tokenIDs The IDs of the native tokens to transfer.
/// @param amounts The quantities of native tokens to transfer.
/// @param callee The address of the contract to call after the transfer.
/// @param data The call data to pass to the `callee`.
function transferMultipleAndCall(
address to,
address recipientAndCallee,
uint256[] calldata tokenIDs,
uint256[] calldata amounts,
address callee,
bytes calldata data
)
internal
{
// ABI encode the input parameters.
bytes memory callData =
abi.encodeCall(INativeTokens.transferMultipleAndCall, (to, tokenIDs, amounts, callee, data));
abi.encodeCall(INativeTokens.transferMultipleAndCall, (recipientAndCallee, tokenIDs, amounts, data));

// Call the precompile, ignoring the response because the VM will panic if there's an issue.
(bool response,) = PRECOMPILE_NATIVE_TOKENS.delegatecall(callData);
Expand Down
4 changes: 4 additions & 0 deletions src/standards/srf-20/NaiveTokenTransferrerMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ contract NaiveTokenTransferrerMock {
to.transfer(tokenID, amount);
}

function transferAndCall(address recipientAndCallee, uint256 tokenID, uint256 amount, bytes calldata data) {
recipientAndCallee.transferAndCall(tokenID, amount, data);
}

function transferMultiple(address to, uint256[] calldata tokenIDs, uint256[] calldata amounts) external {
to.transferMultiple(tokenIDs, amounts);
}
Expand Down

0 comments on commit 060666f

Please sign in to comment.