|
| 1 | +--- |
| 2 | +title: USDC on XDC QuickStart Guide |
| 3 | +--- |
| 4 | + |
| 5 | +# USDC on XDC QuickStart Guide |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +USDC is a digital dollar issued by Circle, also known as a stablecoin, and is available on many of the world’s leading blockchains. Designed to represent US dollars on the internet, USDC is backed 100% by highly liquid cash and cash-equivalent assets, making it redeemable 1:1 for USD. |
| 10 | + |
| 11 | +On the XDC Network, USDC behaves like any standard ERC-20 token — enabling fast, secure, and programmable digital dollar transactions. |
| 12 | + |
| 13 | +This guide walks you through building a standalone `index.js` script using **Viem** and **Node.js** to check your USDC balance and send a test transfer to another address on the **XDC Apothem Testnet**. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +- Node.js v18+ with `"type": "module"` in `package.json` |
| 20 | +- `viem` and `dotenv` packages installed |
| 21 | +- An XDC Apothem testnet wallet funded with testnet USDC and XDC (for gas fees) |
| 22 | +- A `.env` file with: |
| 23 | + - `PRIVATE_KEY` |
| 24 | + - `RECIPIENT_ADDRESS` |
| 25 | + |
| 26 | +> To get testnet USDC, use Circle’s CCTP v2 Sample App to transfer USDC cross-chain to your XDC wallet. |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## Project Setup |
| 31 | + |
| 32 | +### 1. Initialize Project & Install Dependencies |
| 33 | + |
| 34 | +```bash |
| 35 | +npm init -y |
| 36 | +npm install viem dotenv |
| 37 | +```` |
| 38 | + |
| 39 | +### 2. Create Environment File |
| 40 | + |
| 41 | +In your project root, create a `.env` file and add: |
| 42 | + |
| 43 | +```env |
| 44 | +PRIVATE_KEY=<YOUR_PRIVATE_KEY> # Must be 0x-prefixed 64-character hex |
| 45 | +RECIPIENT_ADDRESS=0x<RECIPIENT_ADDRESS> |
| 46 | +``` |
| 47 | + |
| 48 | +### 3. Create Your Script File |
| 49 | + |
| 50 | +In the same directory, create a file named `index.js`. |
| 51 | + |
| 52 | +Ensure `"type": "module"` is set in your `package.json`. |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## Script Breakdown |
| 57 | + |
| 58 | +### 1. Import Modules & Define USDC Constants |
| 59 | + |
| 60 | +```js |
| 61 | +import 'dotenv/config'; |
| 62 | +import { createPublicClient, createWalletClient, http, formatUnits, parseUnits } from 'viem'; |
| 63 | +import { privateKeyToAccount } from 'viem/accounts'; |
| 64 | +import { xdcTestnet } from 'viem/chains'; |
| 65 | + |
| 66 | +const USDC_ADDRESS = '0xb5AB69F7bBada22B28e79C8FFAECe55eF1c771D4'; |
| 67 | +const USDC_DECIMALS = 6; |
| 68 | +const USDC_ABI = [ |
| 69 | + { |
| 70 | + name: 'balanceOf', |
| 71 | + type: 'function', |
| 72 | + stateMutability: 'view', |
| 73 | + inputs: [{ name: 'account', type: 'address' }], |
| 74 | + outputs: [{ name: '', type: 'uint256' }], |
| 75 | + }, |
| 76 | + { |
| 77 | + name: 'transfer', |
| 78 | + type: 'function', |
| 79 | + stateMutability: 'nonpayable', |
| 80 | + inputs: [ |
| 81 | + { name: 'to', type: 'address' }, |
| 82 | + { name: 'amount', type: 'uint256' }, |
| 83 | + ], |
| 84 | + outputs: [{ name: '', type: 'bool' }], |
| 85 | + }, |
| 86 | +]; |
| 87 | +``` |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +### 2. Load & Validate Environment Variables |
| 92 | + |
| 93 | +```js |
| 94 | +const PRIVATE_KEY_RAW = process.env.PRIVATE_KEY; |
| 95 | +const RECIPIENT = process.env.RECIPIENT_ADDRESS || process.env.RECIPIENT; |
| 96 | + |
| 97 | +if (!PRIVATE_KEY_RAW) { |
| 98 | + console.error('Error: Set PRIVATE_KEY in your .env file'); |
| 99 | + process.exit(1); |
| 100 | +} |
| 101 | +if (!RECIPIENT) { |
| 102 | + console.error('Error: Set RECIPIENT_ADDRESS or RECIPIENT in your .env file'); |
| 103 | + process.exit(1); |
| 104 | +} |
| 105 | +if (!/^0x[a-fA-F0-9]{40}$/.test(RECIPIENT)) { |
| 106 | + console.error('Error: Recipient address is not a valid Ethereum address'); |
| 107 | + process.exit(1); |
| 108 | +} |
| 109 | + |
| 110 | +const PRIVATE_KEY = PRIVATE_KEY_RAW.startsWith('0x') ? PRIVATE_KEY_RAW : '0x' + PRIVATE_KEY_RAW; |
| 111 | +``` |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +### 3. Initialize Viem Clients |
| 116 | + |
| 117 | +```js |
| 118 | +const account = privateKeyToAccount(PRIVATE_KEY); |
| 119 | +const publicClient = createPublicClient({ chain: xdcTestnet, transport: http() }); |
| 120 | +const walletClient = createWalletClient({ account, chain: xdcTestnet, transport: http() }); |
| 121 | +``` |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +### 4. Main Transfer Logic |
| 126 | + |
| 127 | +```js |
| 128 | +(async () => { |
| 129 | + try { |
| 130 | + const balance = await publicClient.readContract({ |
| 131 | + address: USDC_ADDRESS, |
| 132 | + abi: USDC_ABI, |
| 133 | + functionName: 'balanceOf', |
| 134 | + args: [account.address], |
| 135 | + }); |
| 136 | + |
| 137 | + const balanceFormatted = Number(formatUnits(balance, USDC_DECIMALS)); |
| 138 | + const amount = 10; |
| 139 | + |
| 140 | + console.log('Sender:', account.address); |
| 141 | + console.log('Recipient:', RECIPIENT); |
| 142 | + console.log('USDC balance:', balanceFormatted); |
| 143 | + |
| 144 | + if (amount > balanceFormatted) { |
| 145 | + console.error('Error: Insufficient USDC balance'); |
| 146 | + process.exit(1); |
| 147 | + } |
| 148 | + |
| 149 | + const amountInDecimals = parseUnits(amount.toString(), USDC_DECIMALS); |
| 150 | + |
| 151 | + const hash = await walletClient.writeContract({ |
| 152 | + address: USDC_ADDRESS, |
| 153 | + abi: USDC_ABI, |
| 154 | + functionName: 'transfer', |
| 155 | + args: [RECIPIENT, amountInDecimals], |
| 156 | + }); |
| 157 | + |
| 158 | + console.log('Transfer successful!'); |
| 159 | + console.log('Tx hash:', hash); |
| 160 | + console.log('Explorer:', `https://testnet.xdcscan.com/tx/${hash}`); |
| 161 | + } catch (err) { |
| 162 | + console.error('Transfer failed:', err.message || err); |
| 163 | + process.exit(1); |
| 164 | + } |
| 165 | + |
| 166 | + process.exit(0); |
| 167 | +})(); |
| 168 | +``` |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +## Run the Script |
| 173 | + |
| 174 | +Use the following command: |
| 175 | + |
| 176 | +```bash |
| 177 | +node index.js |
| 178 | +``` |
| 179 | + |
| 180 | +If successful, you’ll see output like: |
| 181 | + |
| 182 | +``` |
| 183 | +Sender: 0x1A2b...7890 |
| 184 | +Recipient: 0x9F8f...1234 |
| 185 | +USDC balance: 250.0 |
| 186 | +Transfer successful! |
| 187 | +Tx hash: 0xabc123...def456 |
| 188 | +Explorer: https://testnet.xdcscan.com/tx/0xabc123...def456 |
| 189 | +``` |
| 190 | + |
| 191 | +--- |
| 192 | + |
| 193 | +## Important Notes |
| 194 | + |
| 195 | +* **Testnet Only**: USDC on Apothem has no real value. |
| 196 | +* **Security**: Never commit your `.env` file. Treat private keys as sensitive. |
| 197 | +* **Gas Fees**: Get free XDC from the [XDC Faucet](https://faucet.apothem.network/). |
| 198 | +* **Lightweight ABI**: Only the necessary functions (`balanceOf`, `transfer`) are used. |
| 199 | +* **Viem Behavior**: Viem auto-handles RPC interaction, account signing, and encoding/decoding. |
| 200 | + |
| 201 | +--- |
| 202 | + |
| 203 | +## Learn More |
| 204 | + |
| 205 | +Explore the full Circle USDC integration guide in the [Circle Developer Docs](https://developers.circle.com/docs/usdc). |
| 206 | + |
| 207 | +``` |
| 208 | + |
| 209 | +--- |
0 commit comments