All pallets

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

Index: 28 31 items

Calls 6

set_fee_rate

call details

Sets the swap fee rate for a specific subnet's liquidity pool.

Parameters

NameTypeFormatDescription
netuidNetUid—netuid: Subnet ID (u16, 0-65535)
rateu16Integer (0-65535)rate (u16)
Modifies: FeeRate
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,
});

add_liquidity

call details

Deprecated since v423. Always fails with Swap.Deprecated; the pool holds protocol liquidity only.

Parameters

NameTypeFormatDescription
hotkeyAccountIdSS58 Addresshotkey: Account address (32 bytes, SS58-encoded)
netuidNetUid—netuid: Subnet ID (u16, 0-65535)
tick_lowTickIndexTick Index (i32)tick_low: Tick index (AMM)
tick_highTickIndexTick Index (i32)tick_high: Tick index (AMM)
liquidityu64Integerliquidity (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,
});

remove_liquidity

call details

Deprecated since v423. Always fails with Swap.Deprecated.

Parameters

NameTypeFormatDescription
hotkeyAccountIdSS58 Addresshotkey: Account address (32 bytes, SS58-encoded)
netuidNetUid—netuid: Subnet ID (u16, 0-65535)
position_idPositionIdPosition 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,
});

modify_position

call details

Deprecated since v423. Always fails with Swap.Deprecated.

Parameters

NameTypeFormatDescription
hotkeyAccountIdSS58 Addresshotkey: Account address (32 bytes, SS58-encoded)
netuidNetUid—netuid: Subnet ID (u16, 0-65535)
position_idPositionIdPosition ID (u64)position_id: AMM position identifier
liquidity_deltai64Signed Integerliquidity_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,
});

toggle_user_liquidity

call details

Deprecated since v423. Always fails with Swap.Deprecated; user liquidity is permanently disabled.

Parameters

NameTypeFormatDescription
netuidNetUid—netuid: Subnet ID (u16, 0-65535)
enableboolBoolean (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,
});

disable_lp

call details

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

FeeRateSet

event details

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

NameTypeFormatDescription
netuidNetUid—Subnet ID for which the fee rate was set
rateu16Integer (0-65535)rate (u16)
Emitted by: set_fee_rate
Modifies: FeeRate
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

BalancerAlphaReservoir

storage details

Alpha protocol liquidity that could not be injected without exceeding balancer weight bounds.

Modifier: Default Keys: 1 Returns: AlphaBalance

Keys

NameTypeFormatDescription
key1NetUid——
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);

BalancerTaoReservoir

storage details

TAO protocol liquidity that could not be injected without exceeding balancer weight bounds.

Modifier: Default Keys: 1 Returns: TaoBalance

Keys

NameTypeFormatDescription
key1NetUid——
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);

FeeRate

storage details

Swap fee rate per subnet, encoded as u16. Fraction = value / u16::MAX (i.e. value / 65535). NOT basis points or percent.

Modifier: Default Keys: 1 Returns: u16 (Integer (0-65535))

Keys

NameTypeFormatDescription
key1NetUid——
Modified by: set_fee_rate
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);

HasMigrationRun

storage details

--- Storage for migration run status

Modifier: Default Keys: 1 Returns: bool (Boolean (true/false))

Keys

NameTypeFormatDescription
key1BoundedVec——
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);

PalSwapInitialized

storage details

Storage to determine whether balancer swap was initialized for a specific subnet.

Modifier: Default Keys: 1 Returns: bool (Boolean (true/false))

Keys

NameTypeFormatDescription
key1NetUid——
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);

ScrapReservoirAlpha

storage details

Alpha reservoir for fee scraps per subnet.

Modifier: Default Keys: 1 Returns: AlphaBalance

Keys

NameTypeFormatDescription
key1NetUid——
Modified by: remove_liquidity
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);

SwapBalancer

storage details

u64-normalized reserve weight

Modifier: Default Keys: 1 Returns: Balancer

Keys

NameTypeFormatDescription
key1NetUid——
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

MaxFeeRate

constant details

Maximum allowed fee rate for swaps.

Type: u16
Value: 10000
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 MaxFeeRate constant
const value = await api.constants.Swap.MaxFeeRate();
console.log("MaxFeeRate:", value);

MinimumLiquidity

constant details

Minimum liquidity safe for rounding.

Type: u64
Value: "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;

MinimumReserve

constant details

Minimum TAO/Alpha reserve required in pools.

Type: NonZeroU64
Value: "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);

ProtocolId

constant details

PalletId (b"ten/swap") that seeds the swap pallet protocol account address.

Type: PalletId
Value (hex): 0x74656e2f73776170
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 ProtocolId constant
const value = await api.constants.Swap.ProtocolId();
console.log("ProtocolId:", value);

Errors 13

FeeRateTooHigh

error details #0

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

Thrown by: set_fee_rate

InsufficientInputAmount

error details #1

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

InsufficientLiquidity

error details #2

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

PriceLimitExceeded

error details #3

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

InsufficientBalance

error details #4

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

Thrown by: add_liquidity

InvalidTickRange

error details #5

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

InvalidLiquidityValue

error details #6

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

ReservesTooLow

error details #7

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

MechanismDoesNotExist

error details #8

The specified subnet doesn't exist.

Common Causes

  • Invalid netuid
  • Subnet dissolved
  • Typo in netuid

How to Fix

Verify netuid exists,Check NetworksAdded storage

SubtokenDisabled

error details #9

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

ReservesOutOfBalance

error details #10

Swap reserves are too imbalanced

How to Fix

SwapInputTooLarge

error details #11

Swap input is too large relative to input-side liquidity

How to Fix

Deprecated

error details #12

The extrinsic is deprecated

How to Fix