Monitoring UniswapV3Factory Pool Creations
In this tutorial, we’ll show you how to set up a Tenderly Web3 Action to monitor new pool creations in the UniswapV3Factory contract and perform automated actions. This project demonstrates how to use Web3 Actions to interact with smart contracts and Tenderly’s features to create a custom monitoring system.UniswapV3Factory monitoring Web3 Action template
Project Overview
The purpose of this project is to set up a system that monitors new pool creations in the UniswapV3Factory contract and performs automated actions when specific conditions are met. Here’s a quick breakdown of the project’s flow:- The Web3 Action listens for
PoolCreatedevents from the UniswapV3Factory contract on the Ethereum mainnet. - When an event is detected, it extracts the new pool’s address from the event data.
- The action adds the new pool contract to your Tenderly project and tags it for easier management.
Prerequisites
Before you begin, make sure you have the following:- Tenderly CLI installed
- Node.js (v20 currently supported)
- npm
- A Tenderly account and project
Setup
Let’s go through the steps to set up yourUniswapV3Factory Monitoring system:
- Contract Address:
0x1f98431c8ad98523631ae4a59f267346ea31f984(UniswapV3Factory contract) - Event Signature:
PoolCreated(address,address,uint24,int24,address) - Name your alert appropriately for easy reference
id of an alert for later useaccount_id: "<YOUR_ACCOUNT_ID>"
actions:
<YOUR_ACCOUNT_ID>/<YOUR_PROJECT_SLUG>:
runtime: v2
sources: actions
specs:
uniswapV3:
description: "Monitoring UniswapV3 Factory Contract and adding Child/Pool to Contracts page with proper tag"
function: uniswapV3Monitoring:actionFn
trigger:
type: alert
alert: { <ALERT_ID> }
execution_type: parallel
project_slug: "<YOUR_PROJECT_SLUG>"
Replace
<YOUR_ACCOUNT_ID>, <YOUR_PROJECT_SLUG>, and <ALERT_ID> with your actual Tenderly Account Name, Project Slug, and the Alert ID you noted earlier.ACCESS-KEY: Your Tenderly API access keyCreate a new file in your actions directory named
uniswapV3Monitoring.ts and implement your Web3 Action logic as follows:const ethers = require('ethers');
const actionFn = async (context: Context, alertEvent: AlertEvent) => {
const key = await context.secrets.get('ACCESS-KEY');
const accountSlug = '<YOUR_ACCOUNT_ID>';
const projectSlug = '<YOUR_PROJECT_SLUG>';
const tagName = 'pool';
const tenderly = new Tenderly({
accountName: accountSlug,
projectName: projectSlug,
accessKey: key,
network: Number(alertEvent.network),
});
const poolCreatedSignature = ethers.utils.id("PoolCreated(address,address,uint24,int24,address)");
let poolAddress = "";
for (const log of alertEvent.logs) {
if (log.topics[0] === poolCreatedSignature) {
const data = log.data;
const addressHex = data.substring(data.length - 40);
poolAddress = ethers.utils.getAddress('0x' + addressHex).toLowerCase();
break;
}
}
try {
await tenderly.contracts.add(poolAddress, {
displayName: 'Pool'
});
await tenderly.contracts.update(poolAddress, { appendTags: [tagName] });
console.log(`Pool contract is: ${poolAddress}, and has been added with tag ${tagName}`);
} catch (error) {
console.error('Error adding contract:', error);
}
};
module.exports = { actionFn };
Make sure to replace
<YOUR_ACCOUNT_ID> and <YOUR_PROJECT_SLUG> with your actual Tenderly Account Name and Project Slug.How it Works
The Web3 Action you’ve set up will automatically trigger whenever thePoolCreated event is emitted by the UniswapV3Factory contract. Here’s a breakdown of the actionFn function:
- Retreive the
ACCESS-KEYsecret from the Tenderly context. - Initialize a new Tenderly SDK instance with your account and project details.
- Define the
PoolCreatedevent signature using ethers.js. - Loop through the event logs to find the
PoolCreatedevent and extracts the new pool’s address. - Using the Tenderly SDK, add the new pool contract to your Tenderly project with the display name
Pool. - Tag the new pool contract with the
pooltag for easier management. - Log the pool address and confirmation of the tagging process.
This implementation automatically adds new Uniswap V3 pools to your Tenderly project and tags them, making it easier to monitor and manage these contracts.
Customization
To modify the behavior of your Web3 Action:- Edit the
actionFnin theuniswapV3Monitoring.tsfile. - You can change the
tagNamevariable to use a different tag for the pools. - Add additional logic to process more information from the event or perform other actions with the Tenderly SDK.
Monitoring and Troubleshooting
To ensure your Web3 Action is working correctly:- Monitor your Web3 Action executions in the Tenderly dashboard
- Check the Tenderly logs for any error messages or execution details
- Use Tenderly’s debugging tools to inspect transaction details and contract interactions
- Check your Tenderly project’s Contracts page to see the newly added and tagged pool contracts
Conclusion
You’ve now set up a custom monitoring system using Tenderly Web3 Actions to track new pool creations in theUniswapV3Factory contract. This project demonstrates how to interact with smart contracts, use Tenderly’s SDK, and create an automated system for monitoring and responding to specific blockchain events.
For more information on Uniswap V3 and its contracts, refer to the Uniswap V3 documentation.