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>Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
inputMint | string | Yes | Input token mint address |
outputMint | string | Yes | Output token mint address |
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') |
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 tokensconst 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');SOL Mint Addresses
Section titled “SOL Mint Addresses”You can use the standard Solana addresses:
| Type | Address |
|---|---|
| Native SOL | So11111111111111111111111111111111111111111 |
| Wrapped SOL | So11111111111111111111111111111111111111112 |
When to Use swap() vs buy()/sell()
Section titled “When to Use swap() vs buy()/sell()”| Use Case | Method |
|---|---|
| Buy tokens with SOL | buy() is simpler |
| Sell tokens for SOL | sell() is simpler |
Need exactOut mode | Use swap() |
| Custom input/output control | Use swap() |
- 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. - 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