Simulate Transfer Erc20
simulateTransferErc20 in the Tatum SDK Transaction Simulator previews an ERC-20 transfer's gas, balance changes and token transfers without sending it.
The simulateTransferErc20 function in the Transaction Simulator extension simulates an ERC-20 token transfer on EVM-based blockchains. It returns the transaction details, the status, the native balance change of the sender (gas) and the token balance changes, so you can test a token transfer before you broadcast it. This guide shows how to call simulateTransferErc20 from the Tatum SDK.
How to Simulate an ERC20 Transfer
Install @tatumio/transaction-simulator next to @tatumio/tatum, register TransactionSimulator as an extension, and call simulateTransferErc20 through tatumSdk.extension(TransactionSimulator). The simulator traces the transaction with debug_traceCall, which the RPC Gateway serves on paid plans only. See Limits and Method Availability.
// Import necessary libraries
import { TatumSDK, Network } from '@tatumio/tatum';
import { TransactionSimulator } from '@tatumio/transaction-simulator';
// Initialize the SDK for a specific blockchain network
const tatumSdk = await TatumSDK.init({
network: Network.ETHEREUM,
apiKey: '{YOUR_API_KEY}',
configureExtensions: [
TransactionSimulator,
]
})
// Define your payload
const tokenTransferPayload = {
to: '0xaf758da9f7bdaa7590175193388e9c99427cc2d2',
from: '0xDce92f40cAdDE2C4e3EA78b8892c540e6bFe2f81',
gas: '0xB411', // optional
gasPrice: '0x4B16C370A', // optional
value: 420, // token amount to send, in whole tokens (420 USDT)
tokenContractAddress: '0xdac17f958d2ee523a2206206994597c13d831ec7' // USDT
};
// Call simulateTransferErc20
const tokenSimulationResult = await tatumSdk.extension(TransactionSimulator)
.simulateTransferErc20(tokenTransferPayload);
// Check simulation results
console.log(tokenSimulationResult);
await tatumSdk.destroy();// Import necessary libraries
import { TatumSDK, Network, Ethereum } from '@tatumio/tatum';
import { TransactionSimulator, TokenTransfer } from '@tatumio/transaction-simulator';
// Initialize the SDK for a specific blockchain network
const tatumSdk = await TatumSDK.init<Ethereum>({
network: Network.ETHEREUM,
apiKey: '{YOUR_API_KEY}',
configureExtensions: [
TransactionSimulator,
]
})
// Define your payload
const tokenTransferPayload: TokenTransfer = {
to: '0xaf758da9f7bdaa7590175193388e9c99427cc2d2',
from: '0xDce92f40cAdDE2C4e3EA78b8892c540e6bFe2f81',
gas: '0xB411', // optional
gasPrice: '0x4B16C370A', // optional
value: 420, // token amount to send, in whole tokens (420 USDT)
tokenContractAddress: '0xdac17f958d2ee523a2206206994597c13d831ec7' // USDT
};
// Call simulateTransferErc20
const tokenSimulationResult = await tatumSdk.extension(TransactionSimulator)
.simulateTransferErc20(tokenTransferPayload);
// Check simulation results
console.log(tokenSimulationResult);
await tatumSdk.destroy();Parameters
to: Address of the recipient.from: Address of the sender.gas: (Optional) Gas limit for the transaction, as a hex string. If you omit it, the simulator estimates it witheth_estimateGas.gasPrice: (Optional) Gas price for the transaction, as a hex string. If you omit it, the simulator useseth_gasPrice.value: Amount of tokens to send, in whole token units (for example420for 420 USDT). The simulator multiplies it by the token's decimals.tokenContractAddress: Address of the ERC-20 token contract.
Expected Response
{
"transactionDetails": {
"from": "0xDce92f40cAdDE2C4e3EA78b8892c540e6bFe2f81",
"to": "0xaf758da9f7bdaa7590175193388e9c99427cc2d2",
"tokenContractAddress": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"data": "0xa9059cbb000000000000000000000000af758da9f7bdaa7590175193388e9c99427cc2d2000000000000000000000000000000000000000000000000000000001908b100",
"gasLimit": 46097,
"gasPrice": 20156528394
},
"status": "success",
"balanceChanges": {
"0xDce92f40cAdDE2C4e3EA78b8892c540e6bFe2f81": {
"from": 243656557299636170000,
"to": 243655628144146780000
}
},
"tokenTransfers": {
"0xdac17f958d2ee523a2206206994597c13d831ec7": {
"name": "TetherUSD",
"symbol": "USDT",
"decimals": 6,
"0xDce92f40cAdDE2C4e3EA78b8892c540e6bFe2f81": {
"from": 2387468.080258,
"to": 2387048.080258
},
"0xaf758da9f7bdaa7590175193388e9c99427cc2d2": {
"from": 422.304,
"to": 842.304
}
}
}
}Use Cases
- Development and Testing: Check token transfers from your app before you send them, without broadcasting a transaction.
- Fee estimation: Read the gas limit and gas price that the token transfer needs.
- Error handling: Catch a token transfer that would revert before you sign it: the method throws an error with the revert message.
Summary
The simulateTransferErc20 function in the Transaction Simulator extension simulates an ERC-20 token transfer on EVM-based blockchains and returns its gas details, status, and native and token balance changes. For native currency transfers, use simulateTransfer.
Updated 3 days ago