Module iota_system::staking_pool
- Struct
StakingPoolV1
- Struct
PoolTokenExchangeRate
- Struct
StakedIota
- Constants
- Function
new
- Function
request_add_stake
- Function
request_withdraw_stake
- Function
withdraw_from_principal
- Function
unwrap_staked_iota
- Function
deposit_rewards
- Function
process_pending_stakes_and_withdraws
- Function
process_pending_stake_withdraw
- Function
process_pending_stake
- Function
withdraw_rewards
- Function
activate_staking_pool
- Function
deactivate_staking_pool
- Function
iota_balance
- Function
pool_id
- Function
staked_iota_amount
- Function
stake_activation_epoch
- Function
is_preactive
- Function
is_inactive
- Function
split
- Function
split_staked_iota
- Function
join_staked_iota
- Function
is_equal_staking_metadata
- Function
pool_token_exchange_rate_at_epoch
- Function
pending_stake_amount
- Function
pending_stake_withdraw_amount
- Function
exchange_rates
- Function
iota_amount
- Function
pool_token_amount
- Function
is_preactive_at_epoch
- Function
get_iota_amount
- Function
get_token_amount
- Function
initial_exchange_rate
- Function
check_balance_invariants
use iota::address;
use iota::bag;
use iota::balance;
use iota::coin;
use iota::config;
use iota::deny_list;
use iota::dynamic_field;
use iota::dynamic_object_field;
use iota::event;
use iota::hex;
use iota::iota;
use iota::object;
use iota::table;
use iota::transfer;
use iota::tx_context;
use iota::types;
use iota::url;
use std::address;
use std::ascii;
use std::bcs;
use std::option;
use std::string;
use std::type_name;
use std::u64;
use std::vector;
Struct StakingPoolV1
A staking pool embedded in each validator struct in the system state object.
public struct StakingPoolV1 has key, store
Fields
id: iota::object::UID
activation_epoch: std::option::Option<u64>
The epoch at which this pool became active. The value is
None
if the pool is pre-active andSome(<epoch_number>)
if active or inactive.deactivation_epoch: std::option::Option<u64>
The epoch at which this staking pool ceased to be active.
None
= {pre-active, active},Some(<epoch_number>)
if in-active, and it was de-activated at epoch<epoch_number>
.iota_balance: u64
The total number of IOTA tokens in this pool, including the IOTA in the rewards_pool, as well as in all the principal in the
StakedIota
object, updated at epoch boundaries.rewards_pool: iota::balance::Balance<iota::iota::IOTA>
The epoch stake rewards will be added here at the end of each epoch.
pool_token_balance: u64
Total number of pool tokens issued by the pool.
exchange_rates: iota::table::Table<u64, iota_system::staking_pool::PoolTokenExchangeRate>
Exchange rate history of previous epochs. Key is the epoch number. The entries start from the
activation_epoch
of this pool and contains exchange rates at the beginning of each epoch, i.e., right after the rewards for the previous epoch have been deposited into the pool.pending_stake: u64
Pending stake amount for this epoch, emptied at epoch boundaries.
pending_total_iota_withdraw: u64
Pending stake withdrawn during the current epoch, emptied at epoch boundaries. This includes both the principal and rewards IOTA withdrawn.
pending_pool_token_withdraw: u64
Pending pool token withdrawn during the current epoch, emptied at epoch boundaries.
extra_fields: iota::bag::Bag
Any extra fields that's not defined statically.
Struct PoolTokenExchangeRate
Struct representing the exchange rate of the stake pool token to IOTA.
public struct PoolTokenExchangeRate has copy, drop, store
Fields
iota_amount: u64
pool_token_amount: u64
Struct StakedIota
A self-custodial object holding the staked IOTA tokens.
public struct StakedIota has key, store
Fields
id: iota::object::UID
pool_id: iota::object::ID
ID of the staking pool we are staking with.
stake_activation_epoch: u64
The epoch at which the stake becomes active.
principal: iota::balance::Balance<iota::iota::IOTA>
The staked IOTA tokens.
Constants
const EActivationOfInactivePool: u64 = 16;
const EDeactivationOfInactivePool: u64 = 11;
const EDelegationOfZeroIota: u64 = 17;
const EDelegationToInactivePool: u64 = 10;
const EDestroyNonzeroBalance: u64 = 5;
const EIncompatibleStakedIota: u64 = 12;
const EInsufficientIotaTokenBalance: u64 = 3;
const EInsufficientPoolTokenBalance: u64 = 0;
const EInsufficientRewardsPoolBalance: u64 = 4;
const EPendingDelegationDoesNotExist: u64 = 8;
const EPoolAlreadyActive: u64 = 14;
const EPoolNotPreactive: u64 = 15;
const EStakedIotaBelowThreshold: u64 = 18;
const ETokenBalancesDoNotMatchExchangeRate: u64 = 9;
const ETokenTimeLockIsSome: u64 = 6;
const EWithdrawAmountCannotBeZero: u64 = 2;
const EWithdrawalInSameEpoch: u64 = 13;
const EWrongDelegation: u64 = 7;
const EWrongPool: u64 = 1;
StakedIota objects cannot be split to below this amount.
const MIN_STAKING_THRESHOLD: u64 = 1000000000;
Function new
Create a new, empty staking pool.
public(package) fun new(ctx: &mut iota::tx_context::TxContext): iota_system::staking_pool::StakingPoolV1
Implementation
public(package) fun new(ctx: &mut TxContext): StakingPoolV1 {
let exchange_rates = table::new(ctx);
StakingPoolV1 {
id: object::new(ctx),
activation_epoch: option::none(),
deactivation_epoch: option::none(),
iota_balance: 0,
rewards_pool: balance::zero(),
pool_token_balance: 0,
exchange_rates,
pending_stake: 0,
pending_total_iota_withdraw: 0,
pending_pool_token_withdraw: 0,
extra_fields: bag::new(ctx),
}
}
Function request_add_stake
Request to stake to a staking pool. The stake starts counting at the beginning of the next epoch,
public(package) fun request_add_stake(pool: &mut iota_system::staking_pool::StakingPoolV1, stake: iota::balance::Balance<iota::iota::IOTA>, stake_activation_epoch: u64, ctx: &mut iota::tx_context::TxContext): iota_system::staking_pool::StakedIota
Implementation
public(package) fun request_add_stake(
pool: &mut StakingPoolV1,
stake: Balance<IOTA>,
stake_activation_epoch: u64,
ctx: &mut TxContext,
): StakedIota {
let iota_amount = stake.value();
assert!(!is_inactive(pool), EDelegationToInactivePool);
assert!(iota_amount > 0, EDelegationOfZeroIota);
let staked_iota = StakedIota {
id: object::new(ctx),
pool_id: object::id(pool),
stake_activation_epoch,
principal: stake,
};
pool.pending_stake = pool.pending_stake + iota_amount;
staked_iota
}
Function request_withdraw_stake
Request to withdraw the given stake plus rewards from a staking pool. Both the principal and corresponding rewards in IOTA are withdrawn. A proportional amount of pool token withdraw is recorded and processed at epoch change time.
public(package) fun request_withdraw_stake(pool: &mut iota_system::staking_pool::StakingPoolV1, staked_iota: iota_system::staking_pool::StakedIota, ctx: &iota::tx_context::TxContext): iota::balance::Balance<iota::iota::IOTA>
Implementation
public(package) fun request_withdraw_stake(
pool: &mut StakingPoolV1,
staked_iota: StakedIota,
ctx: &TxContext,
): Balance<IOTA> {
// stake is inactive
if (staked_iota.stake_activation_epoch > ctx.epoch()) {
let principal = unwrap_staked_iota(staked_iota);
let withdraw_amount = principal.value();
if (pool.is_preactive()) {
// Stake for preactive validators is always processed immediately and not added to the pending stake.
// The exchange rate of a preactive pool is always 1:1, so we can subtract the withdraw_amount
// from both iota_balance and pool_token_balance.
pool.iota_balance = pool.iota_balance - withdraw_amount;
pool.pool_token_balance = pool.pool_token_balance - withdraw_amount;
check_balance_invariants(pool, ctx.epoch());
} else {
// The stake for active validators is only updated at epoch boundaries,
// so we can remove it from the pending_stake here.
pool.pending_stake = pool.pending_stake - withdraw_amount;
};
return principal
};
let (pool_token_withdraw_amount, mut principal_withdraw) = withdraw_from_principal(
pool,
staked_iota,
);
let principal_withdraw_amount = principal_withdraw.value();
let rewards_withdraw = withdraw_rewards(
pool,
principal_withdraw_amount,
pool_token_withdraw_amount,
ctx.epoch(),
);
let total_iota_withdraw_amount = principal_withdraw_amount + rewards_withdraw.value();
pool.pending_total_iota_withdraw =
pool.pending_total_iota_withdraw + total_iota_withdraw_amount;
pool.pending_pool_token_withdraw =
pool.pending_pool_token_withdraw + pool_token_withdraw_amount;
// TODO: implement withdraw bonding period here.
principal_withdraw.join(rewards_withdraw);
principal_withdraw
}
Function withdraw_from_principal
Withdraw the principal IOTA stored in the StakedIota object, and calculate the corresponding amount of pool tokens using exchange rate at staking epoch. Returns values are amount of pool tokens withdrawn and withdrawn principal portion of IOTA.
public(package) fun withdraw_from_principal(pool: &iota_system::staking_pool::StakingPoolV1, staked_iota: iota_system::staking_pool::StakedIota): (u64, iota::balance::Balance<iota::iota::IOTA>)
Implementation
public(package) fun withdraw_from_principal(
pool: &StakingPoolV1,
staked_iota: StakedIota,
): (u64, Balance<IOTA>) {
// Check that the stake information matches the pool.
assert!(staked_iota.pool_id == object::id(pool), EWrongPool);
let exchange_rate_at_staking_epoch = pool_token_exchange_rate_at_epoch(
pool,
staked_iota.stake_activation_epoch,
);
let principal_withdraw = unwrap_staked_iota(staked_iota);
let pool_token_withdraw_amount = get_token_amount(
&exchange_rate_at_staking_epoch,
principal_withdraw.value(),
);
(pool_token_withdraw_amount, principal_withdraw)
}
Function unwrap_staked_iota
fun unwrap_staked_iota(staked_iota: iota_system::staking_pool::StakedIota): iota::balance::Balance<iota::iota::IOTA>
Implementation
fun unwrap_staked_iota(staked_iota: StakedIota): Balance<IOTA> {
let StakedIota {
id,
pool_id: _,
stake_activation_epoch: _,
principal,
} = staked_iota;
object::delete(id);
principal
}
Function deposit_rewards
Called at epoch advancement times to add rewards (in IOTA) to the staking pool.
public(package) fun deposit_rewards(pool: &mut iota_system::staking_pool::StakingPoolV1, rewards: iota::balance::Balance<iota::iota::IOTA>)
Implementation
public(package) fun deposit_rewards(pool: &mut StakingPoolV1, rewards: Balance<IOTA>) {
pool.iota_balance = pool.iota_balance + rewards.value();
pool.rewards_pool.join(rewards);
}
Function process_pending_stakes_and_withdraws
public(package) fun process_pending_stakes_and_withdraws(pool: &mut iota_system::staking_pool::StakingPoolV1, ctx: &iota::tx_context::TxContext)
Implementation
public(package) fun process_pending_stakes_and_withdraws(
pool: &mut StakingPoolV1,
ctx: &TxContext,
) {
let new_epoch = ctx.epoch() + 1;
process_pending_stake_withdraw(pool);
process_pending_stake(pool);
pool
.exchange_rates
.add(
new_epoch,
PoolTokenExchangeRate {
iota_amount: pool.iota_balance,
pool_token_amount: pool.pool_token_balance,
},
);
check_balance_invariants(pool, new_epoch);
}
Function process_pending_stake_withdraw
Called at epoch boundaries to process pending stake withdraws requested during the epoch. Also called immediately upon withdrawal if the pool is inactive or if the pool is preactive.
public(package) fun process_pending_stake_withdraw(pool: &mut iota_system::staking_pool::StakingPoolV1)
Implementation
public(package) fun process_pending_stake_withdraw(pool: &mut StakingPoolV1) {
pool.iota_balance = pool.iota_balance - pool.pending_total_iota_withdraw;
pool.pool_token_balance = pool.pool_token_balance - pool.pending_pool_token_withdraw;
pool.pending_total_iota_withdraw = 0;
pool.pending_pool_token_withdraw = 0;
}
Function process_pending_stake
Called at epoch boundaries to process the pending stake.
public(package) fun process_pending_stake(pool: &mut iota_system::staking_pool::StakingPoolV1)
Implementation
public(package) fun process_pending_stake(pool: &mut StakingPoolV1) {
// Use the most up to date exchange rate with the rewards deposited and withdraws effectuated.
let latest_exchange_rate = PoolTokenExchangeRate {
iota_amount: pool.iota_balance,
pool_token_amount: pool.pool_token_balance,
};
pool.iota_balance = pool.iota_balance + pool.pending_stake;
pool.pool_token_balance = get_token_amount(&latest_exchange_rate, pool.iota_balance);
pool.pending_stake = 0;
}
Function withdraw_rewards
This function does the following:
- Calculates the total amount of IOTA (including principal and rewards) that the provided pool tokens represent at the current exchange rate.
- Using the above number and the given
principal_withdraw_amount
, calculates the rewards portion of the stake we should withdraw. - Withdraws the rewards portion from the rewards pool at the current exchange rate. We only withdraw the rewards portion because the principal portion was already taken out of the staker's self custodied StakedIota.
fun withdraw_rewards(pool: &mut iota_system::staking_pool::StakingPoolV1, principal_withdraw_amount: u64, pool_token_withdraw_amount: u64, epoch: u64): iota::balance::Balance<iota::iota::IOTA>
Implementation
fun withdraw_rewards(
pool: &mut StakingPoolV1,
principal_withdraw_amount: u64,
pool_token_withdraw_amount: u64,
epoch: u64,
): Balance<IOTA> {
let exchange_rate = pool_token_exchange_rate_at_epoch(pool, epoch);
let total_iota_withdraw_amount = get_iota_amount(&exchange_rate, pool_token_withdraw_amount);
let mut reward_withdraw_amount = if (total_iota_withdraw_amount >= principal_withdraw_amount)
total_iota_withdraw_amount - principal_withdraw_amount else 0;
// This may happen when we are withdrawing everything from the pool and
// the rewards pool balance may be less than reward_withdraw_amount.
// TODO: FIGURE OUT EXACTLY WHY THIS CAN HAPPEN.
reward_withdraw_amount = reward_withdraw_amount.min(pool.rewards_pool.value());
pool.rewards_pool.split(reward_withdraw_amount)
}
Function activate_staking_pool
Called by validator
module to activate a staking pool.
public(package) fun activate_staking_pool(pool: &mut iota_system::staking_pool::StakingPoolV1, activation_epoch: u64)
Implementation
public(package) fun activate_staking_pool(pool: &mut StakingPoolV1, activation_epoch: u64) {
// Add the initial exchange rate to the table.
pool
.exchange_rates
.add(
activation_epoch,
initial_exchange_rate(),
);
// Check that the pool is preactive and not inactive.
assert!(is_preactive(pool), EPoolAlreadyActive);
assert!(!is_inactive(pool), EActivationOfInactivePool);
// Fill in the active epoch.
pool.activation_epoch.fill(activation_epoch);
}