Skip to content

swap()

Swap between any supported token pair. This is the power endpoint that offers more flexibility than buy() and sell().

Static method

Curently we support only trading against SOL or wSOL.

swap(options: SwapOptions): Promise<TransactionResult>
ParameterTypeRequiredDescription
inputMintstringYesInput token mint address
outputMintstringYesOutput token mint address
amountnumberYesAmount to swap
swapModeSwapModeYes'exactIn' or 'exactOut'
slippagenumberYesSlippage tolerance (0.01 = 1%, 0.05 = 5%)
priorityPriorityYesTransaction priority ('economy', 'fast', 'faster', 'fastest')
maxPriorityCostnumberNoMaximum priority fee in SOL
ModeDescription
exactInYou specify the exact input amount. Output amount is estimated.
exactOutYou specify the exact output amount. Input amount is estimated.

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
outputMint: string; // Output token mint address
tradeResult: {
inputAmount: number; // Amount of input token spent (actual on-chain amount)
outputAmount: number; // Amount of output token 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',
});
// Swap exactly 0.01 SOL for tokens
const result = await sdk.swap({
inputMint: 'So11111111111111111111111111111111111111112', // Wrapped SOL
outputMint: 'token-mint-address',
amount: 0.01,
swapMode: 'exactIn',
slippage: 0.05,
priority: 'fast',
});
console.log('Transaction:', result.signature);
console.log('SOL spent:', result.tradeResult.inputAmount);
console.log('Tokens received:', result.tradeResult.outputAmount);
console.log('Priority fee:', result.priorityCost, 'SOL');

You can use the standard Solana addresses:

TypeAddress
Native SOLSo11111111111111111111111111111111111111111
Wrapped SOLSo11111111111111111111111111111111111111112
Use CaseMethod
Buy tokens with SOLbuy() is simpler
Sell tokens for SOLsell() is simpler
Need exactOut modeUse swap()
Custom input/output controlUse swap()
  • Input/Output perspective: The inputMint and outputMint parameters and response fields are from the DEX’s perspective (what goes into the DEX vs what comes out). You specify what token the DEX receives as input and what token the DEX sends as output.
  • 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.