Skip to content

buy()

Buy tokens by spending a specified amount of a quote currency (SOL, WSOL or USDC).

buy(options: BuyOptions): Promise<TransactionResult>
ParameterTypeRequiredDescription
mintstringYesToken mint address to buy
quoteAmountnumberYesAmount of the quote currency to spend
quoteMintMintInputYesQuote currency ('SOL', 'WSOL', 'USDC' or a supported mint address)
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 (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
}
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');
const result = await sdk.buy({
mint: 'token-mint-address',
quoteAmount: 10,
quoteMint: 'USDC',
slippage: 0.05,
priority: 'fast',
});
  • Mode: This is an exactIn operation. 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.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 the quote currency (what the DEX receives) and outputMint is the token (what the DEX sends out).
  • tradeResult.inputAmount: Reported in quote-currency human units (SOL, WSOL, USDC).