Dedicated Nodes can optionally use the Swap API plug-in. When enabled, this allows you to build transactions and get on-chain prices for any token on major DEXs with slippage protection. Select the Swap API plug-in when you purchase a dedicated node to use it. You then interact with it via HTTP requests or using the SDK we provide.
Using the SDK
Our Typescript SDK for the Swap API is hosted at https://www.npmjs.com/package/@hellomoon/swap-api. It has examples of how to create a connection, get a swap quote, and execute a swap transaction.
Connecting to the Swap API
The Swap API is hosted on your dedicated node at the base URL of<RPC_URL>/api/swap/
. Use this base URL with the endpoints below.
Authorization
The Swap API routes can be accessed with your RPC token in the authorization header of the POST request. For example if your dedicated node is https://raging-river.hellomoon.io/hf0e87h7ewh98 you would set an authorization header like { Authorization: "Bearer hf0e87h7ewh98" }
on all routes.
Get a quote
You can get a quote for a token swap by sending a POST request to the /get_quote
endpoint of the Swap API.
The parameters it takes are:
tokenIn: string; // public key of the token you are providing
tokenOut: string; // public key of the token you want to swap for
action: Action; // Set to 'exactIn' or 'exactOut'
amount: string; // Amount specified in Lamports
slippageBps?: number; // Slippage Basis Points. Optional.
maxHops?: number; // Optional.
exemptFees?: boolean; // Optional.
Response
This will return a response in the form of:
type GetQuoteResponse = {
aggregatorFee: string; // u64 as string
slot: string; // u64 as string
timestamp: string; // u64 as string
path: GenericQuote[]; // Vec of quotes
minAmountOutOrMaxIn: string; // u64 as string
};
type GenericQuote = {
pool: string;
swapContext: any; // This might need a more specific type depending on your needs
direction: string;
amountIn: string; // u64 as string
amountOut: string; // u64 as string
feeTaken: string; // u64 as string
token0Impact: string; // Bps as decimal string
token1Impact: string; // Bps as decimal string
};
Create a versioned transaction
Get a versioned transaction by sending a POST request to the /get_transaction
endpoint with the following parameters
tokenIn: string; // public key of the token you are providing
tokenOut: string; // public key of the token you want to swap for
action: Action; // set to 'exactIn' or 'exactOut'
amount: string; // amount in Lamports
userPubkey: string; // public key of your wallet
maxHops?: number; // optional since it has a default
slippageBps?: number; // optional since it has a default
Response
This will return a response in the form:
txn: string; // this is the versioned transaction
Execute the versioned transaction
Once you have a versioned transaction, you need to execute it. This is not specific to the Swap API and you can do this using any library. An example of how to do this in TypeScript might look like:
// Import the libraries we need
import { Connection, VersionedTransaction } from "@solana/web3.js";
import bs58 from "bs58";
// Assume you already have these variables set up
conn = // this is a connection to your RPC
versionedTxn = // this is the versioned transaction returned from the Swap API that you want to execute
// Deserialize the transaction
txn = VersionedTransaction.deserialize(bs58.decode(versionedTxn));
// Sign the transaction with your wallet
txn.sign([keyPair]);
// Simulate the transation
const sim = await conn.simulateTransaction(txn);
// If there are any problems, bail out
if (sim.value.err) {
console.error(sim.value.err);
console.error(sim.value.logs);
throw new Error("Transaction Sim Response resulted in error");
}
// Send the transaction!
await conn.sendRawTransaction(txn.serialize());