Go-Ethereum
如何從單個合約呼叫多個 oracalisized 查詢
我想從同一個合約中呼叫 2 個不同的 orcalized 查詢。我看到了一些文章,但沒有一個答案有效。我們有什麼辦法可以做到嗎?這是我的契約。我期待這份契約同時記錄兩者
11111111
,222222222222222
但它列印錯誤VM Exception: invalid opcode
我正在使用http://dapps.oraclize.it/。當我呼叫單個查詢時,它有效,但不是 2 個查詢。pragma solidity ^0.4.0; import "github.com/oraclize/ethereum-api/oraclizeAPI.sol"; import "github.com/Arachnid/solidity-stringutils/strings.sol"; contract verifySettlement is usingOraclize { uint public price; string public landingtime; string public expectedtime;` string public tempvalue; mapping(bytes32 => uint) queries; event Log(string text); function verifySettlement() payable { Log("Contract created."); getActualFlightDetails(); getExpectedFlightDetails(); } function getActualLandingHour() constant returns (string) { return landingtime; } function getExpectedLandingHour() constant returns (string) { return expectedtime; } function __callback(bytes32 _myid, string _result) { require (msg.sender == oraclize_cbAddress()); if(queries[_myid] == 1) { Log("1111111111111111111111111"); } if(queries[_myid] == 2) { Log("2222222222222222222222222222222"); } //Log(_result); //tempvalue = _result; } function getActualFlightDetails() payable { Log("Oraclize query was sent, waiting for the answer for getting actual flight details.."); queries[oraclize_query("URL","http://169.53.241.139:5000/actual/flight/1")]=1; //queries[oraclize_query("URL","http://169.53.241.139:5000/expected/flight/1")]=2; } function getExpectedFlightDetails() payable { Log("Oraclize query was sent, waiting for the answer for getting actual flight details.."); //queries[oraclize_query("URL","http://169.53.241.139:5000/expected/flight/1")]=1; queries[oraclize_query("URL","http://169.53.241.139:5000/expected/flight/1")]=2; } }
中的第一個參數將
id
在__callback
同一個回調函式中處理不同的請求。contract verifySettlement is usingOraclize { uint public price; string public landingtime; string public expectedtime;` string public tempvalue; event Log(string text); enum oraclizeState { ForActual, ForExpected } struct oraclizeCallback { oraclizeState oState; } mapping (bytes32 => oraclizeCallback) public oraclizeCallbacks; function verifySettlement() payable { Log("Contract created."); getActualFlightDetails(); getExpectedFlightDetails(); } function getActualLandingHour() constant returns (string) { return landingtime; } function getExpectedLandingHour() constant returns (string) { return expectedtime; } function __callback(bytes32 _myid, string _result) { require (msg.sender == oraclize_cbAddress()); oraclizeCallback memory o = oraclizeCallbacks[myid]; if (o.oState == oraclizeState.ForActual) { Log("1111111111111111111111111"); } else if(o.oState == oraclizeState.Forxpected) { Log("2222222222222222222222222222222"); } } function getActualFlightDetails() payable { Log("Oraclize query was sent, waiting for the answer for getting actual flight details.."); bytes32 queryId=oraclize_query("URL","http://169.53.241.139:5000/actual/flight/1"); oraclizeCallbacks[queryId] = oraclizeCallback(oraclizeState.ForActual); } function getExpectedFlightDetails() payable { Log("Oraclize query was sent, waiting for the answer for getting actual flight details.."); bytes32 queryId=oraclize_query("URL","http://169.53.241.139:5000/expected/flight/1"); oraclizeCallbacks[queryId] = oraclizeCallback(oraclizeState.ForExpected); } }
為了跟踪不同的查詢,我們使用 struct 來保存和定義 queryid 和查詢狀態之間的映射。