Module 0x1::transaction_fee

use 0x1::aptos_account;
use 0x1::aptos_coin;
use 0x1::coin;
use 0x1::error;
use 0x1::event;
use 0x1::features;
use 0x1::fungible_asset;
use 0x1::option;
use 0x1::system_addresses;

Resource AptosFABurnCapabilities

Stores burn capability to burn the gas fees.

struct AptosFABurnCapabilities has key
Fields
burn_ref: fungible_asset::BurnRef

Resource AptosCoinMintCapability

Stores the coin mint capability to mint the refunds. Used as the fallback path when GAS_REFUND_FA_MINT is disabled or AptosFAMintCapabilities is not yet published.

struct AptosCoinMintCapability has key
Fields
mint_cap: coin::MintCapability<aptos_coin::AptosCoin>

Resource AptosFAMintCapabilities

Stores mint ref to mint the refunds as APT FA. Used when GAS_REFUND_FA_MINT is enabled.

struct AptosFAMintCapabilities has key
Fields
mint_ref: fungible_asset::MintRef

Struct FeeStatement

Breakdown of fee charge and refund for a transaction. The structure is:

  • Net charge or refund (not in the statement)
  • total charge: total_charge_gas_units, matches gas_used in the on-chain TransactionInfo. This is the sum of the sub-items below. Notice that there's potential precision loss when the conversion between internal and external gas units and between native token and gas units, so it's possible that the numbers don't add up exactly. -- This number is the final charge, while the break down is merely informational.
  • gas charge for execution (CPU time): execution_gas_units
  • gas charge for IO (storage random access): io_gas_units
  • storage fee charge (storage space): storage_fee_octas, to be included in total_charge_gas_unit, this number is converted to gas units according to the user specified gas_unit_price on the transaction.
  • storage deletion refund: storage_fee_refund_octas, this is not included in gas_used or total_charge_gas_units, the net charge / refund is calculated by total_charge_gas_units * gas_unit_price - storage_fee_refund_octas.

This is meant to emitted as a module event.

#[event]
struct FeeStatement has drop, store
Fields
total_charge_gas_units: u64
Total gas charge.
execution_gas_units: u64
Execution gas charge.
io_gas_units: u64
IO gas charge.
storage_fee_octas: u64
Storage fee charge.
storage_fee_refund_octas: u64
Storage fee refund.

Resource AptosCoinCapabilities

Stores burn capability to burn the gas fees.

#[deprecated]
struct AptosCoinCapabilities has key
Fields
burn_cap: coin::BurnCapability<aptos_coin::AptosCoin>

Resource CollectedFeesPerBlock

DEPRECATED: Stores information about the block proposer and the amount of fees collected when executing the block.

#[deprecated]
struct CollectedFeesPerBlock has key
Fields
amount: coin::AggregatableCoin<aptos_coin::AptosCoin>
proposer: option::Option<address>
burn_percentage: u8

Constants

Gas fees are already being collected and the struct holding information about collected amounts is already published.

const EALREADY_COLLECTING_FEES: u64 = 1;

The burn percentage is out of range [0, 100].

const EINVALID_BURN_PERCENTAGE: u64 = 3;

No longer supported.

const ENO_LONGER_SUPPORTED: u64 = 4;

Function burn_fee

Burn transaction fees in epilogue.

public(friend) fun burn_fee(account: address, fee: u64)
Implementation
friend fun burn_fee(
    account: address, fee: u64
) {
    let burn_ref = &AptosFABurnCapabilities[@aptos_framework].burn_ref;
    aptos_account::burn_from_fungible_store_for_gas(burn_ref, account, fee);
}

Function mint_and_refund

Mint refund in epilogue.

public(friend) fun mint_and_refund(account: address, refund: u64)
Implementation
friend fun mint_and_refund(
    account: address, refund: u64
) {
    if (features::gas_refund_fa_mint_enabled()
        && exists<AptosFAMintCapabilities>(@aptos_framework)) {
        let mint_ref = &AptosFAMintCapabilities[@aptos_framework].mint_ref;
        aptos_account::mint_to_fungible_store_for_gas(mint_ref, account, refund);
    } else {
        let mint_cap = &AptosCoinMintCapability[@aptos_framework].mint_cap;
        let refund_coin = coin::mint(refund, mint_cap);
        coin::deposit_for_gas_fee(account, refund_coin);
    }
}

Function store_aptos_coin_burn_cap

Only called during genesis.

public(friend) fun store_aptos_coin_burn_cap(aptos_framework: &signer, burn_cap: coin::BurnCapability<aptos_coin::AptosCoin>)
Implementation
friend fun store_aptos_coin_burn_cap(
    aptos_framework: &signer, burn_cap: BurnCapability<AptosCoin>
) {
    system_addresses::assert_aptos_framework(aptos_framework);

    let burn_ref = coin::convert_and_take_paired_burn_ref(burn_cap);
    move_to(aptos_framework, AptosFABurnCapabilities { burn_ref });
}

Function store_aptos_coin_mint_cap

Only called during genesis.

public(friend) fun store_aptos_coin_mint_cap(aptos_framework: &signer, mint_cap: coin::MintCapability<aptos_coin::AptosCoin>)
Implementation
friend fun store_aptos_coin_mint_cap(
    aptos_framework: &signer, mint_cap: MintCapability<AptosCoin>
) {
    system_addresses::assert_aptos_framework(aptos_framework);
    // Store the FA mint ref (used when GAS_REFUND_FA_MINT is enabled) and keep the legacy
    // coin mint cap as the fallback, so the feature flag remains a safe two-way switch.
    let mint_ref = coin::get_paired_mint_copy_ref(&mint_cap);
    move_to(aptos_framework, AptosFAMintCapabilities { mint_ref });
    move_to(aptos_framework, AptosCoinMintCapability { mint_cap });
}

Function convert_to_aptos_fa_mint_ref

Migrate an existing chain from the legacy AptosCoinMintCapability (coin MintCapability) to AptosFAMintCapabilities (FA MintRef), so gas refunds mint APT FA directly without touching the legacy coin supply aggregator. Gated by the aptos_framework signer (governance).

public entry fun convert_to_aptos_fa_mint_ref(aptos_framework: &signer)
Implementation
public entry fun convert_to_aptos_fa_mint_ref(
    aptos_framework: &signer
) acquires AptosCoinMintCapability {
    system_addresses::assert_aptos_framework(aptos_framework);
    let mint_cap = &AptosCoinMintCapability[@aptos_framework].mint_cap;
    let mint_ref = coin::get_paired_mint_copy_ref(mint_cap);
    move_to(aptos_framework, AptosFAMintCapabilities { mint_ref });
}

Function emit_fee_statement

Called by epilogue only.

public(friend) fun emit_fee_statement(fee_statement: transaction_fee::FeeStatement)
Implementation
friend fun emit_fee_statement(fee_statement: FeeStatement) {
    event::emit(fee_statement)
}

Function storage_fee_refund_octas

public(friend) fun storage_fee_refund_octas(self: &transaction_fee::FeeStatement): u64
Implementation
friend fun storage_fee_refund_octas(self: &FeeStatement): u64 {
    self.storage_fee_refund_octas
}

Function initialize_fee_collection_and_distribution

DEPRECATED

#[deprecated]
public fun initialize_fee_collection_and_distribution(_aptos_framework: &signer, _burn_percentage: u8)
Implementation
public fun initialize_fee_collection_and_distribution(
    _aptos_framework: &signer, _burn_percentage: u8
) {
    abort error::not_implemented(ENO_LONGER_SUPPORTED)
}

Function upgrade_burn_percentage

DEPRECATED

#[deprecated]
public fun upgrade_burn_percentage(_aptos_framework: &signer, _new_burn_percentage: u8)
Implementation
public fun upgrade_burn_percentage(
    _aptos_framework: &signer, _new_burn_percentage: u8
) {
    abort error::not_implemented(ENO_LONGER_SUPPORTED)
}

Function initialize_storage_refund

#[deprecated]
public fun initialize_storage_refund(_: &signer)
Implementation
public fun initialize_storage_refund(_: &signer) {
    abort error::not_implemented(ENO_LONGER_SUPPORTED)
}

Function convert_to_aptos_fa_burn_ref

#[deprecated]
public entry fun convert_to_aptos_fa_burn_ref(_aptos_framework: &signer)
Implementation
public entry fun convert_to_aptos_fa_burn_ref(
    _aptos_framework: &signer
) {
    abort error::not_implemented(ENO_LONGER_SUPPORTED)
}

Specification

High-level Requirements

No.RequirementCriticalityImplementationEnforcement
1 Given the blockchain is in an operating state, it guarantees that the Aptos framework signer may burn Aptos coins. Critical The AptosCoinCapabilities structure is defined in this module and it stores burn capability to burn the gas fees. Formally Verified via module.
2 The initialization function may only be called once. Medium The initialize_fee_collection_and_distribution function ensures CollectedFeesPerBlock does not already exist. The fee collection mechanism no longer exists; initialize_fee_collection_and_distribution always aborts.
3 Only the admin address is authorized to call the initialization function. Critical The initialize_fee_collection_and_distribution function ensures only the Aptos framework address calls it. The fee collection mechanism no longer exists; initialize_fee_collection_and_distribution always aborts.
4 The percentage of the burnt collected fee is always a value from 0 to 100. Medium During the initialization of CollectedFeesPerBlock in Initialize_fee_collection_and_distribution, and while upgrading burn percentage, it asserts that burn_percentage is within the specified limits. Formally verified via CollectedFeesPerBlock.
5 Prior to upgrading the burn percentage, it must process all the fees collected up to that point. Critical The upgrade_burn_percentage function ensures process_collected_fees function is called before updating the burn percentage. The fee collection mechanism no longer exists; upgrade_burn_percentage always aborts.
6 The presence of the resource, indicating collected fees per block under the Aptos framework account, is a prerequisite for the successful execution of the following functionalities: Upgrading burn percentage. Registering a block proposer. Processing collected fees. Low The functions: upgrade_burn_percentage, register_proposer_for_fee_collection, and process_collected_fees all ensure that the CollectedFeesPerBlock resource exists under aptos_framework by calling the is_fees_collection_enabled method, which returns a boolean value confirming if the resource exists or not. The fee collection mechanism no longer exists; CollectedFeesPerBlock is never created and upgrade_burn_percentage always aborts.

Module-level Specification

pragma aborts_if_is_strict;
// This enforces high-level requirement 1:
invariant [suspendable] chain_status::is_operating() ==> exists<AptosCoinCapabilities>(@aptos_framework) || exists<AptosFABurnCapabilities>(@aptos_framework);

Resource CollectedFeesPerBlock

#[deprecated]
struct CollectedFeesPerBlock has key
amount: coin::AggregatableCoin<aptos_coin::AptosCoin>
proposer: option::Option<address>
burn_percentage: u8
// This enforces high-level requirement 4:
invariant burn_percentage <= 100;

Function burn_fee

public(friend) fun burn_fee(account: address, fee: u64)

Burns fee from the APT primary store of account with the stored burn ref.

aborts_if !exists<AptosFABurnCapabilities>(@aptos_framework);
include aptos_account::BurnFromAptStore {
    ref: global<AptosFABurnCapabilities>(@aptos_framework).burn_ref,
    amount: fee
};

Function mint_and_refund

public(friend) fun mint_and_refund(account: address, refund: u64)

Mints refund into the primary store of account, with the FA mint ref if enabled, else with the coin mint capability.

pragma aborts_if_is_partial;
let fa_path = features::spec_is_enabled(features::GAS_REFUND_FA_MINT)
    && exists<AptosFAMintCapabilities>(@aptos_framework);
let apt_store = aptos_account::spec_apt_store_address(account);
let paired = coin::spec_paired_metadata(type_info::type_of<AptosCoin>());
let coin_store = coin::spec_paired_store_address(account, paired);
let apt_store_exists = exists<fungible_asset::FungibleStore>(apt_store);
let coin_store_exists = coin::spec_has_paired_store(account, paired);
aborts_if !fa_path && !exists<AptosCoinMintCapability>(@aptos_framework);
ensures fa_path && apt_store_exists ==>
    fungible_asset::spec_balance(apt_store) == old(fungible_asset::spec_balance(apt_store)) + refund;
ensures !fa_path && coin_store_exists ==>
    fungible_asset::spec_balance(coin_store) == old(fungible_asset::spec_balance(coin_store)) + refund;

Function store_aptos_coin_burn_cap

public(friend) fun store_aptos_coin_burn_cap(aptos_framework: &signer, burn_cap: coin::BurnCapability<aptos_coin::AptosCoin>)

Ensure caller is admin. Takes the paired burn ref of APT and stores it.

let addr = signer::address_of(aptos_framework);
let metadata = option::borrow(coin::spec_paired_metadata(type_info::type_of<AptosCoin>()));
let refs = global<coin::PairedFungibleAssetRefs>(metadata.inner);
aborts_if !system_addresses::is_aptos_framework_address(addr);
include coin::PairedRefsAbortsIf<AptosCoin>;
aborts_if option::is_none(refs.burn_ref_opt);
aborts_if exists<AptosFABurnCapabilities>(addr);
ensures exists<AptosFABurnCapabilities>(addr);
ensures global<AptosFABurnCapabilities>(addr).burn_ref == option::borrow(refs.burn_ref_opt);

Function store_aptos_coin_mint_cap

public(friend) fun store_aptos_coin_mint_cap(aptos_framework: &signer, mint_cap: coin::MintCapability<aptos_coin::AptosCoin>)

Ensure caller is admin. Aborts if AptosFAMintCapabilities or AptosCoinMintCapability already exists.

let addr = signer::address_of(aptos_framework);
aborts_if !system_addresses::is_aptos_framework_address(addr);
include PairedMintRefAbortsIf;
aborts_if exists<AptosFAMintCapabilities>(addr);
aborts_if exists<AptosCoinMintCapability>(addr);
ensures exists<AptosFAMintCapabilities>(addr);
ensures exists<AptosCoinMintCapability>(addr);

APT is paired and its mint ref has not been taken.

schema PairedMintRefAbortsIf {
    let metadata = option::borrow(coin::spec_paired_metadata(type_info::type_of<AptosCoin>()));
    include coin::PairedRefsAbortsIf<AptosCoin>;
    aborts_if option::is_none(global<coin::PairedFungibleAssetRefs>(metadata.inner).mint_ref_opt);
}

Function convert_to_aptos_fa_mint_ref

public entry fun convert_to_aptos_fa_mint_ref(aptos_framework: &signer)

Ensure caller is admin.

let addr = signer::address_of(aptos_framework);
aborts_if !system_addresses::is_aptos_framework_address(addr);
aborts_if !exists<AptosCoinMintCapability>(addr);
include PairedMintRefAbortsIf;
aborts_if exists<AptosFAMintCapabilities>(addr);
ensures exists<AptosFAMintCapabilities>(addr);

Function emit_fee_statement

public(friend) fun emit_fee_statement(fee_statement: transaction_fee::FeeStatement)

Never aborts.

Function initialize_fee_collection_and_distribution

#[deprecated]
public fun initialize_fee_collection_and_distribution(_aptos_framework: &signer, _burn_percentage: u8)

Deprecated; always aborts.

aborts_if true;

Function upgrade_burn_percentage

#[deprecated]
public fun upgrade_burn_percentage(_aptos_framework: &signer, _new_burn_percentage: u8)

Deprecated; always aborts.

aborts_if true;

Function initialize_storage_refund

#[deprecated]
public fun initialize_storage_refund(_: &signer)

Historical. Aborts.

aborts_if true;

Function convert_to_aptos_fa_burn_ref

#[deprecated]
public entry fun convert_to_aptos_fa_burn_ref(_aptos_framework: &signer)

Deprecated; always aborts.

aborts_if true;