How to Get All the Contracts Deployed by a Wallet
Learn how to get all the contract addresses deployed by a given wallet address
Don’t have an API key?
Start using Alchemy APIs in your app today. Get started for free
Introduction
In the world of Web3 development, you might want to find all the contracts deployed by a specific wallet address. This information can be useful for tracking / analyzing wallets and development activities. In this tutorial, we will walk you through how to programmatically retrieve all the contracts deployed by a wallet.
Alchemy SDK is the most comprehensive, stable, and powerful Javascript SDK available today to interact with the blockchain. We will be using Alchemy SDK’s getAssetTransfers method to fetch transaction data for a specific wallet address and, from there, extract the contracts deployed by it using the getTransactionReceipt method.
The Gameplan
To achieve our goal, we will follow these steps:
-
First, we will use the getAssetTransfers method of Alchemy SDK (Alchemy’s Transfers API) to fetch all transactions associated with the wallet address of interest. This method allows us to retrieve a list of transactions
to
orfrom
the specified wallet address. -
Next, we will filter the list of transactions to find only those where the
to
field isnull
. This condition indicates that the transaction resulted in the deployment of a new contract. This is true because when a contract is deployed, it does not have an address yet, as it does not exist on the blockchain. Therefore, theto
field in the transaction is set tonull
. -
Finally, for each of the filtered transactions, we will call the getTransactionReceipt method. This method will return the transaction receipt containing the contract address for the newly deployed contract.
-
We will store the addresses of the deployed contracts in an array and finally log that array to the console.
Now, let’s execute our plan!
Setting up the project
Install Node and npm
In case you haven’t already, install node and npm on your local machine.
Make sure that node is at least v14 or higher by typing the following in your terminal:
Create an Alchemy App
In case you haven’t already, sign up for a free Alchemy account.
Alchemy’s account dashboard where developers can create a new app on the Ethereum blockchain.
Next, navigate to the Alchemy Dashboard and create a new app.
Make sure you set the chain to Ethereum and the network to Mainnet. Once the app is created, click on your app’s View Key button on the dashboard.
Take note of the HTTP URL.
The URL will be in this form: https://eth-mainnet.g.alchemy.com/v2/xxxxxxxxx
You will need this later.
Create a node project
Let’s now create an empty repository and install all node dependencies.
Run the following commands in order to create your node project and install Alchemy SDK.
This will create a repository named get-deployed-contracts
that holds all your files and dependencies.
Next, open this repo in your favorite code editor.
Now our project is set up and we are ready to write code. We will write all our code in the main.js
file.
Writing and Testing the Script
Coding the Script
The script below utilizes Alchemy’s SDK to find all the contract addresses deployed by a wallet address:
Let’s break down the code in bullet points:
- The script first imports the necessary modules from the Alchemy SDK and initializes an Alchemy instance with the provided API key and network.
- Then, it defines an asynchronous function
findContractsDeployed
that accepts a wallet address as its argument. - Inside the
findContractsDeployed
function, it fetches the transaction history of the wallet address using Alchemy’sgetAssetTransfers
method. - The script paginates through the results and aggregates them in the
transfers
array. - It then filters the transfers to only include contract deployments by checking if the
"to"
property isnull
. - Then it maps the filtered deployments to their corresponding transaction hashes and fetches the transaction receipts for each of the deployment transactions using Alchemy’s
getTransactionReceipt
method. - Finally, the script waits for all the transaction receipts to be fetched and maps them to their respective deployed contract addresses using the
contractAddress
property in the transaction receipt. - The function then returns the array of these contract addresses.
- The main function initializes the wallet address and calls the
findContractsDeployed
function to get the array of contract addresses deployed by the given wallet address. - It then loops through the array to display the contract addresses in a readable format.
If you are facing the "ENS name not configured"
error, try replacing the "demo"
API key with your own API key that you copied in the “Create an Alchemy App” section.
Testing the Script
Now let’s test our script to verify if it’s working properly. Run your script using the command below:
You should see an output listing the contract addresses deployed by the given wallet address:
Conclusion
In this tutorial, we learned how to programmatically retrieve all the contracts deployed by a wallet address.
By following the code and explanations provided, you should now be able to adapt this script to suit your specific needs or integrate it into a larger project.
If you have any questions or need further assistance, feel free to join Alchemy’s Discord and engage with the community. Also, don’t forget to follow Alchemy on Twitter @Alchemy for the latest updates and news.