如何用Oraclize 寫跨鏈的應用

來源:互聯網
上載者:User

Question:

so I have a contract deployed on Ropsten and now I'm trying to access the public variable using a JSON RPC call through Infura and Oraclize.

Here's my contract: contract test_contract { uint public last_a;

function double(uint a) public returns(uint) {   last_a = a;  return 2*a;    } function getLastA() public constant returns(uint){     return last_a; } function getMethod() public constant returns(bytes4){     return bytes4(keccak256("getLastA())")); }}

Ok. So I get the method getLastA using the getMethod, pad it to 32, get the contract address on Ropsten...and now this is my call:

url post - json(https://ropsten.infura.io/).result params -  {"jsonrpc":"2.0","id":3,"method":"eth_call","params":[{"to":"0x76a83b371ab7232706eac16edf2b726f3a2dbe82","data":"4b9a1598000000000000000000000000000000000000000000000000000000000000000"], "latest"}

You can see my oraclize query here

Just wondering if I'm doing something wrong or if this is even possible. The value it should return is 17 if anyone else thinks they have an answer.

Answer: 

There were a few things wrong with your HTTP request: The JSON was malformed due to a missing curly brace and a mis-placed square bracket. The data field had the wrong function selector in it. The data field was missing the leading 0x. The data field was the wrong length. Although there's no need to pad with zeros, you can do that as long as you get the length right. You had an odd number of digits (71).

Here's a valid JSON payload:

{    "id": 3,    "jsonrpc": "2.0",    "method": "eth_call",    "params": [        {            "data": "0xad3b80a8",            "to": "0x76a83b371ab7232706eac16edf2b726f3a2dbe82"        },        "latest"    ]}

POSTing this to https://ropsten.infura.io yields a result of 0x0000000000000000000000000000000000000000000000000000000000000011 (17 in decimal).

Here's a working Oraclize query: http://app.oraclize.it/home/test_query#VVJMKFBPU1Qp:anNvbihodHRwczovL3JvcHN0ZW4uaW5mdXJhLmlvLykucmVzdWx0:IHsianNvbnJwYyI6IjIuMCIsImlkIjozLCJtZXRob2QiOiJldGhfY2FsbCIsInBhcmFtcyI6W3sidG8iOiIweDc2YTgzYjM3MWFiNzIzMjcwNmVhYzE2ZWRmMmI3MjZmM2EyZGJlODIiLCJkYXRhIjoiMHhhZDNiODBhOCJ9LCAibGF0ZXN0Il19.

Out of curiosity, what's the reason for using Oraclize rather than just calling the contract directly? Are you doing cross-chain communication between contracts? wow...you rock man. And good job with the guess. Just playing around with using oraclize to bridge two chains –  thefett  13 hours ago And just curious, does my 'getMethod' not get me the proper function selector? –  thefett  13 hours ago Just spotted the issue. You have an extra parenthesis in the string you're hashing. By the way, I think you can just do return this.getLastA.selector. –  smarx  13 hours ago Figures..and that is a much easier way. Thanks for the help! –  thefett  13 hours ago https://ethereum.stackexchange.com/questions/42973/eth-call-through-oraclize

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.