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)
      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})

  stateful entrypoint respond_to_greets() =
    [ respond_to_greet(caller_addr, query) | (caller_addr, query) <- state.greets ]

  entrypoint get_oracle(): oracle(string, string) =

  entrypoint get_balance() : int =

  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"
      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) => {
     const response = await this.client.pollForQueryResponse(this.oracleId, query[0].id);
     console.log('Response:', response);
} catch (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==">>).
8> aeser_api_encoder:decode(<<"or_Xfbg4g==">>).                                              

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 @milenradkov.chain

Hey Gabriel,

You can check an example here: and here:

The sdk gives you a decode function.

1 Like