smartcontractkit / near-protocol-contracts

MIT License
26 stars 8 forks source link

Address 11 and 12: improve json for requests, add get_all_requests function #13

Closed mikedotexe closed 4 years ago

mikedotexe commented 4 years ago

fixes #12

Thanks for noticing that, @krebernisak. It's not often that I am using the plain curl commands or another language to hit the RPC directly. You're right the JSON was quite messy. I have these changes deployed to oracle.o.mike.testnet

Here are two curl commands that should give the new output.

curl -d '{"jsonrpc": "2.0", "method": "query", "id": "chainlink", "params": {"request_type": "call_function", "finality": "final", "account_id": "oracle.o.mike.testnet", "method_name": "get_requests_summary", "args_base64": "eyJtYXhfbnVtX2FjY291bnRzIjogIjEwIn0="}}' -H 'Content-Type: application/json' https://rpc.testnet.near.org

This will return: {"jsonrpc":"2.0","result":{"result":[91,123,34,97,99,99,111,117,110,116,34,58,34,99,108,105,101,110,116,46,111,46,109,105,107,101,46,116,101,115,116,110,101,116,34,44,34,116,111,116,97,108,95,114,101,113,117,101,115,116,115,34,58,49,125,93],"logs":[],"block_height":9742421,"block_hash":"GfFdv3ntXK5AgYZ25jrQvr91tKxtpVVTrV6JBUL51HC3"},"id":"chainlink"}

Inside the result key is: [91,123,34,97,99,99,111,117,110,116,34,58,34,99,108,105,101,110,116,46,111,46,109,105,107,101,46,116,101,115,116,110,101,116,34,44,34,116,111,116,97,108,95,114,101,113,117,101,115,116,115,34,58,49,125,93]

One way to easily decode this is to open the python repl.

python
(see >>>)
res = [91,123,34,97,99,99,111,117,110,116,34,58,34,99,108,105,101,110,116,46,111,46,109,105,107,101,46,116,101,115,116,110,101,116,34,44,34,116,111,116,97,108,95,114,101,113,117,101,115,116,115,34,58,49,125,93]
''.join(chr(x) for x in res)

which returns: '[{"account":"client.o.mike.testnet","total_requests":1}]'

Note: there is still the opening and closing single quotes.

The same process can be followed for the get_requests command as well. (I will still implement the other method that shows all requests for all users, by the way, in another PR):

curl -d '{"jsonrpc": "2.0", "method": "query", "id": "chainlink", "params": {"request_type": "call_function", "finality": "final", "account_id": "oracle.o.mike.testnet", "method_name": "get_requests", "args_base64": "eyJhY2NvdW50IjogImNsaWVudC5vLm1pa2UudGVzdG5ldCIsICJtYXhfcmVxdWVzdHMiOiAiMTAifQ=="}}' -H 'Content-Type: application/json' https://rpc.testnet.near.org -v


Update: I decided to use this pull request to fix #11 as well as that made the most sense.

The new function added is get_all_requests and can be called with near-shell like so:

near view oracle.o.mike.testnet get_all_requests '{"max_num_accounts": "100", "max_requests": "100"}'

and with curl:

curl -d '{"jsonrpc": "2.0", "method": "query", "id": "chainlink", "params": {"request_type": "call_function", "finality": "final", "account_id": "oracle.o.mike.testnet", "method_name": "get_all_requests", "args_base64": "eyJtYXhfbnVtX2FjY291bnRzIjogIjEwMCIsICJtYXhfcmVxdWVzdHMiOiAiMTAwIn0="}}' -H 'Content-Type: application/json' https://rpc.testnet.near.org

which ultimately results in: '{"client.o.mike.testnet":[{"nonce":"1","request":{"caller_account":"client.o.mike.testnet","request_spec":"dW5pcXVlIHNwZWMgaWQ=","callback_address":"client.o.mike.testnet","callback_method":"token_price_callback","data":"QkFU","payment":10,"expiration":1906293427246306700}},{"nonce":"2","request":{"caller_account":"client.o.mike.testnet","request_spec":"dW5pcXVlIHNwZWMgaWQ=","callback_address":"client.o.mike.testnet","callback_method":"token_price_callback","data":"QkFU","payment":10,"expiration":1906293427246306700}}],"o.mike.testnet":[{"nonce":"1","request":{"caller_account":"o.mike.testnet","request_spec":"dW5pcXVlIHNwZWMgaWQ=","callback_address":"client.o.mike.testnet","callback_method":"token_price_callback","data":"QkFU","payment":10,"expiration":1906293427246306700}},{"nonce":"3","request":{"caller_account":"o.mike.testnet","request_spec":"dW5pcXVlIHNwZWMgaWQ=","callback_address":"client.o.mike.testnet","callback_method":"token_price_callback","data":"QkFU","payment":10,"expiration":1906293427246306700}},{"nonce":"19","request":{"caller_account":"o.mike.testnet","request_spec":"dW5pcXVlIHNwZWMgaWQ=","callback_address":"client.o.mike.testnet","callback_method":"token_price_callback","data":"QkFU","payment":10,"expiration":1906293427246306700}}]}'

or put into pretty print:

{
  "client.o.mike.testnet": [
    {
      "nonce": "1",
      "request": {
        "caller_account": "client.o.mike.testnet",
        "request_spec": "dW5pcXVlIHNwZWMgaWQ=",
        "callback_address": "client.o.mike.testnet",
        "callback_method": "token_price_callback",
        "data": "QkFU",
        "payment": 10,
        "expiration": 1906293427246306800
      }
    },
    {
      "nonce": "2",
      "request": {
        "caller_account": "client.o.mike.testnet",
        "request_spec": "dW5pcXVlIHNwZWMgaWQ=",
        "callback_address": "client.o.mike.testnet",
        "callback_method": "token_price_callback",
        "data": "QkFU",
        "payment": 10,
        "expiration": 1906293427246306800
      }
    }
  ],
  "o.mike.testnet": [
    {
      "nonce": "1",
      "request": {
        "caller_account": "o.mike.testnet",
        "request_spec": "dW5pcXVlIHNwZWMgaWQ=",
        "callback_address": "client.o.mike.testnet",
        "callback_method": "token_price_callback",
        "data": "QkFU",
        "payment": 10,
        "expiration": 1906293427246306800
      }
    },
    {
      "nonce": "3",
      "request": {
        "caller_account": "o.mike.testnet",
        "request_spec": "dW5pcXVlIHNwZWMgaWQ=",
        "callback_address": "client.o.mike.testnet",
        "callback_method": "token_price_callback",
        "data": "QkFU",
        "payment": 10,
        "expiration": 1906293427246306800
      }
    },
    {
      "nonce": "19",
      "request": {
        "caller_account": "o.mike.testnet",
        "request_spec": "dW5pcXVlIHNwZWMgaWQ=",
        "callback_address": "client.o.mike.testnet",
        "callback_method": "token_price_callback",
        "data": "QkFU",
        "payment": 10,
        "expiration": 1906293427246306800
      }
    }
  ]
}

^ except for it begins and ends with a single quote, as mentioned before.

Also note that I didn't want to deploy this to oracle.oracle.testnet, which is what I believe you've been querying, Kristijan. As you can see, the above examples are for oracle.o.mike.testnet. I have the intention of getting your feedback, and ultimately deploying and updating oracle.oracle.testnet

Happy to jump on a call almost any time.