Skip to main content

Overview

The Swap Instructions endpoint returns individual Solana instructions instead of a serialized transaction. This allows you to:
  • Add your own instructions (e.g., Jito tips, custom fee transfers)
  • Combine multiple swaps in a single transaction
  • Have full control over transaction construction
  • Avoid deserializing/re-serializing transactions which adds latency
Note: This endpoint is only available for Solana chains. Mobula’s execution engine handles the instruction generation internally, supporting multiple DEXs and liquidity sources (Raydium, Orca, Pumpfun, etc.).

Query Parameters

  • chainId (required) — The blockchain identifier. Must be solana or solana:solana
  • tokenIn (required) — Address of the token to swap from (use So11111111111111111111111111111111111111111 for native SOL)
  • tokenOut (required) — Address of the token to swap to (use So11111111111111111111111111111111111111111 for native SOL)
  • amount (required if amountRaw not provided) — Human-readable amount of tokenIn to swap (e.g., "1.5" for 1.5 tokens)
  • amountRaw (required if amount not provided) — Raw amount as a string (e.g., "1500000000" for 1.5 SOL with 9 decimals)
  • walletAddress (required) — Wallet address that will execute the swap
  • slippage (optional) — Maximum acceptable slippage percentage (0-100). Default: 1
  • excludedProtocols (optional) — Comma-separated list of factory addresses to exclude from routing
  • onlyProtocols (optional) — Comma-separated list of tradable pool types to restrict routing
  • poolAddress (optional) — Specific pool address to use for the swap
  • priorityFee (optional) — Priority fee configuration. Can be auto, a preset (low, medium, high, veryHigh), or a number in microLamports per CU
  • computeUnitLimit (optional) — Compute unit limit. Can be true for dynamic or a specific number. Default: 400,000
  • jitoTipLamports (optional) — Jito tip amount in lamports for block engine priority
  • feePercentage (optional) — Fee percentage to charge on the swap (0.01 to 99). Fee is always taken from native SOL (deducted from the swap amount). At least one side of the swap must be native SOL. Must be used together with feeWallet.
  • feeWallet (optional) — Wallet address to receive fees. Required when feePercentage is set.

Usage Examples

Basic Swap Instructions

curl -X GET "https://api.mobula.io/api/2/swap/quoting-instructions?chainId=solana&tokenIn=So11111111111111111111111111111111111111111&tokenOut=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v&amount=1&walletAddress=YourWalletAddress&slippage=1"

With Priority Fee

curl -X GET "https://api.mobula.io/api/2/swap/quoting-instructions?chainId=solana&tokenIn=So11111111111111111111111111111111111111111&tokenOut=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v&amount=1&walletAddress=YourWalletAddress&slippage=1&priorityFee=high"

With Jito Tip

curl -X GET "https://api.mobula.io/api/2/swap/quoting-instructions?chainId=solana&tokenIn=So11111111111111111111111111111111111111111&tokenOut=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v&amount=1&walletAddress=YourWalletAddress&slippage=1&jitoTipLamports=10000"

With Integration Fee

curl -X GET "https://api.mobula.io/api/2/swap/quoting-instructions?chainId=solana&tokenIn=So11111111111111111111111111111111111111111&tokenOut=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v&amount=1&walletAddress=YourWalletAddress&slippage=1&feePercentage=1&feeWallet=FeeWalletAddressHere"
This example charges a 1% fee on the swap. The fee is deducted from native SOL and sent to your feeWallet via a SystemProgram.transfer instruction.

Response Format

{
  "data": {
    "amountOutTokens": "150.123456",
    "slippagePercentage": 1,
    "tokenIn": {
      "address": "So11111111111111111111111111111111111111111",
      "name": "Wrapped SOL",
      "symbol": "SOL",
      "decimals": 9,
      "logo": "https://metadata.mobula.io/assets/logos/solana_solana_So11111111111111111111111111111111111111111.webp"
    },
    "tokenOut": {
      "address": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
      "name": "USD Coin",
      "symbol": "USDC",
      "decimals": 6,
      "logo": "https://metadata.mobula.io/assets/logos/solana_solana_EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v.webp"
    },
    "requestId": "550e8400-e29b-41d4-a716-446655440000",
    "solana": {
      "instructions": {
        "computeBudgetInstructions": [
          {
            "programId": "ComputeBudget111111111111111111111111111111",
            "accounts": [],
            "data": "AgAAABAnAAAAAAAA"
          }
        ],
        "setupInstructions": [
          {
            "programId": "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
            "accounts": [
              {
                "pubkey": "YourWalletAddress",
                "isSigner": true,
                "isWritable": true
              }
            ],
            "data": "..."
          }
        ],
        "swapInstructions": [
          {
            "programId": "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8",
            "accounts": [
              {
                "pubkey": "YourWalletAddress",
                "isSigner": true,
                "isWritable": true
              }
            ],
            "data": "..."
          }
        ],
        "cleanupInstructions": [
          {
            "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
            "accounts": [
              {
                "pubkey": "YourWalletAddress",
                "isSigner": false,
                "isWritable": true
              }
            ],
            "data": "..."
          }
        ],
        "addressLookupTableAddresses": [
          "GxS6FiQ3mNnAar9HGQ6mxP7t6FcwmHkU7peSeQDUHmpN"
        ]
      },
      "lastValidBlockHeight": 305123456,
      "recentBlockhash": "EkSnNWid2cvwEVnVx9aBqawnmiCNiDgp3gUdkDPTKN1N"
    }
  }
}

Response Fields

Data Object

  • amountOutTokens (string, optional) — Estimated output amount in tokens
  • slippagePercentage (number, optional) — Slippage percentage
  • tokenIn (object, optional) — Input token metadata
    • address (string) — Token contract address
    • name (string, optional) — Token name
    • symbol (string, optional) — Token symbol
    • decimals (number) — Token decimals
    • logo (string | null, optional) — Token logo URL
  • tokenOut (object, optional) — Output token metadata
  • requestId (string) — Unique identifier for tracking this request
  • solana (object) — Solana instructions container
    • instructions (object) — All instructions needed for the swap
      • computeBudgetInstructions (array, optional) — Instructions to set compute budget
      • setupInstructions (array, optional) — Setup instructions (e.g., create token accounts)
      • swapInstructions (array) — The swap instructions (can be multiple for multi-hop routes)
      • cleanupInstructions (array, optional) — Cleanup instructions (e.g., close token accounts)
      • addressLookupTableAddresses (array, optional) — ALT addresses for versioned transactions
    • lastValidBlockHeight (number) — The last block height at which the blockhash is valid
    • recentBlockhash (string) — Recent blockhash to use when building the transaction

Instruction Format

Each instruction contains:
  • programId (string) — The program that will process this instruction
  • accounts (array) — Account keys involved in the instruction
    • pubkey (string) — Account public key
    • isSigner (boolean) — Whether the account must sign
    • isWritable (boolean) — Whether the account is writable
  • data (string) — Instruction data as base64 encoded string

Building a Transaction

After receiving instructions, you need to build and sign the transaction yourself:
import {
  Connection,
  TransactionMessage,
  VersionedTransaction,
  PublicKey,
  TransactionInstruction,
  AddressLookupTableAccount,
} from '@solana/web3.js';

const { solana, requestId } = response.data;

// Save requestId for support
console.log('Request ID:', requestId);

// Helper to convert instruction format
function toTransactionInstruction(ix) {
  return new TransactionInstruction({
    programId: new PublicKey(ix.programId),
    keys: ix.accounts.map(acc => ({
      pubkey: new PublicKey(acc.pubkey),
      isSigner: acc.isSigner,
      isWritable: acc.isWritable,
    })),
    data: Buffer.from(ix.data, 'base64'),
  });
}

// Build all instructions
const instructions = [];

// 1. Add compute budget instructions (if any)
if (solana.instructions.computeBudgetInstructions) {
  for (const ix of solana.instructions.computeBudgetInstructions) {
    instructions.push(toTransactionInstruction(ix));
  }
}

// 2. Add setup instructions (if any)
if (solana.instructions.setupInstructions) {
  for (const ix of solana.instructions.setupInstructions) {
    instructions.push(toTransactionInstruction(ix));
  }
}

// 3. Add YOUR custom instructions here (e.g., Jito tip, fee transfer)
// Example: Add a Jito tip
const JITO_TIP_ACCOUNTS = [
  '96gYZGLnJYVFmbjzopPSU6QiEV5fGqZNyN9nmNhvrZU5',
  'HFqU5x63VTqvQss8hp11i4wVV8bD44PvwucfZ2bU7gRe',
  // ... more tip accounts
];
const tipAccount = JITO_TIP_ACCOUNTS[Math.floor(Math.random() * JITO_TIP_ACCOUNTS.length)];
const tipInstruction = SystemProgram.transfer({
  fromPubkey: wallet.publicKey,
  toPubkey: new PublicKey(tipAccount),
  lamports: 10000, // 0.00001 SOL
});
instructions.push(tipInstruction);

// 4. Add the swap instructions (can be multiple for multi-hop routes)
if (solana.instructions.swapInstructions) {
  for (const ix of solana.instructions.swapInstructions) {
    instructions.push(toTransactionInstruction(ix));
  }
}

// 5. Add cleanup instructions (if any)
if (solana.instructions.cleanupInstructions) {
  for (const ix of solana.instructions.cleanupInstructions) {
    instructions.push(toTransactionInstruction(ix));
  }
}

// Fetch Address Lookup Tables (if any)
const addressLookupTables = [];
if (solana.instructions.addressLookupTableAddresses?.length > 0) {
  const connection = new Connection('YOUR_RPC_URL');
  for (const address of solana.instructions.addressLookupTableAddresses) {
    const result = await connection.getAddressLookupTable(new PublicKey(address));
    if (result.value) {
      addressLookupTables.push(result.value);
    }
  }
}

// Build the versioned transaction
const messageV0 = new TransactionMessage({
  payerKey: wallet.publicKey,
  recentBlockhash: solana.recentBlockhash,
  instructions,
}).compileToV0Message(addressLookupTables);

const transaction = new VersionedTransaction(messageV0);

// Sign the transaction
transaction.sign([wallet]);

// Send the transaction
const signature = await connection.sendTransaction(transaction);

// Confirm with lastValidBlockHeight
await connection.confirmTransaction({
  signature,
  blockhash: solana.recentBlockhash,
  lastValidBlockHeight: solana.lastValidBlockHeight,
});

console.log('Transaction confirmed:', signature);

Important Notes

  • Solana Only: This endpoint only works with Solana chains
  • Mobula Execution Engine: Instructions are generated by Mobula’s internal execution engine, which routes through multiple DEXs (Raydium, Orca, Pumpfun, etc.) for optimal pricing
  • Blockhash Expiry: The recentBlockhash and lastValidBlockHeight have a limited validity window (~150 blocks / ~1 minute). Build and send your transaction quickly.
  • ALT Required: For complex swaps, Address Lookup Tables are required. Always fetch and include them if addressLookupTableAddresses is provided.
  • Request ID: Keep the requestId for troubleshooting with Mobula support

Comparison with /swap/quoting

Feature/swap/quoting/swap/quoting-instructions
ReturnsSerialized transactionIndividual instructions
Custom instructionsNeed to deserializeDirect insertion
LatencyLower (ready to sign)Slightly higher (need to build)
FlexibilityLimitedFull control
Chain supportSolana + EVMSolana only
Use caseSimple swapsAdvanced integrations