找回密码
 注册免广告
搜索
免绑卡永久免费服务器 🌐Neverless 交易所带个人 IBAN 的 OKX 交易所
熊猫速汇 50 元券 🔥MyFin 5 欧元羊毛 🔥人人必备的 Wise 💳
英、德、香港转运 📦,送 $25最便宜的 eSIM 流量手机号 📱数字货币银行卡,注册送 7 美元💲
查看: 87|回复: 0

[其它] 关于 cosmos 链的 gas fee 参数

[复制链接]
HelloWorld 发表于 4 天前 | 显示全部楼层 |阅读模式

注册免广告

您需要 登录 才可以下载或查看,没有账号?注册免广告

×
本帖最后由 HelloWorld 于 2025-5-26 06:25 编辑

一般我们看到的 fee 参数如下
  1. const fee = {
  2.   amount: [{
  3.     denom: "uatom",
  4.     amount: "5000" // max_gas_fee
  5.   }],
  6.   gas: "200000" // max_gas_units
  7. };
复制代码
其中 amount 表示我们愿意支付的最大 token 量去作为 gas 费,上述参数表示当前 transaction 我们愿意支付的最大 gas 费为 5000uatom(0.005 ATOM)
gas 表示允许 transaction 支付的最大 gas 单元
真实消耗的 gasFee = gas_units * gasPrice,其中 gas_units 要求 <= gas(这里设置了 200000),gasFee 要求 <= amount(例子中是 5000uatom 或者说 0.005 ATOM)

gasPrice 从哪来?
答案是,链参数里的固定值,我们无法从 rest/rpc api 读取,但是可以从 https://www.mintscan.io/cosmos/parameters 这里查到 Fee Rate Low 为 0.025uatom
事实上,是节点提供者写在 ~/.simapp/config/app.toml 文件里的参数,mintscan 里显示的是人工维护的数据

gas_units 从哪来?
这个取决于你 transaction 的复杂度,可以通过 simulate 的方式获取,命令行为
  1. simd tx bank send ... --gas=auto --gas-adjustment=1.2
复制代码
或者用 ethersjs 提供的 simulate 功能
simulate 得到的数额(gas_units)再乘以 1.2 去作为 gas。如果你设置的 gas<simulate_gas_units,那么上链会失败

所以构建 fee 参数的步骤为:
1. 查一下链的 gasPrice,一般大部分 cosmos 生态链为 0.025,keplr 钱包源码也用 0.025 固定值:https://github.com/chainapsis/ke ... n/src/config.ts#L89
2. simulate 你的 transaction 获得 gas_units
3. 定下参数 gas = gas_units * 1.2
4. 定下参数 amount = gas * 0.025

总之,amount 和 gas 太小都会导致上链失败,amount 不建议太大,一般在你愿意支付的范围内,或者按照上述公式计算

扩展:
https://www.mintscan.io/cosmos/parameters 还提供了一个 Tiny gasPrice 为 0.005,如果用这个作为 gasPirce,在网络拥堵时可能会上链失败,因此建议用 Low gasPrice(0.025)
如果帖子/回帖帮助到你,请给作者评分/点赞
您需要登录后才可以回帖 登录 | 注册免广告

本版积分规则

排行榜|意见建议|黑名单|数字居民论坛

GMT+8, 2025-5-30 14:44

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表