buy()
Buy tokens by spending a specified amount of a quote currency (SOL, WSOL or USDC).
buy(options: BuyOptions): Promise<TransactionResult>Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
mint | string | Yes | Token mint address to buy |
quoteAmount | number | Yes | Amount of the quote currency to spend |
quoteMint | MintInput | Yes | Quote currency ('SOL', 'WSOL', 'USDC' or a supported mint address) |
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 (quote currency for buy) outputMint: string; // Output token mint address tradeResult: { inputAmount: number; // Quote currency spent (actual on-chain amount) outputAmount: number; // 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: '5UUH9RTDiSpq6HKS6bp4NdU9PNJpXRXuiw6ShBTBhgH2', // TROLL quoteAmount: 0.01, quoteMint: 'SOL', slippage: 0.05, priority: 'fast',});
console.log('Transaction:', result.signature);console.log('Tokens received:', result.tradeResult.outputAmount);console.log('Quote spent:', result.tradeResult.inputAmount);console.log('Priority fee:', result.priorityCost, 'SOL');Buy with USDC
Section titled “Buy with USDC”const result = await sdk.buy({ mint: 'token-mint-address', quoteAmount: 10, quoteMint: 'USDC', slippage: 0.05, priority: 'fast',});- Mode: This is an
exactInoperation. You specify the exact quote amount 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 the quote currency (what the DEX receives) andoutputMintis the token (what the DEX sends out). tradeResult.inputAmount: Reported in quote-currency human units (SOL, WSOL, USDC).
Related
Section titled “Related”- sell() - Sell tokens for a quote currency
- swap() - Generic swap endpoint
- Priority - Priority explained
- POST /v1/tx/swap - Underlying API endpoint reference
- POST /v1/tx/submit - Submit API endpoint reference