Swap docs
Look up any call, event, or storage item with code examples. Runtime v490 · Block #9,478,217
Developer documentation for the Swap pallet of the Subtensor blockchain, part of the Bittensor decentralized AI network. This page is designed for developers integrating with the chain: look up any call, event, or storage item, copy code examples in TypeScript, Python, or Rust, and filter by your role. For in-depth explanations, version history, and relationships between items, see the Reference section.
Swap
Set the fee rate for swaps on a specific subnet (normalized value).
Calls 6
Sets the swap fee rate for a specific subnet's liquidity pool.
Parameters
| Name | Type | Format | Description |
|---|---|---|---|
netuid | NetUid | — | netuid: Subnet ID (u16, 0-65535) |
rate | u16 | Integer (0-65535) | rate (u16) |
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Build set_fee_rate call (typed, named args)
const netuid = 1;
const rate = 0;
const tx = api.tx.Swap.set_fee_rate({
netuid,
rate,
});Deprecated since v423. Always fails with Swap.Deprecated; the pool holds protocol liquidity only.
Parameters
| Name | Type | Format | Description |
|---|---|---|---|
hotkey | AccountId | SS58 Address | hotkey: Account address (32 bytes, SS58-encoded) |
netuid | NetUid | — | netuid: Subnet ID (u16, 0-65535) |
tick_low | TickIndex | Tick Index (i32) | tick_low: Tick index (AMM) |
tick_high | TickIndex | Tick Index (i32) | tick_high: Tick index (AMM) |
liquidity | u64 | Integer | liquidity (u64) |
// ----------------------------------------------------------------------
// HEADS UP: 2 args below have a complex type with no usable default.
// Look for `undefined as any` and replace them with real values
// before running — the snippet compiles, but will fail at runtime as-is.
// ----------------------------------------------------------------------
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Build add_liquidity call (typed, named args)
const hotkey = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
const netuid = 1;
const tick_low = undefined as any /* TickIndex — replace with real value */;
const tick_high = undefined as any /* TickIndex — replace with real value */;
const liquidity = 1_000_000_000n;
const tx = api.tx.Swap.add_liquidity({
hotkey,
netuid,
tick_low,
tick_high,
liquidity,
});Deprecated since v423. Always fails with Swap.Deprecated.
Parameters
| Name | Type | Format | Description |
|---|---|---|---|
hotkey | AccountId | SS58 Address | hotkey: Account address (32 bytes, SS58-encoded) |
netuid | NetUid | — | netuid: Subnet ID (u16, 0-65535) |
position_id | PositionId | Position ID (u64) | position_id: AMM position identifier |
// ----------------------------------------------------------------------
// HEADS UP: 1 arg below has a complex type with no usable default.
// Look for `undefined as any` and replace it with real value
// before running — the snippet compiles, but will fail at runtime as-is.
// ----------------------------------------------------------------------
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Build remove_liquidity call (typed, named args)
const hotkey = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
const netuid = 1;
const position_id = undefined as any /* PositionId — replace with real value */;
const tx = api.tx.Swap.remove_liquidity({
hotkey,
netuid,
position_id,
});Deprecated since v423. Always fails with Swap.Deprecated.
Parameters
| Name | Type | Format | Description |
|---|---|---|---|
hotkey | AccountId | SS58 Address | hotkey: Account address (32 bytes, SS58-encoded) |
netuid | NetUid | — | netuid: Subnet ID (u16, 0-65535) |
position_id | PositionId | Position ID (u64) | position_id: AMM position identifier |
liquidity_delta | i64 | Signed Integer | liquidity_delta (i64) |
// ----------------------------------------------------------------------
// HEADS UP: 1 arg below has a complex type with no usable default.
// Look for `undefined as any` and replace it with real value
// before running — the snippet compiles, but will fail at runtime as-is.
// ----------------------------------------------------------------------
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Build modify_position call (typed, named args)
const hotkey = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
const netuid = 1;
const position_id = undefined as any /* PositionId — replace with real value */;
const liquidity_delta = 0n;
const tx = api.tx.Swap.modify_position({
hotkey,
netuid,
position_id,
liquidity_delta,
});Deprecated since v423. Always fails with Swap.Deprecated; user liquidity is permanently disabled.
Parameters
| Name | Type | Format | Description |
|---|---|---|---|
netuid | NetUid | — | netuid: Subnet ID (u16, 0-65535) |
enable | bool | Boolean (true/false) | enable (bool) |
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Build toggle_user_liquidity call (typed, named args)
const netuid = 1;
const enable = true;
const tx = api.tx.Swap.toggle_user_liquidity({
netuid,
enable,
});Deprecated since v423. Always fails with Swap.Deprecated.
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Build disable_lp call (no parameters)
const tx = api.tx.Swap.disable_lp();Events 1
Emitted when the swap fee rate is updated for a subnet's liquidity pool.
When it fires
- Admin/sudo call succeeded
- Subnet exists
- Fee rate within MaxFeeRate
Fields
| Name | Type | Format | Description |
|---|---|---|---|
netuid | NetUid | — | Subnet ID for which the fee rate was set |
rate | u16 | Integer (0-65535) | rate (u16) |
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Subscribe to FeeRateSet events
client.finalizedBlock$.subscribe(async (block) => {
const events = await api.event.Swap.FeeRateSet.get(block.hash);
for (const evt of events) {
console.log("FeeRateSet:", evt.payload);
}
});Storage 7
Alpha protocol liquidity that could not be injected without exceeding balancer weight bounds.
AlphaBalance Keys
| Name | Type | Format | Description |
|---|---|---|---|
key1 | NetUid | — | — |
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Query BalancerAlphaReservoir storage
const key1 = 0;
const result = await api.query.Swap.BalancerAlphaReservoir.getValue(key1);
console.log("BalancerAlphaReservoir:", result);TAO protocol liquidity that could not be injected without exceeding balancer weight bounds.
TaoBalance Keys
| Name | Type | Format | Description |
|---|---|---|---|
key1 | NetUid | — | — |
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Query BalancerTaoReservoir storage
const key1 = 0;
const result = await api.query.Swap.BalancerTaoReservoir.getValue(key1);
console.log("BalancerTaoReservoir:", result);Swap fee rate per subnet, encoded as u16. Fraction = value / u16::MAX (i.e. value / 65535). NOT basis points or percent.
u16 (Integer (0-65535))Keys
| Name | Type | Format | Description |
|---|---|---|---|
key1 | NetUid | — | — |
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Query FeeRate storage
const key1 = 0;
const result = await api.query.Swap.FeeRate.getValue(key1);
console.log("FeeRate:", result);--- Storage for migration run status
bool (Boolean (true/false))Keys
| Name | Type | Format | Description |
|---|---|---|---|
key1 | BoundedVec | — | — |
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Query HasMigrationRun storage
const key1 = new Uint8Array(0);
const result = await api.query.Swap.HasMigrationRun.getValue(key1);
console.log("HasMigrationRun:", result);Storage to determine whether balancer swap was initialized for a specific subnet.
bool (Boolean (true/false))Keys
| Name | Type | Format | Description |
|---|---|---|---|
key1 | NetUid | — | — |
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Query PalSwapInitialized storage
const key1 = 0;
const result = await api.query.Swap.PalSwapInitialized.getValue(key1);
console.log("PalSwapInitialized:", result);Alpha reservoir for fee scraps per subnet.
AlphaBalance Keys
| Name | Type | Format | Description |
|---|---|---|---|
key1 | NetUid | — | — |
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Query ScrapReservoirAlpha storage
const key1 = 0;
const result = await api.query.Swap.ScrapReservoirAlpha.getValue(key1);
console.log("ScrapReservoirAlpha:", result);u64-normalized reserve weight
Balancer Keys
| Name | Type | Format | Description |
|---|---|---|---|
key1 | NetUid | — | — |
import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Query SwapBalancer storage
const key1 = 0;
const result = await api.query.Swap.SwapBalancer.getValue(key1);
console.log("SwapBalancer:", result);Constants 4
Maximum allowed fee rate for swaps.
u1610000import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Query MaxFeeRate constant
const value = await api.constants.Swap.MaxFeeRate();
console.log("MaxFeeRate:", value);Minimum liquidity safe for rounding.
u64"1000"import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Query MinimumLiquidity constant
const value = await api.constants.Swap.MinimumLiquidity();
console.log("MinimumLiquidity:", value);
// Balance: divide by 10^9 for TAO
const tao = value / 1_000_000_000n;Minimum TAO/Alpha reserve required in pools.
NonZeroU64"1000000"import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Query MinimumReserve constant
const value = await api.constants.Swap.MinimumReserve();
console.log("MinimumReserve:", value);PalletId (b"ten/swap") that seeds the swap pallet protocol account address.
PalletId0x74656e2f73776170import { createClient, Binary } from "polkadot-api";
import { getWsProvider } from "polkadot-api/ws";
import { sub } from "@polkadot-api/descriptors"; // generated by: npx papi add sub -w wss://entrypoint-finney.opentensor.ai:443
const client = createClient(getWsProvider("wss://entrypoint-finney.opentensor.ai:443"));
const api = client.getTypedApi(sub);
// Query ProtocolId constant
const value = await api.constants.Swap.ProtocolId();
console.log("ProtocolId:", value);Errors 13
The specified fee rate exceeds the maximum allowed.
Common Causes
- Fee rate exceeds MaxFeeRate constant
- Misunderstanding of normalized value format
How to Fix
Check Swap.MaxFeeRate constant,Use a lower fee rate value
The provided amount is too small for the swap.
Common Causes
- Amount below minimum threshold
- Not accounting for fees
- Dust amount provided
How to Fix
Increase the swap amount,Ensure amount exceeds minimum after fees
Not enough liquidity in the pool for this operation.
Common Causes
- Position has less liquidity than removal amount
- Pool depleted
- Trying to remove more than exists
How to Fix
Query Positions to check available liquidity,Reduce removal amount,Use different subnet
Your transaction was rejected because the current Alpha exchange rate is worse than the limit_price you specified. This is your slippage protection working as intended — the price moved against you between when you submitted and when the chain tried to execute.
Common Causes
- Alpha price moved between extrinsic submission and block inclusion
- limit_price set too tight (not enough buffer for normal price movement)
- Large swap relative to available liquidity caused price impact beyond your limit
- Multiple swaps in the same block moved the price before yours executed
How to Fix
Query Swap.AlphaSqrtPrice for the subnet — square the value to get the current RAO-per-Alpha rate, then set your limit_price with a buffer above (for add_stake) or below (for remove_stake),Widen your limit_price to allow more slippage (e.g., current_price * 1.02 for 2% tolerance on add_stake),Split large operations into smaller swaps to reduce price impact,Set limit_price=0 to disable the protection entirely (no slippage guard),Set allow_partial=true to accept a partial fill at the limit price instead of all-or-nothing failure
You don't have enough TAO or Alpha for this operation.
Common Causes
- TAO balance too low
- Alpha balance too low
- Not accounting for fees
How to Fix
Check balances before operation,Reduce operation amount,Acquire more tokens
Historical (v290 to v422): the tick range for add_liquidity was invalid. Since v423 the liquidity calls return Deprecated before any tick check.
Common Causes
- tick_low >= tick_high
- Ticks outside valid bounds
- Invalid tick spacing
How to Fix
Ensure tick_low < tick_high,Check tick bounds,Use valid tick values
The liquidity value is invalid (likely too small).
Common Causes
- Liquidity below MinimumLiquidity
- Zero liquidity provided
How to Fix
Check MinimumLiquidity constant,Increase liquidity amount
Pool reserves are too low for this operation.
Common Causes
- Operation would drain pool below minimum
- Pool nearly empty
How to Fix
Reduce operation size,Wait for more liquidity
The specified subnet doesn't exist.
Common Causes
- Invalid netuid
- Subnet dissolved
- Typo in netuid
How to Fix
Verify netuid exists,Check NetworksAdded storage
The subnet doesn't have subtoken (Alpha) enabled.
Common Causes
- Subnet hasn't enabled subtoken
- Legacy subnet without Alpha
How to Fix
Check SubtokenEnabled storage,Use a subnet with Alpha enabled
Swap reserves are too imbalanced
How to Fix
Swap input is too large relative to input-side liquidity
How to Fix
The extrinsic is deprecated