Nhảy đến nội dung chính

Indexing Historical Data

In our frontend, we can easily display the previous bid since it's stored in the contract's state. However, we're unable to see previous bids to the auction. An indexer is used to fetch historical data from the blockchain and store it in a database. Since indexers can take a while to set up and can be expensive to run, we will use a pre-defined API endpoint provided by NEAR Blocks to query an indexer they run that will fetch us the data we need.


NEAR Blocks API key

NEAR Blocks provides a free tier that allows you to make 6 calls per minute, which will be plenty for our use case. To get an API key, head over to https://dash.nearblocks.io/user/overview and sign up. Once signed go to API Keys then click Add key and give it whatever name you like.

We'll create a new file named .env.local to store our API key.

API_KEY=YOUR_API_KEY_GOES_HERE

We put the API key in a .env.local file so the user cannot access it in the browser and use our key elsewhere. We should also add .env.local to our .gitignore file so it is not pushed to GitHub.


Calling the API endpoint

NextJS allows us to easily create server-side functions with API routes. We need to make this API call on the server-side rather than the client side so as to not expose our API key. We'll create a new file in src/pages/api named getBidHistory.js. Here we'll define our function to get the bid history.

Here we are retrieving the auction contract ID from the API route call and then calling the NEAR Blocks API. This specific API endpoint allows us to retrieve transactions made to a specific contract calling a specific function. Some details are worth discussing here:

  • We pass the account ID of the auction contract, which is basic-auction-example.testnet in the example repo.
  • We specify the function name on the auction contract that we want the transactions for, in our case it will be bid
  • We'll receive a JSON object of up to 25 transactions, ordered by the most recent first.
  • We pass our API key to authenticate the request.

Retrieving the bids from the API result

From our API call, we receive a JSON object containing up to 25 transactions made to the bid function on the auction contract.

We want to display the 5 most recent valid bids. To do this we loop through each transaction and check whether the transaction was successful by checking receipt_outcome.status is true. If so we check the first action (since there should only be one function call action in this case) and store the deposit, which is equal to the bid amount, and store the predecessor account ID, which is the account ID of the bidder.

Once we have 5 valid bids we can stop looping through the transactions.

Note that in our example if the previous 25 bids were invalid the API will return an empty array. The function could be set up such that it calls the API again to get the new page of transactions if this is the case.

Learn More

You can read more about transaction actions in this section of the documentation: Actions


Using the API Route

In our main page, we'll define a function to call the API route we just created. This function will be called each time the page timer reaches zero.

The pastBids will then be passed into the Bid component to be displayed.


You may like to explore NEAR Blocks APIs further to see what other data you can retrieve from the blockchain. You can find the documentation at https://api.nearblocks.io/api-docs/


Using the frontend

Now we have implemented the frontend and indexer you can go ahead and actually use the frontend. From the root of the frontend directory run the following commands:

# install dependencies
npm install

# run the frontend locally
npm run dev

Conclusion

In this short part of the tutorial, we've added the ability to display the previous 5 valid bids made to the auction contract. In doing this we learned how to interact with the NEAR Blocks APIs to retrieve historical data from the blockchain and how to make server-side calls in NextJS to not expose our API key. Now we have a pretty good frontend that displays all the information we need about the auction contract.

In the next section of the tutorial we're going to improve our contract by adding primitives to the auction contract starting with adding NFTs as a prize.

Was this page helpful?