# swap-stake (/docs/tx/swap-stake)

Moves part of a position from the origin subnet to the destination subnet
while staying on the same hotkey: the alpha is swapped to TAO in the
origin pool and then to alpha in the destination pool, so both legs can
incur slippage. By default the call is slippage-protected: it fails
(`SlippageTooHigh`) instead of filling once the origin/destination price
ratio falls more than `rate_tolerance` (5%) below the ratio at submission
— raise the tolerance or set `slippage_protection` to False to execute at
any price. The two netuids must differ (`SameNetuid`). Use `move_stake`
when the hotkey should change too, and `remove_stake` plus `add_stake`
only if you want to control each leg separately.

| Signer    | Origin                                 | Pallet          | Wraps                                                                                                                                                                                            |
| --------- | -------------------------------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `coldkey` | signed account (pallet role may apply) | SubtensorModule | [`SubtensorModule.swap_stake`](/code/pallets/subtensor/src/macros/dispatches.rs#L1341-L1357), [`SubtensorModule.swap_stake_limit`](/code/pallets/subtensor/src/macros/dispatches.rs#L1489-L1509) |

## Parameters [#parameters]

| Parameter             | Type              | Required | Description                                                                                                                                                                                                                        |
| --------------------- | ----------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `hotkey_ss58`         | string            | yes      | Hotkey the stake is held on (the validator backing the position).                                                                                                                                                                  |
| `origin_netuid`       | integer           | yes      | Subnet the stake currently sits on.                                                                                                                                                                                                |
| `dest_netuid`         | integer           | yes      | Subnet the stake ends up on. When it differs from the origin, the position is swapped through both subnet pools, which can incur slippage on each leg.                                                                             |
| `amount_alpha`        | number \| `"all"` | yes      | How much of the origin position to swap across (an explicit amount; `all` is not accepted).                                                                                                                                        |
| `slippage_protection` | boolean           | no       | Bound the price the swap may execute at (on by default): the call fails (`SlippageTooHigh`) instead of filling once the pool price moves more than `rate_tolerance` from the price at submission. Disable to execute at any price. |
| `rate_tolerance`      | number            | no       | Maximum price move slippage protection accepts, as a fraction (0.05 = 5%). Ignored when slippage protection is disabled.                                                                                                           |

Address parameters (`--hotkey`, `--coldkey`, `--dest`, ...) accept a raw ss58
address, an address-book or proxy-book name, or a local wallet/hotkey name.

## CLI [#cli]

Preview with `--dry-run` (shows fee, effects, and policy result without
submitting), then submit:

```bash
btcli tx swap-stake \
  --hotkey <ss58|name> \
  --origin-netuid <int> \
  --dest-netuid <int> \
  --amount-alpha <amount|all> --dry-run
btcli tx swap-stake \
  --hotkey <ss58|name> \
  --origin-netuid <int> \
  --dest-netuid <int> \
  --amount-alpha <amount|all> -w my_coldkey
```

## Python [#python]

```python
import bittensor as bt
from bittensor.wallet import Wallet

wallet = Wallet(name="my_coldkey", hotkey="my_hotkey")
intent = bt.SwapStake(hotkey_ss58="5F...", origin_netuid=0, dest_netuid=0, amount_alpha=1.0)

sub = bt.Subtensor()
plan = sub.plan(intent, wallet)   # fee, effects, policy — no submission
result = sub.execute(intent, wallet)
if not result.success:
    print(result.error.code, result.error.remediation)
```

(`bt.Subtensor` is also the async client — `async with bt.Subtensor() as client:`
— see [The client](/docs/concepts/client).) Or build the intent by op name, as
an agent would:

```python
result = sub.execute_tool("swap_stake", {...}, wallet)
```

## On-chain implementation [#on-chain-implementation]

`SubtensorModule.swap_stake` — [`pallets/subtensor/src/macros/dispatches.rs#L1343`](/code/pallets/subtensor/src/macros/dispatches.rs#L1341-L1357):

```rust
#[pallet::call_index(87)]
#[pallet::weight(<T as crate::pallet::Config>::WeightInfo::swap_stake())]
pub fn swap_stake(
    origin: OriginFor<T>,
    hotkey: T::AccountId,
    origin_netuid: NetUid,
    destination_netuid: NetUid,
    alpha_amount: AlphaBalance,
) -> DispatchResult {
    Self::do_swap_stake(
        origin,
        hotkey,
        origin_netuid,
        destination_netuid,
        alpha_amount,
    )
}
```

Delegates to [`do_swap_stake`](/code/pallets/subtensor/src/staking/move_stake.rs#L260).

`SubtensorModule.swap_stake_limit` — [`pallets/subtensor/src/macros/dispatches.rs#L1491`](/code/pallets/subtensor/src/macros/dispatches.rs#L1489-L1509):

```rust
#[pallet::call_index(90)]
#[pallet::weight(<T as crate::pallet::Config>::WeightInfo::swap_stake_limit())]
pub fn swap_stake_limit(
    origin: OriginFor<T>,
    hotkey: T::AccountId,
    origin_netuid: NetUid,
    destination_netuid: NetUid,
    alpha_amount: AlphaBalance,
    limit_price: TaoBalance,
    allow_partial: bool,
) -> DispatchResult {
    Self::do_swap_stake_limit(
        origin,
        hotkey,
        origin_netuid,
        destination_netuid,
        alpha_amount,
        limit_price,
        allow_partial,
    )
}
```

Delegates to [`do_swap_stake_limit`](/code/pallets/subtensor/src/staking/move_stake.rs#L325).

Every file is browsable under [/code](/code) exactly as built into the runtime, or as plain text under `/code/raw/<path>` (index: [`/code/index.json`](/code/index.json)).
