Using the Greeter Oracle example in sophia, Im trying to understand the responses. the contract is this:
contract Greeter =
record state =
{greeter_oracle : oracle(string, string),
greets: list((string * oracle_query(string, string)))}
stateful entrypoint init() : state =
let greeter_oracle : oracle(string, string) = register_oracle()
{greeter_oracle = greeter_oracle, greets = []}
payable stateful entrypoint greet_oracle(message : string) : bool =
let val = Call.value
if(val != 10)
false
else
let query : oracle_query(string, string) =
Oracle.query(state.greeter_oracle, message, 10, RelativeTTL(50), RelativeTTL(50))
let caller_address : string = Address.to_str(Call.caller)
put(state{greets @ g = (caller_address, query) :: g})
true
stateful entrypoint respond_to_greets() =
[ respond_to_greet(caller_addr, query) | (caller_addr, query) <- state.greets ]
entrypoint get_oracle(): oracle(string, string) =
state.greeter_oracle
entrypoint get_balance() : int =
Contract.balance
stateful function respond_to_greet(caller_address : string, query : oracle_query(string, string)) =
let greet : string = Oracle.get_question(state.greeter_oracle, query)
let greet_is_valid : bool = greet == "Hello" || greet == "Hi" || greet == "Greetings"
if(greet_is_valid)
let comma_space_address : string = String.concat(", ", caller_address)
let response : string = String.concat(greet, comma_space_address)
Oracle.respond(state.greeter_oracle, query, response)
stateful function register_oracle() : oracle(string, string) =
Oracle.register(Contract.address, 10, RelativeTTL(200))
with the sdk in node, Im connected to the oracle with an instance client, something like this:
try {
this.client.pollForQueries(this.oracleId, async (query) => {
console.log(query[0]);
const response = await this.client.pollForQueryResponse(this.oracleId, query[0].id);
console.log('Response:', response);
});
} catch (err) {
console.error(err);
return null;
}
The console.log of query[0] gives me something like this:
{
fee: 10,
id: 'oq_EPjD6o1jF3ParHjpWV3NJsSyksLcN2rS5Z3iy5GaKEoAtUNdJ',
oracleId: 'ok_Uo1E2Xr8QYgtSBYjj47F9sLz8ezkmJMB44np4iPcViaxSPX8m',
query: 'ov_aUhvbGFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhx2NsBw==',
response: 'or_Xfbg4g==',
responseTtl: { type: 'delta', value: 50 },
senderId: 'ak_Uo1E2Xr8QYgtSBYjj47F9sLz8ezkmJMB44np4iPcViaxSPX8m',
senderNonce: 1,
ttl: 301212
}
So I know its working, but I really dont understand the response, it is supose to say Hi, Hello or Grettings in the response, so I tried using the pollForQueryResponse function, but after 100seconds, it reject for no response
How can I know the responses from the oracle?