0x1::TransactionFee
TransactionFee
collect gas fees used by transactions in blocks temporarily.
Then they are distributed in TransactionManager
.
TransactionFee
initialize
add_txn_fee_token
pay_fee
distribute_transaction_fees
use 0x1::CoreAddresses;
use 0x1::STC;
use 0x1::Timestamp;
use 0x1::Token;
TransactionFee
The TransactionFee
resource holds a preburn resource for each
fiat TokenType
that can be collected as a transaction fee.
struct TransactionFee<TokenType> has key
fee: Token::Token<TokenType>
initialize
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)
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);
}
aborts_if !Timestamp::is_genesis();
aborts_if Signer::address_of(account) != CoreAddresses::GENESIS_ADDRESS();
aborts_if exists<TransactionFee<STC>>(Signer::address_of(account));
add_txn_fee_token
publishing a wrapper of the Preburn<TokenType>
resource under fee_account
fun add_txn_fee_token<TokenType: store>(account: &signer)
fun add_txn_fee_token<TokenType: store>(
account: &signer,
) {
move_to(
account,
TransactionFee<TokenType> {
fee: Token::zero(),
}
)
}
aborts_if exists<TransactionFee<TokenType>>(Signer::address_of(account));
pay_fee
Deposit token
into the transaction fees bucket
public fun pay_fee<TokenType: store>(token: Token::Token<TokenType>)
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)
}
aborts_if !exists<TransactionFee<TokenType>>(CoreAddresses::GENESIS_ADDRESS());
aborts_if global<TransactionFee<TokenType>>(CoreAddresses::GENESIS_ADDRESS()).fee.value + token.value > max_u128();
distribute_transaction_fees
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>
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>()
}
}
pragma verify = false;
pragma verify;
pragma aborts_if_is_strict;