Prerequisites
Before you begin, make sure you have:- Installed Node.js v18+.
- Prepared an Ink Sepolia testnet wallet funded with:
- Testnet USDC for the transfer.
- Testnet ETH for gas fees.
- A private key and a recipient address stored in a
.envfile.
To get Ink testnet USDC
Use Circle’s CCTP sample app to transfer USDC cross-chain to your Ink wallet.To get testnet ETH
Use the Sepolia faucet to request free testnet ETH for gas fees.
Use Circle’s CCTP sample app to transfer USDC cross-chain to your Ink wallet.To get testnet ETH
Use the Sepolia faucet to request free testnet ETH for gas fees.
Step 1: Set up your project
-
Initialize a new Node.js project and install dependencies:
Shell
-
In the project root, create a
.envfile with your private key and recipient address:Shell -
Create an
index.jsfile. You’ll add code step by step in the following sections.
Step 2: Configure chain and contract
Inindex.js, import required modules and define constants for the Ink Sepolia
testnet and the USDC contract:
Javascript
Learn more: Chain and contract configuration
Learn more: Chain and contract configuration
Here’s what this section does:
- Loads the Ink Sepolia testnet chain configuration from Viem.
- Defines the USDC contract address, decimals, and minimal ABI.
- Only includes the
balanceOfandtransferfunctions, since they’re all that’s needed for this guide.
Step 3: Load environment variables
Next, load your private key and recipient address from.env:
Javascript
Learn more: Environment variables
Learn more: Environment variables
This section ensures sensitive values are handled safely:
- Loads values from the
.envfile so you don’t hardcode sensitive information. - Checks that both the private key and recipient address are set.
- Validates the recipient address format with a regex.
- Ensures the private key is properly prefixed with
0xso Viem can use it.
Step 4: Initialize Viem clients
Create a public client for reading blockchain data and a wallet client for sending transactions:Javascript
Learn more: Viem clients
Learn more: Viem clients
These client methods give you read and write access to Ink Sepolia:
privateKeyToAccountcreates an account object from your private key.createPublicClientis used for reading data from the blockchain (no private key needed).createWalletClientuses your account to sign and send transactions.
Step 5: Transfer USDC
Now, write the main logic to check your balance and send a transfer:Javascript
Learn more: Transfer logic
Learn more: Transfer logic
Here’s how the transfer process works:
- Reads your USDC balance using the
balanceOffunction. - Converts the balance into a human-readable format with
formatUnits. - Sets the transfer amount (1 USDC in this example).
- Validates that your balance is sufficient.
- Converts the transfer amount back into smallest units with
parseUnits. - Calls the
transferfunction on the USDC contract to send tokens. - Logs the transaction hash and a block explorer link so you can verify the transfer.
Step 6: Run the script
After running the script:Shell
Explorer: line
and open it in your browser. This will take you to the Ink Sepolia testnet
explorer, where you can view full transaction details.
Tip: If your script doesn’t output a full explorer URL, you can manually
paste the transaction hash into the Ink Sepolia explorer.
Summary
In this quickstart, you learned how to check balances and transfer USDC on the Ink Sepolia testnet using Viem and Node.js. Here are the key points to remember:- Testnet only. Ink Sepolia testnet USDC has no real value.
- Gas fees. You need a small amount of testnet ETH for gas.
- Security. Keep your private key in
.env. Never commit secrets. - Minimal ABI. The script only uses
balanceOfandtransferfor simplicity. - Viem behavior.
readContractandwriteContracthandle reads and writes. Viem’sprivateKeyToAccountfunction requires a0x-prefixed hex string, so the script ensures the prefix is present.