How to Get a Contract's First Transfer Event
This tutorial uses the alchemy_getAssetTransfers endpoint.
One of the best ways to study a smart contract is to look at its transfer events. In this tutorial, we will query the very first transfer event of the BAYC smart contract. However, you are welcome to use any contract address you are interested in!
See the following for use cases for retrieving a contract’s first transfer event:
- Finding the first addresses to interact with a contract (e.g., the first address to mint a Bored Ape).
- Tracking the first interaction an address had with a smart contract.
- Verifying whether a contract facilitated transfers before a certain date.
If you already completed “How to get a contract’s last 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 a 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.
Setup for API Calls
We’ll use the built-in fetch API (available in Node.js 18+) to interact with Alchemy’s endpoints and make JSON-RPC requests. No additional dependencies are required!
Get Contract’s First Transfer Event
In this section, we will use Alchemy’s Transfer API to retrieve the contract’s first transfer event. We can 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 the alchemy_getAssetTransfers API:
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, run the following command in your terminal:
If successful, you should see the following transfer object in your output:
Congratulations, you’ve successfully retrieved the first transfer event in the BAYC contract!
If you enjoyed this tutorial for retrieving a contract’s first transfer event, give us a tweet @Alchemy! And don’t forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs!