Prerequisites
Before you begin, make sure you have:- Installed Node.js v18+.
- Prepared an XDC Apothem testnet wallet funded with:
- Testnet USDC for the transfer.
- Testnet XDC for gas fees.
- A private key and a recipient address stored in a
.envfile.
To get XDC testnet USDC
Use Circle’s CCTP sample app to transfer USDC cross-chain to your XDC wallet.To get testnet XDC
Use the XDC faucet from the XDC Apothem testnet network.
Use Circle’s CCTP sample app to transfer USDC cross-chain to your XDC wallet.To get testnet XDC
Use the XDC faucet from the XDC Apothem testnet network.
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 XDC Apothem
testnet and the USDC contract:
Javascript
Learn more: Chain and contract configuration
Learn more: Chain and contract configuration
- Imports required modules from Viem and your
.envfile. - Loads the XDC Apothem testnet configuration.
- 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
- 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
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.- Together, these clients give you read and write access to XDC Apothem.
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
- Reads your USDC balance using the
balanceOffunction. - Converts the balance into a human-readable format with
formatUnits. - Sets the transfer amount (10 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 XDC Apothem 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 XDC testnet explorer.
Summary
In this quickstart, you learned how to check balances and transfer USDC on the XDC Apothem testnet using Viem and Node.js. Here are the key points to remember:- Testnet only. XDC testnet USDC has no real value.
- Gas fees. You need a small amount of testnet XDC for gas.
- Security. Keep your private key in
.env. Never commit secrets. - Minimal ABI. The script only uses
balanceOfandtransferfor simplicity.