mukesh prajapati
mukesh prajapati

Reputation: 117

How to get all the Transactions from a block in NEAR protocol using a single call?

I want to fetch all the transaction from a block using the single rpc call. I know we can fetch using the chunk id but in that case we have to make a call for each chunk.

Upvotes: 1

Views: 1115

Answers (2)

khorolets
khorolets

Reputation: 726

Unfortunately, it's impossible to do in a single call. However, it is possible in N+1 where N is a number of shards.

  1. Request a block (by height, hash or finality - depends on your quest, lets assume you need latest)

https://docs.near.org/docs/api/rpc/block-chunk#block-details

httpie example

$ http post https://rpc.testnet.near.org/ id=dontcare jsonrpc=2.0 method=block params:='{"finality": "final"}'
  1. Collect Chunks hashes from the response. You can find them in the response JSON
{
    "id": "dontcare",
    "jsonrpc": "2.0",
    "result": {
        "author": "node1",
        "chunks": [
            {
              ...
              chunk_hash: 6ZJzhK4As3UGkyH2kxHmRFYoV7hiyXareMo1qzyxS624,

Using a jq

$ http post https://rpc.testnet.near.org/ id=dontcare jsonrpc=2.0 method=block params:='{"finality": "final"}' | jq .result.chunks[].chunk_hash
"GchAtNdcc16bKvnTa7RA3xkYAt2eMg22Qkmc9FfFTrK2"
"8P6u7zwsLvYMH5vbV4hnaCaL7FKuPnfJU4yNJY52WCd2"
"8p1XaC4BzCBVUhfYWyf6nBXF4m9uzJVEJmHCYnBMLuUn"
"7TkVTzCGMyxNnumX6ZsES5v3Wa3UnBZZAavF9zjMzDKC"
  1. You need to perform a query to get every chunk like:
$ http post https://rpc.testnet.near.org/ id=dontcare jsonrpc=2.0 method=chunk params:='{"chunk_id": "GchAtNdcc16bKvnTa7RA3xkYAt2eMg22Qkmc9FfFTrK2"}' | jq .result.transactions[]
{
"signer_id": "art.artcoin.testnet",
"public_key": "ed25519:4o6mz55p1mNmfwg5EeTDXdtYFxQev672eU5wy5RjRCbw",
"nonce": 570906,
"receiver_id": "art.artcoin.testnet",
"actions": [
  {
    "FunctionCall": {
      "method_name": "submit_asset_price",
      "args": "eyJhc3NldCI6ImFCVEMiLCJwcmljZSI6IjM4MzQyOTEyMzgzNTEifQ==",
      "gas": 30000000000000,
      "deposit": "0"
    }
  }
],
"signature": "ed25519:2E6Bs8U1yRtAtYuzNSB1PUXeAywrTbXMpcM8Z8w6iSXNEtLRDd1aXDCGrv3fBTn1QZC7MoistoEsD5FzGSTJschi",
"hash": "BYbqKJq3c9qW77wspsmQG3KTKAAfZcSeoTLWXhk6KKuz"
}

And this way you can collect all the transactions from the block.


Alternatively, as @amgando said you can query the Indexer for Explorer database using public credentials

https://github.com/near/near-indexer-for-explorer#shared-public-access

But please be aware that the number of connections to the database is limited (resources) and often it's not that easy to get connected because a lot of people around the globe are using it.

Upvotes: 2

amgando
amgando

Reputation: 1591

Since NEAR is sharded, a "chunk" is what we call that piece of a block that was handled by a single shard

To build up the entire block you can either

  • construct the block from its chunk parts
  • use an indexer to capture what you need in real time

Upvotes: 0

Related Questions