Skip to content

First Trade

Time to execute your first trade. You’ll pick a token from pump.fun and buy it using the Darkfibre SDK.

  1. Go to pump.fun
  2. Browse the tokens and find one you’d like to buy
  3. Click on the token to open its page
  4. 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.

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.

Execute your trade:

Terminal window
tsx index.ts

You should see output like:

Transaction: 5Kj8...xyz
SOL spent: 0.005
Tokens received: 1234567.89
Priority fee: 0.00001 SOL
View on Solscan: https://solscan.io/tx/5Kj8...xyz

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

The SDK handled the complete flow for you:

  1. Built an optimized transaction via the Darkfibre API
  2. Signed the transaction locally with your private key
  3. Submitted the signed transaction for priority execution
  4. 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.

  • 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 slippage value.
  • AUTH_ERROR - Your API key is invalid. Check that DARKFIBRE_API_KEY in your .env file is correct.

Congratulations! You’ve executed your first trade on Solana with Darkfibre.

Here’s where to go next:

Happy trading!