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>Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
inputMint | MintInput | Yes | Input mint address or alias ('SOL', 'WSOL', 'USDC') |
outputMint | MintInput | Yes | Output mint address or alias |
amount | number | Yes | Amount to swap |
swapMode | SwapMode | Yes | 'exactIn' or 'exactOut' |
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 |
Swap Modes
Section titled “Swap Modes”| Mode | Description |
|---|---|
exactIn | You specify the exact input amount. Output amount is estimated. |
exactOut | You specify the exact output amount. Input amount is estimated. |
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 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}Example
Section titled “Example”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');Swap USDC for tokens
Section titled “Swap USDC for tokens”const result = await sdk.swap({ inputMint: 'USDC', outputMint: 'token-mint-address', amount: 5, swapMode: 'exactIn', slippage: 0.05, priority: 'fast',});Quote currencies
Section titled “Quote currencies”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
inputMintandoutputMintparameters 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) wheninputMintis a quote currency; otherwise in token human units.tradeResult.outputAmount: Reported in quote-currency human units (SOL, WSOL, USDC) whenoutputMintis a quote currency; otherwise in token human units.- 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.
Related
Section titled “Related”- buy() - Simplified buying
- sell() - Simplified selling
- Priority - Priority explained
- POST /v1/tx/swap - Underlying API endpoint
- POST /v1/tx/submit - Submit API endpoint reference