Interact with ERC-20 tokens

ERC-20 is a simple token standard and the most common contract type on Ethereum.

You can:

ERC-20 token functions and events

An ERC-20 token must implement the following functions:

  • totalSupply() Returns the total token supply.

  • balanceOf(owner) Returns the account balance of another account with address owner.

  • allowance(owner, spender) Returns the amount which spender is still allowed to withdraw from owner.

  • transfer(to, value) Transfers value amount of tokens to address to.

  • approve(spender, value) Allows spender to withdraw from your account multiple times, up to the value amount.

  • transferFrom(from, to, value) Transfers value amount of tokens from address from to address to.

At certain times, an ERC-20 token also must emit the following events:

  • Transfer(from, to, value) Must trigger when tokens are transferred, including zero value transfers.

  • Approval(owner, spender, value) Must trigger on any successful call to approve(spender, value).

View EIP-20 for more details about how these functions work and when to emit these events.

Send transactions

Use eth_sendRawTransaction to send ERC-20 token transactions. This JSON-RPC method expects a data field format that requires normalization of the Transfer function to a short function selector.

To do this, set the parameters for the function and run it through Ethereum’s sha3 keccak hash:

To use cast you must first install Foundry.

cast sig "Transfer(address,uint256)"

The above examples produce the following function selector:

0x69ca02dd

Next, the transaction data field should be constructed as follows:

  • Start with the function selector: 0xa9059cbb

  • Append the recipient's address (padded to 32 bytes)

  • Append the token amount (padded to 32 bytes)

For example:

0xa9059cbb000000000000000000000001234567890123456789012345678901234567890000000000000000000000000000000000000000000000000de0b6b3a7640000

Where:

  • 0xa9059cbb: Function selector

  • 000000000000000000000001234567890123456789012345678901234567890: Recipient address (padded)

  • 0000000000000000000000000000000000000000000000000de0b6b3a7640000: Token amount (padded, in this example, 1 token with 18 decimals)

With the data field value, you can now create a transaction object with the following fields:

  • to: The ERC-20 token contract address

  • value: 0 (since we're not sending ETH)

  • data: The prepared data string from above

  • gasPrice: Current gas price (in wei)

  • gasLimit: Estimated gas limit for the transaction

  • nonce: The sender's current transaction count

Finally, you can use your private key to sign the transaction object. This step to do that depend on the library you're using (e.g. Ethers.js, Web3.js).

Now, you can send your signed transaction using eth_sendRawTransaction:

curl https://mainnet.infura.io/v3/<YOUR-API-KEY> \
  -X POST \
  -H "Content-Type: application/json" \
  -d '
{
  "jsonrpc": "2.0",
  "method": "eth_sendRawTransaction",
  "params": [
    "0xf901968080830493e08080b901456080604052348015600e575f5ffd5b506101298061001c5f395ff3fe6080604052348015600e575f5ffd5b50600436106030575f3560e01c806360fe47b11460345780636d4ce63c14604c575b5f5ffd5b604a60048036038101906046919060a9565b6066565b005b6052606f565b604051605d919060dc565b60405180910390f35b805f8190555050565b5f5f54905090565b5f5ffd5b5f819050919050565b608b81607b565b81146094575f5ffd5b50565b5f8135905060a3816084565b92915050565b5f6020828403121560bb5760ba6077565b5b5f60c6848285016097565b91505092915050565b60d681607b565b82525050565b5f60208201905060ed5f83018460cf565b9291505056fea2646970667358221220c9d450006b906ff115167c8c7bba9154cc401a7b54810387f4dd053155e2b19664736f6c634300081c003383255a45a092b2aa5f92eb91535430e4e6063d8b5b85e09f108cb176f6734761e56f461e7ca0150910a8aa56e4670c2f116b6f189d1dac3d055ce2075c9fbb3d0d7c6e09bb33"
  ],
  "id": 1
}'

Observe logs of processed transactions

Last updated

Was this helpful?