Search code examples
nearprotocol

What to get the actual number using call_function in jsonrpc near protocol?


The following query using call_function in jsonrpc in near protocol

http post https://rpc.testnet.near.org jsonrpc=2.0 id=test method=query   params:='{
    "request_type": "call_function",
    "finality": "final",
    "account_id": "dev-1591261827342",
    "method_name": "get_total_supply",
    "args_base64": "e30="
  }'

gives the following result:

{
    "id": "test",
    "jsonrpc": "2.0",
    "result": {
        "block_hash": "FrKNvsEbqPsdT1ijLkUBNoX3SnUQbTCXjoPj7yC2WW5i",
        "block_height": 9616038,
        "logs": [],
        "result": [
            34,
            49,
            48,
            48,
            48,
            48,
            48,
            48,
            48,
            48,
            48,
            48,
            48,
            48,
            48,
            48,
            48,
            34
        ]
    }
}

How to convert the result to the actual number which is '1000000000000000'?


Solution

  • "result": [
                34,
                49,
                48,
                48,
                48,
                48,
                48,
                48,
                48,
                48,
                48,
                48,
                48,
                48,
                48,
                48,
                48,
                34
            ]
    

    is an array of bytes. NEAR SDKs use JSON encoding for input and output by default, but not limited by that, so if you convert it, you get "1000000000000000". Here is the Python snippet to convert it:

    >>> result = [34, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 34]
    >>> ''.join(chr(x) for x in result)
    '"1000000000000000"'