Note: This quickstart provides all the code you need to create a unified
crosschain balance and transfer it to a new chain. You can find the
full code for this quickstart on GitHub.
Prerequisites
Before you start building the sample app, ensure that Node.js and npm are installed. You can download Node.js directly, or use a version manager like nvm. The npm binary comes with Node.js. You should also have created an Ethereum testnet wallet and have the address and the private key available.Step 1: Set up your project
The following sections demonstrate how to initialize your project and set up the required clients and smart contracts to interact with Circle Gateway.1.1. Set up your development environment
Create a new directory for your project and initialize it with npm. Set the package type tomodule and install the necessary dependencies:
Shell
.env file.
Shell
.env file and add the following variables, replacing
{YOUR_PRIVATE_KEY} with the private key for your testnet wallet:
1.2. Define contract ABIs
Create a file calledabis.js in the project directory and add the following
code to it. This helper code defines the contract functions that you use to
establish a unified USDC balance and mint it on a new chain.
Note: For brevity, the ABIs in this code are not complete ABIs for the
wallet and minter contracts. They only define the functions necessary for this
quickstart. See
Contract Interfaces and Events
for a full description of each contract’s methods.
Javascript
1.3. Define the Gateway API client
Create a file calledgateway-client.js in the project directory and add the
following code to it. This helper code provides data and methods for interacting
with the Gateway API.
Javascript
1.4. Define typed data
Create a file calledtyped-data.js in the project directory and add the
following code to it:
Javascript
1.5. Initialize clients and contracts
Create a file calledsetup.js in the project directory and add the following
code to it. This code provides a client and contract addresses on the relevant
chains for this quickstart:
Javascript
1.6. Add testnet funds to your wallet
To interact with Gateway, you need USDC and testnet native tokens in your wallet on each chain that you deposit from. You also need testnet native tokens on the destination chain to make the call to the Gateway Minter contract. Use the Circle Faucet to get 10 USDC on the Avalanche Fuji and Ethereum Sepolia testnets. Use the following faucets to get testnet native tokens in your wallet:Step 2: Create a unified crosschain balance
In this step, you deposit USDC into the Gateway Wallet contract on Avalanche Fuji and Ethereum Sepolia, creating a unified crosschain balance for your address in Gateway.2.1. Deposit USDC into the Gateway Wallet contracts
Create a file calleddeposit.js in the project directory and add the following
code to it. This code deposits USDC into the Gateway Wallet contracts.
Note that for the transfer, the code is not calling the ERC-20 transfer
function, as doing so would result in a loss of funds. Instead, the code first
calls the approve function on the USDC contract to allow the Gateway Wallet
contract to transfer USDC on behalf of your account. Then the code calls the
deposit function on the Gateway Wallet contract to move the USDC.
Javascript
2.2. Run the script to create a crosschain balance
Run thedeposit.js script to make the deposits. Note that if you don’t have
enough USDC or testnet native tokens, the code returns an error message.
Shell
Note: You must wait for finality on both chains for the unified balance to
be updated. On Avalanche, finality is reached immediately. On Ethereum, you
may need to wait up to 20 minutes for finality.
Step 3: Transfer USDC from the crosschain balance to Base
In this step, you create a burn intent, submit it to the API and receive an attestation, which you can use to mint USDC on Base Sepolia. Funds are burned from the crosschain balance on Avalanche and Base.3.1. Sign burn intents
Create a file calledtransfer.js in the project directory and add the
following code to it. This code performs checks that the API supports the chains
that you want to interact with, and verifies that your balance on the source
chains is enough to cover the transfer.
Javascript
3.2. Submit burn intents to the API
Add the following code totransfer.js to submit the burn intents to the
Gateway in exchange for an attestation:
Javascript
3.3. Mint USDC on Base Sepolia
Add the following code totransfer.js to mint USDC on Base Sepolia using the
attestation:
Javascript
3.4. Run the transfer script
Run thetransfer.js script to perform the USDC transfer from the crosschain
balance to Base.
Shell