# set-auto-stake (/docs/tx/set-auto-stake)

Sets the coldkey's autostake destination for the subnet: all future
rewards earned there are automatically staked to the chosen hotkey
(defaulting to the wallet's own hotkey) instead of accumulating unstaked.
A configuration change only — it moves no funds by itself and applies just
to that subnet. The hotkey must be registered on the subnet
(`HotKeyNotRegisteredInSubNet`), and setting the hotkey that is
already the destination fails (`SameAutoStakeHotkeyAlreadySet`). Call
it again with a different hotkey to redirect; read the current setting
back with the `auto_stake` read.

| Signer    | Origin                                 | Pallet          | Wraps                                                                                                           |
| --------- | -------------------------------------- | --------------- | --------------------------------------------------------------------------------------------------------------- |
| `coldkey` | signed account (pallet role may apply) | SubtensorModule | [`SubtensorModule.set_coldkey_auto_stake_hotkey`](/code/pallets/subtensor/src/macros/dispatches.rs#L1784-L1826) |

## Parameters [#parameters]

| Parameter     | Type    | Required | Description                                                            |
| ------------- | ------- | -------- | ---------------------------------------------------------------------- |
| `netuid`      | integer | yes      | Subnet whose future rewards are auto-staked.                           |
| `hotkey_ss58` | string  | no       | Hotkey the rewards are staked to. Defaults to the wallet's own hotkey. |

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 set-auto-stake \
  --netuid <int> --dry-run
btcli tx set-auto-stake \
  --netuid <int> -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.SetAutoStake(netuid=1)

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("set_auto_stake", {...}, wallet)
```

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

`SubtensorModule.set_coldkey_auto_stake_hotkey` — [`pallets/subtensor/src/macros/dispatches.rs#L1786`](/code/pallets/subtensor/src/macros/dispatches.rs#L1784-L1826):

```rust
#[pallet::call_index(114)]
#[pallet::weight(<T as crate::pallet::Config>::WeightInfo::set_coldkey_auto_stake_hotkey())]
pub fn set_coldkey_auto_stake_hotkey(
    origin: OriginFor<T>,
    netuid: NetUid,
    hotkey: T::AccountId,
) -> DispatchResult {
    let coldkey = ensure_signed(origin)?;
    ensure!(Self::if_subnet_exist(netuid), Error::<T>::SubnetNotExists);
    ensure!(
        Uids::<T>::contains_key(netuid, &hotkey),
        Error::<T>::HotKeyNotRegisteredInSubNet
    );

    let current_hotkey = AutoStakeDestination::<T>::get(coldkey.clone(), netuid);
    if let Some(current_hotkey) = current_hotkey {
        ensure!(
            current_hotkey != hotkey,
            Error::<T>::SameAutoStakeHotkeyAlreadySet
        );

        // Remove the coldkey from the old hotkey (if present)
        AutoStakeDestinationColdkeys::<T>::mutate(current_hotkey.clone(), netuid, |v| {
            v.retain(|c| c != &coldkey);
        });
    }

    // Add the coldkey to the new hotkey (if not already present)
    AutoStakeDestination::<T>::insert(coldkey.clone(), netuid, hotkey.clone());
    AutoStakeDestinationColdkeys::<T>::mutate(hotkey.clone(), netuid, |v| {
        if !v.contains(&coldkey) {
            v.push(coldkey.clone());
        }
    });

    Self::deposit_event(Event::AutoStakeDestinationSet {
        coldkey,
        netuid,
        hotkey,
    });

    Ok(())
}
```

Delegates to [`if_subnet_exist`](/code/pallets/subtensor/src/subnets/subnet.rs#L39).

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