Get responses from Oracle

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?

Both the query and the response is right there in the oracle object…

7> aeser_api_encoder:decode(<<"ov_aUhvbGFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhx2NsBw==">>).
{oracle_query,<<"iHolaaaaaaaaaaaaaaaaaaaaaaa">>}  
8> aeser_api_encoder:decode(<<"or_Xfbg4g==">>).                                              
{oracle_response,<<>>}   

But I don’t know why you didn’t get the response you expected… I’ve never played with JavaScript.

1 Like

Yes, everything is right, but I really dont know how to decode The query response through The npm package of The sdk

Since this now is a question not for Oracles but for the JS SDK, I summon @bruteforce.chain

Hey Gabriel,

You can check an example here: waellet-oracle-service/index.js at 9fb72c0246a32d10b14be6a19449baddfd18d489 · waellet/waellet-oracle-service · GitHub and here: tipping-oracle-service/oracleService.js at 11b66432f60aa51be6885387e1ea74b37c3a1e1b · aeternity/tipping-oracle-service · GitHub

The sdk gives you a decode function.

1 Like