Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PTB friendly staking funs #12092

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions crates/sui-framework/docs/staking_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ Both the principal and corresponding rewards in SUI are withdrawn and transferre
A proportional amount of pool token withdraw is recorded and processed at epoch change time.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="staking_pool.md#0x3_staking_pool_request_withdraw_stake">request_withdraw_stake</a>(pool: &<b>mut</b> <a href="staking_pool.md#0x3_staking_pool_StakingPool">staking_pool::StakingPool</a>, staked_sui: <a href="staking_pool.md#0x3_staking_pool_StakedSui">staking_pool::StakedSui</a>, ctx: &<b>mut</b> <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): u64
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="staking_pool.md#0x3_staking_pool_request_withdraw_stake">request_withdraw_stake</a>(pool: &<b>mut</b> <a href="staking_pool.md#0x3_staking_pool_StakingPool">staking_pool::StakingPool</a>, staked_sui: <a href="staking_pool.md#0x3_staking_pool_StakedSui">staking_pool::StakedSui</a>, ctx: &<b>mut</b> <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): <a href="../../../.././build/Sui/docs/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../../../.././build/Sui/docs/sui.md#0x2_sui_SUI">sui::SUI</a>&gt;
</code></pre>


Expand All @@ -517,10 +517,9 @@ A proportional amount of pool token withdraw is recorded and processed at epoch
pool: &<b>mut</b> <a href="staking_pool.md#0x3_staking_pool_StakingPool">StakingPool</a>,
staked_sui: <a href="staking_pool.md#0x3_staking_pool_StakedSui">StakedSui</a>,
ctx: &<b>mut</b> TxContext
) : u64 {
) : Balance&lt;SUI&gt; {
<b>let</b> (pool_token_withdraw_amount, principal_withdraw) =
<a href="staking_pool.md#0x3_staking_pool_withdraw_from_principal">withdraw_from_principal</a>(pool, staked_sui);
<b>let</b> staker = <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_sender">tx_context::sender</a>(ctx);
<b>let</b> principal_withdraw_amount = <a href="../../../.././build/Sui/docs/balance.md#0x2_balance_value">balance::value</a>(&principal_withdraw);

<b>let</b> rewards_withdraw = <a href="staking_pool.md#0x3_staking_pool_withdraw_rewards">withdraw_rewards</a>(
Expand All @@ -536,8 +535,7 @@ A proportional amount of pool token withdraw is recorded and processed at epoch

// TODO: implement withdraw bonding period here.
<a href="../../../.././build/Sui/docs/balance.md#0x2_balance_join">balance::join</a>(&<b>mut</b> principal_withdraw, rewards_withdraw);
<a href="../../../.././build/Sui/docs/transfer.md#0x2_transfer_public_transfer">transfer::public_transfer</a>(<a href="../../../.././build/Sui/docs/coin.md#0x2_coin_from_balance">coin::from_balance</a>(principal_withdraw, ctx), staker);
total_sui_withdraw_amount
principal_withdraw
}
</code></pre>

Expand Down
31 changes: 31 additions & 0 deletions crates/sui-framework/docs/sui_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ the SuiSystemStateInner version, or vice versa.
- [Function `request_add_stake`](#0x3_sui_system_request_add_stake)
- [Function `request_add_stake_mul_coin`](#0x3_sui_system_request_add_stake_mul_coin)
- [Function `request_withdraw_stake`](#0x3_sui_system_request_withdraw_stake)
- [Function `request_withdraw_stake_non_entry`](#0x3_sui_system_request_withdraw_stake_non_entry)
- [Function `report_validator`](#0x3_sui_system_report_validator)
- [Function `undo_report_validator`](#0x3_sui_system_undo_report_validator)
- [Function `rotate_operation_cap`](#0x3_sui_system_rotate_operation_cap)
Expand Down Expand Up @@ -571,6 +572,36 @@ Withdraw some portion of a stake from a validator's staking pool.
staked_sui: StakedSui,
ctx: &<b>mut</b> TxContext,
) {
<b>let</b> withdrawn_stake = <a href="sui_system.md#0x3_sui_system_request_withdraw_stake_non_entry">request_withdraw_stake_non_entry</a>(wrapper, staked_sui, ctx);
<a href="../../../.././build/Sui/docs/transfer.md#0x2_transfer_public_transfer">transfer::public_transfer</a>(<a href="../../../.././build/Sui/docs/coin.md#0x2_coin_from_balance">coin::from_balance</a>(withdrawn_stake, ctx), <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_sender">tx_context::sender</a>(ctx));
}
</code></pre>



</details>

<a name="0x3_sui_system_request_withdraw_stake_non_entry"></a>

## Function `request_withdraw_stake_non_entry`

Non-entry version of <code>request_withdraw_stake</code> that returns the withdrawn SUI instead of transferring it to the sender.


<pre><code><b>public</b> <b>fun</b> <a href="sui_system.md#0x3_sui_system_request_withdraw_stake_non_entry">request_withdraw_stake_non_entry</a>(wrapper: &<b>mut</b> <a href="sui_system.md#0x3_sui_system_SuiSystemState">sui_system::SuiSystemState</a>, staked_sui: <a href="staking_pool.md#0x3_staking_pool_StakedSui">staking_pool::StakedSui</a>, ctx: &<b>mut</b> <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): <a href="../../../.././build/Sui/docs/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../../../.././build/Sui/docs/sui.md#0x2_sui_SUI">sui::SUI</a>&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="sui_system.md#0x3_sui_system_request_withdraw_stake_non_entry">request_withdraw_stake_non_entry</a>(
wrapper: &<b>mut</b> <a href="sui_system.md#0x3_sui_system_SuiSystemState">SuiSystemState</a>,
staked_sui: StakedSui,
ctx: &<b>mut</b> TxContext,
) : Balance&lt;SUI&gt; {
<b>let</b> self = <a href="sui_system.md#0x3_sui_system_load_system_state_mut">load_system_state_mut</a>(wrapper);
<a href="sui_system_state_inner.md#0x3_sui_system_state_inner_request_withdraw_stake">sui_system_state_inner::request_withdraw_stake</a>(self, staked_sui, ctx)
}
Expand Down
6 changes: 3 additions & 3 deletions crates/sui-framework/docs/sui_system_state_inner.md
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ Add stake to a validator's staking pool using multiple coins.
Withdraw some portion of a stake from a validator's staking pool.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="sui_system_state_inner.md#0x3_sui_system_state_inner_request_withdraw_stake">request_withdraw_stake</a>(self: &<b>mut</b> <a href="sui_system_state_inner.md#0x3_sui_system_state_inner_SuiSystemStateInnerV2">sui_system_state_inner::SuiSystemStateInnerV2</a>, staked_sui: <a href="staking_pool.md#0x3_staking_pool_StakedSui">staking_pool::StakedSui</a>, ctx: &<b>mut</b> <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="sui_system_state_inner.md#0x3_sui_system_state_inner_request_withdraw_stake">request_withdraw_stake</a>(self: &<b>mut</b> <a href="sui_system_state_inner.md#0x3_sui_system_state_inner_SuiSystemStateInnerV2">sui_system_state_inner::SuiSystemStateInnerV2</a>, staked_sui: <a href="staking_pool.md#0x3_staking_pool_StakedSui">staking_pool::StakedSui</a>, ctx: &<b>mut</b> <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): <a href="../../../.././build/Sui/docs/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../../../.././build/Sui/docs/sui.md#0x2_sui_SUI">sui::SUI</a>&gt;
</code></pre>


Expand All @@ -1304,14 +1304,14 @@ Withdraw some portion of a stake from a validator's staking pool.
self: &<b>mut</b> <a href="sui_system_state_inner.md#0x3_sui_system_state_inner_SuiSystemStateInnerV2">SuiSystemStateInnerV2</a>,
staked_sui: StakedSui,
ctx: &<b>mut</b> TxContext,
) {
) : Balance&lt;SUI&gt; {
<b>assert</b>!(
stake_activation_epoch(&staked_sui) &lt;= <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_epoch">tx_context::epoch</a>(ctx),
<a href="sui_system_state_inner.md#0x3_sui_system_state_inner_EStakeWithdrawBeforeActivation">EStakeWithdrawBeforeActivation</a>
);
<a href="validator_set.md#0x3_validator_set_request_withdraw_stake">validator_set::request_withdraw_stake</a>(
&<b>mut</b> self.validators, staked_sui, ctx,
);
)
}
</code></pre>

Expand Down
10 changes: 6 additions & 4 deletions crates/sui-framework/docs/validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ Request to add stake to the validator's staking pool at genesis
Request to withdraw stake from the validator's staking pool, processed at the end of the epoch.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="validator.md#0x3_validator_request_withdraw_stake">request_withdraw_stake</a>(self: &<b>mut</b> <a href="validator.md#0x3_validator_Validator">validator::Validator</a>, staked_sui: <a href="staking_pool.md#0x3_staking_pool_StakedSui">staking_pool::StakedSui</a>, ctx: &<b>mut</b> <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="validator.md#0x3_validator_request_withdraw_stake">request_withdraw_stake</a>(self: &<b>mut</b> <a href="validator.md#0x3_validator_Validator">validator::Validator</a>, staked_sui: <a href="staking_pool.md#0x3_staking_pool_StakedSui">staking_pool::StakedSui</a>, ctx: &<b>mut</b> <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): <a href="../../../.././build/Sui/docs/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../../../.././build/Sui/docs/sui.md#0x2_sui_SUI">sui::SUI</a>&gt;
</code></pre>


Expand All @@ -984,11 +984,12 @@ Request to withdraw stake from the validator's staking pool, processed at the en
self: &<b>mut</b> <a href="validator.md#0x3_validator_Validator">Validator</a>,
staked_sui: StakedSui,
ctx: &<b>mut</b> TxContext,
) {
) : Balance&lt;SUI&gt; {
<b>let</b> principal_amount = <a href="staking_pool.md#0x3_staking_pool_staked_sui_amount">staking_pool::staked_sui_amount</a>(&staked_sui);
<b>let</b> stake_activation_epoch = <a href="staking_pool.md#0x3_staking_pool_stake_activation_epoch">staking_pool::stake_activation_epoch</a>(&staked_sui);
<b>let</b> withdraw_amount = <a href="staking_pool.md#0x3_staking_pool_request_withdraw_stake">staking_pool::request_withdraw_stake</a>(
<b>let</b> withdrawn_stake = <a href="staking_pool.md#0x3_staking_pool_request_withdraw_stake">staking_pool::request_withdraw_stake</a>(
&<b>mut</b> self.<a href="staking_pool.md#0x3_staking_pool">staking_pool</a>, staked_sui, ctx);
<b>let</b> withdraw_amount = <a href="../../../.././build/Sui/docs/balance.md#0x2_balance_value">balance::value</a>(&withdrawn_stake);
<b>let</b> reward_amount = withdraw_amount - principal_amount;
self.next_epoch_stake = self.next_epoch_stake - withdraw_amount;
<a href="../../../.././build/Sui/docs/event.md#0x2_event_emit">event::emit</a>(
Expand All @@ -1001,7 +1002,8 @@ Request to withdraw stake from the validator's staking pool, processed at the en
principal_amount,
reward_amount,
}
)
);
withdrawn_stake
}
</code></pre>

Expand Down
25 changes: 12 additions & 13 deletions crates/sui-framework/docs/validator_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ staking pool's pending stake withdraw entries, processed at the end of the epoch
the stake and any rewards corresponding to it will be immediately processed.


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="validator_set.md#0x3_validator_set_request_withdraw_stake">request_withdraw_stake</a>(self: &<b>mut</b> <a href="validator_set.md#0x3_validator_set_ValidatorSet">validator_set::ValidatorSet</a>, staked_sui: <a href="staking_pool.md#0x3_staking_pool_StakedSui">staking_pool::StakedSui</a>, ctx: &<b>mut</b> <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>)
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="validator_set.md#0x3_validator_set_request_withdraw_stake">request_withdraw_stake</a>(self: &<b>mut</b> <a href="validator_set.md#0x3_validator_set_ValidatorSet">validator_set::ValidatorSet</a>, staked_sui: <a href="staking_pool.md#0x3_staking_pool_StakedSui">staking_pool::StakedSui</a>, ctx: &<b>mut</b> <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): <a href="../../../.././build/Sui/docs/balance.md#0x2_balance_Balance">balance::Balance</a>&lt;<a href="../../../.././build/Sui/docs/sui.md#0x2_sui_SUI">sui::SUI</a>&gt;
</code></pre>


Expand All @@ -931,19 +931,18 @@ the stake and any rewards corresponding to it will be immediately processed.
self: &<b>mut</b> <a href="validator_set.md#0x3_validator_set_ValidatorSet">ValidatorSet</a>,
staked_sui: StakedSui,
ctx: &<b>mut</b> TxContext,
) {
) : Balance&lt;SUI&gt; {
<b>let</b> staking_pool_id = pool_id(&staked_sui);
// This is an active <a href="validator.md#0x3_validator">validator</a>.
<b>if</b> (<a href="../../../.././build/Sui/docs/table.md#0x2_table_contains">table::contains</a>(&self.staking_pool_mappings, staking_pool_id)) {
<b>let</b> validator_address = *<a href="../../../.././build/Sui/docs/table.md#0x2_table_borrow">table::borrow</a>(&self.staking_pool_mappings, pool_id(&staked_sui));
<b>let</b> <a href="validator.md#0x3_validator">validator</a> = <a href="validator_set.md#0x3_validator_set_get_candidate_or_active_validator_mut">get_candidate_or_active_validator_mut</a>(self, validator_address);
<a href="validator.md#0x3_validator_request_withdraw_stake">validator::request_withdraw_stake</a>(<a href="validator.md#0x3_validator">validator</a>, staked_sui, ctx);
} <b>else</b> { // This is an inactive pool.
<b>assert</b>!(<a href="../../../.././build/Sui/docs/table.md#0x2_table_contains">table::contains</a>(&self.inactive_validators, staking_pool_id), <a href="validator_set.md#0x3_validator_set_ENoPoolFound">ENoPoolFound</a>);
<b>let</b> wrapper = <a href="../../../.././build/Sui/docs/table.md#0x2_table_borrow_mut">table::borrow_mut</a>(&<b>mut</b> self.inactive_validators, staking_pool_id);
<b>let</b> <a href="validator.md#0x3_validator">validator</a> = <a href="validator_wrapper.md#0x3_validator_wrapper_load_validator_maybe_upgrade">validator_wrapper::load_validator_maybe_upgrade</a>(wrapper);
<a href="validator.md#0x3_validator_request_withdraw_stake">validator::request_withdraw_stake</a>(<a href="validator.md#0x3_validator">validator</a>, staked_sui, ctx);
}
<b>let</b> <a href="validator.md#0x3_validator">validator</a> =
<b>if</b> (<a href="../../../.././build/Sui/docs/table.md#0x2_table_contains">table::contains</a>(&self.staking_pool_mappings, staking_pool_id)) { // This is an active <a href="validator.md#0x3_validator">validator</a>.
<b>let</b> validator_address = *<a href="../../../.././build/Sui/docs/table.md#0x2_table_borrow">table::borrow</a>(&self.staking_pool_mappings, pool_id(&staked_sui));
<a href="validator_set.md#0x3_validator_set_get_candidate_or_active_validator_mut">get_candidate_or_active_validator_mut</a>(self, validator_address)
} <b>else</b> { // This is an inactive pool.
<b>assert</b>!(<a href="../../../.././build/Sui/docs/table.md#0x2_table_contains">table::contains</a>(&self.inactive_validators, staking_pool_id), <a href="validator_set.md#0x3_validator_set_ENoPoolFound">ENoPoolFound</a>);
<b>let</b> wrapper = <a href="../../../.././build/Sui/docs/table.md#0x2_table_borrow_mut">table::borrow_mut</a>(&<b>mut</b> self.inactive_validators, staking_pool_id);
<a href="validator_wrapper.md#0x3_validator_wrapper_load_validator_maybe_upgrade">validator_wrapper::load_validator_maybe_upgrade</a>(wrapper)
};
<a href="validator.md#0x3_validator_request_withdraw_stake">validator::request_withdraw_stake</a>(<a href="validator.md#0x3_validator">validator</a>, staked_sui, ctx)
}
</code></pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,9 @@ module sui_system::staking_pool {
pool: &mut StakingPool,
staked_sui: StakedSui,
ctx: &mut TxContext
) : u64 {
) : Balance<SUI> {
let (pool_token_withdraw_amount, principal_withdraw) =
withdraw_from_principal(pool, staked_sui);
let staker = tx_context::sender(ctx);
let principal_withdraw_amount = balance::value(&principal_withdraw);

let rewards_withdraw = withdraw_rewards(
Expand All @@ -157,8 +156,7 @@ module sui_system::staking_pool {

// TODO: implement withdraw bonding period here.
balance::join(&mut principal_withdraw, rewards_withdraw);
transfer::public_transfer(coin::from_balance(principal_withdraw, ctx), staker);
total_sui_withdraw_amount
principal_withdraw
}

/// Withdraw the principal SUI stored in the StakedSui object, and calculate the corresponding amount of pool
Expand Down
12 changes: 11 additions & 1 deletion crates/sui-framework/packages/sui-system/sources/sui_system.move
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
module sui_system::sui_system {
use sui::balance::Balance;

use sui::coin::Coin;
use sui::coin::{Self, Coin};
use sui::object::UID;
use sui_system::staking_pool::StakedSui;
use sui::sui::SUI;
Expand Down Expand Up @@ -261,6 +261,16 @@ module sui_system::sui_system {
staked_sui: StakedSui,
ctx: &mut TxContext,
) {
let withdrawn_stake = request_withdraw_stake_non_entry(wrapper, staked_sui, ctx);
transfer::public_transfer(coin::from_balance(withdrawn_stake, ctx), tx_context::sender(ctx));
}

/// Non-entry version of `request_withdraw_stake` that returns the withdrawn SUI instead of transferring it to the sender.
public fun request_withdraw_stake_non_entry(
wrapper: &mut SuiSystemState,
staked_sui: StakedSui,
ctx: &mut TxContext,
) : Balance<SUI> {
let self = load_system_state_mut(wrapper);
sui_system_state_inner::request_withdraw_stake(self, staked_sui, ctx)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,14 @@ module sui_system::sui_system_state_inner {
self: &mut SuiSystemStateInnerV2,
staked_sui: StakedSui,
ctx: &mut TxContext,
) {
) : Balance<SUI> {
assert!(
stake_activation_epoch(&staked_sui) <= tx_context::epoch(ctx),
EStakeWithdrawBeforeActivation
);
validator_set::request_withdraw_stake(
&mut self.validators, staked_sui, ctx,
);
)
}

/// Report a validator as a bad or non-performant actor in the system.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,12 @@ module sui_system::validator {
self: &mut Validator,
staked_sui: StakedSui,
ctx: &mut TxContext,
) {
) : Balance<SUI> {
let principal_amount = staking_pool::staked_sui_amount(&staked_sui);
let stake_activation_epoch = staking_pool::stake_activation_epoch(&staked_sui);
let withdraw_amount = staking_pool::request_withdraw_stake(
let withdrawn_stake = staking_pool::request_withdraw_stake(
&mut self.staking_pool, staked_sui, ctx);
let withdraw_amount = balance::value(&withdrawn_stake);
let reward_amount = withdraw_amount - principal_amount;
self.next_epoch_stake = self.next_epoch_stake - withdraw_amount;
event::emit(
Expand All @@ -371,7 +372,8 @@ module sui_system::validator {
principal_amount,
reward_amount,
}
)
);
withdrawn_stake
}

/// Request to set new gas price for the next epoch.
Expand Down