All transactions in this guide take place on the Solana Devnet testnet. No real
funds are transferred. You can adapt this code for mainnet transactions by
changing the RPC endpoint and using mainnet contract addresses.
Prerequisites
Before you start building the sample app to perform a USDC transfer, ensure you have:- Node.js and npm installed on your development machine. You can download and install Node.js directly, or use a version manager like nvm. The npm binary comes with Node.js.
- Created two Solana wallets: a sender wallet (with the private key available) and a receiver wallet (you only need the public address).
- Funded your sender wallet with the following testnet tokens:
- Solana Devnet SOL (native token) from a public faucet.
- Solana Devnet USDC from the Circle Faucet.
Part 1: Project setup
Set up your project environment and install the required dependencies.1.1. Create a new project
Create a new directory and initialize a new Node.js project with default settings:Shell
1.2. Install dependencies
Install the required dependencies for Solana interactions:Shell
1.3. Configure environment variables
Create a.env file in your project directory and add your wallet private key
and the receiver address:
Shell
SOLANA_PRIVATE_KEY should be a JSON array of bytes representing your
private key. You can export this from most Solana wallets.
Converting Base58 private key to JSON array
Converting Base58 private key to JSON array
Some wallets export Solana private keys as Base58 encoded strings. If you have a
Base58 encoded private key, you can convert it to a JSON array with the
following Node.js script:
JavaScript
Part 2: Configure the script
Define the configuration constants for interacting with Solana Devnet.2.1. Define configuration constants
The script predefines the RPC endpoints, USDC mint address, and transfer parameters:JavaScript
USDC uses 6 decimal places. To transfer 1 USDC, you specify
1_000_000 (1 *
106) as the amount.Part 3: Implement the transfer logic
The following sections outline the core transfer logic for sending USDC on Solana.3.1. Get token accounts
Derive the Associated Token Account (ATA) addresses for both the sender and receiver. These accounts are program-derived addresses that hold SPL tokens for a specific wallet:JavaScript
3.2. Transfer USDC
Build and send the transfer transaction using the SPL Token program’s transfer instruction:JavaScript
Part 4: Complete script
Create atransfer.js file in your project directory and populate it with the
complete code below.
transfer.js
Part 5: Test the script
Run the following command to execute the script:Shell
If the receiver’s wallet has never held USDC before, you may need to create
their Associated Token Account first. The transfer will fail if the destination
token account does not exist. You can use the
getOrCreateAssociatedTokenAccount pattern to handle this automatically.