为什么需要监控工具?
智能合约一旦部署到链上,源代码就无法修改。这意味着任何bug都可能导致资产永久损失——没有”Ctrl+Z”可以撤销。与传统后端服务不同,合约出问题后你可能只能眼睁睁看着资金流失,然后紧急组织社区投票迁移合约。
监控工具的价值在于:让你在问题发生时第一时间发现,而不是被用户在社交媒体上@才知道。以下是几个典型场景:
- 异常交易监测:合约出现大额转账、批量套利等可疑行为时立即告警
- Gas费用异常:用户交易Gas费用远超正常水平
- 合约状态异常:某个数值不应该超过阈值,却被突破了
- 安全事件联动:当DeFi协议被攻击时,自动通知相关用户
本文将带你系统性地了解这些工具,并给出实际项目中的选型建议。

Tenderly:全方位开发与监控平台
平台概述
Tenderly是Web3领域最受欢迎的开发者平台之一,提供从开发、调试到监控的全套工具链。它的核心优势是:
- 实时监控:支持自定义告警规则
- 交易模拟:在正式发送前预测交易结果
- 调试器:类似IDE的断点调试体验
- Gas分析:精确追踪每笔交易的Gas消耗
基础配置
安装Tenderly CLI:
bash
npm install -g @tenderly/cli
# 登录
tenderly login
# 初始化配置
tenderly init
创建配置文件:
yaml
# tenderly.yaml
project: your-project-slug
contracts:
# 主网合约
- name: "MainPool"
address: "0x1234..."
network: "1"
# 测试网合约
- name: "TestPool"
address: "0x5678..."
network: "11155111"
access_key: your_api_key
自定义告警
Tenderly的告警系统非常灵活,支持多种触发条件:
javascript
// 定义告警规则
// 方式一:使用Dashboard创建(图形界面)
// 方式二:使用API创建
const alertRule = {
name: "Large Transfer Alert",
description: "Triggered when a single transfer exceeds 100 ETH",
conditions: [
{
type: "function", // 函数调用触发
contract: {
slug: "MainPool",
function: "transfer",
},
filter: {
"outputs.value": {
op: "gt",
value: "100000000000000000000" // 100 ETH in wei
}
}
},
{
type: "balance", // 余额变化触发
contract: {
slug: "MainPool",
},
change_type: "absolute", // absolute 或 relative
threshold: "1000000000000000000000" // 1000 ETH
}
],
actions: [
{
type: "webhook",
url: "https://your-webhook-endpoint.com/alert",
headers: {
"Authorization": "Bearer your-secret"
}
},
{
type: "email",
recipients: ["security@yourprotocol.com"]
},
{
type: "slack",
channel: "#security-alerts"
}
]
};
// 创建告警
await fetch('https://api.tenderly.co/api/v1/alert-rules', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Access-Key': process.env.TENDERLY_ACCESS_KEY
},
body: JSON.stringify(alertRule)
});
交易模拟器
在发送交易前使用Tenderly模拟结果:
bash
# 模拟交易
tenderly simulate \
--network mainnet \
--from 0xABCD... \
--to 0x1234... \
--value 1 \
--data 0x...
或者使用SDK:
typescript
import { Tenderly, Network, SimulationParams } from '@tenderly/sdk';
const tenderly = new Tenderly({
token: process.env.TENDERLY_ACCESS_KEY,
accountId: process.env.TENDERLY_ACCOUNT_ID,
projectSlug: process.env.TENDERLY_PROJECT_SLUG
});
const simulation = await tenderly.simulator.simulate({
network: Network.MAINNET,
from: '0xABCD...',
to: '0x1234...',
value: '1000000000000000000', // 1 ETH
input: '0x...',
save: true, // 保存到Tenderly Dashboard
saveIfFails: true,
gas: 5000000,
gasPrice: '30000000000' // 30 gwei
});
console.log('Simulation Result:', {
status: simulation.simulation.status,
gasUsed: simulation.simulation.gas_used,
returnValue: simulation.simulation.return_value,
logs: simulation.simulation.logs,
trace: simulation.transaction.transaction_info.state_diff
});
Gas分析
分析交易Gas消耗:
typescript
// 获取详细Gas报告
const gasAnalysis = await tenderly.inspector.analyzeGas({
projectSlug: 'your-project',
transactionHash: '0x...'
});
console.log('Gas Breakdown:', {
totalGas: gasAnalysis.total_gas_used,
gasByFunction: gasAnalysis.gas_by_function,
executionSteps: gasAnalysis.execution_steps,
storageReads: gasAnalysis.storage_reads,
storageWrites: gasAnalysis.storage_writes
});
OpenZeppelin Defender:企业级安全平台
平台概述
OpenZeppelin是智能合约领域最受信赖的安全公司之一,其Defender平台专注于生产环境运维,提供:
- Autotasks:无服务器函数,执行自动化任务
- Admin:合约管理界面,无需私钥即可操作合约
- Sentinel:实时监控合约事件和函数调用
- Relay:安全地中继交易
- Defender Forta:集成Forta网络进行安全监控
Sentinel监控规则
创建Sentinel监控合约状态:
typescript
import { Defender } from '@openzeppelin/defender-sdk';
const client = new Defender({
relayerApiKey: process.env.DEFENDER_RELAYER_API_KEY!,
relayerApiSecret: process.env.DEFENDER_RELAYER_API_SECRET!
});
// 创建Sentinel
async function createSentinel() {
const sentinel = await client.addSentinel(
{
name: 'Transfer Sentinel',
// 监控特定函数调用
type: 'Function',
addresses: ['0x1234...'], // 监控的合约地址
abi: [
{
type: 'function',
name: 'transfer',
inputs: [
{ name: 'to', type: 'address' },
{ name: 'value', type: 'uint256' }
],
outputs: [{ type: 'bool' }]
}
],
// 函数参数过滤
functionConditions: {
'inputs.value': {
// 监控大于100 ETH的转账
gt: '100000000000000000000'
}
},
// 告警通知渠道
notificationChannels: ['telegram', 'email'],
// 监控网络
network: 'mainnet'
}
);
console.log('Sentinel created:', sentinel.sentinelId);
}
// 监听事件
async function createEventSentinel() {
const sentinel = await client.addSentinel(
{
name: 'Large Transfer Event',
type: 'Event',
addresses: ['0x1234...'],
// ERC-20 Transfer事件
eventABI: {
anonymous: false,
inputs: [
{ indexed: true, name: 'from', type: 'address' },
{ indexed: true, name: 'to', type: 'address' },
{ indexed: false, name: 'value', type: 'uint256' }
],
name: 'Transfer',
type: 'event'
},
// 只关注满足条件的事件
eventConditions: {
'0': { // from参数
// 过滤来自特定地址的转账
equals: '0xABCD...'
},
'2': { // value参数
gt: '1000000000000000000000' // > 1000 ETH
}
},
// 监控确认数
confirmBlocks: 2,
notificationChannels: ['slack']
}
);
}
Relay安全交易
使用Defender Relay安全地发送交易:
typescript
import { Defender } from '@openzeppelin/defender-sdk';
const client = new Defender({
relayerApiKey: process.env.DEFENDER_RELAYER_API_KEY!,
relayerApiSecret: process.env.DEFENDER_RELAYER_API_SECRET!
});
// 发送交易
async function sendTransaction() {
const params = {
to: '0x1234...',
value: 0,
data: '0x...', // encoded function call
gasLimit: 100000,
// 可选:指定Gas策略
maxFeePerGas: 100000000000, // 100 gwei
maxPriorityFeePerGas: 2000000000 // 2 gwei
};
const tx = await client.relay.signTransaction(params);
console.log('Transaction sent:', tx.hash);
}
// 查询交易状态
async function getTransactionStatus(hash: string) {
const tx = await client.relay.getTransaction(hash);
return {
hash: tx.hash,
status: tx.status, // pending, confirmed, failed
gasUsed: tx.gasUsed,
nonce: tx.nonce
};
}
Autotasks自动化任务
Autotasks允许你执行自动化脚本,无需运行自己的服务器:
typescript
// src/autotask.ts - Autotask入口函数
import { DefenderRelay } from '@openzeppelin/defender-relay-client';
// Autotask处理函数
export async function handler(credentials: DefenderRelay) {
const { apiKey, apiSecret } = credentials;
// 创建Defender客户端
const client = new DefenderRelay(credentials);
// 查询合约状态
const value = await client.query(['0x1234...'], {
network: 'mainnet',
abi: [{
type: 'function',
name: 'totalValue',
outputs: [{ type: 'uint256' }]
}]
});
console.log('Total Value:', value);
// 如果超过阈值,发送告警
if (value > BigInt('10000000000000000000000')) { // > 10000 ETH
// 调用外部告警服务
await fetch('https://your-alerting.com/alert', {
method: 'POST',
body: JSON.stringify({ type: 'HIGH_VALUE', value: value.toString() })
});
}
}
// 自动触发器示例
export async function scheduledHandler(event: any) {
// 定时任务逻辑
// 由Defender自动按配置的时间间隔调用
}
Blocknative:交易监控与MEV保护
平台特色
Blocknative专注于交易层面的监控和优化:
- 实时交易池监控:追踪待处理交易池中的交易
- MEV保护:保护用户交易不被抢先交易
- Gas预测:准确的Gas价格预测
- 交易通知:交易状态变化实时通知
SDK集成
typescript
import { Blocknative } from 'bnc-sdk';
import { Optionals } from 'bnc-sdk/dist/src/types';
// 初始化Blocknative
const bnc = new Blocknative({
dappId: process.env.BLOCKNATIVE_DAPP_ID!,
networkId: 1, // Mainnet
// 交易确认通知
onTransactionConfirmed: (tx) => {
console.log('Transaction confirmed:', tx.hash);
},
onTransactionFailed: (tx) => {
console.log('Transaction failed:', tx.hash, tx.error);
},
onTransactionDropped: (tx) => {
console.log('Transaction dropped from mempool');
}
});
// 监听特定地址的交易
const address = '0x1234...';
const { remove } = bnc.watch({ address });
// 监听合约事件
const contractAddress = '0x5678...';
const { remove: removeContract } = bnc.watch({
address: contractAddress,
abi: [{
name: 'Transfer',
type: 'event',
inputs: [
{ type: 'address', name: 'from' },
{ type: 'address', name: 'to' },
{ type: 'uint256', name: 'value' }
]
}],
eventABI: {
name: 'Transfer',
signature: '0xddf252ad...'
}
});
// 获取当前Gas价格
const gasOracle = bnc.provider({
method: 'eth_gasPrice'
});
const gasPrice = await gasOracle.request('eth_gasPrice');
console.log('Current Gas Price:', gasPrice);
// 发送带MEV保护的交易
const txHash = await bnc.send({
to: '0xABCD...',
value: '0x0',
data: '0x...',
// 启用MEV保护
networkId: 1,
// 优先Gas费用设置
gasPrice: async () => {
const price = await bnc.gasPrice.getBlockPrice();
return price.fast; // 使用快速Gas价格
}
});
Forta Network:分布式安全监控
概述
Forta是一个去中心化的安全监控网络,任何人都可以部署自己的检测机器人(Bot)来监控链上活动,并获得奖励。不同于中心化平台,Forta利用分布式节点确保监控的可靠性和抗审查性。
开发Forta Bot
typescript
// src/alert-bot.ts
import { BlockEvent, Forta, Finding, FindingType, FindingSeverity } from 'forta-agent';
// 检测器配置
const LARGE_TRANSFER_THRESHOLD = '1000000000000000000000'; // 1000 ETH
// 初始化
export const provideInitialize = (): () => Promise<void> => {
return async () => {
// 设置监控配置
console.log('Large Transfer Alert Bot initialized');
};
};
// 交易处理
export const provideHandleTransaction = (
forta: Forta
): ((txEvent: any) => Promise<Finding[]>) => {
return async (txEvent: any): Promise<Finding[]> => {
const findings: Finding[] = [];
// 遍历ERC20转账
for (const log of txEvent.logs) {
// 检查是否是Transfer事件
if (log.topics[0] === '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df35b9c8') {
// 解析事件数据
const from = '0x' + log.topics[1].slice(26);
const to = '0x' + log.topics[2].slice(26);
const value = BigInt(log.data);
// 检查是否超过阈值
if (value > BigInt(LARGE_TRANSFER_THRESHOLD)) {
findings.push(
Finding.fromObject({
name: 'Large ERC20 Transfer',
description: `Large transfer of ${formatEther(value)} ETH detected`,
alertId: 'LARGE-TRANSFER-1',
severity: FindingSeverity.Info,
type: FindingType.Info,
metadata: {
from,
to,
value: value.toString(),
transactionHash: txEvent.hash,
tokenAddress: log.address
}
})
);
}
}
}
// 检测可疑的合约交互
// 检查是否是与新合约的交互
if (txEvent.traces.length > 0) {
for (const trace of txEvent.traces) {
if (trace.error) {
findings.push(
Finding.fromObject({
name: 'Transaction Execution Failed',
description: `Transaction failed with error: ${trace.error}`,
alertId: 'TX-FAILED-1',
severity: FindingSeverity.Medium,
type: FindingType.Suspicious,
metadata: {
error: trace.error,
from: txEvent.from,
to: trace.to,
input: trace.input
}
})
);
}
}
}
return findings;
};
};
// 区块处理(用于检测模式变化)
export const provideHandleBlock = (
forta: Forta
): ((blockEvent: BlockEvent) => Promise<Finding[]>) => {
return async (blockEvent: BlockEvent): Promise<Finding[]> => {
const findings: Finding[] = [];
// 监控Gas价格异常
const avgGasPrice = blockEvent.gasUsed * blockEvent.baseFeePerGas;
const expectedGas = 15000000; // 预期的平均Gas使用
if (avgGasPrice > expectedGas * 2) {
findings.push(
Finding.fromObject({
name: 'Abnormal Gas Usage',
description: 'Block gas usage significantly higher than average',
alertId: 'HIGH-GAS-BLOCK-1',
severity: FindingSeverity.Low,
type: FindingType.Suspicious,
metadata: {
gasUsed: blockEvent.gasUsed.toString(),
blockNumber: blockEvent.blockNumber
}
})
);
}
return findings;
};
};
// 主入口
export default {
initialize: provideInitialize(),
handleTransaction: provideHandleTransaction(Forta),
handleBlock: provideHandleBlock(Forta)
};
// 辅助函数
function formatEther(wei: bigint): string {
return (Number(wei) / 1e18).toFixed(2);
}
工具对比与选型建议
功能对比表
| 功能 | Tenderly | OpenZeppelin Defender | Blocknative | Forta |
|---|---|---|---|---|
| 交易模拟 | ✅ 强大 | ❌ | ✅ | ❌ |
| 自定义告警 | ✅ | ✅ | ✅ | ✅ |
| 交易池监控 | ❌ | ❌ | ✅ 特色 | ❌ |
| MEV保护 | ❌ | ❌ | ✅ 特色 | ❌ |
| Gas优化 | ✅ | ❌ | ✅ | ❌ |
| 无服务器函数 | ❌ | ✅ Autotasks | ❌ | ❌ |
| 去中心化 | ❌ | ❌ | ❌ | ✅ |
| 价格 | 免费+付费 | 免费+付费 | 免费+付费 | 免费 |
场景选型
小型项目(预算有限):
- Tenderly免费层:足够满足基础监控需求
- Forta:免费使用社区Bot
中型项目(需要平衡功能与成本):
- Tenderly:核心监控+告警
- OpenZeppelin Defender:Admin + Relay管理合约
大型项目(企业级需求):
- 全家桶:Tenderly + Defender + Blocknative
- 监控:Defender Sentinel + Forta社区Bot
- 安全交易:Defender Relay
- 调试分析:Tenderly
- MEV保护:Blocknative
DeFi协议(高安全需求):
- Tenderly:实时监控 + 交易模拟
- Forta:分布式安全监控
- 自定义Bot:部署专有的安全检测逻辑
监控最佳实践
告警分级
建议设置三级告警:
typescript
// 告警分级策略
const alertLevels = {
critical: [
// 立即处理
{ type: 'funds_drain', template: '⚠️ 资金流失风险!' },
{ type: 'admin_key_compromised', template: '🚨 管理员权限异常!' },
{ type: 'oracle_manipulation', template: '⚠️ 预言机价格异常!' }
],
high: [
// 需要关注
{ type: 'large_transfer', threshold: '100 ETH' },
{ type: 'unusual_activity', template: '异常活动请关注' },
{ type: 'gas_spike', multiplier: 3 }
],
info: [
// 日志记录
{ type: 'normal_activity' },
{ type: 'scheduled_task' }
]
};
响应流程
typescript
// 告警响应流程
async function handleAlert(alert: any) {
// 1. 立即记录日志
console.error('ALERT:', JSON.stringify(alert));
// 2. 根据级别处理
switch (alert.severity) {
case 'CRITICAL':
// 紧急响应
await sendEmergencyNotification(alert);
await pauseProtocol(); // 可能的话暂停协议
await notifyTeam(); // 通知核心团队
break;
case 'HIGH':
// 高优先级处理
await notifyOnCall(); // 通知值班人员
await investigateAlert(alert); // 开始调查
break;
case 'INFO':
// 记录即可
await logAlert(alert);
break;
}
}
总结
智能合约监控是Web3开发中不可或缺的一环。本文详细介绍了四大主流监控平台:
Tenderly:开发到生产的全链路工具,特别适合交易模拟和Gas分析
OpenZeppelin Defender:企业级合约管理和安全交易
Blocknative:交易池监控和MEV保护专家
Forta:去中心化的安全监控网络
实际项目中,建议根据项目规模、预算和安全需求组合使用这些工具。记住:监控不是一劳永逸的,需要持续优化告警规则和响应流程。
下一篇文章我们将回到开发教程,看看Rust区块链开发中Move语言的应用。
相关阅读:

发表回复