Skip to content

buy()

Buy tokens by spending a specified amount of SOL.

buy(options: BuyOptions): Promise<TransactionResult>
ParameterTypeRequiredDescription
mintstringYesToken mint address to buy
solAmountnumberYesAmount of SOL to spend
slippagenumberYesSlippage tolerance (0.01 = 1%, 0.05 = 5%)
priorityPriorityYesTransaction priority ('economy', 'fast', 'faster', 'fastest')
maxPriceImpactnumberNoMaximum price impact allowed (0.1 = 10%)
maxPriorityCostnumberNoMaximum priority fee in SOL

Returns a TransactionResult object with transaction details and trade amounts.

interface TransactionResult {
signature: string; // Transaction signature (use for block explorers)
status: string; // Transaction status (e.g., "confirmed")
slot: number; // Solana slot number where transaction landed
platform: string; // Trading platform (e.g., "pump_fun")
inputMint: string; // Input token mint address (SOL for buy)
outputMint: string; // Output token mint address
tradeResult: {
inputAmount: number; // Amount of SOL spent (actual on-chain amount)
outputAmount: number; // Amount of tokens received (actual on-chain amount)
};
priorityCost: number; // Priority fee cost in SOL
}
import { DarkfibreSDK } from '@darkfibre/sdk';
const sdk = new DarkfibreSDK({
apiKey: 'your-api-key',
privateKey: 'your-base58-private-key',
});
const result = await sdk.buy({
mint: 'DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263', // BONK
solAmount: 0.01,
slippage: 0.05,
priority: 'fast',
});
console.log('Transaction:', result.signature);
console.log('Tokens received:', result.tradeResult.outputAmount);
console.log('SOL spent:', result.tradeResult.inputAmount);
console.log('Priority fee:', result.priorityCost, 'SOL');
  • Mode: This is an exactIn operation. You specify the exact SOL to spend, and receive an estimated token amount.
  • Slippage: The maximum price change you are willing to accept during the trade. 0.05 means 5%, anything worse and the transaction fails.
  • Priority: Higher priority means faster execution but higher network fees.
  • Input/Output perspective: The inputMint and outputMint fields in the response are from the DEX’s perspective (what goes into the DEX vs what comes out). For a buy operation, inputMint is SOL (what the DEX receives) and outputMint is the token (what the DEX sends out).