Stake

Unstaking and the cooldown

Request an unstake, wait out the cooldown, and withdraw the deposit.

Leaving a solver takes two instructions with a wait between them. request_unstake marks shares of a stake as leaving and starts the cooldown. withdraw_stake pays them out once the cooldown has passed.

The statuses of a stake

una.stakes() gives every stake one of three statuses, and the app shows the same three.

StatusIn the appThe stake isWhat moves it on
stakedStakedHolding shares with no unstake requested.request_unstake moves it to unstaking.
unstakingUnstaking, with the time leftHolding shares marked as leaving, before their unlock slot.The chain reaching the unlock slot moves it to withdrawable.
withdrawableWithdrawableHolding shares marked as leaving, at or past their unlock slot.withdraw_stake pays them out and moves it to staked.

A stake account stays open after a withdrawal. A stake that requested only part of its shares reports unstaking and then withdrawable while the rest of its shares stay staked, and returns to staked with those after the withdrawal.

While an unstake is in progress the stake carries an unstakeRequest with unlockSlot, unlockAt, slotsLeft, secondsLeft and withdrawable. The fields are under UnstakeRequest.

The cooldown's length

The cooldown is unstake_cooldown_slots in the program's Config, a number of slots above zero that the admin sets with set_config. Una sets it to one proof window: 300 slots by default, about 60 seconds at 200 ms a slot. una.protocol() returns the live value as unstakeCooldownSlots.

TypeScript
console.log(`cooldown: ${protocol.unstakeCooldownSlots} slots of ${protocol.slotMs} ms`)

The unlock slot is fixed when the request lands:

Text
unlock_slot = slot of the request + unstake_cooldown_slots

A batch's proof deadline is its clearing slot plus the proof window. With the cooldown at one proof window, every batch the solver cleared before the request has reached its proof deadline by the unlock slot.

Request an unstake

On /stake, under Your stakes, a stake with the status Staked has an Unstake button. Press it and sign. The button requests the stake's whole staked amount. The status becomes Unstaking with the time left beside it.

The request_unstake instruction

Arguments

sharesu64required
Shares of the solver's pool to mark as leaving. Above zero and no more than the stake holds.

The owner signs. For a stake in a liquid staking token the stake pool account is passed and checked as it is in stake. The program then:

  1. Syncs the stake's rewards.
  2. Values the shares now: est_tokens = floor(shares * pool_amount / pool_shares), and est_lamports is that amount at the current rate.
  3. Moves shares from the stake's shares to its unstake_shares.
  4. Adds est_lamports to the stake's unstake_lamports_reserved and to the solver's pending_unstake_lamports.
  5. Sets unstake_unlock_slot to the current slot plus unstake_cooldown_slots.

No tokens move. It emits UnstakeRequested with stake, solver, owner, shares and unlock_slot.

A stake has one unlock slot. A second request while one is in progress adds its shares to unstake_shares and sets the unlock slot again from the slot of the second request, for all of them.

ErrorRaised when
ZeroLiquidityshares is 0.
UnauthorizedThe signer is not the stake's owner.
InsufficientStakeshares is more than the stake's shares.
InvalidStakePoolThe stake is in a liquid staking token and the stake pool account passed is not the listed one or fails the checks on its rate.
MathOverflowA sum or product leaves 64 bits.

During the cooldown

Shares between the request and the withdrawal stop backing new batches at once. A solver's free bond is its pools valued in lamports, less the bond locked behind cleared batches, less pending_unstake_lamports. clear_batch locks bond only from what is free, so the reserved lamports are out of every clearing after the request.

They keep earning. The stake's reward weight does not change until withdraw_stake, so the stake is credited its full part of every batch the solver proves during the cooldown. Rewards has the arithmetic.

They stay in the pool. The shares still count in the solver's pool for that mint, and a slash lowers the pool's amount for every share in it, the leaving ones included. The withdrawal pays what the shares are worth at the moment of the withdrawal. Slashing and cover has how much a slash takes.

Withdraw

When the time left reaches zero the status becomes Withdrawable and the row's button becomes Withdraw. Press it and sign. The deposit arrives in the wallet's token account for the bond token.

The withdraw_stake instruction

It takes no arguments and pays out all of the stake's unstake_shares. The owner signs.

Text
payout = floor(unstake_shares * pool_amount / pool_shares)

payout atoms of the bond mint move from the solver's vault to the owner's associated token account for that mint, and the program accepts no other destination. A stake in SOL is paid in wrapped SOL.

The same instruction:

  1. Syncs the stake's rewards before anything changes.
  2. Takes payout from the pool's amount and unstake_shares from the pool's shares.
  3. Takes the stake's unstake_lamports_reserved off the solver's pending_unstake_lamports.
  4. Lowers the stake's reward weight by floor(reward_weight * unstake_shares / (shares + unstake_shares)) and the solver's total weight by the same.
  5. Sets unstake_shares, unstake_lamports_reserved and unstake_unlock_slot to zero.

It emits StakeWithdrawn with stake, solver, owner, bond_mint_idx, shares and amount.

Rewards are a separate claim. A withdrawal leaves the stake's pending_rewards in place, and claim_rewards pays them after it as before.

ErrorRaised when
UnauthorizedThe signer is not the stake's owner, or the destination is not the owner's associated token account for the bond mint.
NothingToClaimThe stake has no unstake_shares.
CooldownActiveThe current slot is before unstake_unlock_slot.
UnsupportedBondMintThe mint or token program passed is not the one listed for the stake's bond mint.
MathOverflowA product leaves 64 bits.

What it costs

The program takes no fee on a request or a withdrawal. The wallet pays the network fee of each, and the rent of a token account for the bond mint if it has none when it withdraws.