First Trade
Time to execute your first trade. You’ll pick a token from pump.fun and buy it using the Darkfibre SDK.
Find a Token
Section titled “Find a Token”- Go to pump.fun
- Browse the tokens and find one you’d like to buy
- Click on the token to open its page
- Copy the token address (the long string in the URL or on the page, it will look something like this:
7GCihgDB8fe6KNjn2MYtkzZcRjQy3t9GHdC8uHY7pump)
You’ll use this address in the code below.
The Code
Section titled “The Code”Replace the contents of index.ts with:
import { DarkfibreSDK } from '@darkfibre/sdk';import 'dotenv/config';
async function firstTrade() { // Initialize the SDK with your credentials const sdk = new DarkfibreSDK({ apiKey: process.env.DARKFIBRE_API_KEY!, privateKey: process.env.SOLANA_PRIVATE_KEY!, });
// Replace with the token address you copied from pump.fun const TOKEN_ADDRESS = 'PASTE_YOUR_TOKEN_ADDRESS_HERE';
const result = await sdk.buy({ mint: TOKEN_ADDRESS, solAmount: 0.005, slippage: 0.05, // 5% slippage tolerance priority: 'fast', // Transaction priority });
console.log('Transaction:', result.signature); console.log('SOL spent:', result.tradeResult.inputAmount); console.log('Tokens received:', result.tradeResult.outputAmount); console.log('Priority fee:', result.priorityCost, 'SOL'); console.log(`View on Solscan: https://solscan.io/tx/${result.signature}`);}
firstTrade().catch(console.error);Replace PASTE_YOUR_TOKEN_ADDRESS_HERE with the token address you copied from pump.fun.
Run It
Section titled “Run It”Execute your trade:
tsx index.tsYou should see output like:
Transaction: 5Kj8...xyzSOL spent: 0.005Tokens received: 1234567.89Priority fee: 0.00001 SOLView on Solscan: https://solscan.io/tx/5Kj8...xyzVerify on Solscan
Section titled “Verify on Solscan”Click the Solscan link in the output (or copy it to your browser) to see your transaction on-chain. You’ll see:
- The SOL you spent
- The tokens you received
- Transaction fees
- The exact time it landed
What Just Happened?
Section titled “What Just Happened?”The SDK handled the complete flow for you:
- Built an optimized transaction via the Darkfibre API
- Signed the transaction locally with your private key
- Submitted the signed transaction for priority execution
- Returned the confirmed result with exact trade amounts
Your private key never left your device. The API built the transaction, but you signed it locally.
Troubleshooting
Section titled “Troubleshooting”- INSUFFICIENT_FUNDS - You don’t have enough SOL. Check your wallet balance and add more SOL.
- INSUFFICIENT_SOL - You have tokens but not enough SOL for transaction fees. Add more SOL to cover fees.
- SLIPPAGE_EXCEEDED - The price moved more than your 5% slippage tolerance. Try again (prices fluctuate) or increase the
slippagevalue. - AUTH_ERROR - Your API key is invalid. Check that
DARKFIBRE_API_KEYin your.envfile is correct.
Next Steps
Section titled “Next Steps”Congratulations! You’ve executed your first trade on Solana with Darkfibre.
Here’s where to go next:
- SDK Documentation - Learn about the SDK
- Code Examples - Browse through ready-to-go examples
- WebSocket - Subscribe to real-time on-chain events
- API Reference - Full REST API documentation
- Core Concepts - Understand the build-sign-submit flow in depth
Happy trading!