Prerequisites
Before you start building the sample app to perform a USDC transfer, ensure you have met the following prerequisites:-
Install Node.js and npm
- Download and install Node.js directly or use a version manager like nvm.
- npm is included with Node.js.
-
Set up a non-custodial wallet (for example, MetaMask)
- You can download, install, and create a MetaMask wallet from its official website.
- During setup, create a wallet on the Ethereum Sepolia testnet.
- Retrieve the private key for your wallet, as it will be required in the script below.
-
Fund your wallet with testnet tokens
- Obtain Sepolia ETH (native token) from a public faucet.
- Get Sepolia USDC from the Circle Faucet.
Project setup
To build the script, first set up your project environment and install the required dependencies.- Set up a new project
Node.js project with default
settings:
Shell
package.json file.
- Install dependencies
viem:
Shell
package.json file with the
dependencies.
- Add module type
"type": "module" to the package.json file:
package.json
- Configure environment variables
.env file in your project directory and add your wallet private key:
Shell
Warning: This is strictly for testing purposes. Never share your private
key.
Script setup
This section covers the necessary setup for the transfer.js script, including defining keys and addresses, and configuring the wallet client for interacting with the source and destination chains.- Replace with your private key and wallet address
Javascript
- Set up wallet clients
viem.
In this example, the script connects to the Ethereum Sepolia testnet and the
Avalanche Fuji testnet.
Javascript
CCTP transfer process
The following sections outline the relevant transfer logic of the sample script. You can view the full source code in the Build the script section below. To perform the actual transfer of USDC from Ethereum Sepolia to Avalanche Fuji using CCTP V2, follow the steps below:1. Approve USDC
The first step is to grant approval for the TokenMessengerV2 contract deployed on the Ethereum Sepolia testnet to withdraw USDC from your wallet on that source chain. This allows the contract to withdraw USDC from the specified wallet address.Javascript
2. Burn USDC
In this step, you call thedepositForBurn function from the
TokenMessengerV2 contract
deployed on the Ethereum Sepolia testnet to burn USDC on that source chain.
You specify the following parameters:
- Burn amount: The amount of USDC to burn
- Destination domain: the target blockchain for minting USDC
- Mint recipient: The wallet address that will receive the minted USDC
- Burn token: The contract address of the USDC token being burned on the source chain
- Destination caller: The address on the target chain to call
receiveMessage - Max fee: The maximum fee allowed for the transfer
- Finality threshold: Determines whether it’s a Fast Transfer or a Standard Transfer
Javascript
3. Retrieve attestation
In this step, you retrieve the attestation required to complete the CCTP transfer.- Call Circle’s GET /v2/messages API
endpoint to retrieve the attestation.
- Pass the
srcDomainargument from the CCTP Domain for your source chain. - Pass
transactionHashfrom the value returned bysendTransactionwithin theburnUSDCfunction above.
- Pass the
Javascript
4. Mint USDC
In this final step, you call thereceiveMessage function from the
MessageTransmitterV2 contract
deployed on the Avalanche Fuji testnet to mint USDC on that destination
chain.
- Pass the signed attestation and the message data as parameters.
- The function processes the attestation and mints USDC to the specified Avalanche Fuji wallet address.
Javascript
Build the script
Now that you understand the core steps for programmatically transferring USDC from Ethereum Sepolia to Avalanche Fuji using CCTP V2, create atransfer.js in your project directory and populate it with the sample code
below.
Note: The source wallet must contain native testnet tokens (to cover
gas fees) and testnet USDC to complete the transfer.
transfer.js
Javascript
transfer.js script provides a complete end-to-end solution for
transfering USDC in CCTP V2 with a non-custodial wallet. In the next
section, you can test the script.
Test the script
To test the script, run the following command:Shell
Rate Limit:The attestation service rate limit is 35 requests per second. If you exceed
this limit, the service blocks all API requests for the next 5 minutes and
returns an HTTP 429 (Too Many Requests) response.
WHAT’S NEXT