crates/sui-framework/docs/sui_system/staking_pool.md
StakingPoolPoolTokenExchangeRateStakedSuiFungibleStakedSuiFungibleStakedSuiDataFungibleStakedSuiDataKeyUnderflowSuiBalancenewrequest_add_stakerequest_withdraw_stakeredeem_fungible_staked_suicalculate_fungible_staked_sui_withdraw_amountconvert_to_fungible_staked_suiwithdraw_from_principalunwrap_staked_suideposit_rewardsprocess_pending_stakes_and_withdrawsprocess_pending_stake_withdrawprocess_pending_stakewithdraw_rewardsactivate_staking_pooldeactivate_staking_poolsui_balancepool_idfungible_staked_sui_pool_idstaked_sui_amountstake_activation_epochis_preactiveactivation_epochis_inactivefungible_staked_sui_valuesplit_fungible_staked_suijoin_fungible_staked_suisplitsplit_staked_suijoin_staked_suiis_equal_staking_metadatapool_token_exchange_rate_at_epochpending_stake_amountpending_stake_withdraw_amountexchange_ratessui_amountpool_token_amountis_preactive_at_epochget_sui_amountget_token_amountinitial_exchange_ratecheck_balance_invariantsmul_divcalculate_rewards<a name="sui_system_staking_pool_StakingPool"></a>
StakingPoolA staking pool embedded in each validator struct in the system state object.
<pre><code><b>public</b> <b>struct</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a> <b>has</b> key, store </code></pre> <details> <summary>Fields</summary> <dl> <dt> <code>id: <a href="../sui/object.md#sui_object_UID">sui::object::UID</a></code> </dt> <dd> </dd> <dt> <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a>: <a href="../std/option.md#std_option_Option">std::option::Option</a><u64></code> </dt> <dd> The epoch at which this pool became active. The value is <code>None</code> if the pool is pre-active and <code>Some(<epoch_number>)</code> if active or inactive. </dd> <dt> <code>deactivation_epoch: <a href="../std/option.md#std_option_Option">std::option::Option</a><u64></code> </dt> <dd> The epoch at which this staking pool ceased to be active. <code>None</code> = {pre-active, active}, <code>Some(<epoch_number>)</code> if in-active, and it was de-activated at epoch <code><epoch_number></code>. </dd> <dt> <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a>: u64</code> </dt> <dd> The total number of SUI tokens in this pool, including the SUI in the rewards_pool, as well as in all the principal in the <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a></code> object, updated at epoch boundaries. </dd> <dt> <code>rewards_pool: <a href="../sui/balance.md#sui_balance_Balance">sui::balance::Balance</a><<a href="../sui/sui.md#sui_sui_SUI">sui::sui::SUI</a>></code> </dt> <dd> The epoch stake rewards will be added here at the end of each epoch. </dd> <dt> <code>pool_token_balance: u64</code> </dt> <dd> Total number of pool tokens issued by the pool. </dd> <dt> <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_exchange_rates">exchange_rates</a>: <a href="../sui/table.md#sui_table_Table">sui::table::Table</a><u64, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_PoolTokenExchangeRate">sui_system::staking_pool::PoolTokenExchangeRate</a>></code> </dt> <dd> Exchange rate history of previous epochs. Key is the epoch number. The entries start from the <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a></code> 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. </dd> <dt> <code>pending_stake: u64</code> </dt> <dd> Pending stake amount for this epoch, emptied at epoch boundaries. </dd> <dt> <code>pending_total_sui_withdraw: u64</code> </dt> <dd> Pending stake withdrawn during the current epoch, emptied at epoch boundaries. This includes both the principal and rewards SUI withdrawn. </dd> <dt> <code>pending_pool_token_withdraw: u64</code> </dt> <dd> Pending pool token withdrawn during the current epoch, emptied at epoch boundaries. </dd> <dt> <code>extra_fields: <a href="../sui/bag.md#sui_bag_Bag">sui::bag::Bag</a></code> </dt> <dd> Any extra fields that's not defined statically. </dd> </dl> </details><a name="sui_system_staking_pool_PoolTokenExchangeRate"></a>
PoolTokenExchangeRateStruct representing the exchange rate of the stake pool token to SUI.
<pre><code><b>public</b> <b>struct</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_PoolTokenExchangeRate">PoolTokenExchangeRate</a> <b>has</b> <b>copy</b>, drop, store </code></pre> <details> <summary>Fields</summary> <dl> <dt> <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_amount">sui_amount</a>: u64</code> </dt> <dd> </dd> <dt> <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_token_amount">pool_token_amount</a>: u64</code> </dt> <dd> </dd> </dl> </details><a name="sui_system_staking_pool_StakedSui"></a>
StakedSuiA self-custodial object holding the staked SUI tokens.
<pre><code><b>public</b> <b>struct</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a> <b>has</b> key, store </code></pre> <details> <summary>Fields</summary> <dl> <dt> <code>id: <a href="../sui/object.md#sui_object_UID">sui::object::UID</a></code> </dt> <dd> </dd> <dt> <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_id">pool_id</a>: <a href="../sui/object.md#sui_object_ID">sui::object::ID</a></code> </dt> <dd> ID of the staking pool we are staking with. </dd> <dt> <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>: u64</code> </dt> <dd> The epoch at which the stake becomes active. </dd> <dt> <code>principal: <a href="../sui/balance.md#sui_balance_Balance">sui::balance::Balance</a><<a href="../sui/sui.md#sui_sui_SUI">sui::sui::SUI</a>></code> </dt> <dd> The staked SUI tokens. </dd> </dl> </details><a name="sui_system_staking_pool_FungibleStakedSui"></a>
FungibleStakedSuiAn alternative to <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a></code> that holds the pool token amount instead of the SUI balance. StakedSui objects can be converted to FungibleStakedSuis after the initial warmup period. The advantage of this is that you can now merge multiple StakedSui objects from different activation epochs into a single FungibleStakedSui object.
<pre><code><b>public</b> <b>struct</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_FungibleStakedSui">FungibleStakedSui</a> <b>has</b> key, store </code></pre> <details> <summary>Fields</summary> <dl> <dt> <code>id: <a href="../sui/object.md#sui_object_UID">sui::object::UID</a></code> </dt> <dd> </dd> <dt> <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_id">pool_id</a>: <a href="../sui/object.md#sui_object_ID">sui::object::ID</a></code> </dt> <dd> ID of the staking pool we are staking with. </dd> <dt> <code>value: u64</code> </dt> <dd> The pool token amount. </dd> </dl> </details><a name="sui_system_staking_pool_FungibleStakedSuiData"></a>
FungibleStakedSuiDataHolds useful information
<pre><code><b>public</b> <b>struct</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_FungibleStakedSuiData">FungibleStakedSuiData</a> <b>has</b> key, store </code></pre> <details> <summary>Fields</summary> <dl> <dt> <code>id: <a href="../sui/object.md#sui_object_UID">sui::object::UID</a></code> </dt> <dd> </dd> <dt> <code>total_supply: u64</code> </dt> <dd> fungible_staked_sui supply </dd> <dt> <code>principal: <a href="../sui/balance.md#sui_balance_Balance">sui::balance::Balance</a><<a href="../sui/sui.md#sui_sui_SUI">sui::sui::SUI</a>></code> </dt> <dd> principal balance. Rewards are withdrawn from the reward pool </dd> </dl> </details><a name="sui_system_staking_pool_FungibleStakedSuiDataKey"></a>
FungibleStakedSuiDataKey<a name="sui_system_staking_pool_UnderflowSuiBalance"></a>
UnderflowSuiBalanceHolds the amount of SUI that was underflowed when withdrawing from the pool post safe mode. Cleaned up in the same transaction.
<pre><code><b>public</b> <b>struct</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_UnderflowSuiBalance">UnderflowSuiBalance</a> <b>has</b> <b>copy</b>, drop, store </code></pre> <details> <summary>Fields</summary> <dl> </dl> </details><a name="@Constants_0"></a>
<a name="sui_system_staking_pool_MIN_STAKING_THRESHOLD"></a>
StakedSui objects cannot be split to below this amount.
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_MIN_STAKING_THRESHOLD">MIN_STAKING_THRESHOLD</a>: u64 = 1000000000; </code></pre><a name="sui_system_staking_pool_EInsufficientPoolTokenBalance"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EInsufficientPoolTokenBalance">EInsufficientPoolTokenBalance</a>: u64 = 0; </code></pre><a name="sui_system_staking_pool_EWrongPool"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EWrongPool">EWrongPool</a>: u64 = 1; </code></pre><a name="sui_system_staking_pool_EWithdrawAmountCannotBeZero"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EWithdrawAmountCannotBeZero">EWithdrawAmountCannotBeZero</a>: u64 = 2; </code></pre><a name="sui_system_staking_pool_EInsufficientSuiTokenBalance"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EInsufficientSuiTokenBalance">EInsufficientSuiTokenBalance</a>: u64 = 3; </code></pre><a name="sui_system_staking_pool_EInsufficientRewardsPoolBalance"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EInsufficientRewardsPoolBalance">EInsufficientRewardsPoolBalance</a>: u64 = 4; </code></pre><a name="sui_system_staking_pool_EDestroyNonzeroBalance"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EDestroyNonzeroBalance">EDestroyNonzeroBalance</a>: u64 = 5; </code></pre><a name="sui_system_staking_pool_ETokenTimeLockIsSome"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_ETokenTimeLockIsSome">ETokenTimeLockIsSome</a>: u64 = 6; </code></pre><a name="sui_system_staking_pool_EWrongDelegation"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EWrongDelegation">EWrongDelegation</a>: u64 = 7; </code></pre><a name="sui_system_staking_pool_EPendingDelegationDoesNotExist"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EPendingDelegationDoesNotExist">EPendingDelegationDoesNotExist</a>: u64 = 8; </code></pre><a name="sui_system_staking_pool_ETokenBalancesDoNotMatchExchangeRate"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_ETokenBalancesDoNotMatchExchangeRate">ETokenBalancesDoNotMatchExchangeRate</a>: u64 = 9; </code></pre><a name="sui_system_staking_pool_EDelegationToInactivePool"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EDelegationToInactivePool">EDelegationToInactivePool</a>: u64 = 10; </code></pre><a name="sui_system_staking_pool_EDeactivationOfInactivePool"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EDeactivationOfInactivePool">EDeactivationOfInactivePool</a>: u64 = 11; </code></pre><a name="sui_system_staking_pool_EIncompatibleStakedSui"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EIncompatibleStakedSui">EIncompatibleStakedSui</a>: u64 = 12; </code></pre><a name="sui_system_staking_pool_EWithdrawalInSameEpoch"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EWithdrawalInSameEpoch">EWithdrawalInSameEpoch</a>: u64 = 13; </code></pre><a name="sui_system_staking_pool_EPoolAlreadyActive"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EPoolAlreadyActive">EPoolAlreadyActive</a>: u64 = 14; </code></pre><a name="sui_system_staking_pool_EPoolPreactiveOrInactive"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EPoolPreactiveOrInactive">EPoolPreactiveOrInactive</a>: u64 = 15; </code></pre><a name="sui_system_staking_pool_EActivationOfInactivePool"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EActivationOfInactivePool">EActivationOfInactivePool</a>: u64 = 16; </code></pre><a name="sui_system_staking_pool_EDelegationOfZeroSui"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EDelegationOfZeroSui">EDelegationOfZeroSui</a>: u64 = 17; </code></pre><a name="sui_system_staking_pool_EStakedSuiBelowThreshold"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EStakedSuiBelowThreshold">EStakedSuiBelowThreshold</a>: u64 = 18; </code></pre><a name="sui_system_staking_pool_ECannotMintFungibleStakedSuiYet"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_ECannotMintFungibleStakedSuiYet">ECannotMintFungibleStakedSuiYet</a>: u64 = 19; </code></pre><a name="sui_system_staking_pool_EInvariantFailure"></a>
<pre><code><b>const</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EInvariantFailure">EInvariantFailure</a>: u64 = 20; </code></pre><a name="sui_system_staking_pool_new"></a>
newCreate a new, empty staking pool.
<pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_new">new</a>(ctx: &<b>mut</b> <a href="../sui/tx_context.md#sui_tx_context_TxContext">sui::tx_context::TxContext</a>): <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_new">new</a>(ctx: &<b>mut</b> TxContext): <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a> { <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a> { id: object::new(ctx), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a>: option::none(), deactivation_epoch: option::none(), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a>: 0, rewards_pool: balance::zero(), pool_token_balance: 0, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_exchange_rates">exchange_rates</a>: table::new(ctx), pending_stake: 0, pending_total_sui_withdraw: 0, pending_pool_token_withdraw: 0, extra_fields: bag::new(ctx), } } </code></pre> </details><a name="sui_system_staking_pool_request_add_stake"></a>
request_add_stakeRequest to stake to a staking pool. The stake starts counting at the beginning of the next epoch,
<pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_request_add_stake">request_add_stake</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>, stake: <a href="../sui/balance.md#sui_balance_Balance">sui::balance::Balance</a><<a href="../sui/sui.md#sui_sui_SUI">sui::sui::SUI</a>>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>: u64, ctx: &<b>mut</b> <a href="../sui/tx_context.md#sui_tx_context_TxContext">sui::tx_context::TxContext</a>): <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_request_add_stake">request_add_stake</a>( pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>, stake: Balance<SUI>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>: u64, ctx: &<b>mut</b> TxContext, ): <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a> { <b>let</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_amount">sui_amount</a> = stake.value(); <b>assert</b>!(!pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_inactive">is_inactive</a>(), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EDelegationToInactivePool">EDelegationToInactivePool</a>); <b>assert</b>!(<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_amount">sui_amount</a> > 0, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EDelegationOfZeroSui">EDelegationOfZeroSui</a>); pool.pending_stake = pool.pending_stake + <a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_amount">sui_amount</a>; <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a> { id: object::new(ctx), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_id">pool_id</a>: object::id(pool), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>, principal: stake, } } </code></pre> </details><a name="sui_system_staking_pool_request_withdraw_stake"></a>
request_withdraw_stakeRequest to withdraw the given stake plus rewards from a staking pool. Both the principal and corresponding rewards in SUI are withdrawn. A proportional amount of pool token withdraw is recorded and processed at epoch change time.
<pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_request_withdraw_stake">request_withdraw_stake</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>, staked_sui: <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a>, ctx: &<a href="../sui/tx_context.md#sui_tx_context_TxContext">sui::tx_context::TxContext</a>): <a href="../sui/balance.md#sui_balance_Balance">sui::balance::Balance</a><<a href="../sui/sui.md#sui_sui_SUI">sui::sui::SUI</a>> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_request_withdraw_stake">request_withdraw_stake</a>( pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>, staked_sui: <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a>, ctx: &TxContext, ): Balance<SUI> { // stake is inactive and the pool is not preactive - allow direct withdraw // the reason why we exclude preactive pools is to avoid potential underflow // on subtraction, and we need to enforce `pending_stake_withdraw` call. <b>if</b> (staked_sui.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a> > ctx.epoch() && !pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_preactive">is_preactive</a>()) { <b>let</b> principal = staked_sui.into_balance(); pool.pending_stake = pool.pending_stake - principal.value(); <b>return</b> principal }; <b>let</b> (pool_token_withdraw_amount, <b>mut</b> principal_withdraw) = pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_withdraw_from_principal">withdraw_from_principal</a>( staked_sui, ); <b>let</b> principal_withdraw_amount = principal_withdraw.value(); <b>let</b> rewards_withdraw = pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_withdraw_rewards">withdraw_rewards</a>( principal_withdraw_amount, pool_token_withdraw_amount, ctx.epoch(), ); <b>let</b> total_sui_withdraw_amount = principal_withdraw_amount + rewards_withdraw.value(); pool.pending_total_sui_withdraw = pool.pending_total_sui_withdraw + total_sui_withdraw_amount; pool.pending_pool_token_withdraw = pool.pending_pool_token_withdraw + pool_token_withdraw_amount; // If the pool is inactive or preactive, we immediately process the withdrawal. <b>if</b> (pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_inactive">is_inactive</a>() || pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_preactive">is_preactive</a>()) pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_process_pending_stake_withdraw">process_pending_stake_withdraw</a>(); // TODO: implement withdraw bonding period here. principal_withdraw.join(rewards_withdraw); principal_withdraw } </code></pre> </details><a name="sui_system_staking_pool_redeem_fungible_staked_sui"></a>
redeem_fungible_staked_sui<a name="sui_system_staking_pool_calculate_fungible_staked_sui_withdraw_amount"></a>
calculate_fungible_staked_sui_withdraw_amountwritten in separate function so i can test with random values returns (principal_withdraw_amount, rewards_withdraw_amount)
<pre><code><b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_calculate_fungible_staked_sui_withdraw_amount">calculate_fungible_staked_sui_withdraw_amount</a>(latest_exchange_rate: <a href="../sui_system/staking_pool.md#sui_system_staking_pool_PoolTokenExchangeRate">sui_system::staking_pool::PoolTokenExchangeRate</a>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_fungible_staked_sui_value">fungible_staked_sui_value</a>: u64, fungible_staked_sui_data_principal_amount: u64, fungible_staked_sui_data_total_supply: u64): (u64, u64) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_calculate_fungible_staked_sui_withdraw_amount">calculate_fungible_staked_sui_withdraw_amount</a>( latest_exchange_rate: <a href="../sui_system/staking_pool.md#sui_system_staking_pool_PoolTokenExchangeRate">PoolTokenExchangeRate</a>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_fungible_staked_sui_value">fungible_staked_sui_value</a>: u64, fungible_staked_sui_data_principal_amount: u64, // fungible_staked_sui_data.principal.value() fungible_staked_sui_data_total_supply: u64, // fungible_staked_sui_data.total_supply ): (u64, u64) { // 1. <b>if</b> the entire <a href="../sui_system/staking_pool.md#sui_system_staking_pool_FungibleStakedSuiData">FungibleStakedSuiData</a> supply is redeemed, how much sui should we receive? <b>let</b> total_sui_amount = latest_exchange_rate.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_get_sui_amount">get_sui_amount</a>( fungible_staked_sui_data_total_supply, ); // min with total_sui_amount to prevent underflow <b>let</b> fungible_staked_sui_data_principal_amount = fungible_staked_sui_data_principal_amount.min( total_sui_amount, ); // 2. how much do we need to withdraw from the rewards pool? <b>let</b> total_rewards = total_sui_amount - fungible_staked_sui_data_principal_amount; // 3. proportionally withdraw from both wrt the <a href="../sui_system/staking_pool.md#sui_system_staking_pool_fungible_staked_sui_value">fungible_staked_sui_value</a>. <b>let</b> principal_withdraw_amount = <a href="../sui_system/staking_pool.md#sui_system_staking_pool_mul_div">mul_div</a>!( <a href="../sui_system/staking_pool.md#sui_system_staking_pool_fungible_staked_sui_value">fungible_staked_sui_value</a>, fungible_staked_sui_data_principal_amount, fungible_staked_sui_data_total_supply, ); <b>let</b> rewards_withdraw_amount = <a href="../sui_system/staking_pool.md#sui_system_staking_pool_mul_div">mul_div</a>!( <a href="../sui_system/staking_pool.md#sui_system_staking_pool_fungible_staked_sui_value">fungible_staked_sui_value</a>, total_rewards, fungible_staked_sui_data_total_supply, ); // <b>invariant</b> check, just in case <b>let</b> expected_sui_amount = latest_exchange_rate.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_get_sui_amount">get_sui_amount</a>(<a href="../sui_system/staking_pool.md#sui_system_staking_pool_fungible_staked_sui_value">fungible_staked_sui_value</a>); <b>assert</b>!( principal_withdraw_amount + rewards_withdraw_amount <= expected_sui_amount, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EInvariantFailure">EInvariantFailure</a>, ); (principal_withdraw_amount, rewards_withdraw_amount) } </code></pre> </details><a name="sui_system_staking_pool_convert_to_fungible_staked_sui"></a>
convert_to_fungible_staked_suiConvert the given staked SUI to an FungibleStakedSui object
<pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_convert_to_fungible_staked_sui">convert_to_fungible_staked_sui</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>, staked_sui: <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a>, ctx: &<b>mut</b> <a href="../sui/tx_context.md#sui_tx_context_TxContext">sui::tx_context::TxContext</a>): <a href="../sui_system/staking_pool.md#sui_system_staking_pool_FungibleStakedSui">sui_system::staking_pool::FungibleStakedSui</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_convert_to_fungible_staked_sui">convert_to_fungible_staked_sui</a>( pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>, staked_sui: <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a>, ctx: &<b>mut</b> TxContext, ): <a href="../sui_system/staking_pool.md#sui_system_staking_pool_FungibleStakedSui">FungibleStakedSui</a> { <b>let</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a> { id, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_id">pool_id</a>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>, principal } = staked_sui; <b>assert</b>!(<a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_id">pool_id</a> == object::id(pool), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EWrongPool">EWrongPool</a>); <b>assert</b>!(ctx.epoch() >= <a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_ECannotMintFungibleStakedSuiYet">ECannotMintFungibleStakedSuiYet</a>); <b>assert</b>!(!pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_preactive">is_preactive</a>() && !pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_inactive">is_inactive</a>(), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EPoolPreactiveOrInactive">EPoolPreactiveOrInactive</a>); id.delete(); <b>let</b> exchange_rate_at_staking_epoch = pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_token_exchange_rate_at_epoch">pool_token_exchange_rate_at_epoch</a>( <a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>, ); <b>let</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_token_amount">pool_token_amount</a> = exchange_rate_at_staking_epoch.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_get_token_amount">get_token_amount</a>(principal.value()); <b>let</b> key = <a href="../sui_system/staking_pool.md#sui_system_staking_pool_FungibleStakedSuiDataKey">FungibleStakedSuiDataKey</a> {}; <b>if</b> (!pool.extra_fields.contains(key)) { pool .extra_fields .add( key, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_FungibleStakedSuiData">FungibleStakedSuiData</a> { id: object::new(ctx), total_supply: <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_token_amount">pool_token_amount</a>, principal, }, ); } <b>else</b> { <b>let</b> fungible_staked_sui_data: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_FungibleStakedSuiData">FungibleStakedSuiData</a> = &<b>mut</b> pool.extra_fields[key]; fungible_staked_sui_data.total_supply = fungible_staked_sui_data.total_supply + <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_token_amount">pool_token_amount</a>; fungible_staked_sui_data.principal.join(principal); }; <a href="../sui_system/staking_pool.md#sui_system_staking_pool_FungibleStakedSui">FungibleStakedSui</a> { id: object::new(ctx), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_id">pool_id</a>, value: <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_token_amount">pool_token_amount</a>, } } </code></pre> </details><a name="sui_system_staking_pool_withdraw_from_principal"></a>
withdraw_from_principalWithdraw the principal SUI stored in the StakedSui 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 SUI.
<pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_withdraw_from_principal">withdraw_from_principal</a>(pool: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>, staked_sui: <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a>): (u64, <a href="../sui/balance.md#sui_balance_Balance">sui::balance::Balance</a><<a href="../sui/sui.md#sui_sui_SUI">sui::sui::SUI</a>>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_withdraw_from_principal">withdraw_from_principal</a>( pool: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>, staked_sui: <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a>, ): (u64, Balance<SUI>) { // Check that the stake information matches the pool. <b>assert</b>!(staked_sui.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_id">pool_id</a> == object::id(pool), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EWrongPool">EWrongPool</a>); <b>let</b> exchange_rate_at_staking_epoch = pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_token_exchange_rate_at_epoch">pool_token_exchange_rate_at_epoch</a>(staked_sui.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>); <b>let</b> principal_withdraw = staked_sui.into_balance(); <b>let</b> pool_token_withdraw_amount = exchange_rate_at_staking_epoch.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_get_token_amount">get_token_amount</a>(principal_withdraw.value()); (pool_token_withdraw_amount, principal_withdraw) } </code></pre> </details><a name="sui_system_staking_pool_unwrap_staked_sui"></a>
unwrap_staked_sui<a name="sui_system_staking_pool_deposit_rewards"></a>
deposit_rewardsCalled at epoch advancement times to add rewards (in SUI) to the staking pool.
<pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_deposit_rewards">deposit_rewards</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>, rewards: <a href="../sui/balance.md#sui_balance_Balance">sui::balance::Balance</a><<a href="../sui/sui.md#sui_sui_SUI">sui::sui::SUI</a>>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_deposit_rewards">deposit_rewards</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>, rewards: Balance<SUI>) { pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a> = pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a> + rewards.value(); pool.rewards_pool.join(rewards); } </code></pre> </details><a name="sui_system_staking_pool_process_pending_stakes_and_withdraws"></a>
process_pending_stakes_and_withdraws<a name="sui_system_staking_pool_process_pending_stake_withdraw"></a>
process_pending_stake_withdrawCalled at epoch boundaries to process pending stake withdraws requested during the epoch. Also called immediately upon withdrawal if the pool is inactive.
<pre><code><b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_process_pending_stake_withdraw">process_pending_stake_withdraw</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_process_pending_stake_withdraw">process_pending_stake_withdraw</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>) { pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a> = <b>if</b> (pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a> >= pool.pending_total_sui_withdraw) { pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a> - pool.pending_total_sui_withdraw } <b>else</b> { // the diff will be applied in the `<a href="../sui_system/staking_pool.md#sui_system_staking_pool_process_pending_stake">process_pending_stake</a>` function. <b>let</b> diff = pool.pending_total_sui_withdraw - pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a>; pool.extra_fields.add(<a href="../sui_system/staking_pool.md#sui_system_staking_pool_UnderflowSuiBalance">UnderflowSuiBalance</a> {}, diff); 0 }; pool.pool_token_balance = <b>if</b> (pool.pool_token_balance >= pool.pending_pool_token_withdraw) { pool.pool_token_balance - pool.pending_pool_token_withdraw } <b>else</b> { 0 }; pool.pending_total_sui_withdraw = 0; pool.pending_pool_token_withdraw = 0; } </code></pre> </details><a name="sui_system_staking_pool_process_pending_stake"></a>
process_pending_stakeCalled at epoch boundaries to process the pending stake.
<pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_process_pending_stake">process_pending_stake</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_process_pending_stake">process_pending_stake</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>) { // Use the most up to date exchange rate with the rewards deposited and withdraws effectuated. <b>let</b> latest_exchange_rate = <a href="../sui_system/staking_pool.md#sui_system_staking_pool_PoolTokenExchangeRate">PoolTokenExchangeRate</a> { <a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_amount">sui_amount</a>: pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_token_amount">pool_token_amount</a>: pool.pool_token_balance, }; // This key is only present <b>if</b> the `<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a>` underflowed, hence, the current value of `<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a>` // is `0`. Pool token balance will be recalculated automatically <b>for</b> `0` value. <b>let</b> sui_diff = { <b>let</b> key = <a href="../sui_system/staking_pool.md#sui_system_staking_pool_UnderflowSuiBalance">UnderflowSuiBalance</a> {}; <b>if</b> (pool.extra_fields.contains(key)) pool.extra_fields.remove(key) <b>else</b> 0 }; pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a> = pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a> + pool.pending_stake - sui_diff; pool.pool_token_balance = latest_exchange_rate.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_get_token_amount">get_token_amount</a>(pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_sui_balance">sui_balance</a>); pool.pending_stake = 0; } </code></pre> </details><a name="sui_system_staking_pool_withdraw_rewards"></a>
withdraw_rewardsThis function does the following:
<a name="sui_system_staking_pool_activate_staking_pool"></a>
activate_staking_poolCalled by <code><a href="../sui_system/validator.md#sui_system_validator">validator</a></code> module to activate a staking pool.
<pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_activate_staking_pool">activate_staking_pool</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a>: u64) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_activate_staking_pool">activate_staking_pool</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a>: u64) { // Add the initial exchange rate to the table. pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_exchange_rates">exchange_rates</a>.add(<a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_initial_exchange_rate">initial_exchange_rate</a>()); // Check that the pool is preactive and not inactive. <b>assert</b>!(pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_preactive">is_preactive</a>(), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EPoolAlreadyActive">EPoolAlreadyActive</a>); <b>assert</b>!(!pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_inactive">is_inactive</a>(), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EActivationOfInactivePool">EActivationOfInactivePool</a>); // Fill in the active epoch. pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a>.fill(<a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a>); } </code></pre> </details><a name="sui_system_staking_pool_deactivate_staking_pool"></a>
deactivate_staking_poolDeactivate a staking pool by setting the <code>deactivation_epoch</code>. After this pool deactivation, the pool stops earning rewards. Only stake withdraws can be made to the pool.
<pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_deactivate_staking_pool">deactivate_staking_pool</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>, deactivation_epoch: u64) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_deactivate_staking_pool">deactivate_staking_pool</a>(pool: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>, deactivation_epoch: u64) { // We can't deactivate an already deactivated pool. <b>assert</b>!(!pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_inactive">is_inactive</a>(), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EDeactivationOfInactivePool">EDeactivationOfInactivePool</a>); pool.deactivation_epoch = option::some(deactivation_epoch); } </code></pre> </details><a name="sui_system_staking_pool_sui_balance"></a>
sui_balance<a name="sui_system_staking_pool_pool_id"></a>
pool_id<a name="sui_system_staking_pool_fungible_staked_sui_pool_id"></a>
fungible_staked_sui_pool_id<a name="sui_system_staking_pool_staked_sui_amount"></a>
staked_sui_amountReturns the principal amount of <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a></code>.
<pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_staked_sui_amount">staked_sui_amount</a>(staked_sui: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a>): u64 </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_staked_sui_amount">staked_sui_amount</a>(staked_sui: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a>): u64 { staked_sui.principal.value() } </code></pre> </details><a name="sui_system_staking_pool_stake_activation_epoch"></a>
stake_activation_epochReturns the activation epoch of <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a></code>.
<pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>(staked_sui: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a>): u64 </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>(staked_sui: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a>): u64 { staked_sui.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a> } </code></pre> </details><a name="sui_system_staking_pool_is_preactive"></a>
is_preactiveReturns true if the input staking pool is preactive.
<pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_preactive">is_preactive</a>(pool: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>): bool </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_preactive">is_preactive</a>(pool: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>): bool { pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a>.is_none() } </code></pre> </details><a name="sui_system_staking_pool_activation_epoch"></a>
activation_epochReturns the activation epoch of the <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a></code>. For validator candidates, or pending validators, the value returned is <code>None</code>. For active validators, the value is the epoch before the validator was activated.
<pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a>(pool: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>): <a href="../std/option.md#std_option_Option">std::option::Option</a><u64> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b>(package) <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a>(pool: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>): Option<u64> { pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a> } </code></pre> </details><a name="sui_system_staking_pool_is_inactive"></a>
is_inactiveReturns true if the input staking pool is inactive.
<pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_inactive">is_inactive</a>(pool: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>): bool </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_inactive">is_inactive</a>(pool: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>): bool { pool.deactivation_epoch.is_some() } </code></pre> </details><a name="sui_system_staking_pool_fungible_staked_sui_value"></a>
fungible_staked_sui_value<a name="sui_system_staking_pool_split_fungible_staked_sui"></a>
split_fungible_staked_sui<a name="sui_system_staking_pool_join_fungible_staked_sui"></a>
join_fungible_staked_sui<a name="sui_system_staking_pool_split"></a>
splitSplit StakedSui <code>self</code> to two parts, one with principal <code>split_amount</code>, and the remaining principal is left in <code>self</code>. All the other parameters of the StakedSui like <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a></code> or <code><a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_id">pool_id</a></code> remain the same.
<pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_split">split</a>(self: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a>, split_amount: u64, ctx: &<b>mut</b> <a href="../sui/tx_context.md#sui_tx_context_TxContext">sui::tx_context::TxContext</a>): <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a> </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_split">split</a>(self: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a>, split_amount: u64, ctx: &<b>mut</b> TxContext): <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a> { <b>let</b> original_amount = self.principal.value(); <b>assert</b>!(split_amount <= original_amount, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EInsufficientSuiTokenBalance">EInsufficientSuiTokenBalance</a>); <b>let</b> remaining_amount = original_amount - split_amount; // Both resulting parts should have at least <a href="../sui_system/staking_pool.md#sui_system_staking_pool_MIN_STAKING_THRESHOLD">MIN_STAKING_THRESHOLD</a>. <b>assert</b>!(remaining_amount >= <a href="../sui_system/staking_pool.md#sui_system_staking_pool_MIN_STAKING_THRESHOLD">MIN_STAKING_THRESHOLD</a>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EStakedSuiBelowThreshold">EStakedSuiBelowThreshold</a>); <b>assert</b>!(split_amount >= <a href="../sui_system/staking_pool.md#sui_system_staking_pool_MIN_STAKING_THRESHOLD">MIN_STAKING_THRESHOLD</a>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EStakedSuiBelowThreshold">EStakedSuiBelowThreshold</a>); <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a> { id: object::new(ctx), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_id">pool_id</a>: self.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_id">pool_id</a>, <a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>: self.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>, principal: self.principal.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_split">split</a>(split_amount), } } </code></pre> </details><a name="sui_system_staking_pool_split_staked_sui"></a>
split_staked_suiSplit the given StakedSui to the two parts, one with principal <code>split_amount</code>, transfer the newly split part to the sender address.
<pre><code><b>public</b> <b>entry</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_split_staked_sui">split_staked_sui</a>(stake: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a>, split_amount: u64, ctx: &<b>mut</b> <a href="../sui/tx_context.md#sui_tx_context_TxContext">sui::tx_context::TxContext</a>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>entry</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_split_staked_sui">split_staked_sui</a>(stake: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a>, split_amount: u64, ctx: &<b>mut</b> TxContext) { transfer::transfer(stake.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_split">split</a>(split_amount, ctx), ctx.sender()); } </code></pre> </details><a name="sui_system_staking_pool_join_staked_sui"></a>
join_staked_suiConsume the staked sui <code>other</code> and add its value to <code>self</code>. Aborts if some of the staking parameters are incompatible (pool id, stake activation epoch, etc.)
<pre><code><b>public</b> <b>entry</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_join_staked_sui">join_staked_sui</a>(self: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a>, other: <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a>) </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>entry</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_join_staked_sui">join_staked_sui</a>(self: &<b>mut</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a>, other: <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a>) { <b>assert</b>!(<a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_equal_staking_metadata">is_equal_staking_metadata</a>(self, &other), <a href="../sui_system/staking_pool.md#sui_system_staking_pool_EIncompatibleStakedSui">EIncompatibleStakedSui</a>); <b>let</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a> { id, principal, .. } = other; id.delete(); self.principal.join(principal); } </code></pre> </details><a name="sui_system_staking_pool_is_equal_staking_metadata"></a>
is_equal_staking_metadataReturns true if all the staking parameters of the staked sui except the principal are identical
<pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_equal_staking_metadata">is_equal_staking_metadata</a>(self: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a>, other: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">sui_system::staking_pool::StakedSui</a>): bool </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_equal_staking_metadata">is_equal_staking_metadata</a>(self: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a>, other: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakedSui">StakedSui</a>): bool { (self.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_id">pool_id</a> == other.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_pool_id">pool_id</a>) && (self.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a> == other.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_stake_activation_epoch">stake_activation_epoch</a>) } </code></pre> </details><a name="sui_system_staking_pool_pool_token_exchange_rate_at_epoch"></a>
pool_token_exchange_rate_at_epoch<a name="sui_system_staking_pool_pending_stake_amount"></a>
pending_stake_amountReturns the total value of the pending staking requests for this staking pool.
<pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pending_stake_amount">pending_stake_amount</a>(<a href="../sui_system/staking_pool.md#sui_system_staking_pool">staking_pool</a>: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>): u64 </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pending_stake_amount">pending_stake_amount</a>(<a href="../sui_system/staking_pool.md#sui_system_staking_pool">staking_pool</a>: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>): u64 { <a href="../sui_system/staking_pool.md#sui_system_staking_pool">staking_pool</a>.pending_stake } </code></pre> </details><a name="sui_system_staking_pool_pending_stake_withdraw_amount"></a>
pending_stake_withdraw_amountReturns the total withdrawal from the staking pool this epoch.
<pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pending_stake_withdraw_amount">pending_stake_withdraw_amount</a>(<a href="../sui_system/staking_pool.md#sui_system_staking_pool">staking_pool</a>: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>): u64 </code></pre> <details> <summary>Implementation</summary> <pre><code><b>public</b> <b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_pending_stake_withdraw_amount">pending_stake_withdraw_amount</a>(<a href="../sui_system/staking_pool.md#sui_system_staking_pool">staking_pool</a>: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>): u64 { <a href="../sui_system/staking_pool.md#sui_system_staking_pool">staking_pool</a>.pending_total_sui_withdraw } </code></pre> </details><a name="sui_system_staking_pool_exchange_rates"></a>
exchange_rates<a name="sui_system_staking_pool_sui_amount"></a>
sui_amount<a name="sui_system_staking_pool_pool_token_amount"></a>
pool_token_amount<a name="sui_system_staking_pool_is_preactive_at_epoch"></a>
is_preactive_at_epochReturns true if the provided staking pool is preactive at the provided epoch.
<pre><code><b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_preactive_at_epoch">is_preactive_at_epoch</a>(pool: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">sui_system::staking_pool::StakingPool</a>, epoch: u64): bool </code></pre> <details> <summary>Implementation</summary> <pre><code><b>fun</b> <a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_preactive_at_epoch">is_preactive_at_epoch</a>(pool: &<a href="../sui_system/staking_pool.md#sui_system_staking_pool_StakingPool">StakingPool</a>, epoch: u64): bool { // Either the pool is currently preactive or the pool's starting epoch is later than the provided epoch. pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_is_preactive">is_preactive</a>() || (*pool.<a href="../sui_system/staking_pool.md#sui_system_staking_pool_activation_epoch">activation_epoch</a>.borrow() > epoch) } </code></pre> </details><a name="sui_system_staking_pool_get_sui_amount"></a>
get_sui_amount<a name="sui_system_staking_pool_get_token_amount"></a>
get_token_amount<a name="sui_system_staking_pool_initial_exchange_rate"></a>
initial_exchange_rate<a name="sui_system_staking_pool_check_balance_invariants"></a>
check_balance_invariants<a name="sui_system_staking_pool_mul_div"></a>
mul_div<a name="sui_system_staking_pool_calculate_rewards"></a>
calculate_rewards