您正在查看: EOS-新手教程 分类下的文章

EOS time_point_sec 时间计算

EOS专门对时间计算进行了封装,在eosiolib/time.hpp中提供了多种关于时间计算的封装,简单介绍一下time_point_sec的用法

auto time_now = time_point_sec(now()/*获取当前区块时间*/);
uint32_t five_minutes = 5 * 60;
five_minutes_later = time_now + five_minutes;
five_minutes_later > time_now;

得到的time_point_sec类型可以与uint32_t类型进行简单的加/减数值运算,同类型间也可以直接用大于/小于等比较符进行运算,满足大多数时间运算需求了已经。
转载自:github

创建默认的genesis.json

创建genesis.json文件

 nodeos --extract-genesis-json genesis.json

打印genesis-json

--print-genesis-json

获取RAM实时价格

命令如下

cleos -u https://nodes.get-scatter.com:443 get table eosio eosio rammarket

获取的结果为

{
  "rows": [{
      "supply": "10000000000.0000 RAMCORE",
      "base": {
        "balance": "12341975994 RAM",
        "weight": "0.50000000000000000"
      },
      "quote": {
        "balance": "5568015.6361 EOS",
        "weight": "0.50000000000000000"
      }
    }
  ],
  "more": false
}

然后使用Bancor算法,计算出需要nKB的RAM的价格

RAM价格 = (n * quote.balance) / (n + base.balance / 1024)

RPC代码实现如下

const wif = '5JC9FdRjX3c3fsB62S5Tz22LgTuRHegb1XEV16uft8u3njmm9E5'
eos = Eos({
    httpEndpoint: 'https://nodes.get-scatter.com:443',
    chainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
    keyProvider: wif,
    verbose: true
    })
eos.getTableRows(true,"eosio", "eosio", "rammarket").then(result => {
    console.log(1 * result.rows[0].quote.balance.split(" ")[0] / (1 + result.rows[0].base.balance.split(" ")[0] / 1024));
}).catch(e => {
    console.error(e);
})

当前返回价格:0.4716754832469308 EOS/Kib

参考:github

EOS发起交易时,提示ram,net或cpu资源不足问题分析与解决(Error 3080001: account using more than allotted RAM usage)

一般创建账号,或者三方购买的账号都是最低的资源创建的 (cleos system newaccount 消耗最少的EOS资源
基本账号下CPU和NET都是零,RAM 3kb以下,因为RAM收费逻辑(EOS零手续费免费?你不知道的EOS收费细节

  • 只要action中执行了持久化存储相关的逻辑就需要收取ram使用费,比如系统合约的newaccount ,updateauth, setcode, setabi, schedule_deferred_transaction, eosio.token的transfer

所以在账号资源不足的情况下,转账操作会提示以下错误

surou@surou-C-H110M-K-Pro:~/.local/share/eosio/nodeos/config$ cleos push action eosio.token transfer '["eosio", "bcskillsurou","100.0000 EOS","vote"]' -p eosio
executed transaction: e233ccc335b833ac9d7746a43e3b4e591795151c95f989a36976f8ceb98a08c7  136 bytes  562 us
#   eosio.token <= eosio.token::transfer        {"from":"eosio","to":"bcskillsurou","quantity":"100.0000 EOS","memo":"vote"}
#         eosio <= eosio.token::transfer        {"from":"eosio","to":"bcskillsurou","quantity":"100.0000 EOS","memo":"vote"}
#  bcskillsurou <= eosio.token::transfer        {"from":"eosio","to":"bcskillsurou","quantity":"100.0000 EOS","memo":"vote"}
warning: transaction executed locally, but may not be confirmed by the network yet
surou@surou-C-H110M-K-Pro:~/.local/share/eosio/nodeos/config$ cleos get currency balance eosio.token bcskillsurou
100.0000 EOS
surou@surou-C-H110M-K-Pro:~/.local/share/eosio/nodeos/config$ cleos push action eosio.token transfer '["bcskillsurou", "eosio","1.0000 EOS","vote"]' -p bcskillsurou
Error 3080001: account using more than allotted RAM usage

提示RAM不足已支持转账
如果自己给自己直接购买RAM的话

surou@surou-C-H110M-K-Pro:~/.local/share/eosio/nodeos/config$ cleos system buyram bcskillsurou bcskillsurou "0.1000 EOS"
3536793ms thread-0   main.cpp:429                  create_action        ] result: {"binargs":"a0e9d5384607313aa0e9d5384607313ae80300000000000004454f5300000000"} arg: {"code":"eosio","action":"buyram","args":{"payer":"bcskillsurou","receiver":"bcskillsurou","quant":"0.1000 EOS"}} 
Error 3080002: transaction exceeded the current network usage limit imposed on the transaction

提示NET不足
如果自己给自己抵押换取NET和CPU时

surou@surou-C-H110M-K-Pro:~/.local/share/eosio/nodeos/config$ cleos system delegatebw bcskillsurou bcskillsurou '0.1000 EOS' '0.1000 EOS' -p bcskillsurou
48402ms thread-0   main.cpp:1084                 operator()           ] act_payload: {"from":"bcskillsurou","receiver":"bcskillsurou","stake_net_quantity":"0.1000 EOS","stake_cpu_quantity":"0.1000 EOS","transfer":false} 
48403ms thread-0   main.cpp:429                  create_action        ] result: {"binargs":"a0e9d5384607313aa0e9d5384607313ae80300000000000004454f5300000000e80300000000000004454f530000000000"} arg: {"code":"eosio","action":"delegatebw","args":{"from":"bcskillsurou","receiver":"bcskillsurou","stake_net_quantity":"0.1000 EOS","stake_cpu_quantity":"0.1000 EOS","transfer":false}} 
Error 3080001: account using more than allotted RAM usage

又提示RAM不足...先有鸡还是先有蛋,死循环。所以只能让其他RAM资源充足的账号例如 eosio 帮此账号bcskillsurou购买

surou@surou-C-H110M-K-Pro:~/.local/share/eosio/nodeos/config$ cleos system buyram eosio bcskillsurou "0.1000 EOS"
177473ms thread-0   main.cpp:429                  create_action        ] result: {"binargs":"0000000000ea3055a0e9d5384607313ae80300000000000004454f5300000000"} arg: {"code":"eosio","action":"buyram","args":{"payer":"eosio","receiver":"bcskillsurou","quant":"0.1000 EOS"}} 
executed transaction: f5aa4dd7f6a850b65884174a4f86285414ca5c50ba1ab6ddc2a7f9ba5b65c051  128 bytes  1814 us
#         eosio <= eosio::buyram                {"payer":"eosio","receiver":"bcskillsurou","quant":"0.1000 EOS"}
#   eosio.token <= eosio.token::transfer        {"from":"eosio","to":"eosio.ram","quantity":"0.0995 EOS","memo":"buy ram"}
#         eosio <= eosio.token::transfer        {"from":"eosio","to":"eosio.ram","quantity":"0.0995 EOS","memo":"buy ram"}
#     eosio.ram <= eosio.token::transfer        {"from":"eosio","to":"eosio.ram","quantity":"0.0995 EOS","memo":"buy ram"}
#   eosio.token <= eosio.token::transfer        {"from":"eosio","to":"eosio.ramfee","quantity":"0.0005 EOS","memo":"ram fee"}
#         eosio <= eosio.token::transfer        {"from":"eosio","to":"eosio.ramfee","quantity":"0.0005 EOS","memo":"ram fee"}
#  eosio.ramfee <= eosio.token::transfer        {"from":"eosio","to":"eosio.ramfee","quantity":"0.0005 EOS","memo":"ram fee"}
warning: transaction executed locally, but may not be confirmed by the network yet

bcskillsurouRAM足够后,就可以自己或者其他账号抵押购买CPU和NET了

surou@surou-C-H110M-K-Pro:~/.local/share/eosio/nodeos/config$ cleos system delegatebw bcskillsurou bcskillsurou '0.1000 EOS' '0.1000 EOS' -p bcskillsurou
214840ms thread-0   main.cpp:1084                 operator()           ] act_payload: {"from":"bcskillsurou","receiver":"bcskillsurou","stake_net_quantity":"0.1000 EOS","stake_cpu_quantity":"0.1000 EOS","transfer":false} 
214841ms thread-0   main.cpp:429                  create_action        ] result: {"binargs":"a0e9d5384607313aa0e9d5384607313ae80300000000000004454f5300000000e80300000000000004454f530000000000"} arg: {"code":"eosio","action":"delegatebw","args":{"from":"bcskillsurou","receiver":"bcskillsurou","stake_net_quantity":"0.1000 EOS","stake_cpu_quantity":"0.1000 EOS","transfer":false}} 
executed transaction: b57bad80a397c6fee9aab0519600b9c063f6084fadd542402fe07ce93d2643bb  144 bytes  1720 us
#         eosio <= eosio::delegatebw            {"from":"bcskillsurou","receiver":"bcskillsurou","stake_net_quantity":"0.1000 EOS","stake_cpu_quanti...
#   eosio.token <= eosio.token::transfer        {"from":"bcskillsurou","to":"eosio.stake","quantity":"0.2000 EOS","memo":"stake bandwidth"}
#  bcskillsurou <= eosio.token::transfer        {"from":"bcskillsurou","to":"eosio.stake","quantity":"0.2000 EOS","memo":"stake bandwidth"}
#   eosio.stake <= eosio.token::transfer        {"from":"bcskillsurou","to":"eosio.stake","quantity":"0.2000 EOS","memo":"stake bandwidth"}
warning: transaction executed locally, but may not be confirmed by the network yet

此时再次转账的话

surou@surou-C-H110M-K-Pro:~/.local/share/eosio/nodeos/config$ cleos get currency balance eosio.token bcskillsurou
99.8000 EOS
surou@surou-C-H110M-K-Pro:~/.local/share/eosio/nodeos/config$ cleos push action eosio.token transfer '["bcskillsurou", "eosio","1.0000 EOS","vote"]' -p bcskillsurou
executed transaction: cd9a0780b51db71a0efd096578dc453d26a528af6fa4e4e7ab0b139e9d9753ae  136 bytes  548 us
#   eosio.token <= eosio.token::transfer        {"from":"bcskillsurou","to":"eosio","quantity":"1.0000 EOS","memo":"vote"}
#  bcskillsurou <= eosio.token::transfer        {"from":"bcskillsurou","to":"eosio","quantity":"1.0000 EOS","memo":"vote"}
#         eosio <= eosio.token::transfer        {"from":"bcskillsurou","to":"eosio","quantity":"1.0000 EOS","memo":"vote"}
warning: transaction executed locally, but may not be confirmed by the network yet
surou@surou-C-H110M-K-Pro:~/.local/share/eosio/nodeos/config$ cleos get currency balance eosio.token bcskillsurou
98.8000 EOS

就一切OK了

EOS主网账户名交易

这里主要是要教会大家如果将自己公钥里面的账户名转给其他人。能转出给另外一个公钥就可以实现线下交易,线上转账户名了。
代码如下:

#转移active权限
cleos push action eosio updateauth '{"permission":"active","parent":"owner","account":"你要转移的帐号","auth": {"accounts": [], "waits": [], "keys": [{"key": "EOS开头的对方的公钥", "weight": 1}], "threshold": 1}}' -p 你要转移的帐号@active

#转移owner权限
cleos push action eosio updateauth '{"permission":"owner","parent":"","account":"你要转移的帐号","auth": {"accounts": [], "waits": [], "keys": [{"key": "EOS开头的对方的公钥", "weight": 1}], "threshold": 1}}' -p 你要转移的帐号@owner

如果是网络情况报错可能是没有连接上主网,则改为:
#转移active权限
cleos -u http://mainnet.eoswz.com push action eosio updateauth '{"permission":"active","parent":"owner","account":"你要转移的帐号","auth": {"accounts": [], "waits": [], "keys": [{"key": "EOS开头的对方的公钥", "weight": 1}], "threshold": 1}}' -p 你要转移的帐号@active

#转移owner权限
cleos -u http://mainnet.eoswz.com push action eosio updateauth '{"permission":"owner","parent":"","account":"你要转移的帐号","auth": {"accounts": [], "waits": [], "keys": [{"key": "EOS开头的对方的公钥", "weight": 1}], "threshold": 1}}' -p 你要转移的帐号@owner

注意!

这里可能会提示一些报错。例如:

#CPU不够
Error 3080004: transaction exceeded the current CPU usage limit imposed on the transaction
Error Details:
billed CPU time (574 us) is greater than the maximum billable CPU time for the transaction (0 us)
#.NET不够
Error 3080002: transaction exceeded the current network usage limit imposed on the transaction
Error Details:
net usage of transaction is too high: 160 > 0

这是因为你在建立这个账户名的时候,账户名下没有足够的资源导致。所以只需要其他有资源的账户给这个账户转入一些资源就可以了。