Zondax / namadexer

Namada indexer
39 stars 57 forks source link

Endpoint to retrieve the current token balance #201

Closed cryptosystems6300 closed 4 months ago

cryptosystems6300 commented 7 months ago

Reading the balance from the node is a hassle. Apparently the only way to do it is by making this call:

To retrieve the balance for tnam1qph60hh43apm3pvw6fk6cnzjaw5g2l3mrcdu9f9p this call must be made:

curl --location 'https://namada-testnet-rpc.bwarelabs.com' \
--header 'Content-Type: application/json' \
--data '{
  "jsonrpc": "2.0",
  "id": "0916acdb-6b3b-4463-9714-6232262a9c92",
  "method": "abci_query",
  "params": {
    "path": "/shell/value/#tnam1pyqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqej6juv/#tnam1qxvg64psvhwumv3mwrrjfcz0h3t3274hwggyzcee/balance/#tnam1qph60hh43apm3pvw6fk6cnzjaw5g2l3mrcdu9f9p",
    "data": "",
    "height":"0",
    "prove": false
  }
}'

Response:

{
  "jsonrpc": "2.0",
  "id": "0916acdb-6b3b-4463-9714-6232262a9c92",
  "result": {
    "response": {
      "code": 0,
      "log": "",
      "info": "",
      "index": "0",
      "key": null,
      "value": "enTDL4kAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
      "proofOps": null,
      "height": "1",
      "codespace": ""
    }
  }
}

I have learned that the response.result.value must be deserialized with base64 with borsh into a request specific object (here it would an Amount object).

Ideally, this complexity is abstracted away and easily accessible through the indexer.

:link: zboto Link

rllola commented 7 months ago

We can do that. There is two options : we proxy the request (but I would rather avoid having the server to have to be aware of the node) or we recalculate the balance using the transactions collected.

joel-u410 commented 6 months ago

I like the idea of recalculating the balance using the collected transactions. This is also in line with something that I would like to add at some point: an endpoint for reconciling the balance from the database vs. querying the node.

I've poked around the schema a bit and it's not immediately clear to me how one would fully calculate the balance for an address. Not all of the different transaction views provide an amount column (e.g. tx_ibc) although, perhaps that's not relevant if we limit the scope to native token only.

Also, how do shielded transactions factor in? Are those simply excluded from the reported balance?

cryptosystems6300 commented 6 months ago

To calculate the balance, we must not forget about the tx fees on all transaction types...

rllola commented 6 months ago

Lets go with option 2 then.

It is not entirely clear for me how to do it neither but I am confident it is doable. We will need the amounts of the different transactions and the fees like mentioned.

Shielded transactions and their amount would be excluded from the final amount because there is no way to tell if they are still owned by the address or not (I think that was the point of it).

cryptosystems6300 commented 6 months ago

Actually, there might be no way around from getting the balance from the node directly since some balances cannot be calculated based on transactions. E.g. the reward balance continuously increases over time without an actual on-chain transaction. So I would rather prefer having an easy way to query the balance directly towards the node (or at least some of the balances). Here is a list of balances that I know of that are of interest:

Example of available balances for a cosmos chain: https://celestia.explorers.guru/account/celestia1uvytvhunccudw8fzaxvsrumec53nawyjqw83yr

joel-u410 commented 6 months ago

Do Namada rewards follow the same mechanisms as cosmos chains? i.e. continuously (& silently) updating unclaimed rewards & commissions balances? My understanding is that Namada uses tendermint, but not cosmos SDK, and all the rewards functionality of networks such as celestia is provided by cosmos SDK not tendermint.

I think, even the concept of how balance state is kept, i.e. the idea of multiple types of balances like rewards & commissions, is part of cosmos SDK and thus, as far as I know, not used in Namada.

That said, I haven't yet studied how Namada handles rewards, so it's quite possible that it acts the same way, where rewards accrue without any transactions.

ainhoa-a commented 4 months ago

This indexer is no longer being maintained by request of heliax/namada team and it will be archived soon.