Skip to content

swap()

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

swap(options: SwapOptions): Promise<TransactionResult>
ParameterTypeRequiredDescription
inputMintMintInputYesInput mint address or alias ('SOL', 'WSOL', 'USDC')
outputMintMintInputYesOutput mint address or alias
amountnumberYesAmount to swap
swapModeSwapModeYes'exactIn' or 'exactOut'
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
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 (alias form)
const result = await sdk.swap({
inputMint: 'SOL',
outputMint: 'token-mint-address',
amount: 0.01,
swapMode: 'exactIn',
slippage: 0.05,
priority: 'fast',
});
console.log('Transaction:', result.signature);
console.log('Input spent:', result.tradeResult.inputAmount);
console.log('Tokens received:', result.tradeResult.outputAmount);
console.log('Priority fee:', result.priorityCost, 'SOL');
const result = await sdk.swap({
inputMint: 'USDC',
outputMint: 'token-mint-address',
amount: 5,
swapMode: 'exactIn',
slippage: 0.05,
priority: 'fast',
});

Pass quote sides as aliases ('SOL', 'WSOL', 'USDC') or import Mints from @darkfibre/sdk:

import { Mints } from '@darkfibre/sdk';
Mints.SOL; // 'So11111111111111111111111111111111111111112'
Mints.WSOL; // 'So11111111111111111111111111111111111111112'
Mints.USDC; // 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'

SOL and WSOL resolve to the same mint address in the SDK. You can also pass any supported mint address directly as a string.

  • 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.
  • tradeResult.inputAmount: Reported in quote-currency human units (SOL, WSOL, USDC) when inputMint is a quote currency; otherwise in token human units.
  • tradeResult.outputAmount: Reported in quote-currency human units (SOL, WSOL, USDC) when outputMint is a quote currency; otherwise in token human units.
  • 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.