BCSkill (Block chain skill )
区块链中文技术社区

只讨论区块链底层技术
遵守一切相关法律政策!

RPC获取常见资源数据

实例化EOS对象

const wif = '私钥'
eos = Eos({
        httpEndpoint: 'https://api.eosnewyork.io',
        chainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
        keyProvider: wif,
        verbose: true
    })

RAM

  • 获取总网RAM (GB)

    eos.getTableRows(true,"eosio","eosio","global",10).then(result => {
      console.log(result.rows[0].max_ram_size/(1024*1024*1024));
    })
  • 获取RAM当前价格
    参考(获取RAM实时价格)

  • 获取总网RAM使用量

    eos.getTableRows(true,"eosio", "eosio", "rammarket").then(result => {
      var ramBaseBalance = result.rows[0].base.balance; // Amount of RAM bytes in use
      ramBaseBalance = ramBaseBalance.substr(0,ramBaseBalance.indexOf(' '));
      console.log(ramBaseBalance);
    })
  • 获取总网已购买RAM的EOS总数

    eos.getTableRows(true,"eosio", "eosio", "rammarket").then(result => {
      var ramQuoteBalance = result.rows[0].quote.balance; // Amount of EOS in the RAM collector
      ramQuoteBalance = ramQuoteBalance.substr(0,ramQuoteBalance.indexOf(' '));
      console.log(ramQuoteBalance);
    })

NET

  • 获取当前NET价格(EOS/KiB/Day)
    eos.getAccount("eosnewyorkio").then(result => {
      var netStaked = result.total_resources.net_weight.substr(0,result.total_resources.net_weight.indexOf(' '));
      var netAvailable = result.net_limit.max / 1024; //~ convert bytes to kilobytes
      netPriceEos = ((netStaked / netAvailable)/3).toFixed(8); //~ divide by 3 to get average per day from 3 day avg
      console.log(netPriceEos);
    })

CPU

  • 获取当前CPU价格(EOS/ms/Day)
    eos.getAccount("eosnewyorkio").then(result => {
      var cpuStaked = result.total_resources.cpu_weight.substr(0,result.total_resources.cpu_weight.indexOf(' '));
      var cpuAvailable = result.cpu_limit.max / 1000; // convert microseconds to milliseconds
      cpuPriceEos = ((cpuStaked / cpuAvailable)/3).toFixed(8); //~ divide by 3 to get average per day from 3 day avg
      console.log(cpuPriceEos);
    })

    科普

    什么是RAM?

    RAM需要在区块链上存储数据,必须购买。 您可以根据当前的RAM市场价格获得一定数量的存储字节数。 价格会根据买卖行为自动调整。 当您释放存储空间时,您可以以当前的市场价格出售RAM以恢复EOS。

    什么是网络带宽?

    网络带宽的测量值是过去3天内的平均消耗量(以字节为单位)。 每次发送操作或事务时都会暂时消耗净带宽,但会随着时间的推移而减少返回0.净带宽所占用的令牌越多,您使用的越多。 您可以随时取消收回您的EOS令牌。

    什么是CPU带宽?

    CPU带宽测量为过去3天内的平均消耗量(以微秒为单位)。 发送操作或事务时临时消耗CPU带宽,但随着时间的推移逐渐减少,返回0.事务运行的时间越长,消耗的CPU带宽就越多。 您可以随时取消收回您的EOS令牌。

数据来自:github erp

获取EOS实时价格 USD price

http api:https://api.coinmarketcap.com/v2/ticker/1765/

{
    "data": {
        "id": 1765, 
        "name": "EOS", 
        "symbol": "EOS", 
        "website_slug": "eos", 
        "rank": 5, 
        "circulating_supply": 906245118, 
        "total_supply": 1006245120, 
        "max_supply": 1000000000, 
        "quotes": {
            "USD": {
                "price": 7.1827637594, 
                "volume_24h": 636473463.658721, 
                "market_cap": 6509344588, 
                "percent_change_1h": -0.09, 
                "percent_change_24h": -1.29, 
                "percent_change_7d": -16.8
            }
        }, 
        "last_updated": 1533193294
    }, 
    "metadata": {
        "timestamp": 1533192860, 
        "error": null
    }
}

https://api.coinmarketcap.com/v1/ticker/${symbol}/?convert=${convert_symbol}
price字段即为当前的EOS USD价格 (具体准确度,自己衡量)
数据来自:github erp

EOS 获取资源抵押赎回数据

用户可以根据需要,自己或者别人帮助抵押EOS,获取NET或者CPU资源。
查看此时账户bcskillsurou信息

root@iZj6cbx3duprxf6dasczbpZ:~# cleos -u http://167.99.91.77:8888 get account bcskillsurou
permissions: 
     owner     1:    1 EOS8127zAiVVtPMJoiXyX8oLiReNaj32VBsJXKuTLkJsQyubKcSt7
        active     1:    1 EOS6Zfj72f6kBVZQrbkcCCGhWGnUfkmFBZ6b4Ya76GVunTanVYhtC
memory: 
     quota:     50.88 KiB    used:     3.365 KiB  

net bandwidth: 
     staked:        100.0000 EOS           (total stake delegated from account to self)
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:                 0 bytes
     available:        18.78 MiB  
     limit:            18.78 MiB  

cpu bandwidth:
     staked:        100.0000 EOS           (total stake delegated from account to self)
     delegated:       0.0000 EOS           (total staked delegated to account from others)
     used:                 0 us   
     available:        3.751 sec  
     limit:            3.751 sec  

EOS balances: 
     liquid:        10000.0000 EOS
     staked:          200.0000 EOS
     unstaking:         0.0000 EOS
     total:         10200.0000 EOS

producers:     <not voted>

对于账户抵押分为stakeddelegated

  • staked:自己给自己抵押,或者别人给你抵押时移交了币的所属(添加参数 --transfer
  • delegated:别人帮你抵押的,币的所属权归对方。

下面演示抵押测试

先让dappplaytestbcskillsurou NET和CPU各抵押10EOS,不转移币的所有权

root@iZj6cbx3duprxf6dasczbpZ:~# cleos -u http://167.99.91.77:8888 system delegatebw dappplaytest bcskillsurou "10 EOS" "10 EOS"
3081849ms thread-0   main.cpp:438                  create_action        ] result: {"binargs":"90b1cadec45aab49a0e9d5384607313aa08601000000000004454f5300000000a08601000000000004454f530000000000"} arg: {"code":"eosio","action":"delegatebw","args":{"from":"dappplaytest","receiver":"bcskillsurou","stake_net_quantity":"10.0000 EOS","stake_cpu_quantity":"10.0000 EOS","transfer":false}} 
executed transaction: ff8d82345bfb095bec396f258c62ee5c244577828993da26abb94e7deff5deb1  144 bytes  3307 us
#         eosio <= eosio::delegatebw            {"from":"dappplaytest","receiver":"bcskillsurou","stake_net_quantity":"10.0000 EOS","stake_cpu_quant...
#   eosio.token <= eosio.token::transfer        {"from":"dappplaytest","to":"eosio.stake","quantity":"20.0000 EOS","memo":"stake bandwidth"}
#  dappplaytest <= eosio.token::transfer        {"from":"dappplaytest","to":"eosio.stake","quantity":"20.0000 EOS","memo":"stake bandwidth"}
#   eosio.stake <= eosio.token::transfer        {"from":"dappplaytest","to":"eosio.stake","quantity":"20.0000 EOS","memo":"stake bandwidth"}

查看bcskillsurou账户信息

root@iZj6cbx3duprxf6dasczbpZ:~# cleos -u http://167.99.91.77:8888 get account bcskillsurou
permissions: 
     owner     1:    1 EOS8127zAiVVtPMJoiXyX8oLiReNaj32VBsJXKuTLkJsQyubKcSt7
        active     1:    1 EOS6Zfj72f6kBVZQrbkcCCGhWGnUfkmFBZ6b4Ya76GVunTanVYhtC
memory: 
     quota:     50.88 KiB    used:     3.365 KiB  

net bandwidth: 
     staked:        100.0000 EOS           (total stake delegated from account to self)
     delegated:      10.0000 EOS           (total staked delegated to account from others)
     used:                 0 bytes
     available:        20.66 MiB  
     limit:            20.66 MiB  

cpu bandwidth:
     staked:        100.0000 EOS           (total stake delegated from account to self)
     delegated:      10.0000 EOS           (total staked delegated to account from others)
     used:                 0 us   
     available:        4.126 sec  
     limit:            4.126 sec  

EOS balances: 
     liquid:        10000.0000 EOS
     staked:          200.0000 EOS
     unstaking:         0.0000 EOS
     total:         10200.0000 EOS

NET和CPU的delegated 都显示了新转的10EOS
查看EOSpark,显示的是staked + delegated总抵押,并没有区分。

下面演示取消抵押测试

对于delegated只能抵押方撤回抵押

root@iZj6cbx3duprxf6dasczbpZ:~# cleos -u http://167.99.91.77:8888 system undelegatebw dappplaytest bcskillsurou "2 EOS" "2 EOS"
3549315ms thread-0   main.cpp:438                  create_action        ] result: {"binargs":"90b1cadec45aab49a0e9d5384607313a204e00000000000004454f5300000000204e00000000000004454f5300000000"} arg: {"code":"eosio","action":"undelegatebw","args":{"from":"dappplaytest","receiver":"bcskillsurou","unstake_net_quantity":"2.0000 EOS","unstake_cpu_quantity":"2.0000 EOS"}} 
executed transaction: db39983263b7e81f758360d62a1e62fa605d7f14abee288def78996e160f0e28  184 bytes  2437 us
#         eosio <= eosio::undelegatebw          {"from":"dappplaytest","receiver":"bcskillsurou","unstake_net_quantity":"2.0000 EOS","unstake_cpu_qu...

查看bcskillsurou账户信息

root@iZj6cbx3duprxf6dasczbpZ:~# cleos -u http://167.99.91.77:8888 get account bcskillsurou
permissions: 
     owner     1:    1 EOS8127zAiVVtPMJoiXyX8oLiReNaj32VBsJXKuTLkJsQyubKcSt7
        active     1:    1 EOS6Zfj72f6kBVZQrbkcCCGhWGnUfkmFBZ6b4Ya76GVunTanVYhtC
memory: 
     quota:     50.88 KiB    used:     3.365 KiB  

net bandwidth: 
     staked:        100.0000 EOS           (total stake delegated from account to self)
     delegated:       8.0000 EOS           (total staked delegated to account from others)
     used:                 0 bytes
     available:        20.28 MiB  
     limit:            20.28 MiB  

cpu bandwidth:
     staked:        100.0000 EOS           (total stake delegated from account to self)
     delegated:       8.0000 EOS           (total staked delegated to account from others)
     used:                 0 us   
     available:        4.051 sec  
     limit:            4.051 sec  

EOS balances: 
     liquid:        10000.0000 EOS
     staked:          200.0000 EOS
     unstaking:         0.0000 EOS
     total:         10200.0000 EOS

producers:     <not voted>

CPU和NET delegated 都减少了2EOS

对于staked自己撤回抵押

root@iZj6cbx3duprxf6dasczbpZ:~# cleos -u http://167.99.91.77:8888 system undelegatebw bcskillsurou bcskillsurou "2 EOS" "2 EOS"
271082ms thread-0   main.cpp:438                  create_action        ] result: {"binargs":"a0e9d5384607313aa0e9d5384607313a204e00000000000004454f5300000000204e00000000000004454f5300000000"} arg: {"code":"eosio","action":"undelegatebw","args":{"from":"bcskillsurou","receiver":"bcskillsurou","unstake_net_quantity":"2.0000 EOS","unstake_cpu_quantity":"2.0000 EOS"}} 
executed transaction: f5f05f18655f4ded25ad8f8d56b6b3b0dfc340c6fd017019e9ca05612149554a  184 bytes  2209 us
#         eosio <= eosio::undelegatebw          {"from":"bcskillsurou","receiver":"bcskillsurou","unstake_net_quantity":"2.0000 EOS","unstake_cpu_qu...

查看bcskillsurou账户信息

root@iZj6cbx3duprxf6dasczbpZ:~# cleos -u http://167.99.91.77:8888 get account bcskillsurou
permissions: 
     owner     1:    1 EOS8127zAiVVtPMJoiXyX8oLiReNaj32VBsJXKuTLkJsQyubKcSt7
        active     1:    1 EOS6Zfj72f6kBVZQrbkcCCGhWGnUfkmFBZ6b4Ya76GVunTanVYhtC
memory: 
     quota:     50.88 KiB    used:     3.951 KiB  

net bandwidth: 
     staked:         98.0000 EOS           (total stake delegated from account to self)
     delegated:       8.0000 EOS           (total staked delegated to account from others)
     used:               185 bytes
     available:        19.91 MiB  
     limit:            19.91 MiB  

cpu bandwidth:
     staked:         98.0000 EOS           (total stake delegated from account to self)
     delegated:       8.0000 EOS           (total staked delegated to account from others)
     used:             5.182 ms   
     available:        3.971 sec  
     limit:            3.976 sec  

unstaking tokens:
     time of unstake request:  2018-08-02T06:04:32 (funds will be available in 71.99 hr)
     from net bandwidth:              2.0000 EOS
     from cpu bandwidth:              2.0000 EOS
     total:                           4.0000 EOS

EOS balances: 
     liquid:        10000.0000 EOS
     staked:          196.0000 EOS
     unstaking:         4.0000 EOS
     total:         10200.0000 EOS

producers:     <not voted>

CPU和NET staked 都减少了2EOS

总结

获取当前账号下NET或CPU抵押数 分别等于staked + delegated
获取当前账号下NET或CPU赎回数和赎回时的当前时间(只显示最后一次发起时间)查看 unstaking tokens字段 (3天后完成)

RPC返回数据
{
    "account_name": "bcskillsurou", 
    "head_block_num": 7878445, 
    "head_block_time": "2018-08-02T06:18:01.500", 
    "privileged": false, 
    "last_code_update": "1970-01-01T00:00:00.000", 
    "created": "2018-08-02T04:25:06.500", 
    "core_liquid_balance": "10000.0000 EOS", 
    "ram_quota": 52098, 
    "net_weight": 1060000, 
    "cpu_weight": 1060000, 
    "net_limit": {
        "used": 185, 
        "available": 20873227, 
        "max": 20873412
    }, 
    "cpu_limit": {
        "used": 5182, 
        "available": 3970991, 
        "max": 3976173
    }, 
    "ram_usage": 4046, 
    "permissions": [
        {
            "perm_name": "active", 
            "parent": "owner", 
            "required_auth": {
                "threshold": 1, 
                "keys": [
                    {
                        "key": "EOS6Zfj72f6kBVZQrbkcCCGhWGnUfkmFBZ6b4Ya76GVunTanVYhtC", 
                        "weight": 1
                    }
                ], 
                "accounts": [ ], 
                "waits": [ ]
            }
        }, 
        {
            "perm_name": "owner", 
            "parent": "", 
            "required_auth": {
                "threshold": 1, 
                "keys": [
                    {
                        "key": "EOS8127zAiVVtPMJoiXyX8oLiReNaj32VBsJXKuTLkJsQyubKcSt7", 
                        "weight": 1
                    }
                ], 
                "accounts": [ ], 
                "waits": [ ]
            }
        }
    ], 
    "total_resources": {
        "owner": "bcskillsurou", 
        "net_weight": "106.0000 EOS", 
        "cpu_weight": "106.0000 EOS", 
        "ram_bytes": 52098
    }, 
    "self_delegated_bandwidth": {
        "from": "bcskillsurou", 
        "to": "bcskillsurou", 
        "net_weight": "98.0000 EOS", 
        "cpu_weight": "98.0000 EOS"
    }, 
    "refund_request": {
        "owner": "bcskillsurou", 
        "request_time": "2018-08-02T06:04:32", 
        "net_amount": "2.0000 EOS", 
        "cpu_amount": "2.0000 EOS"
    }, 
    "voter_info": {
        "owner": "bcskillsurou", 
        "proxy": "", 
        "producers": [ ], 
        "staked": 1960000, 
        "last_vote_weight": "0.00000000000000000", 
        "proxied_vote_weight": "0.00000000000000000", 
        "is_proxy": 0
    }
}

相对于cleos Rpc返回的数据要清晰一些

  • CPU或NET的总抵押等于cleos中的staked + delegated
    total_resources["net_weight"] NET的总抵押,
    total_resources["cpu_weight"] CPU的总抵押
  • CPU或者NET属于自己的抵押,等于cleos中的staked
    self_delegated_bandwidth["net_weight"] NET的自己抵押,
    self_delegated_bandwidth["cpu_weight"] CPU的自己抵押
  • 赎回信息
    refund_request["request_time"] 最后发起的赎回时间,3天后抵押EOS到账。
    refund_request["net_amount"] NET总赎回
    refund_request["cpu_amount"] CPU总赎回

    参考

    测试网测试方法参考(EOSpark EOS浏览器 + EOS Jungle Testnet

EOSpark EOS浏览器 + EOS Jungle Testnet

测试EOS交易等数据时,必不可少的需要一款EOS浏览器方便查看相关的数据。
目前我没有全方面的测试,只测过4-5款,目前来说EOSpark从数据展示,交互,响应速度都能满足我的需求,大家有什么推荐的可以留言。
本文主要讲的是,EOSpark支持EOS Jungle Testnet测试网,在测试网中,可以免费领取大量EOS测试币(6h/10000),方便测试中不必要的消耗。

下面演示如果在EOS Jungle Testnet测试网中创建账号,申请EOS测试币,并在EOSpark浏览器显示账号信息。

  • 打开EOS Jungle Testnet网站(点击打开)此时版本 V0.5.7
  • 点击 Create Account,输入账户名,owner和active的公钥,点击创建
  • 点击Faucet获取EOS测试币

在EOSpark浏览器中查看账号

  • 打开EOSpark浏览器官网(点击打开),切换Jungle Testnet测试网络
  • 查看刚才在Jungle Testnet测试网络中新建的bcskillsurou账号

cleos get account

root@iZj6cbx3duprxf6dasczbpZ:~# cleos -u http://167.99.91.77:8888 get account bcskillsurou
permissions: 
     owner     1:    1 EOS8127zAiVVtPMJoiXyX8oLiReNaj32VBsJXKuTLkJsQyubKcSt7
        active     1:    1 EOS6Zfj72f6kBVZQrbkcCCGhWGnUfkmFBZ6b4Ya76GVunTanVYhtC
memory: 
     quota:     50.88 KiB    used:     3.365 KiB  

    net bandwidth: 
         staked:        100.0000 EOS           (total stake delegated from account to self)
         delegated:       0.0000 EOS           (total staked delegated to account from others)
         used:                 0 bytes
         available:        18.78 MiB  
         limit:            18.78 MiB  

    cpu bandwidth:
         staked:        100.0000 EOS           (total stake delegated from account to self)
         delegated:       0.0000 EOS           (total staked delegated to account from others)
         used:                 0 us   
         available:        3.751 sec  
         limit:            3.751 sec  

    EOS balances: 
         liquid:        10000.0000 EOS
         staked:          200.0000 EOS
         unstaking:         0.0000 EOS
         total:         10200.0000 EOS

    producers:     <not voted>

使用CLion查看EOS代码

本文将会介绍如何使用CLion查看EOS源码。

EOS的智能合约基于C++开发,官方在github上提供了很多合约样例,地址: https://github.com/EOSIO/eos/tree/master/contracts

直接查看这些样例的源码无疑是学习合约开发最快速的方法,这时我们需要一个合适的IDE工具,尤其是IDE的代码追查功能,能够大大提高阅读源码的效率,在这推荐大家使用CLion。

Clion简介

CLion是一款专为开发C及C++所设计的跨平台IDE。由大名鼎鼎的JetBrains公司开发,以IntelliJ为基础设计,支持Linux、macOS及Windows平台。

下载地址:https://www.jetbrains.com/clion/download/

CLion需要收费,初次下载有30天的免费试用期。

编译EOS源码

在使用CLion之前,需要先编译eos源码,这样才能在导入源码之后使用IDE的代码追查功能。
eos提供了自动编译脚本,一键傻瓜式操作。下载源码后,进入eos目录,运行自动编译脚本。

cd eos
./eosio_build.sh

编译过程中会根据当前系统环境,下载相关依赖,以MacOS为例,有如下依赖。

如果这些依赖不存在,会自动下载并且安装,编译成功后会显示下面的内容。

安装生成文件

cd build
sudo make install

导入EOS源码到CLion
启动CLion,选择Import Project from Sources导入eos目录中的内容

导入时,会提示eos目录下已有CMakeList.txt文件,点击 Open Project按钮。

导入后,CLion需要根据CmakeLists.txt文件编译整个项目,在CMake窗口会显示当前的编译进度,如果编译完成后,能够如下图显示[Finished]并且没有报任何错误,那么恭喜你导入成功,接下来就可以愉快的阅读源码了。

常见问题

系统:MacOS 10.13.3

  • Failed to find Gettext libintl (missing: Intl_INCLUDE_DIR)
    解决方案
    brew unlink gettext && brew link --force gettext
    find /usr -name libintl* -print 2>/dev/null
  • Unable to find the requested Boost libraries
    错误信息:
    Unable to find the requested Boost libraries.
    Unable to find the Boost header files.  Please set BOOST_ROOT to the root
    directory containing Boost or BOOST_INCLUDEDIR to the directory containing Boost's headers.

    解决方案

    brew uninstall boost
    brew install boost

    进入CLion,重新编译项目。在项目名称上右键,选择“Reload CMake Project”

  • Could not find a package configuration file provided by “LLVM”.
    错误信息:
    Could not find a package configuration file provided by "LLVM" (requested version 4.0) with any of the following names:
    LLVMConfig.cmake
    llvm-config.cmake
    Add the installation prefix of "LLVM" to CMAKE_PREFIX_PATH or set "LLVM_DIR" to a directory containing one of the above files.  If "LLVM" provides a separate development package or SDK, be sure it has been installed.

    解决方案: 首先确保已经安装了llvm,通过下面的命令确认llvm版本信息。

    brew info llvm

    如果已经正确安装,打开CLion的设置页面(command + ,),找到cmake的环境配置。

    在Environment中指定LLVM_DIR,对应llvm的本地路径,例如: LLVM_DIR=/usr/local/Cellar/llvm/4.0.1/lib/cmake/llvm
    设置完成后重新编译项目。
    转载自:Tao's Blog