Understanding the Code
Before we start coding, let's take a look at the code we will be working with.
There will be two main files that we will be working with in this section.
Page.tsx
This is the code that will be rendered on the client side, as distinguished by "use client";
at the top of the file. It contains the React components that will be displayed to the user and is responsible for making the API calls to our backend, which in turn calls the Data API.
It is important to understand that when you "use client"
in a NextJS project, it will be rendered on the client side. This means that the code will be executed in the user's browser and not on the server. This is important to keep in mind when working with sensitive data or when you want to keep your API keys secure.
Besides this, we have two main functions that we will be working with in this file:
handleSetAddress
is a simple function that will be called when the user clicks the "Set Address" button. It will ensure the address is valid then set the inputted address to the React State. Next, we call our next function fetchERC20Balances
to get the user's balance.
fetchERC20Balances
is a function that will make a call to our backend to get the user's ERC-20 token balances. It will first get the current block height, then call the listErc20Balances
method on our backend with the user's address and the block height. It will then return the balances as an array of Erc20TokenBalance
objects.
Route.ts
This code will be executed on the server side, as distinguished by "use server";
at the top of the file. It contains the code that will be executed on the server side and is responsible for making the API calls to the Data API.
There are a few key components to understand in this file:
Here we initialize the AvaCloudSDK
with our AvaCloud API key and the chainId of 43114
for the Avalanche Mainnet. This will allow us to make calls to the Data API.
Here we define the internal API methods for our backend. We have two methods that we will be working with in this section: getBlockHeight
and listErc20Balances
. We create both of these methods internally, then forward the request to the Data API. We then return the result to the client.
In the next section, we will implement the listErc20Balances
and getBlockHeight
function to call the Data API through the AvaCloudSDK.