How to Get a Contract’s Last Transfer Event
Learn how to use Alchemy’s SDK to query the transfer history of one or multiple smart contracts in a single request.
This tutorial uses the alchemy_getAssetTransfers endpoint.
Have you ever wanted to know what’s going on under the hood of a smart contract? One way to get insight into how a smart contract is used is to track a contract’s transfer events. This allows you to examine how users/addresses interact with it.
In this guide, you will query the last transfer even of the BAYC smart contract, though you may choose another contract of your preference.
The following is a list of potential use cases:
- Create an NFT tracker for reporting on the latest trades.
- Create a DeFi tracker of a particular dex to get the latest transfer info or provide current information.
- Create Crypto Whale Twitter bot.
- Create the transfer history of a particular address or smart contract.
- Build smart contract logic that requires the most up-to-date transfer info.
If you already completed “How to get a contract’s first transfer event” you may skip the setup and installation steps.
Install Node.js
Head to Node.js and download the LTS version.
You can verify your installation was successful by running npm -version
in your macOS terminal or Windows command prompt. A successful installation will display a version number, such as:
Setup Project Environment
Open VS Code (or your preferred IDE) and enter the following in terminal:
Once inside our project directory, initialize npm (node package manager) with the following command:
Press enter and answer the project prompt as follows:
Press enter again to complete the prompt. If successful, a package.json
file will have been created in your directory.
Install Alchemy’s SDK
Alchemy’s SDK allows us to more efficiently interact with Alchemy’s endpoints and make JSON-RPC requests.
Ensure you are inside your project folder and type the following command in the terminal:
Get Contract’s Last Transfer Event
In this section, we will use Alchemy’s [Transfer API]ref:transfers-api) to retrieve the contract’s last transfer event. The Alchemy SDK allows us to call the alchemy_getAssetTransfers
function and filter transfers by passing in the following object parameters:
For reference, here is an example of how the above parameters could be passed into getAssetTransfers
:
To get started finding a contract’s first transfer, let’s create a file inside our project folder named FirstTransfer.js
and add the following code to utilize the alchemy_getAssetTransfers
endpoint:
Above, we created an async function called getFirstTransfer
.To learn more about how what it does, view the commented notes above.
To use your script, type the following command in your terminal:
If successful, you should see the following transfer object in your output:
If your output includes a page key
, you can use the value for pagination in subsequent requests. If you’ve received the latest transfer event, your result will not include a page key and reads as:
In that scenario, congratulations, you’ve successfully queried some of the latest transfer events!
However, in our case, we did receive a UUID page key. This is because the BAYC contract contains more than 1000 transfer events (the maximum allowed per getAssetTransfers
request). To learn more about how to use page keys, continue on to the following section.
Use Page Keys
To account for potential page keys, we will create a loop to check whether getAssetTransfer
returns a page key. If it does, the loop will continuously call getAssetTransfers
until a page key no longer returns.
To do this, we need to reorganize our code to make room for the while loop. Remove lines 18-25 of LastTransfer.js
:
Now, replace lines 18-25 with the following code block:
To dive into the loop’s details, check out the commented code above.
Your entire LastTransfer.js
script should look like this:
To test our script, run the following code in your terminal:
If successful, your script should log each request:
Once your script loops through the contract’s entire transfer history, you should see an output similar to the following:
Hooray! You have successfully used pagination to get the latest transfer of the BAYC contract!
If you enjoyed this tutorial for retrieving a contract’s latest transfer event, give us a tweet @Alchemy! And don’t forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs!