TransactionFee
collect gas fees used by transactions in blocks temporarily.
Then they are distributed in TransactionManager
.
- Resource
TransactionFee
- Function
initialize
- Function
add_txn_fee_token
- Function
pay_fee
- Function
distribute_transaction_fees
- Module Specification
use 0x1::CoreAddresses;
use 0x1::STC;
use 0x1::Timestamp;
use 0x1::Token;
The TransactionFee
resource holds a preburn resource for each
fiat TokenType
that can be collected as a transaction fee.
struct TransactionFee<TokenType> has key
Fields
-
fee: Token::Token<TokenType>
Called in genesis. Sets up the needed resources to collect transaction fees from the
TransactionFee
resource with the TreasuryCompliance account.
public fun initialize(account: &signer)
Implementation
public fun initialize(
account: &signer,
) {
Timestamp::assert_genesis();
CoreAddresses::assert_genesis_address(account);
// accept fees in all the currencies
add_txn_fee_token<STC>(account);
}
Specification
aborts_if !Timestamp::is_genesis();
aborts_if Signer::address_of(account) != CoreAddresses::SPEC_GENESIS_ADDRESS();
aborts_if exists<TransactionFee<STC>>(Signer::address_of(account));
publishing a wrapper of the Preburn<TokenType>
resource under fee_account
fun add_txn_fee_token<TokenType: store>(account: &signer)
Implementation
fun add_txn_fee_token<TokenType: store>(
account: &signer,
) {
move_to(
account,
TransactionFee<TokenType> {
fee: Token::zero(),
}
)
}
Specification
aborts_if exists<TransactionFee<TokenType>>(Signer::address_of(account));
Deposit token
into the transaction fees bucket
public fun pay_fee<TokenType: store>(token: Token::Token<TokenType>)
Implementation
public fun pay_fee<TokenType: store>(token: Token<TokenType>) acquires TransactionFee {
let txn_fees = borrow_global_mut<TransactionFee<TokenType>>(
CoreAddresses::GENESIS_ADDRESS()
);
Token::deposit(&mut txn_fees.fee, token)
}
Specification
aborts_if !exists<TransactionFee<TokenType>>(CoreAddresses::SPEC_GENESIS_ADDRESS());
aborts_if global<TransactionFee<TokenType>>(CoreAddresses::SPEC_GENESIS_ADDRESS()).fee.value + token.value > max_u128();
Distribute the transaction fees collected in the TokenType
token.
If the TokenType
is STC, it unpacks the token and preburns the
underlying fiat.
public fun distribute_transaction_fees<TokenType: store>(account: &signer): Token::Token<TokenType>
Implementation
public fun distribute_transaction_fees<TokenType: store>(
account: &signer,
): Token<TokenType> acquires TransactionFee {
let fee_address = CoreAddresses::GENESIS_ADDRESS();
CoreAddresses::assert_genesis_address(account);
// extract fees
let txn_fees = borrow_global_mut<TransactionFee<TokenType>>(fee_address);
let value = Token::value<TokenType>(&txn_fees.fee);
if (value > 0) {
Token::withdraw(&mut txn_fees.fee, value)
}else {
Token::zero<TokenType>()
}
}
Specification
pragma verify = false;
pragma verify;
pragma aborts_if_is_strict;