Skip to content

Commit

Permalink
Add a view function to return address of StakePool to be created (#8877)
Browse files Browse the repository at this point in the history
* Add a view function to return address to be created

* Fix naming

* Add a view function for delegation_pool

GitOrigin-RevId: 748a7f4775ef6b3ab86ec35cedfbc5249ab14aa4
  • Loading branch information
xindingw authored and aptos-bot committed Oct 24, 2024
1 parent 19047d1 commit 90daf72
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 24 deletions.
68 changes: 63 additions & 5 deletions aptos-framework/doc/delegation_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ transferred to A
- [Function `calculate_and_update_voter_total_voting_power`](#0x1_delegation_pool_calculate_and_update_voter_total_voting_power)
- [Function `calculate_and_update_remaining_voting_power`](#0x1_delegation_pool_calculate_and_update_remaining_voting_power)
- [Function `calculate_and_update_delegator_voter`](#0x1_delegation_pool_calculate_and_update_delegator_voter)
- [Function `get_expected_stake_pool_address`](#0x1_delegation_pool_get_expected_stake_pool_address)
- [Function `initialize_delegation_pool`](#0x1_delegation_pool_initialize_delegation_pool)
- [Function `enable_partial_governance_voting`](#0x1_delegation_pool_enable_partial_governance_voting)
- [Function `vote`](#0x1_delegation_pool_vote)
Expand All @@ -161,6 +162,7 @@ transferred to A
- [Function `get_delegator_active_shares`](#0x1_delegation_pool_get_delegator_active_shares)
- [Function `get_delegator_pending_inactive_shares`](#0x1_delegation_pool_get_delegator_pending_inactive_shares)
- [Function `get_used_voting_power`](#0x1_delegation_pool_get_used_voting_power)
- [Function `create_resource_account_seed`](#0x1_delegation_pool_create_resource_account_seed)
- [Function `borrow_mut_used_voting_power`](#0x1_delegation_pool_borrow_mut_used_voting_power)
- [Function `update_and_borrow_mut_delegator_vote_delegation`](#0x1_delegation_pool_update_and_borrow_mut_delegator_vote_delegation)
- [Function `update_and_borrow_mut_delegated_votes`](#0x1_delegation_pool_update_and_borrow_mut_delegated_votes)
Expand Down Expand Up @@ -1659,6 +1661,34 @@ latest state.



</details>

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

## Function `get_expected_stake_pool_address`

Return the address of the stake pool to be created with the provided owner, and seed.


<pre><code>#[view]
<b>public</b> <b>fun</b> <a href="delegation_pool.md#0x1_delegation_pool_get_expected_stake_pool_address">get_expected_stake_pool_address</a>(owner: <b>address</b>, delegation_pool_creation_seed: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <b>address</b>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="delegation_pool.md#0x1_delegation_pool_get_expected_stake_pool_address">get_expected_stake_pool_address</a>(owner: <b>address</b>, delegation_pool_creation_seed: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
): <b>address</b> {
<b>let</b> seed = <a href="delegation_pool.md#0x1_delegation_pool_create_resource_account_seed">create_resource_account_seed</a>(delegation_pool_creation_seed);
<a href="account.md#0x1_account_create_resource_address">account::create_resource_address</a>(&owner, seed)
}
</code></pre>



</details>

<a name="0x1_delegation_pool_initialize_delegation_pool"></a>
Expand Down Expand Up @@ -1691,11 +1721,7 @@ Ownership over setting the operator/voter is granted to <code>owner</code> who h
<b>assert</b>!(<a href="delegation_pool.md#0x1_delegation_pool_operator_commission_percentage">operator_commission_percentage</a> &lt;= <a href="delegation_pool.md#0x1_delegation_pool_MAX_FEE">MAX_FEE</a>, <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_invalid_argument">error::invalid_argument</a>(<a href="delegation_pool.md#0x1_delegation_pool_EINVALID_COMMISSION_PERCENTAGE">EINVALID_COMMISSION_PERCENTAGE</a>));

// generate a seed <b>to</b> be used <b>to</b> create the resource <a href="account.md#0x1_account">account</a> hosting the delegation pool
<b>let</b> seed = <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_empty">vector::empty</a>&lt;u8&gt;();
// <b>include</b> <b>module</b> salt (before <a href="../../aptos-stdlib/doc/any.md#0x1_any">any</a> subseeds) <b>to</b> avoid conflicts <b>with</b> other modules creating resource accounts
<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_append">vector::append</a>(&<b>mut</b> seed, <a href="delegation_pool.md#0x1_delegation_pool_MODULE_SALT">MODULE_SALT</a>);
// <b>include</b> an additional salt in case the same resource <a href="account.md#0x1_account">account</a> <b>has</b> already been created
<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_append">vector::append</a>(&<b>mut</b> seed, delegation_pool_creation_seed);
<b>let</b> seed = <a href="delegation_pool.md#0x1_delegation_pool_create_resource_account_seed">create_resource_account_seed</a>(delegation_pool_creation_seed);

<b>let</b> (stake_pool_signer, stake_pool_signer_cap) = <a href="account.md#0x1_account_create_resource_account">account::create_resource_account</a>(owner, seed);
<a href="coin.md#0x1_coin_register">coin::register</a>&lt;AptosCoin&gt;(&stake_pool_signer);
Expand Down Expand Up @@ -2244,6 +2270,38 @@ Get the used voting power of a voter on a proposal.



</details>

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

## Function `create_resource_account_seed`

Create the seed to derive the resource account address.


<pre><code><b>fun</b> <a href="delegation_pool.md#0x1_delegation_pool_create_resource_account_seed">create_resource_account_seed</a>(delegation_pool_creation_seed: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="delegation_pool.md#0x1_delegation_pool_create_resource_account_seed">create_resource_account_seed</a>(
delegation_pool_creation_seed: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt; {
<b>let</b> seed = <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_empty">vector::empty</a>&lt;u8&gt;();
// <b>include</b> <b>module</b> salt (before <a href="../../aptos-stdlib/doc/any.md#0x1_any">any</a> subseeds) <b>to</b> avoid conflicts <b>with</b> other modules creating resource accounts
<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_append">vector::append</a>(&<b>mut</b> seed, <a href="delegation_pool.md#0x1_delegation_pool_MODULE_SALT">MODULE_SALT</a>);
// <b>include</b> an additional salt in case the same resource <a href="account.md#0x1_account">account</a> <b>has</b> already been created
<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_append">vector::append</a>(&<b>mut</b> seed, delegation_pool_creation_seed);
seed
}
</code></pre>



</details>

<a name="0x1_delegation_pool_borrow_mut_used_voting_power"></a>
Expand Down
78 changes: 71 additions & 7 deletions aptos-framework/doc/staking_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pool.
- [Function `staking_contract_amounts`](#0x1_staking_contract_staking_contract_amounts)
- [Function `pending_distribution_counts`](#0x1_staking_contract_pending_distribution_counts)
- [Function `staking_contract_exists`](#0x1_staking_contract_staking_contract_exists)
- [Function `get_expected_stake_pool_address`](#0x1_staking_contract_get_expected_stake_pool_address)
- [Function `create_staking_contract`](#0x1_staking_contract_create_staking_contract)
- [Function `create_staking_contract_with_coins`](#0x1_staking_contract_create_staking_contract_with_coins)
- [Function `add_stake`](#0x1_staking_contract_add_stake)
Expand All @@ -71,6 +72,7 @@ pool.
- [Function `get_staking_contract_amounts_internal`](#0x1_staking_contract_get_staking_contract_amounts_internal)
- [Function `create_stake_pool`](#0x1_staking_contract_create_stake_pool)
- [Function `update_distribution_pool`](#0x1_staking_contract_update_distribution_pool)
- [Function `create_resource_account_seed`](#0x1_staking_contract_create_resource_account_seed)
- [Function `new_staking_contracts_holder`](#0x1_staking_contract_new_staking_contracts_holder)
- [Specification](#@Specification_1)
- [Function `stake_pool_address`](#@Specification_1_stake_pool_address)
Expand Down Expand Up @@ -1027,6 +1029,37 @@ Return true if the staking contract between the provided staker and operator exi



</details>

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

## Function `get_expected_stake_pool_address`

Return the address of the stake pool to be created with the provided staker, operator and seed.


<pre><code>#[view]
<b>public</b> <b>fun</b> <a href="staking_contract.md#0x1_staking_contract_get_expected_stake_pool_address">get_expected_stake_pool_address</a>(staker: <b>address</b>, operator: <b>address</b>, contract_creation_seed: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <b>address</b>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="staking_contract.md#0x1_staking_contract_get_expected_stake_pool_address">get_expected_stake_pool_address</a>(
staker: <b>address</b>,
operator: <b>address</b>,
contract_creation_seed: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
): <b>address</b> {
<b>let</b> seed = <a href="staking_contract.md#0x1_staking_contract_create_resource_account_seed">create_resource_account_seed</a>(staker, operator, contract_creation_seed);
<a href="account.md#0x1_account_create_resource_address">account::create_resource_address</a>(&staker, seed)
}
</code></pre>



</details>

<a name="0x1_staking_contract_create_staking_contract"></a>
Expand Down Expand Up @@ -1821,13 +1854,8 @@ Calculate accumulated rewards and commissions since last update.
contract_creation_seed: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
): (<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, SignerCapability, OwnerCapability) {
// Generate a seed that will be used <b>to</b> create the resource <a href="account.md#0x1_account">account</a> that hosts the staking contract.
<b>let</b> seed = <a href="../../aptos-stdlib/../move-stdlib/doc/bcs.md#0x1_bcs_to_bytes">bcs::to_bytes</a>(&<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(staker));
<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_append">vector::append</a>(&<b>mut</b> seed, <a href="../../aptos-stdlib/../move-stdlib/doc/bcs.md#0x1_bcs_to_bytes">bcs::to_bytes</a>(&operator));
// Include a salt <b>to</b> avoid conflicts <b>with</b> <a href="../../aptos-stdlib/doc/any.md#0x1_any">any</a> other modules out there that might also generate
// deterministic resource accounts for the same staker + operator addresses.
<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_append">vector::append</a>(&<b>mut</b> seed, <a href="staking_contract.md#0x1_staking_contract_SALT">SALT</a>);
// Add an extra salt given by the staker in case an <a href="account.md#0x1_account">account</a> <b>with</b> the same <b>address</b> <b>has</b> already been created.
<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_append">vector::append</a>(&<b>mut</b> seed, contract_creation_seed);
<b>let</b> seed = <a href="staking_contract.md#0x1_staking_contract_create_resource_account_seed">create_resource_account_seed</a>(
<a href="../../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(staker), operator, contract_creation_seed);

<b>let</b> (stake_pool_signer, stake_pool_signer_cap) = <a href="account.md#0x1_account_create_resource_account">account::create_resource_account</a>(staker, seed);
<a href="stake.md#0x1_stake_initialize_stake_owner">stake::initialize_stake_owner</a>(&stake_pool_signer, 0, operator, voter);
Expand Down Expand Up @@ -1896,6 +1924,42 @@ Calculate accumulated rewards and commissions since last update.



</details>

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

## Function `create_resource_account_seed`

Create the seed to derive the resource account address.


<pre><code><b>fun</b> <a href="staking_contract.md#0x1_staking_contract_create_resource_account_seed">create_resource_account_seed</a>(staker: <b>address</b>, operator: <b>address</b>, contract_creation_seed: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="staking_contract.md#0x1_staking_contract_create_resource_account_seed">create_resource_account_seed</a>(
staker: <b>address</b>,
operator: <b>address</b>,
contract_creation_seed: <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt;,
): <a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u8&gt; {
<b>let</b> seed = <a href="../../aptos-stdlib/../move-stdlib/doc/bcs.md#0x1_bcs_to_bytes">bcs::to_bytes</a>(&staker);
<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_append">vector::append</a>(&<b>mut</b> seed, <a href="../../aptos-stdlib/../move-stdlib/doc/bcs.md#0x1_bcs_to_bytes">bcs::to_bytes</a>(&operator));
// Include a salt <b>to</b> avoid conflicts <b>with</b> <a href="../../aptos-stdlib/doc/any.md#0x1_any">any</a> other modules out there that might also generate
// deterministic resource accounts for the same staker + operator addresses.
<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_append">vector::append</a>(&<b>mut</b> seed, <a href="staking_contract.md#0x1_staking_contract_SALT">SALT</a>);
// Add an extra salt given by the staker in case an <a href="account.md#0x1_account">account</a> <b>with</b> the same <b>address</b> <b>has</b> already been created.
<a href="../../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector_append">vector::append</a>(&<b>mut</b> seed, contract_creation_seed);
seed
}
</code></pre>



</details>

<a name="0x1_staking_contract_new_staking_contracts_holder"></a>
Expand Down
32 changes: 27 additions & 5 deletions aptos-framework/sources/delegation_pool.move
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,14 @@ module aptos_framework::delegation_pool {
)
}

#[view]
/// Return the address of the stake pool to be created with the provided owner, and seed.
public fun get_expected_stake_pool_address(owner: address, delegation_pool_creation_seed: vector<u8>
): address {
let seed = create_resource_account_seed(delegation_pool_creation_seed);
account::create_resource_address(&owner, seed)
}

/// Initialize a delegation pool of custom fixed `operator_commission_percentage`.
/// A resource account is created from `owner` signer and its supplied `delegation_pool_creation_seed`
/// to host the delegation pool resource and own the underlying stake pool.
Expand All @@ -585,11 +593,7 @@ module aptos_framework::delegation_pool {
assert!(operator_commission_percentage <= MAX_FEE, error::invalid_argument(EINVALID_COMMISSION_PERCENTAGE));

// generate a seed to be used to create the resource account hosting the delegation pool
let seed = vector::empty<u8>();
// include module salt (before any subseeds) to avoid conflicts with other modules creating resource accounts
vector::append(&mut seed, MODULE_SALT);
// include an additional salt in case the same resource account has already been created
vector::append(&mut seed, delegation_pool_creation_seed);
let seed = create_resource_account_seed(delegation_pool_creation_seed);

let (stake_pool_signer, stake_pool_signer_cap) = account::create_resource_account(owner, seed);
coin::register<AptosCoin>(&stake_pool_signer);
Expand Down Expand Up @@ -835,6 +839,18 @@ module aptos_framework::delegation_pool {
*smart_table::borrow_with_default(votes, key, &0)
}

/// Create the seed to derive the resource account address.
fun create_resource_account_seed(
delegation_pool_creation_seed: vector<u8>,
): vector<u8> {
let seed = vector::empty<u8>();
// include module salt (before any subseeds) to avoid conflicts with other modules creating resource accounts
vector::append(&mut seed, MODULE_SALT);
// include an additional salt in case the same resource account has already been created
vector::append(&mut seed, delegation_pool_creation_seed);
seed
}

/// Borrow the mutable used voting power of a voter on a proposal.
inline fun borrow_mut_used_voting_power(governance_records: &mut GovernanceRecords, voter: address, proposal_id: u64): &mut u64 {
let votes = &mut governance_records.votes;
Expand Down Expand Up @@ -3891,6 +3907,12 @@ module aptos_framework::delegation_pool {
delegate_voting_power(delegator1, pool_address, signer::address_of(voter1));
}

#[test(staker = @0xe256f4f4e2986cada739e339895cf5585082ff247464cab8ec56eea726bd2263)]
public entry fun test_get_expected_stake_pool_address(staker: address) {
let pool_address = get_expected_stake_pool_address(staker, vector[0x42, 0x42]);
assert!(pool_address == @0xe9fc2fbb82b7e1cb7af3daef8c7a24e66780f9122d15e4f1d486ee7c7c36c48d, 0);
}

#[test_only]
public fun assert_delegation(
delegator_address: address,
Expand Down
Loading

0 comments on commit 90daf72

Please sign in to comment.