> For the complete documentation index, see [llms.txt](https://docs.aqua.network/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aqua.network/developers/code-examples/executing-swaps-through-specific-pool.md).

# 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.

{% hint style="info" %}
This page uses the [official SDKs](https://docs.aqua.network/developers/integrating-with-aquarius.md#official-sdks) (0.6.0 or later). The [contract-level flow](#contract-level-flow-any-language) below covers other languages and contract sub-invocations.
{% endhint %}

{% hint style="warning" %}
The signer's account needs a trustline for the asset it receives — otherwise the swap fails until the trustline exists. `ensure_trustline` creates it on the signer's account when it is missing. See the [Stellar documentation on trustlines](https://developers.stellar.org/docs/learn/fundamentals/stellar-data-structures/accounts#trustlines).
{% endhint %}

### 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:

{% tabs %}
{% tab title="Python" %}

```python
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)
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
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);
```

{% endtab %}
{% endtabs %}

### 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:

{% tabs %}
{% tab title="Python" %}

```python
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
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
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
```

{% endtab %}
{% endtabs %}

### 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:

{% tabs %}
{% tab title="Python" %}

```python
receipt = pool.swap(XLM, AQUA, amount_in=1000000, slippage=0.01, retries=3)

print(f"swapped: {receipt.amount_in} -> {receipt.amount_out}")
print(f"transaction: {receipt.tx_hash}")
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
const receipt = await pool.swap({ from: XLM, to: AQUA, amountIn: 1000000n, slippage: 0.01, retries: 3 });

console.log(`swapped: ${receipt.amountIn} -> ${receipt.amountOut}`);
console.log(`transaction: ${receipt.txHash}`);
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
Single-pool swaps are exact-input only: the router has no single-pool strict-receive. For an exact output amount, use the routed path described in [Executing swaps through optimal path](https://docs.aqua.network/developers/code-examples/executing-swaps-through-optimal-path.md).
{% endhint %}

### Contract-level flow (any language)

`execute()` calls the router's single-pool [`swap`](https://docs.aqua.network/developers/reference/router-and-pool-contracts.md#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](https://docs.aqua.network/developers/reference/router-and-pool-contracts.md#pool-contract-functions). The authorization tree of each call is listed in [Contract authorization](https://docs.aqua.network/developers/reference/contract-authorization.md#authorization-trees).

To swap against the pool contract directly, follow these steps:

1\. **Identify pool address**: see [Get pool contract ID and pool hash](https://docs.aqua.network/developers/code-examples/prerequisites-and-basics.md#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](https://docs.aqua.network/developers/reference/contract-authorization.md#authorization-trees). 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`.

<details>

<summary>Copy the full code Python</summary>

```python
from stellar_sdk import Keypair
from aquarius import AquariusClient, AquariusError, Asset, SlippageError, XLM

# =========================================
# Configuration and Input Variables
# =========================================

# This account must have at least 3 XLM.
user_secret_key = "S..."

# Input and output tokens
token_in = XLM
token_out = Asset.classic("AQUA", "GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA")

# The pool to swap through: the volatile XLM/AQUA pool with a 0.3% fee
pool_type = "volatile"
pool_fee_bps = 30

# 0.1 XLM in stroops (1 XLM = 10^7 stroops)
amount_in = 1000000

# Slippage tolerance: 1%
slippage = 0.01

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

# =========================================
# Entry Point
# =========================================

pools = aqua.pools_for_pair(token_in, token_out)
pool = next(p for p in pools if p.type == pool_type and p.fee_bps == pool_fee_bps)
print(f"Pool: {pool.address}")

try:
    # The output asset needs a trustline; this creates it when it is missing.
    aqua.ensure_trustline(token_out)

    # 1. Estimate the output with the router's estimate_swap for this pool
    quote = pool.quote(token_in, token_out, amount_in=amount_in, slippage=slippage)
    print(f"Estimated result: {quote.amount_out / 1e7} {token_out.code}, minimum: {quote.guard / 1e7}")

    # 2. Execute the swap; re-quote once if the pool moved past the guard
    try:
        receipt = quote.execute()
    except SlippageError as error:
        quote = error.requote()
        print("Price moved; executing a fresh quote")
        receipt = quote.execute()
except AquariusError as error:
    if error.tx_hash:
        print(f"Failed or unconfirmed transaction: {error.tx_hash}")
    raise

print(f"Transaction: {receipt.tx_hash}")
print(f"Received token out: {receipt.amount_out / 1e7} {token_out.code}")
```

</details>

<details>

<summary>Copy the full code JavaScript</summary>

```javascript
import { Keypair } from "@stellar/stellar-sdk";
import { AquariusClient, AquariusError, Asset, SlippageError, XLM } from "@aquariusdefi/sdk";

// =========================================
// Configuration and Input Variables
// =========================================

// This account must have at least 3 XLM.
const userSecretKey = "S...";

// Input and output tokens
const tokenIn = XLM;
const tokenOut = Asset.classic("AQUA", "GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA");

// The pool to swap through: the volatile XLM/AQUA pool with a 0.3% fee
const poolType = "volatile";
const poolFeeBps = 30;

// 0.1 XLM in stroops (1 XLM = 10^7 stroops)
const amountIn = 1000000n;

// Slippage tolerance: 1%
const slippage = 0.01;

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

// =========================================
// Entry Point
// =========================================

const pools = await aqua.pools.forPair(tokenIn, tokenOut);
const pool = pools.find(p => p.type === poolType && p.feeBps === poolFeeBps);
console.log(`Pool: ${pool.address}`);

let receipt;
try {
  // The output asset needs a trustline; this creates it when it is missing.
  await aqua.ensureTrustline(tokenOut);

  // 1. Estimate the output with the router's estimate_swap for this pool
  let quote = await pool.quote({ from: tokenIn, to: tokenOut, amountIn, slippage });
  console.log(`Estimated result: ${Number(quote.amountOut) / 1e7} ${tokenOut.code}, minimum: ${Number(quote.guard) / 1e7}`);

  // 2. Execute the swap; re-quote once if the pool moved past the guard
  try {
    receipt = await quote.execute();
  } catch (error) {
    if (!(error instanceof SlippageError)) throw error;
    quote = await error.requote();
    console.log("Price moved; executing a fresh quote");
    receipt = await quote.execute();
  }
} catch (error) {
  if (error instanceof AquariusError && error.txHash) {
    console.log(`Failed or unconfirmed transaction: ${error.txHash}`);
  }
  throw error;
}

console.log(`Transaction: ${receipt.txHash}`);
console.log(`Received token out: ${Number(receipt.amountOut) / 1e7} ${tokenOut.code}`);
```

</details>
