How to Get a Contract’s First 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.
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.
Install Alchemy’s SDK
Alchemy’s SDK allows us to efficiently interact with Alchemy’s endpoints and make JSON-RPC requests.
Ensure you are inside your project folder, then type the following command in terminal:
Get Contract’s First Transfer Event
In this section, we will use Alchemy’s Transfer API to retrieve the contract’s first 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, 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!