Filecoin 的Gas模型及获取

一:前言

最初FileCoin的手续费是以Gasprice来计算的,和eth是类似的。
后续就改掉了。

改为:Filecoin借鉴EIP1559引入“基本费用”机制

官方Gas介绍:
https://spec.filecoin.io/systems/filecoin_vm/gas_fee/

https://filecoin.io/blog/filecoin-features-gas-fees/

所以说FileCoin的fee是包含多部分

 "fee":{
        "baseFeeBurn":"12927745495392",  //基础燃烧
        "overEstimationBurn":"467707757426", //超额燃烧
        "minerPenalty":"0", //惩罚金额
        "minerTip":"61201542675", //旷工费
        "refund":"152664269595747" //返还费用
    },

二:获取与计算

GasFeeCap //最大手续费费率。Base Fee 是变化的,有可能太大,交易发送者,不愿意支付。Gas Fee Cap (上限),就是设置支付费用的上限

GasPremium //提供给矿工的优先打包费率,其实就是小费,用户可以多付增加优先打包(为0也是可以发送交易的)。

BaseFee //用来调节交易的拥堵情况

Gas Limit //最大手续费数量
Gas Used //实际消耗手续费数量

FileCoin的手续费计算主要是gasLimit,gasFeeCap,gasPremium,baseFee
这些应该怎么获取呢?

官方节点rpc获取文档 :
https://github.com/filecoin-project/lotus/blob/master/documentation/en/api-methods.md#GasEstimateMessageGas

request:

{
       "jsonrpc": "2.0",
       "method": "Filecoin.GasEstimateMessageGas",
       "params": [
       {
           "To": "f1wlsdn2phpzczyalerizzrrhhpqjlsn67an54thq",
           "From": "f1wlsdn2phpzczyalerizzrrhhpqjlsn67an54thq",
           "Nonce": 2,
           "Value": "10000000000000000000",
           "GasPremium": "0",
           "GasFeeCap": "0",
           "GasLimit": 0,
           "Method": 0,
           "Params": ""
       },
       {
           "MaxFee": "0"
       },
       null
       ],
       "id": 2323
  }

response:

{
   "jsonrpc": "2.0",
   "result": {
       "Version": 0,
       "To": "f1wlsdn2phpzczyalerizzrrhhpqjlsn67an54thq",
       "From": "f1wlsdn2phpzczyalerizzrrhhpqjlsn67an54thq",
       "Nonce": 2,
       "Value": "10000000000000000000",
       "GasLimit": 2569772,
       "GasFeeCap": "1472660387",
       "GasPremium": "99725",
       "Method": 0,
       "Params": "",
       "CID": {
           "/": "bafy2bzacebdtuvvqmzkrfknzomj37ru3qxzexbh22qi57tuwrn7nkbd5n3amu"
       }
   },
   "id": 2323
}
利用GasEstimateMessageGasrpc

这样就会拿到gasLimit,gasFeeCap,gasPremium
这面说说主要的参数
From,to,value 都会影响到获取的gasLimit,gasFeeCap,gasPremium值
所以说参数的正确性是很有必要的。

GasLimit 设置惩罚:

众所周知,以太坊中的 Gas Limit 可以设置的非常大。一般情况下,多余的 Gas 费用会全数返还。特别注意的是,Filecoin 并不完全是这样。因为 Gas Limit 参与了 Base Fee 和 Gas Premium 的计算,真实的 Gas Limit 是非常重要的。如果一个交易,设置了不合理的 Gas Limit,Filecoin 采取了一种惩罚机制。惩罚的 Gas 费用也被燃烧

BaseFee:

我这面发送交易写的是100attoFIL(但是当网络拥堵的情况,base都会实时变化的。)

baseFee的获取:

em.currentHead, err = lfnc.FullNodeApi.ChainHead(ctx)
if err != nil {
    log.Fatalf("calling chain head: %s", err)
}
log.Printf("Current chain head is: %d\n", em.currentHead.Height())
for _, block := range em.currentHead.Blocks() {
  baseFee := block.ParentBaseFee
    log.Printf("BaseFee: %s\n", baseFee)
}
curl -X POST \
     -H "Content-Type: application/json" \
     --data '{
       "jsonrpc":"2.0",
       "method":"Filecoin.ChainHead",
       "params":[],
       "id":7878
     }' \
{
  "id": 7878,
  "jsonrpc": "2.0",
  "result": {
    "Cids": null,
    "Blocks": null,
    "Height": 0
  }
}
ChainHead --> Block --> ParentBaseFee

具体看官方文档
最新 24 小时的 base Fee 可以在飞狐浏览器查看 (https://filfox.info/zh):

三:总结

根据拿到的值 预估费用:

第一种
fee = Gas Limit * (BaseFee + min(GasPremium, GasFeeCap-BaseFee)) //基础费 + 矿工费

第二种
fee = Gas Limit * GasFeeCap //此次交易支付的最大费用

结论:

第二种相对第一种会偏大,但是可以通过修改GasFeeCap和GasPremium相应的变小

第一种在你发送的刚好是(余额- fee)的数量会广播失败
因为针对节点的fee判断是Gas Limit * GasFeeCap,会导致广播余额不够

你可能感兴趣的:(Filecoin 的Gas模型及获取)