Open stevenlaw123 opened 1 year ago
I also designed an example for the meetup show.
module mygame::play{
public fun gas_validate(ctx: &StorageContext) :bool{
//if the contract pays gas for the current tx, return true
true
}
fun gas_charge_post(ctx: &mut StorageContext, gas_used: u256){
// records gas pay amount for users
}
#[gas_free(gas_validate="mygame::gas_validate",gas_charge_post="mygame::gas_charge_update")]
public entry play(ctx: &mut StorageContext, sender: &signer){
}
}
gas_free
means the contract account provides the gas.gas_validate
references a function. If the function returns true, the gas will be paid by the contract account, otherwise, it will be paid by the user account.The follow refactor tracked via #1334 1334
After compiling a Move module, analyze all the functions. If a function has the
#[pay_gas]
attribute, when a user calls a contract function within this module and needs to pay gas, the MoveVM will invoke the function decorated with#[pay_gas]
. If this function returnstrue
, the gas that the user should have paid will be deducted from the account associated with the current module.A function that can be decorated with
#[pay_gas]
follows the format below:When a user calls
m::0x42::f1()
, after executing them::0x42::f1()
function, the MoveVM will calculate the gas consumed by this function and the calling address. Then, the MoveVM will call thegas_checking(gas: u64, user_addresss: addresss): bool
function and pass in the gas consumed bym::0x42::f1()
and the calling address. The MoveVM will receive the return value ofgas_checking.
If it is true, gas will be deducted from the account of thetest_module
module.For functions that replace user gas payment within a module, the following rules apply:
func_name(gas: u64, user_addresss: addresss): bool
.#[pay_gas]
can only appear once within the module.The implementation approach is as follows:
#[pay_gas]
. If so, check if the number and types of parameters and return values of the functions are valid. If not, the compiler throws an error.Metadata
.Metadata
of the corresponding module and retrieves the gas payment function information from it.