How to Send ERC20 Tokens in an EIP-1559 Transaction
Learn how to send an EIP-1559 transaction that transfers ERC20 tokens.
Don’t have an API key?
Sign up to get access. Get started for free
This tutorial uses the getFeeData and getTransactionCount endpoints.
Introduction
The London Hardfork introduced a new EIP called EIP-1559 that modifies how gas estimation and costs work for transactions on Ethereum.
In this tutorial, we will show you how to send ERC20 tokens in an EIP-1559 transaction using the Alchemy SDK. We will use the USDC token as an example, but the same principle applies to any ERC20 token. To learn more about EIP-1559, check out this blog post.
Prerequisites
- You should know about EIP-1559.
- You should have basic knowledge of Javascript & Node.js.
- Ideally, you should have gone through our tutorial on sending transactions with EIP-1559 but it is not required if you already know about EIP-1559.
Developer Environment Setup
Step 1: Install Node and NPM
In case you haven’t already, install node and npm on your local machine.
Make sure that node is at least v14 or higher by typing the following in your terminal:
Step 2: Create an Alchemy app
In case you haven’t already, sign up for a free Alchemy account.
Alchemy’s account dashboard where developers can create a new app on the Ethereum blockchain.
Next, navigate to the Alchemy Dashboard and create a new app. Make sure you set the chain to Ethereum and the network to Sepolia. Once the app is created, click on your app’s View Key button on the dashboard. Take note of the API KEY.
You will need this later.
Step 3: Get Sepolia ETH and Sepolia USDC
In order to send USDC on the Sepolia testnet, we need to have some Sepolia USDC (to send) and Sepolia ETH (to pay for gas).
You can get Sepolia ETH from Alchemy’s Sepolia Faucet and Sepolia USDC from USDC faucet (Make sure to select ETH as your network in the USDC faucet).
Step 4: Create a node project
Let’s now create an empty repository and install all the node dependencies.
This will create a repository named send-erc20-eip1559
that holds all your files and dependencies. Next, open this repo in your favorite code editor. We will set our environment variables in .env
file and write our code in main.js
file.
Step 5: Set up the environment variables
In your .env
file, add the API key you got from #Step 2 and your wallet’s Private key.
Make sure you add the private key of the wallet with Sepolia ETH and Sepolia USDC.
Writing the Main Script
Add the following code to the main.js
file. Here’s an overview of what the code is doing (also explained in the comments below):
- Importing the necessary modules from the Alchemy SDK and the dotenv package.
- Reading the API key and private key from the .env file to use in the script.
- Configuring the Alchemy SDK using the imported API key and network (Sepolia).
- Creating an instance of the Alchemy SDK. This will be used to call
getFeeData
andgetTransactionCount
methods. - Creating a wallet instance to send the transaction using the private key.
- Adding the address you want to send the tokens to.
- Defining the USDC contract address on Sepolia testnet.
- Using
getFeeData
method of Alchemy SDK to get the fee data (maxFeePerGas & maxPriorityFeePerGas) that will be used in the transaction object (These fields are used in an EIP-1559 transaction instead ofgasPrice
). - Defining the ABI for the transfer function of ERC20 token.
- Defining the amount of tokens to send and the decimals of the token. We want to send 2 USDC in this case and the USDC token has 6 decimals. You can find out how many decimal places any currency has by reading the
decimals
value from the contract’s page on Etherscan. - Converting the amount to send to decimals. This step is important because while encoding the transaction data, we are using the
wei
units to specify the number of tokens to send, which means we need to present the amount to send in its smallest units (i.e. decimals).
Solidity does not support decimal point numbers. So, If you want an ERC20 token to have the ability to be subdivided with a precision of 2 decimal places, you need to represent 1 token as 100 (set its decimal places to 2).
Similarly, if you want an ERC20 token to have the ability to be subdivided with a precision of 18 decimal places, you need to represent 1 token as 1000000000000000000 (set its decimal places to 18).
So, the formula for the amount to send in decimals becomes:
amountToSendInDecimals = amountToSend * (10 ** decimals)
-
Encoding the data for the transaction by creating an interface object from the ABI and specifying the function to call, the parameters and the contract to interact with.
-
Creating the EIP-1559 transaction object by defining the transaction properties such as:
nonce
: getting it using thegetTransactionCount
method.maxPriorityFeePerGas
: getting it from thefeeData
object.maxFeePerGas
: getting it from thefeeData
object.type
: setting it to2
depicts an EIP-1559 transaction.chainId
: 11155111, corresponds to Sepolia’s testnet’s chain ID.data
: data that we encoded using ABI interface, this tells the transaction what to do.gasLimit
: gas limit for the transaction.
-
Sending the transaction using the created transaction object and logging it.
Testing the Script
Run the script using the following command:
You should see an output containing details about the sent transaction:
If you copy the hash
from these details and search on Etherscan, you should see an EIP-1559 transaction that transferred 2 USDC from your wallet address to the wallet address you specified!
Conclusion
Congratulations! You now know how to send ERC20 tokens in an EIP-1559 transaction using Alchemy SDK 🎉
Happy Coding and feel free to message us with any related questions on Alchemy’s Discord Server!