index.js script that checks
your USDC balance and sends a test transfer on the Sei Atlantic-2 Testnet. It’s
designed for developers looking to quickly get hands-on with
USDC on Sei.
Prerequisites
Before you begin, make sure you have:- Installed Node.js v18+ and
npm, which comes withNode.js. - Prepared a Sei testnet wallet with USDC and SEI for gas:
- Set up a Sei-compatible wallet.
- Request SEI from the faucet.
- Transfer testnet USDC to Sei using Circle’s CCTP Sample App.
- Stored your private key and a recipient address in a
.envfile.
USDC contract address
Use the following contract address for USDC on the Sei Atlantic-2 Testnet:- USDC contract –
0x4fCF1784B31630811181f670Aea7A7bEF803eaED
Part 1: Set up your project
Follow these steps to install dependencies and set up your script environment.1. Initialize a project
Create a new directory and initialize it withnpm:
Shell
2. Install required dependencies
Installviem and dotenv:
Shell
3. Create your .env file
In the project root, create a .env file and add the following values:
Your private key must be a
0x-prefixed 64-character hex string.Part 2: Build your script
Create a file namedindex.js. You’ll build your script step-by-step in the
following sections.
1. Import libraries and define constants
Inindex.js, start by importing dependencies and setting up the chain and
token configuration:
Typescript
Learn more: Why use a minimal ABI?
Learn more: Why use a minimal ABI?
This ABI includes only the two methods needed for this script:
balanceOf(address)to check the token balance.transfer(to, amount)to send USDC.
- Reduces complexity by excluding unrelated functions.
- Speeds up onboarding for new developers.
- Avoids unnecessary contract data that would be unused in this context.
2. Load environment variables and validate inputs
Next, load your credentials from.env and validate them:
Typescript
Learn more: Input validation and the 'fail fast' principle
Learn more: Input validation and the 'fail fast' principle
Failing fast means catching mistakes early in the execution cycle. This script
validates inputs to avoid:
- Missing or undefined environment variables.
- Private keys that are not prefixed with
0x. - Recipient addresses that don’t follow Ethereum address formatting.
- Prevent runtime failures deeper in the logic.
- Help you debug configuration issues immediately.
- Save time and reduce the risk of sending faulty transactions.
3. Set up Viem clients
Set up the Viem clients and your account object:Typescript
Learn more: When to use `publicClient` vs. `walletClient`
Learn more: When to use `publicClient` vs. `walletClient`
publicClientcallseth_callto read blockchain data. It doesn’t need your private key.- Use this for functions like
balanceOf,symbol, ordecimals.
- Use this for functions like
walletClientsends transactions usingeth_sendTransaction.- It signs data with your private key and requires a funded account.
4. Check balance and send USDC
Now add the logic to check your balance and send tokens:Typescript
Learn more: Handling USDC units with `formatUnits` and `parseUnits`
Learn more: Handling USDC units with `formatUnits` and `parseUnits`
Token contracts use the smallest indivisible unit of a token. For USDC, that’s 6
decimal places.
formatUnits(value, 6)converts raw blockchain balances into readable amounts like250.0.parseUnits('10', 6)converts user inputs into raw values like10000000(10 * 10^6).
- Prevent incorrect math during transfer.
- Ensure compatibility with ERC-20 functions.
- Improve display formatting in logs and UI.
Part 3: Run your script
To run your script, use the following command:Shell
Sample output
If the script runs successfully, your terminal will print a transaction summary like this:Additional notes
- Testnet only: This guide runs on the Sei testnet. Tokens and transfers here are not real and hold no value.
- Security best practices: Always store sensitive data like private keys in
environment variables. Never hardcode or commit
.envfiles to version control. - Gas fees: All EVM-compatible chains require gas. Be sure to request testnet SEI from the faucet so your transactions can be mined.
- Lightweight ABI: The ABI used here is minimal. It includes only the functions necessary for balance checks and transfers, which keeps the script lean.
- Auto-prefixing: Some tools or environments strip the
0xprefix from private keys. This script adds it back if needed to avoid errors.