For the complete documentation index, see llms.txt. This page is also available as Markdown.

Executing swaps through specific pool

Pin a swap to one known pool with the official SDK — quote through the router's own estimator and execute without the path-finding API.

Swap against one pool you choose, bypassing path-finding entirely. pool.quote() asks the router's on-chain estimate_swap for that pool's hash, and execute() submits the router's single-pool swap with a minimum-out guard derived from the quote. The only network dependency is the RPC — for your own routing, a single-market integration, or isolation from other pools.

This page uses the official SDKs (0.4.0 or later). The contract-level flow below covers other languages and contract sub-invocations.

1. Set up the client and pick the pool

A pair can have several pools — different types and fee tiers. Pick the exact one you trust:

from stellar_sdk import Keypair
from aquarius import AquariusClient, Asset, XLM

AQUA = Asset.classic("AQUA", "GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA")

aqua = AquariusClient(network="mainnet", signer=Keypair.from_secret("S..."))

pools = aqua.pools_for_pair(XLM, AQUA)
pool = next(p for p in pools if p.type == "volatile" and p.fee_bps == 30)
import { Keypair } from "@stellar/stellar-sdk";
import { AquariusClient, Asset, XLM } from "@aquariusdefi/sdk";

const AQUA = Asset.classic("AQUA", "GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA");

const aqua = new AquariusClient({ network: "mainnet", signer: Keypair.fromSecret("S...") });

const pools = await aqua.pools.forPair(XLM, AQUA);
const pool = pools.find(p => p.type === "volatile" && p.feeBps === 30);

2. Quote against this pool only

The quote runs the router's estimate_swap for this pool's hash — no signer required, no path-finding API involved:

quote = pool.quote(XLM, AQUA, amount_in=1000000, slippage=0.01)   # 0.1 XLM in stroops

print(f"estimated out: {quote.amount_out}")   # base units
print(f"guaranteed minimum: {quote.guard}")   # estimate reduced by slippage
const quote = await pool.quote({ from: XLM, to: AQUA, amountIn: 1000000n, slippage: 0.01 });   // 0.1 XLM in stroops

console.log(`estimated out: ${quote.amountOut}`);    // base units
console.log(`guaranteed minimum: ${quote.guard}`);   // estimate reduced by slippage

3. Execute

execute() submits the router's single-pool swap pinned to this pool, with the quote's guard as the on-chain minimum. If the pool moves past the guard between quote and inclusion, the transaction fails with a SlippageError whose requote() re-reads the same pool. For bots, pool.swap() combines quote, execute, and retries:

Contract-level flow (any language)

The SDK methods above wrap the router's single-pool entry points. The steps below call the pool contract directly — useful from other languages and for contract sub-invocations, where a direct pool call costs fewer resources than a chained swap.

To perform a swap, you need to follow these steps:

1. Identify pool address: This part was covered earlier, please check corresponding article.

2. Specify user secret key, pool address, amount in, input and output token indices: You need to specify pool address, the amount of the input token you want to swap in stroops and swap direction by in and out tokens.

3. Calculate minimum amount of token to receive: This can be achieved by simulating estimate_swap pool method.

4. Perform swap operation by submitting corresponding transaction.

Complete code examples

This code swaps 0.1 XLM to AQUA with Aquarius AMM on mainnet.

To successfully execute the code, provide the secret key of a Stellar account with at least 3 XLM and an established trustline for AQUA.

Copy the full code Python
Copy the full code JavaScript

Last updated