An autonomous TypeScript agent demonstrating Coinbase Developer Platform's Solana integration capabilities. This project showcases advanced Solana operations including account provisioning, funding, transactions, batch operations, sponsored transactions, and off-chain message signing.
- Automated Account Management: Seamless Solana account creation and provisioning via CDP
- Testnet Funding Integration: Automatic SOL faucet requests with balance monitoring
- Standard SOL Transfers: Execute secure SOL transfers to any Solana address
- Batched Transactions: Efficient multi-recipient payouts in a single transaction
- Sponsored Transactions: Gasless UX with transaction fee sponsorship
- Off-chain Message Signing: Cryptographic proof of account ownership
- Full TypeScript Implementation: Type-safe development with Solana Web3.js integration
- Real-time Transaction Monitoring: Live confirmation tracking with explorer links
- Node.js & TypeScript: Modern JavaScript runtime with type safety
- Coinbase CDP SDK: Core Solana wallet and transaction management
- Solana Web3.js: Official Solana JavaScript SDK for blockchain interactions
- Solana Devnet: Safe testing environment for all operations
- Dotenv: Secure environment variable management
- Node.js (v18 or later)
- npm package manager
- Coinbase Developer Platform (CDP) API credentials
- Basic understanding of Solana blockchain concepts
# Clone the repository
git clone https://github.com/HeimLabs/coinbase-cdp-demos.git
cd 15-CDP-Wallets-Solana-Agent
# Install dependencies
npm installCreate a .env file in the root directory:
# Copy the example environment file (if available)
cp .env.example .envAdd your CDP API credentials:
# Coinbase Developer Platform API Credentials
CDP_API_KEY_NAME=your_cdp_api_key_name
CDP_API_KEY_PRIVATE_KEY=your_cdp_api_private_key
# Optional: Solana RPC Configuration
SOLANA_RPC_URL=https://api.devnet.solana.com# Build the TypeScript project
npm run build
# Run the main agent demo
npm start
# Or run the simplified Solana demo
npm run solana
# Or run in development mode (build + start)
npm run devThe agent demonstrates five key Solana operations:
// Automatic account creation with CDP
const agentAccount = await cdp.solana.getOrCreateAccount({
name: "MySolanaAgent-v2"
});
console.log(`Agent account: ${agentAccount.address}`);// Request SOL from CDP faucet if balance is zero
if (balance === 0) {
const faucetResp = await cdp.solana.requestFaucet({
address: account.address,
token: "sol",
});
console.log(`Faucet transaction: ${faucetResp.signature}`);
}// Send SOL to a recipient address
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: new PublicKey(agentAccount.address),
toPubkey: recipient,
lamports: 0.0001 * LAMPORTS_PER_SOL,
})
);
const result = await cdp.solana.sendTransaction({
network: "solana-devnet",
transaction: serializedTx,
});// Execute multiple transfers in a single transaction
const payouts = [
{ to: recipient1, amount: 0.00005 * LAMPORTS_PER_SOL },
{ to: recipient2, amount: 0.00005 * LAMPORTS_PER_SOL },
];
const payoutInstructions = payouts.map(payout =>
SystemProgram.transfer({
fromPubkey: new PublicKey(agentAccount.address),
toPubkey: payout.to,
lamports: payout.amount,
})
);// Sponsor transaction fees for another user
transaction.feePayer = new PublicKey(sponsorAccount.address);
// Multiple signatures: user authorizes, sponsor pays fees
const userSignedTx = await cdp.solana.signTransaction({
address: sourceAccount.address,
transaction: serializedTx,
});
const finalSignedTx = await cdp.solana.signTransaction({
address: sponsorAccount.address,
transaction: userSignedTx.signedTransaction,
});// Cryptographic proof of account ownership
const message = `I am the owner of ${agentAccount.address} at ${new Date().toISOString()}`;
const { signature } = await agentAccount.signMessage({ message });
console.log(`Message signed: ${signature}`);15-CDP-Wallets-Solana-Agent/
├── src/
│ ├── agent.ts # Main agent demo with all scenarios
│ └── solana.ts # Simplified Solana operations demo
├── dist/ # Compiled JavaScript output
├── package.json # Project dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── .env # Environment variables (create this)
- CDP Client Integration: Initializes Coinbase Developer Platform SDK
- Solana Connection: Connects to Solana Devnet for blockchain operations
- Account Management: Automated account provisioning and funding
- Transaction Builder: Creates and serializes Solana transactions
- Signature Management: Handles transaction signing via CDP
- Confirmation Tracking: Monitors transaction status and confirmations
graph TD
A[Agent Start] --> B[Initialize CDP Client]
B --> C[Create/Get Solana Accounts]
C --> D[Fund Accounts via Faucet]
D --> E[Execute Demo Scenarios]
E --> F[Standard Transfer]
E --> G[Batched Payout]
E --> H[Sponsored Transaction]
E --> I[Message Signing]
F --> J[Wait for Confirmation]
G --> J
H --> J
I --> K[Demo Complete]
J --> K
- Cross-chain Operations: Demonstrate Solana integration alongside EVM chains
- Automated Treasury Management: Multi-recipient payouts and treasury operations
- Gasless UX Development: Sponsored transactions for improved user experience
- Identity Verification: Off-chain message signing for authentication
- Educational Purposes: Learn Solana development with CDP integration
- Bot Development: Automated trading and interaction agents
Extend the agent with additional Solana features:
// Example: SPL Token Transfer
import { TOKEN_PROGRAM_ID, getAssociatedTokenAddress } from '@solana/spl-token';
async function transferSPLToken(fromAccount: any, toAddress: PublicKey, amount: number) {
const mintAddress = new PublicKey('your_token_mint_address');
const fromTokenAccount = await getAssociatedTokenAddress(
mintAddress,
new PublicKey(fromAccount.address)
);
const toTokenAccount = await getAssociatedTokenAddress(
mintAddress,
toAddress
);
// Build SPL token transfer instruction
// ... implementation
}Switch between Solana networks:
// Mainnet
const connection = new Connection("https://api.mainnet-beta.solana.com");
// Testnet
const connection = new Connection("https://api.testnet.solana.com");
// Local
const connection = new Connection("http://localhost:8899");Update recipient addresses in the demo:
// Replace with your own addresses
const recipient1 = new PublicKey("YourSolanaAddress1");
const recipient2 = new PublicKey("YourSolanaAddress2");npm run build: Compile TypeScript to JavaScriptnpm start: Run the main agent demonpm run solana: Run the simplified Solana demonpm run dev: Build and run in development modenpm test: Run tests (when available)
Enable detailed logging by setting environment variables:
DEBUG=true npm startCreate tests for your Solana operations:
// Example test structure
describe('Solana Agent', () => {
test('should create account successfully', async () => {
const account = await cdp.solana.getOrCreateAccount({ name: 'test' });
expect(account.address).toBeTruthy();
});
});- Environment Variables: Store all sensitive keys in environment variables
- Network Selection: Use mainnet only for production deployments
- Amount Validation: Implement proper validation for transaction amounts
- Rate Limiting: Add rate limiting for API calls and transactions
- Error Handling: Comprehensive error handling and retry logic
// Add comprehensive logging
console.log(`[${new Date().toISOString()}] Transaction sent: ${signature}`);
// Monitor account balances
setInterval(async () => {
const balance = await connection.getBalance(new PublicKey(address));
console.log(`Current balance: ${balance / LAMPORTS_PER_SOL} SOL`);
}, 30000);- Connection Pooling: Reuse Solana RPC connections
- Batch Operations: Group multiple operations when possible
- Caching: Cache account information and metadata
- Async Operations: Use Promise.all for parallel operations
- Coinbase Developer Platform Documentation
- Solana Documentation
- Solana Web3.js Documentation
- Solana Explorer (Devnet)
- This demo operates on Solana Devnet with test tokens only
- Never commit private keys or API credentials to version control
- Review all transaction parameters before executing on mainnet
- Implement proper access controls for production deployments
- Monitor for unusual activity and implement alerting
Contributions are welcome! To contribute:
- Fork the repository
- Create a feature branch (
git checkout -b feature/solana-feature) - Commit your changes (
git commit -m 'Add Solana feature') - Push to the branch (
git push origin feature/solana-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Disclaimer: This project is for demonstration and educational purposes. Always test thoroughly on devnet before using mainnet. Solana operations involve transaction fees and potential loss of funds if misconfigured.
Built with ❤️ using Coinbase Developer Platform and Solana Web3.js.