Skip to content

Buy + Sell

A complete buy/sell round-trip that validates your credentials work end-to-end.

View on GitHub

Terminal window
git clone https://github.com/darkfibre-dev/darkfibre-examples.git
cd darkfibre-examples/02-buy-sell
npm install
cp .env.example .env

Edit .env with your credentials:

Terminal window
DARKFIBRE_API_KEY=your-api-key
SOLANA_PRIVATE_KEY=your-base58-private-key

Edit index.ts to set the MINT constant to the token you want to trade.

Terminal window
npm start
import { DarkfibreSDK, APIError, ValidationError, SigningError } from '@darkfibre/sdk';
import 'dotenv/config';
// Configuration
const MINT = 'YOUR_TOKEN_MINT_HERE'; // Token to trade
const SOL_AMOUNT = 0.005; // SOL to spend on buy
const SLIPPAGE = 0.05; // 5% slippage tolerance
const PRIORITY = 'fast' as const; // Transaction priority
// Validate Environment
const { DARKFIBRE_API_KEY, SOLANA_PRIVATE_KEY } = process.env;
if (!DARKFIBRE_API_KEY) {
console.error('Missing DARKFIBRE_API_KEY in .env');
process.exit(1);
}
if (!SOLANA_PRIVATE_KEY) {
console.error('Missing SOLANA_PRIVATE_KEY in .env');
process.exit(1);
}
// Main
const sdk = new DarkfibreSDK({
apiKey: DARKFIBRE_API_KEY,
privateKey: SOLANA_PRIVATE_KEY,
});
try {
// Buy
console.log(`\n[BUY] Buying tokens with ${SOL_AMOUNT} SOL...`);
const buyResult = await sdk.buy({
mint: MINT,
solAmount: SOL_AMOUNT,
slippage: SLIPPAGE,
priority: PRIORITY,
});
console.log(`[BUY] Success!`);
console.log(` Tokens received: ${buyResult.tradeResult.outputAmount}`);
console.log(` SOL spent: ${buyResult.tradeResult.inputAmount}`);
console.log(` Priority fee: ${buyResult.priorityCost} SOL`);
console.log(` https://solscan.io/tx/${buyResult.signature}`);
// Wait
await new Promise((resolve) => setTimeout(resolve, 1000));
// Sell
console.log(`\n[SELL] Selling ${buyResult.tradeResult.outputAmount} tokens...`);
const sellResult = await sdk.sell({
mint: MINT,
tokenAmount: buyResult.tradeResult.outputAmount,
slippage: SLIPPAGE,
priority: PRIORITY,
});
console.log(`[SELL] Success!`);
console.log(` Tokens sold: ${sellResult.tradeResult.inputAmount}`);
console.log(` SOL received: ${sellResult.tradeResult.outputAmount}`);
console.log(` Priority fee: ${sellResult.priorityCost} SOL`);
console.log(` https://solscan.io/tx/${sellResult.signature}`);
} catch (error) {
if (error instanceof ValidationError) {
console.error(`\n[ERROR] Validation: ${error.message}`);
if (error.field) {
console.error(` Field: ${error.field}`);
}
} else if (error instanceof SigningError) {
console.error(`\n[ERROR] Signing: ${error.message}`);
console.error(' Check that your private key is valid and in base58 format.');
} else if (error instanceof APIError) {
console.error(`\n[ERROR] API [${error.code}]: ${error.message}`);
} else {
throw error;
}
process.exit(1);
}
  1. Initializes the SDK with your credentials
  2. Buys tokens with SOL
  3. Waits 1 second
  4. Sells all tokens back for SOL
  5. Prints detailed transaction info with Solscan links

Edit index.ts to change:

VariableDescriptionDefault
MINTToken mint address to trade-
SOL_AMOUNTAmount of SOL to spend on buy0.005
SLIPPAGESlippage tolerance (0.05 = 5%)0.05
PRIORITYTransaction priority'fast'

This example demonstrates handling all SDK error types:

Error TypeDescription
APIErrorAPI failures (auth, validation, insufficient funds)
ValidationErrorTrade limit exceeded (maxPriceImpact, maxPriorityCost)
SigningErrorTransaction signing failures
[BUY] Buying tokens with 0.005 SOL...
[BUY] Success!
Tokens received: 1234567.89
SOL spent: 0.005
Priority fee: 0.0001 SOL
https://solscan.io/tx/5xK7j...abc123
[SELL] Selling 1234567.89 tokens...
[SELL] Success!
Tokens sold: 1234567.89
SOL received: 0.00495
Priority fee: 0.0001 SOL
https://solscan.io/tx/3mN8p...def456