forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 28
update rewards and governed pool MIP-124 #227
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
Merged
Merged
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d24dc17
feat: update distribute rewards
0xmovses 9e05ac8
feat: add friend only function for withdraw ggp
0xmovses a0b6c84
fix: no longer acquire capability
0xmovses bf94242
fix: stake unit tests
0xmovses cd66e92
fixes
0xmovses 876ca01
update to compile and pass test
musitdev 1774dbf
resolve merge conficts
musitdev 8b72cd3
start year APR staking reward definition in config
musitdev 22a320d
Revert "start year APR staking reward definition in config"
musitdev 338e622
correct fmt issues
musitdev 99e4516
correct format issues
musitdev 5831237
correct format issues
musitdev b11b9f9
add withdraw event
musitdev 279f535
add treasury deposit counter
musitdev 13e9103
update to pass unit test
musitdev a7fffff
correct typos
musitdev 4fd6bcd
add reward counter recrease on withdraw
musitdev b779b6c
fix: assert sufficient bal
0xmovses 383ed85
chore: remove blankspace
0xmovses fce0dd6
remove treasure counter update on withdraw
musitdev 3c4ebed
Merge branch '0xmovses/update-rewards' of github.com:movementlabsxyz/…
musitdev 97225a6
add STAKE_REWARD_USING_TREASURY feature
musitdev dcf8c6c
split GGP struct update in a new struct GGPv2
musitdev c1cc00f
add new ggp struct init
musitdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ module aptos_framework::governed_gas_pool { | |
| use aptos_framework::object::{Self}; | ||
| use aptos_framework::aptos_coin::AptosCoin; | ||
| use aptos_framework::coin::{Self, Coin}; | ||
| use aptos_framework::event::{Self, EventHandle}; | ||
| use std::features; | ||
| use aptos_framework::signer; | ||
| use aptos_framework::aptos_account::Self; | ||
|
|
@@ -20,15 +21,28 @@ module aptos_framework::governed_gas_pool { | |
| #[test_only] | ||
| use aptos_framework::aptos_coin::Self; | ||
|
|
||
| friend aptos_framework::stake; | ||
|
|
||
| const MODULE_SALT: vector<u8> = b"aptos_framework::governed_gas_pool"; | ||
|
|
||
| /// Event emitted when token are withdraw from the pool | ||
| struct WithdrawStakingRewardEvent has drop, store { | ||
| amount: u64, | ||
| } | ||
|
|
||
| /// The Governed Gas Pool | ||
| /// Internally, this is a simply wrapper around a resource account. | ||
| struct GovernedGasPool has key { | ||
| /// The signer capability of the resource account. | ||
| signer_capability: SignerCapability, | ||
| } | ||
|
|
||
| /// Contains added variable needed for the GovernedGasPool staking reward update. | ||
| struct GovernedGasPoolV2 has key { | ||
| deposited_treasury_counter: u64, | ||
| withdraw_staking_reward_events: EventHandle<WithdrawStakingRewardEvent>, | ||
| } | ||
|
|
||
| /// Address of APT Primary Fungible Store | ||
| inline fun primary_fungible_store_address(account: address): address { | ||
| object::create_user_derived_object_address(account, @aptos_fungible_asset) | ||
|
|
@@ -71,6 +85,11 @@ module aptos_framework::governed_gas_pool { | |
| move_to(aptos_framework, GovernedGasPool{ | ||
| signer_capability: governed_gas_pool_signer_cap, | ||
| }); | ||
|
|
||
| move_to(aptos_framework, GovernedGasPoolV2{ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is only called during genesis. You need to a separate function to initialize this resource. Otherwise, deposit and withdraw would break |
||
| deposited_treasury_counter: 0, | ||
| withdraw_staking_reward_events: account::new_event_handle<WithdrawStakingRewardEvent>(aptos_framework), | ||
| }); | ||
| } | ||
|
|
||
| /// Initialize the governed gas pool as a module | ||
|
|
@@ -95,6 +114,12 @@ module aptos_framework::governed_gas_pool { | |
| signer::address_of(&governed_gas_signer()) | ||
| } | ||
|
|
||
| #[view] | ||
| /// Return the amount of treasury deposited. | ||
| public fun get_treasury_deposited(): u64 acquires GovernedGasPoolV2 { | ||
| borrow_global<GovernedGasPoolV2>(@aptos_framework).deposited_treasury_counter | ||
| } | ||
|
|
||
| /// Funds the destination account with a given amount of coin. | ||
| /// @param account The account to be funded. | ||
| /// @param amount The amount of coin to be funded. | ||
|
|
@@ -155,13 +180,24 @@ module aptos_framework::governed_gas_pool { | |
| /// @param gas_payer The address of the account that paid the gas fees. | ||
| /// @param gas_fee The amount of gas fees to be deposited. | ||
| public(friend) fun deposit_gas_fee_v2(gas_payer: address, gas_fee: u64) acquires GovernedGasPool { | ||
| if (features::operations_default_to_fa_apt_store_enabled()) { | ||
| if (features::operations_default_to_fa_apt_store_enabled()) { | ||
|
0xmovses marked this conversation as resolved.
|
||
| deposit_from_fungible_store(gas_payer, gas_fee); | ||
| } else { | ||
| deposit_from<AptosCoin>(gas_payer, gas_fee); | ||
| }; | ||
| } | ||
|
|
||
| /// Deposits from the treasury account. Treasury deposit are recorded. | ||
| /// @param treasury_account The address of the account that paid the treasury. | ||
| /// @param amount The amount of treasury to be deposited. | ||
| public entry fun deposit_treasury(treasury_account: &signer, amount: u64) acquires GovernedGasPool, GovernedGasPoolV2 { | ||
| let treasury_account_address = signer::address_of(treasury_account); | ||
| deposit_from<AptosCoin>(treasury_account_address, amount); | ||
|
|
||
| let ggp = borrow_global_mut<GovernedGasPoolV2>(@aptos_framework); | ||
| ggp.deposited_treasury_counter = ggp.deposited_treasury_counter + amount; | ||
| } | ||
|
|
||
| #[view] | ||
| /// Gets the balance of a specified coin type in the governed gas pool. | ||
| /// @return The balance of the coin in the pool. | ||
|
|
@@ -170,6 +206,38 @@ module aptos_framework::governed_gas_pool { | |
| coin::balance<CoinType>(pool_address) | ||
| } | ||
|
|
||
| /// Withdraws coins from the governed gas pool. | ||
| /// | ||
| /// This function allows friend modules to withdraw a specified amount of a given | ||
| /// `CoinType` from the governed gas pool. It uses the internal signer of the | ||
| /// governed gas pool to authorize the withdrawal. | ||
| /// | ||
| /// @param amount The amount of coins to withdraw from the pool. | ||
| /// @return A `Coin<CoinType>` resource containing the withdrawn amount. | ||
| public(friend) fun withdraw_staking_reward<CoinType>( | ||
| amount: u64 | ||
|
areshand marked this conversation as resolved.
|
||
| ): Coin<CoinType> acquires GovernedGasPool, GovernedGasPoolV2 { | ||
| let balance = get_balance<CoinType>(); | ||
| assert!(balance >= amount, 0); // insufficient balance | ||
| let ggpv2 = borrow_global_mut<GovernedGasPoolV2>(@aptos_framework); | ||
|
|
||
| event::emit_event( | ||
| &mut ggpv2.withdraw_staking_reward_events, | ||
| WithdrawStakingRewardEvent { | ||
| amount, | ||
| }, | ||
| ); | ||
|
|
||
| // Withdraw reward coin. | ||
| coin::withdraw<CoinType>(&governed_gas_signer(), amount) | ||
| } | ||
|
|
||
| /// Register Aptos coin with Governed gas signer. | ||
| public(friend) fun register_coin<CoinType>() acquires GovernedGasPool { | ||
|
areshand marked this conversation as resolved.
|
||
| let s = governed_gas_signer(); | ||
| coin::register<CoinType>(&s); | ||
| } | ||
|
|
||
| #[test_only] | ||
| /// The AptosCoin mint capability | ||
| struct AptosCoinMintCapability has key { | ||
|
|
@@ -222,6 +290,9 @@ module aptos_framework::governed_gas_pool { | |
| aptos_framework: &signer, | ||
| ) { | ||
|
|
||
| // Create framework account to be able to send event. | ||
| aptos_framework::account::create_account_for_test(@aptos_framework); | ||
|
|
||
| // initialize the AptosCoin module | ||
| let (burn_cap, mint_cap) = aptos_coin::initialize_for_test(aptos_framework); | ||
|
|
||
|
|
@@ -361,4 +432,39 @@ module aptos_framework::governed_gas_pool { | |
| // initialize the governed gas pool again, no abort | ||
| initialize(aptos_framework, vector::empty<u8>()); | ||
| } | ||
|
|
||
|
|
||
| #[test(aptos_framework = @aptos_framework, treasury = @0xdddd)] | ||
| /// Add some treasury to the governed gas pool. | ||
| /// | ||
| /// @param aptos_framework is the signer of the aptos_framework module. | ||
| fun test_deposite_treasury_and_counter(aptos_framework: &signer, treasury: &signer) acquires GovernedGasPool, GovernedGasPoolV2, AptosCoinMintCapability { | ||
|
|
||
| // initialize the modules | ||
| initialize_for_test(aptos_framework); | ||
|
|
||
| // create the depositor account and fund it | ||
| aptos_account::create_account(signer::address_of(treasury)); | ||
| mint_for_test(signer::address_of(treasury), 1000); | ||
|
|
||
| // get the balances for the depositor and the governed gas pool | ||
| let treasury_balance = coin::balance<AptosCoin>(signer::address_of(treasury)); | ||
| let governed_gas_pool_balance = coin::balance<AptosCoin>(governed_gas_pool_address()); | ||
|
|
||
| // deposit some coin into the governed gas pool | ||
| deposit_treasury(treasury, 100); | ||
|
|
||
| // check the balances after the deposit | ||
| assert!(coin::balance<AptosCoin>(signer::address_of(treasury)) == treasury_balance - 100, 1); | ||
| assert!(coin::balance<AptosCoin>(governed_gas_pool_address()) == governed_gas_pool_balance + 100, 2); | ||
| assert!(get_treasury_deposited() == 100, 3); | ||
|
|
||
| let withdraw = withdraw_staking_reward<AptosCoin>(10); | ||
| assert!(coin::balance<AptosCoin>(governed_gas_pool_address()) == governed_gas_pool_balance + 100 - 10, 4); | ||
| assert!(get_treasury_deposited() == 100, 5); | ||
| assert!(coin::value(&withdraw) == 10, 6); | ||
|
|
||
| coin::deposit(@0xdddd, withdraw); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
naming is hard. But this is not ggp v2. this is more like a GovernedGasPoolExtension.