-
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
Changes from 17 commits
d24dc17
9e05ac8
a0b6c84
bf94242
cd66e92
876ca01
1774dbf
8b72cd3
22a320d
338e622
99e4516
5831237
b11b9f9
279f535
13e9103
a7fffff
4fd6bcd
b779b6c
383ed85
fce0dd6
3c4ebed
97225a6
dcf8c6c
c1cc00f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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,13 +21,22 @@ 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, | ||
| deposited_treasury_counter: u64, | ||
| withdraw_staking_reward_events: EventHandle<WithdrawStakingRewardEvent>, | ||
| } | ||
|
|
||
| /// Address of APT Primary Fungible Store | ||
|
|
@@ -70,6 +80,8 @@ module aptos_framework::governed_gas_pool { | |
|
|
||
| move_to(aptos_framework, GovernedGasPool{ | ||
| signer_capability: governed_gas_pool_signer_cap, | ||
| deposited_treasury_counter: 0, | ||
| withdraw_staking_reward_events: account::new_event_handle<WithdrawStakingRewardEvent>(aptos_framework), | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -95,6 +107,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 GovernedGasPool { | ||
| borrow_global<GovernedGasPool>(@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,11 +173,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()) { | ||
| 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 { | ||
| let treasury_account_address = signer::address_of(treasury_account); | ||
| deposit_from<AptosCoin>(treasury_account_address, amount); | ||
|
|
||
| let ggp = borrow_global_mut<GovernedGasPool>(@aptos_framework); | ||
| ggp.deposited_treasury_counter = ggp.deposited_treasury_counter + amount; | ||
| } | ||
|
|
||
| #[view] | ||
|
|
@@ -170,6 +201,44 @@ 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 { | ||
| let ggp = borrow_global_mut<GovernedGasPool>(@aptos_framework); | ||
|
0xmovses marked this conversation as resolved.
Outdated
|
||
|
|
||
| event::emit_event( | ||
| &mut ggp.withdraw_staking_reward_events, | ||
| WithdrawStakingRewardEvent { | ||
| amount, | ||
| }, | ||
| ); | ||
|
|
||
| // Withdraw reward coin. | ||
| let signer_cap = create_signer_with_capability(&ggp.signer_capability); | ||
| let coin = coin::withdraw<CoinType>(&signer_cap, amount); | ||
|
|
||
| // Decrease the treasury counter. | ||
| if (ggp.deposited_treasury_counter > amount) { | ||
| ggp.deposited_treasury_counter = ggp.deposited_treasury_counter - amount; | ||
|
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. maybe we rename deposited_treasury_counter to total_deposited_treasury_counter. we don't need to decrease it 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. In fact, what is the goal of this treasury counter. If it's to know how many treasures that was deposited, it shouldn't be decrease, if it's the part of the deposited treasure in the current pool, we should decrease it.
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. IMO, we can only know how many treasures that was deposited. During withdraw reward, we dont differentiate gas fund from treasury fund 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. So the need is to know how many treasure has been deposited. I remove the withdraw. 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. I pull the change, the treasure decrease has been removed. |
||
| }; | ||
|
|
||
| coin | ||
| } | ||
|
|
||
| /// 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 +291,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 +433,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, 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 - 10, 5); | ||
| assert!(coin::value(&withdraw) == 10, 6); | ||
|
|
||
| coin::deposit(@0xdddd, withdraw); | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.