Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 109 additions & 2 deletions aptos-move/framework/aptos-framework/sources/governed_gas_pool.move
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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),
});
}

Expand All @@ -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.
Expand Down Expand Up @@ -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()) {
Comment thread
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 {
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]
Expand All @@ -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
Comment thread
areshand marked this conversation as resolved.
): Coin<CoinType> acquires GovernedGasPool {
let ggp = borrow_global_mut<GovernedGasPool>(@aptos_framework);
Comment thread
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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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

@musitdev musitdev Oct 16, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.
Perhaps by adding an event to deposit_treasury we can manage these data off chain and not on chain.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 {
Comment thread
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 {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

}
Loading