You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用ethers.js和Infura监听ETH(原生代币)的转账事件日志?

基于ethers.js和Infura监听ERC-20代币Transfer事件

我目前使用ethers.js配合Infura监听USDT等ERC-20代币的Transfer事件。据了解,要获取ETH的转账记录,需要遍历每个区块中的交易。以下是我当前实现ERC-20转账事件监听的代码:

const { ethers, Contract } = require("ethers");

const { addEvent } = require('./events');
const tokens = require('./contracts/tokenConfigs.json');

const provider = new ethers.providers.JsonRpcProvider(`xxx`);

const main = async ({
    name,
    symbol,
    address,
    eventType,
    threshold,
    decimals,
    blockExplorerUrl,
    ABIpath,
}) => {
    const ERC20_ABI = require(ABIpath);
    const contract = new Contract(address, ERC20_ABI, provider);

    contract.on('Transfer', async (from, to, amount, data) => {
        if (amount.toNumber() >= threshold) {
            console.log(`Whale transfer alert for ${name} ${blockExplorerUrl}${data.transactionHash}`);

            await addEvent({...});
        }
    });
}

内容的提问来源于stack exchange,提问作者ldls

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 18:31:07