UniswapV3Factory 监控 Web3 Action 模板
项目概览
本项目的目的是设置一个系统来监控 UniswapV3Factory 合约中的新池创建,并在满足特定条件时执行自动化操作。以下是项目流程的简要说明:- Web3 Action 监听以太坊主网上 UniswapV3Factory 合约的
PoolCreated事件。 - 检测到事件时,从事件数据中提取新池的地址。
- Action 将新池合约添加到您的 Tenderly 项目中,并为其添加标签以便于管理。
前提条件
在开始之前,请确保您已具备:- 已安装 Tenderly CLI
- Node.js(当前支持 v20)
- npm
- Tenderly 账户和项目
设置
让我们一步步设置您的UniswapV3Factory 监控系统:
- 合约地址:
0x1f98431c8ad98523631ae4a59f267346ea31f984(UniswapV3Factory 合约) - 事件签名:
PoolCreated(address,address,uint24,int24,address) - 为您的告警命名以便于引用
id 以备后用account_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>"
将
<YOUR_ACCOUNT_ID>、<YOUR_PROJECT_SLUG> 和 <ALERT_ID> 替换为您实际的 Tenderly Account Name、Project Slug 和您之前记下的 Alert ID。ACCESS-KEY:您的 Tenderly API access keyconst 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 };
工作原理
您设置的 Web3 Action 将在UniswapV3Factory 合约发出 PoolCreated 事件时自动触发。以下是 actionFn 函数的分解:
- 从 Tenderly context 中检索
ACCESS-KEYsecret。 - 使用您的账户和项目详情初始化新的 Tenderly SDK 实例。
- 使用 ethers.js 定义
PoolCreated事件签名。 - 循环遍历事件日志以查找
PoolCreated事件并提取新池的地址。 - 使用 Tenderly SDK 将新池合约以显示名称
Pool添加到您的 Tenderly 项目。 - 用
pool标签为新池合约打标签以便于管理。 - 记录池地址和打标签过程的确认信息。
此实现自动将新的 Uniswap V3 池添加到您的 Tenderly 项目并为其打标签,使监控和管理这些合约变得更容易。
自定义
要修改您 Web3 Action 的行为:- 编辑
uniswapV3Monitoring.ts文件中的actionFn。 - 您可以更改
tagName变量以对池使用不同的标签。 - 添加额外的逻辑以处理事件中的更多信息或使用 Tenderly SDK 执行其他操作。
监控和故障排除
为确保您的 Web3 Action 正常工作:- 在 Tenderly dashboard 中监控您的 Web3 Action 执行
- 检查 Tenderly 日志以查看任何错误消息或执行详情
- 使用 Tenderly 的调试工具检查事务详情和合约交互
- 查看您 Tenderly 项目的 Contracts 页面以查看新添加并已打标签的池合约
总结
您现在已使用 Tenderly Web3 Actions 设置了一个自定义监控系统,用于跟踪UniswapV3Factory 合约中的新池创建。本项目演示了如何与智能合约交互、使用 Tenderly 的 SDK,以及创建一个用于监控和响应特定区块链事件的自动化系统。
有关 Uniswap V3 及其合约的更多信息,请参阅 Uniswap V3 文档。