Transfer USDC between wallets on Solana Devnet using a Node.js script
This guide demonstrates how to transfer USDC between two wallets on Solana
Devnet using a Node.js script. You use the
Solana Kit library to interact with the
Solana blockchain and perform SPL token transfers.
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.
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.
The 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
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
Report incorrect code
Copy
const fs = require("fs");const bs58 = require("bs58");const privateKeyBase58 = "YOUR_BASE58_PRIVATE_KEY";try { // Decode the Base58 string to a Uint8Array const privateKeyBytes = bs58.decode(privateKeyBase58); // The result is a Uint8Array. Convert it to a JSON array string. const privateKeyJsonString = JSON.stringify(Array.from(privateKeyBytes)); console.log("JSON Array:", privateKeyJsonString);} catch (error) { console.error("Error converting key. Check if the Base58 key is valid.");}
This is strictly for testing purposes. Never share your private keys or
commit them to version control.
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:
Once the script runs and the transfer is finalized, a confirmation message is
logged in the console:
Report incorrect code
Copy
Transferring USDC on Solana Devnet...From: YourSenderAddressTo: YourReceiverAddressAmount: 1 USDCTransaction successful!Signature: 5abc123...View on explorer: https://explorer.solana.com/tx/5abc123...?cluster=devnet
You can verify the transaction by visiting the Solana Explorer link in the
output, or by checking the USDC balance in the receiver’s wallet.
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.