您正在查看: Surou 发布的文章

如何计算一个EOS合约部署的成本,以及消耗

本文由来

最近在做一个Dapp,里面有部分功能涉及帮助用户存储一定量的数据,所以需要有个计算用户成本的逻辑,由用户持续为自己消耗的存储付费。

开始计算合约成本

合约的部署成本

等于合约源码编译后WASM文件大小的10倍(mainnet)。相关源码在(GitHub code)查找setcode_ram_bytes_multiplier。这涵盖了VM和其他内存的开销,这是发布合约的最基本消耗。

合约的数据成本

对于系统账户的RAM消耗计算是在eosio代码(GitHub code)中,通过_db检索资源消耗,这需要访问运行的chainbase数据库。据我目前了解,还不能从合约直接这样获取。
那目前预计方案为,跟踪合约执行,并根据每行中存储的数据估算成本。

初步方案实现

用户发起转账到合约账户,内联购买RAM到合约账户,并记录增加的RAM到合约内对应账户下。
新增加数据时,计算本次的使用量,如果合约内账户剩余RAM足够的话,减去对应的数量,不够的话,提示用户充值。
删除数据时,计算本次数据的删除量,增加的内存量到合约内对应账户下。
如果做公益性质,由于数据估算可能有一定量的偏差,可以引入一定量的手续费,避免损失。

TODO

等方案实现完,再来继续完善本文。

参考:

faucet.cryptokylin reach 24 hours max token amount

麒麟网获取测试代币时
http://faucet.cryptokylin.io/get_token?bcskillsurou
返回

{
    msg: "reach 24 hours max token amount"
}

可尝试使用直接ip 访问获取
http://13.230.87.138/get_token?bcskillsurou

get_actions error in version v1.3.+

目前了解到的方案是,nodeos 重新启动,添加以下参数,重建数据,也许之后版本会有新的修复方案

--hard-replay-blockchain

github

[eosiocpp] account_name 与 std:string 互相转换

  • account_name to std:string
auto username = name{from};
std::string from_name = username.to_string();
  • std::string to account_name
account_name from_name = string_to_name(username);

How to pass struct to action

I have a simple struct with one member and want to pass this to an action.

struct field {
    bool is_private;
  };

  // @abi action
  // Create a new profile
  void create(const account_name& account, const field& first_name);

The .abi looks like this:

  "structs": [{
      "name": "field",
      "base": "",
      "fields": [{
          "name": "is_private",
          "type": "bool"
        }
      ]
    },{
      "name": "create",
      "base": "",
      "fields": [{
          "name": "account",
          "type": "name"
        },{
          "name": "first_name",
          "type": "field"
        }
      ]
    }
...

I tried a lot of different variations along the lines of cleos push action "contract" "create" '[acc_name, {"is_private": "true"}]' -p acc_name@active. But none of them seem to work. Are structs supported and if yes how do you pass the argument?

Thanks a lot!


Found the solution myself. You have to recreate the .abi structure in json:
cleos push action contract create '{"account":"acc_name","first_name":{"is_private":true"}}' -p acc_name@active

https://github.com/EOSIO/eos/issues/4914