The v2 Wallet API allows you to create onchain accounts
and send funds within minutes. In this quickstart, you will learn how to create an EVM account,
fund it with testnet ETH, and transfer funds between accounts.
Follow these instructions to set up your environment to use the v2 Wallet API.
Make sure to set up your environment variables before running the code snippets.
Smart accounts are smart contracts on EVM blockchains that offer advanced functionality such as
gas sponsorships and spend permissions.Smart accounts require an owner account to sign on its behalf. In this example, we will use the
previously created EVM account as the owner.
Solana accounts are similar to EVM accounts in that they are used to send and receive funds,
except on the Solana blockchain.The code below demonstrates creating an account, requesting faucet funds, and sending a transaction.
Install @solana/web3.js to send transactions on Solana
Copy
Ask AI
npm install @solana/web3.js
Run the following code snippet
main.ts
Copy
Ask AI
import { Connection, PublicKey, SystemProgram, Transaction,} from "@solana/web3.js";import { CdpClient } from "@coinbase/cdp-sdk";const cdp = new CdpClient();const connection = new Connection("https://api.devnet.solana.com");async function createAccount() { const account = await cdp.solana.createAccount(); console.log(`Created account: ${account.address}`); return account;}async function requestFaucet(address: string) { await cdp.solana.requestFaucet({ address, token: "sol", });}async function waitForBalance(address: string) { let balance = 0; let attempts = 0; const maxAttempts = 30; while (balance === 0 && attempts < maxAttempts) { balance = await connection.getBalance(new PublicKey(address)); if (balance === 0) { console.log("Waiting for funds..."); await new Promise(resolve => setTimeout(resolve, 1000)); attempts++; } else { console.log("Account funded with", balance / 1e9, "SOL"); } } if (balance === 0) { throw new Error("Account not funded after multiple attempts"); }}async function sendTransaction(address: string) { // Amount of lamports to send (default: 1000 = 0.000001 SOL) const lamportsToSend = 1000; const fromAddress = new PublicKey(address) const toAddress = new PublicKey("EeVPcnRE1mhcY85wAh3uPJG1uFiTNya9dCJjNUPABXzo"); const { blockhash } = await connection.getLatestBlockhash(); const transaction = new Transaction(); transaction.add( SystemProgram.transfer({ fromPubkey: fromAddress, toPubkey: toAddress, lamports: lamportsToSend, }) ); transaction.recentBlockhash = blockhash; transaction.feePayer = fromAddress; const serializedTx = Buffer.from( transaction.serialize({ requireAllSignatures: false }) ).toString("base64"); const { signature: txSignature } = await cdp.solana.signTransaction({ address, transaction: serializedTx, }); const decodedSignedTx = Buffer.from(txSignature, "base64"); console.log("Sending transaction..."); const txSendSignature = await connection.sendRawTransaction(decodedSignedTx); const latestBlockhash = await connection.getLatestBlockhash(); console.log("Waiting for transaction to be confirmed..."); const confirmation = await connection.confirmTransaction({ signature: txSendSignature, blockhash: latestBlockhash.blockhash, lastValidBlockHeight: latestBlockhash.lastValidBlockHeight, }); if (confirmation.value.err) { throw new Error(`Transaction failed: ${confirmation.value.err.toString()}`); } console.log(`Sent SOL: https://explorer.solana.com/tx/${txSendSignature}?cluster=devnet`);}async function main() { const account = await createAccount(); await requestFaucet(account.address); await waitForBalance(account.address); await sendTransaction(account.address);}main().catch(console.error)