buy()
Buy tokens by spending a specified amount of SOL.
buy(options: BuyOptions): Promise<TransactionResult>Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
mint | string | Yes | Token mint address to buy |
solAmount | number | Yes | Amount of SOL to spend |
slippage | number | Yes | Slippage tolerance (0.01 = 1%, 0.05 = 5%) |
priority | Priority | Yes | Transaction priority ('economy', 'fast', 'faster', 'fastest') |
maxPriceImpact | number | No | Maximum price impact allowed (0.1 = 10%) |
maxPriorityCost | number | No | Maximum priority fee in SOL |
Return Value
Section titled “Return Value”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}Example
Section titled “Example”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
exactInoperation. 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.05means5%, anything worse and the transaction fails. - Priority: Higher priority means faster execution but higher network fees.
- Input/Output perspective: The
inputMintandoutputMintfields in the response are from the DEX’s perspective (what goes into the DEX vs what comes out). For a buy operation,inputMintis SOL (what the DEX receives) andoutputMintis the token (what the DEX sends out).
Related
Section titled “Related”- sell() - Sell tokens for SOL
- swap() - Generic swap endpoint
- Priority - Priority explained
- POST /v1/tx/buy - Underlying API endpoint reference
- POST /v1/tx/submit - Submit API endpoint reference