> 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/add-fees-to-swap/claiming-and-swapping-accumulated-fees.md).

# Claiming & swapping accumulated fees

Claim the fees your integration has accumulated, or convert them into one target asset.

Your fee accumulates on the collector as plain token balances — the fee comes out of each swap's output token, so a collector serving many pairs holds many tokens. Claims are per token, only the collector's `operator` can call them, and every claim pays out to the `fee_destination` fixed at deploy time.

{% 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 [complete code examples](#complete-code-examples) run each claim in one script, and the [contract-level flow](#contract-level-flow-any-language) below covers other languages.
{% endhint %}

### Read what has accrued

Reads need no signer. `balance` returns 0 for a token the collector has never held:

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

```python
from aquarius import AquariusClient, Asset, XLM

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

reader = AquariusClient(network="mainnet")
fees = reader.fee_collector("C...")   # your deployed collector

print(fees.fee_destination())         # where claims pay out
print(fees.balance(AQUA))             # accrued fees in AQUA, base units
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
import { AquariusClient, Asset, XLM } from "@aquariusdefi/sdk";

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

const reader = new AquariusClient({ network: "mainnet" });
const fees = reader.feeCollector("C...");   // your deployed collector

console.log(await fees.feeDestination());   // where claims pay out
console.log(await fees.balance(AQUA));      // accrued fees in AQUA, base units
```

{% endtab %}
{% endtabs %}

### Claim and convert into one asset

`claim_and_swap` claims a token's entire accrued balance, swaps it through the router along a fresh quoted route, and sends the output token to the fee destination. The minimum-out guard comes from the quote, so a moving market fails the claim instead of underdelivering. The signer must be the operator:

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

```python
from stellar_sdk import Keypair

USDC = Asset.classic("USDC", "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN")

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

claimed = collector.claim_and_swap(AQUA, USDC, slippage=0.01)
print(f"claimed {claimed.amount_in} AQUA -> {claimed.amount_out} USDC")
print(f"transaction: {claimed.tx_hash}")
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
import { Keypair } from "@stellar/stellar-sdk";

const USDC = Asset.classic("USDC", "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN");

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

const claimed = await collector.claimAndSwap(AQUA, USDC, { slippage: 0.01 });
console.log(`claimed ${claimed.amountIn} AQUA -> ${claimed.amountOut} USDC`);
console.log(`transaction: ${claimed.txHash}`);
```

{% endtab %}
{% endtabs %}

### Claim raw

`claim` sweeps one token's balance to the fee destination without conversion. For classic assets the destination needs a trustline for that token:

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

```python
raw = collector.claim(AQUA)
print(f"claimed {raw.amount_out} AQUA  {raw.tx_hash}")
```

{% endtab %}

{% tab title="JavaScript" %}

```javascript
const raw = await collector.claim(AQUA);
console.log(`claimed ${raw.amountOut} AQUA  ${raw.txHash}`);
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
Claims are operator-gated by the contract: `claim_fees` and `claim_fees_and_swap` require the operator's signature and reject every other caller. The payout address is equally fixed — the operator chooses the token and the route, never the destination.
{% endhint %}

### Complete code examples

The scripts below claim from your collector on the Stellar mainnet. To execute them, provide the secret key of the collector's operator and the address of your collector. Save the JavaScript versions as `.mjs` files: they use top-level `await`.

#### Claim AQUA and swap it into XLM

This script reads the collector's accrued AQUA, claims all of it, swaps it into XLM along a freshly quoted route with 0.5% slippage, and pays the XLM out to the fee destination.

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

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

# =========================================
# Configuration & Setup
# =========================================

# The collector's operator: the only account that can claim
operator_secret_key = "S..."

# Your deployed fee collector
collector_address = "C..."

# Token to claim and the token to receive
token_to_claim = Asset.classic("AQUA", "GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA")
token_out = XLM

# Slippage tolerance: 0.5%
slippage = 0.005

aqua = AquariusClient(network="mainnet", signer=Keypair.from_secret(operator_secret_key))
collector = aqua.fee_collector(collector_address)

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

# 1. Read the accrued balance and the payout address
balance = collector.balance(token_to_claim)
print(f"Accrued: {balance / 1e7} {token_to_claim.code}")
print(f"Pays out to: {collector.fee_destination()}")

# 2. Claim the whole balance and swap it along a freshly quoted route
if balance > 0:
    claimed = collector.claim_and_swap(token_to_claim, token_out, slippage=slippage)
    print(f"Transaction: {claimed.tx_hash}")
    print(f"Claimed {claimed.amount_in / 1e7} {token_to_claim.code} => {claimed.amount_out / 1e7} {token_out.code}")
else:
    print("Nothing to claim")
```

{% endtab %}

{% tab title="JavaScript" %}

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

// =========================================
// Configuration & Setup
// =========================================

// The collector's operator: the only account that can claim
const operatorSecretKey = "S...";

// Your deployed fee collector
const collectorAddress = "C...";

// Token to claim and the token to receive
const tokenToClaim = Asset.classic("AQUA", "GBNZILSTVQZ4R7IKQDGHYGY2QXL5QOFJYQMXPKWRRM5PAV7Y4M67AQUA");
const tokenOut = XLM;

// Slippage tolerance: 0.5%
const slippage = 0.005;

const aqua = new AquariusClient({ network: "mainnet", signer: Keypair.fromSecret(operatorSecretKey) });
const collector = aqua.feeCollector(collectorAddress);

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

// 1. Read the accrued balance and the payout address
const balance = await collector.balance(tokenToClaim);
console.log(`Accrued: ${Number(balance) / 1e7} ${tokenToClaim.code}`);
console.log(`Pays out to: ${await collector.feeDestination()}`);

// 2. Claim the whole balance and swap it along a freshly quoted route
if (balance > 0n) {
  const claimed = await collector.claimAndSwap(tokenToClaim, tokenOut, { slippage });
  console.log(`Transaction: ${claimed.txHash}`);
  console.log(`Claimed ${Number(claimed.amountIn) / 1e7} ${tokenToClaim.code} => ${Number(claimed.amountOut) / 1e7} ${tokenOut.code}`);
} else {
  console.log("Nothing to claim");
}
```

{% endtab %}
{% endtabs %}

#### Claim XLM raw

This script reads the collector's accrued XLM and transfers all of it to the fee destination without conversion.

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

```python
from stellar_sdk import Keypair
from aquarius import AquariusClient, XLM

# =========================================
# Configuration & Setup
# =========================================

# The collector's operator: the only account that can claim
operator_secret_key = "S..."

# Your deployed fee collector
collector_address = "C..."

# Token to claim
token_to_claim = XLM

aqua = AquariusClient(network="mainnet", signer=Keypair.from_secret(operator_secret_key))
collector = aqua.fee_collector(collector_address)

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

# 1. Read the accrued balance and the payout address
balance = collector.balance(token_to_claim)
print(f"Accrued: {balance / 1e7} {token_to_claim.code}")
print(f"Pays out to: {collector.fee_destination()}")

# 2. Transfer the whole balance to the fee destination
if balance > 0:
    claimed = collector.claim(token_to_claim)
    print(f"Transaction: {claimed.tx_hash}")
    print(f"Claimed: {claimed.amount_out / 1e7} {token_to_claim.code}")
else:
    print("Nothing to claim")
```

{% endtab %}

{% tab title="JavaScript" %}

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

// =========================================
// Configuration & Setup
// =========================================

// The collector's operator: the only account that can claim
const operatorSecretKey = "S...";

// Your deployed fee collector
const collectorAddress = "C...";

// Token to claim
const tokenToClaim = XLM;

const aqua = new AquariusClient({ network: "mainnet", signer: Keypair.fromSecret(operatorSecretKey) });
const collector = aqua.feeCollector(collectorAddress);

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

// 1. Read the accrued balance and the payout address
const balance = await collector.balance(tokenToClaim);
console.log(`Accrued: ${Number(balance) / 1e7} ${tokenToClaim.code}`);
console.log(`Pays out to: ${await collector.feeDestination()}`);

// 2. Transfer the whole balance to the fee destination
if (balance > 0n) {
  const claimed = await collector.claim(tokenToClaim);
  console.log(`Transaction: ${claimed.txHash}`);
  console.log(`Claimed: ${Number(claimed.amountOut) / 1e7} ${tokenToClaim.code}`);
} else {
  console.log("Nothing to claim");
}
```

{% endtab %}
{% endtabs %}

### Contract-level flow (any language)

The collector functions behind `claim_and_swap` and `claim`, called directly. Both are listed in [Provider fee contracts](https://docs.aqua.network/developers/reference/router-and-pool-contracts.md#provider-fee-contracts); they need the operator's authorization for the root call only, as listed in [Contract authorization](https://docs.aqua.network/developers/reference/contract-authorization.md#authorization-trees).

#### Claim and immediately swap

* **Function:** `claim_fees_and_swap(e, operator: Address, swaps_chain: Vec<(Vec<Address>, BytesN<32>, Address)>, token: Address, out_min: u128) -> u128`
* **Effect:**
  1. Claims all of `token`.
  2. Swaps via your router along `swaps_chain`.
  3. Sends resulting tokens to `fee_destination`.
* **Returns:**\
  Final output amount in the target asset.

`claim_and_swap` requests `swaps_chain` from the [Find Path API](https://docs.aqua.network/developers/reference/backend-api.md) for the collector's current balance of `token`, and passes the quote's guard as `out_min`.

#### Claim raw fees

* **Function:** `claim_fees(e, operator: Address, token: Address) -> u128`
* **Effect:** Transfers entire `token` balance from the contract to `fee_destination`.
* **Returns:**\
  The amount transferred.
