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.
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 slippageconst 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 slippage3. 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)
execute() calls the router's single-pool swap. Every pool contract also exposes its own swap and swap_strict_receive, which cost fewer resources inside a contract sub-invocation; see Pool contract functions. The authorization tree of each call is listed in Contract authorization.
To swap against the pool contract directly, follow these steps:
1. Identify pool address: see Get pool contract ID and pool hash.
2. Specify the amount and the swap direction: the amount of the input token in stroops, and the indices of the input and output tokens (in_idx, out_idx) in the pool's sorted token vector, which the pool's get_tokens returns.
3. Calculate minimum amount of token to receive: simulate the pool's estimate_swap(in_idx, out_idx, in_amount) and reduce the estimate by your slippage tolerance to get out_min.
4. Perform swap operation by submitting corresponding transaction: call the pool's swap(user, in_idx, out_idx, in_amount, out_min) with the authorization tree listed for a direct pool swap in Contract authorization. The call returns the amount of the output token received.
Complete code examples
This code swaps 0.1 XLM to AQUA through the volatile XLM/AQUA pool with a 0.3% fee on mainnet.
To execute the code, provide the secret key of a Stellar account with at least 3 XLM. The script creates the AQUA trustline when the account has none. Save the JavaScript version as a .mjs file: it uses top-level await.
Last updated