8.3 调用合约和查询合约业务

  • 调用 查询合约constant方法 接口,查询HelloWorld的info值。根据合约查询编码的方法,计算得info的data为 0x370158ea
  1. curl -k -X POST --data '{"jsonrpc":"2.0","method":"eth_call","params":[{"from":"0x7eff122b94897ea5b0e2a9abf47b86337fafebdc","to":"0xd5b0df861803a07f330868104eec92ebdcce4c79","data":"0x370158ea"}, "latest"],"id":1}' https://sandbox-blockchain.xunlei.com/call
  2. {"jsonrpc":"2.0","id":1,"result":"0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"}
  3. // AbiCoder.decode解码得值为空

初始化函数时,info值为空。

  1. 0xfe6b37830000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000
  • 请求 查询预估gas消耗 接口,查询saySomething 函数执行的预估gas值。建议传入执行调用函数的gas值在查询的gas值基础上加上一些,以防出现out-of-gas的情况。
  1. curl -k -XPOST --data '{"jsonrpc":"2.0", "method":"eth_estimateGas", "params":[{"from":"0x7eff122b94897ea5b0e2a9abf47b86337fafebdc","to":"0xd5b0df861803a07f330868104eec92ebdcce4c79", "data":"0xfe6b37830000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000","value":""}], "id":1}' -H 'content-type:application/json' https://sandbox-blockchain.xunlei.com/estimateGas
  2. // 返回值
  3. {"jsonrpc":"2.0","id":1,"result":"0xaebc"}

将上述结果转10进制得:

  1. 0xaebc.toString('10')
  2. // "44732"

在传入到下一步的执行接口时,将在此值的基础上加上10000,最终传入的gas数量为54732。

  • 请求接口 生成扫码支付的URL信息 执行合约的 saySomething 函数。请求参数中的to地址为合约地址0xd5b0df861803a07f330868104eec92ebdcce4c79,data为前面生成的data,gas_limit为上一步的54732。计算sign md5(email=xxx&to=xxx&value=xxx&secret=xxx)。返回 data 的里 url 即为迅雷链助手app扫描二维码需要的地址。链接具有时效性,默认30分钟。
  1. {
  2. "code": 0,
  3. "data": {
  4. "url": "http://fe-blockchain.xunlei.com/#/?action=https%3A%2F%2Fsandbox-blockchain.xunlei.com%2Fapi%2Flinktest%2Ftx_info%2Ff4ebcdf4d756f0d8041c82d8e32e912a",
  5. "expire": 1800000
  6. },
  7. "msg": ""
  8. }
  • 根据上一步的url,使用二维码生成工具,生成一个二维码。然后使用测试版迅雷链助手app扫描。迅雷链助手将调用合约执行页面,用户选择执行合约使用的账户,输入对应的密码。等待交易确认后,交易记录里可以看到合约执行结果。

  • 再次调用 查询合约constant方法 接口,查询HelloWorld的info值。

  1. curl -k -X POST --data '{"jsonrpc":"2.0","method":"eth_call","params":[{"from":"0x7eff122b94897ea5b0e2a9abf47b86337fafebdc","to":"0xd5b0df861803a07f330868104eec92ebdcce4c79","data":"0x370158ea"}, "latest"],"id":1}' https://sandbox-blockchain.xunlei.com/call
  2. {"jsonrpc":"2.0","id":1,"result":"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000"}
  3. // AbiCoder.decode解码得值为 Hello World,调用成功