Logo

Query ID

Using Query ID for Better Control on Process

When user send a request through calling bridge_query() function, a unique query id is dedicated to the specific request. The query id identifies the specific request which is returned to user's smart contract as a parameter of __callback() function.

​ It is recommended that smart contract developers use specified query id as following example in order to ensure that each query response is processed only once and abusage of smart contract logic is avoided.

pragma solidity ^0.5.9;
import "./BridgePublicAPI.sol";
contract TRXprice is BridgePublicAPI {
event price(string TRXprice);
constructor() public {
send_query("json(https://api.kraken.com/0/public/Ticker?pair=TRXUSD).result.TRXUSD.a.0");
}
string public TRXUSD;
mapping (bytes32 => bool) public pendingQueries;
function send_query(string memory arg) public {
bytes32 queryId = bridge_query("URL", arg);
pendingQueries[queryId] = true;
}
function __callback(bytes32 _myid, string memory _result) public {
require(msg.sender == oracle_cbAddress());
require(pendingQueries[_myid] == true);
TRXUSD = _result;
emit price(TRXUSD);
delete pendingQueries[_myid];
}
}

​ Also it should be noted that query id can be used to behave differently into the __callback() method for each specific query id in cases that more than one pending callback result is expected to be injected by Bridge oracle into user's smart contract.

pragma solidity ^0.5.9;
import "./BridgePublicAPI.sol";
contract TRXprice is BridgePublicAPI {
event result(string _result);
mapping (bytes32 => bool) public pendingQueries;
bytes32 public BTCid;
bytes32 public TRXid;
function send_query1() public {
BTCid = bridge_query("URL", "json(https://api.pro.coinbase.com/products/BTC-USD/ticker).price");
pendingQueries[BTCid] = true;
}
function send_query2() public {
TRXid = bridge_query("URL", "json(https://api.kraken.com/0/public/Ticker?pair=TRXUSD).result.TRXUSD.a.0");
pendingQueries[TRXid] = true;
}
function __callback(bytes32 _myid, string memory _result) public {
require(msg.sender == oracle_cbAddress());
if(_myid == BTCid) {
require(pendingQueries[BTCid] == true);
emit result(_result);
delete pendingQueries[BTCid];
}else if (_myid == TRXid) {
require(pendingQueries[TRXid] == true);
emit result("hello world");
delete pendingQueries[TRXid];
}
}
}