【问题标题】:How do I listen to events from a smart contract using ethers.js contract.on() in a node.js application?如何在 node.js 应用程序中使用 ethers.js contract.on() 监听来自智能合约的事件?
【发布时间】:2021-06-28 18:40:54
【问题描述】:

我正在尝试在 node.js 应用程序中使用 ethers.js(不是 web3)监听 USDT 合约传输函数发出的事件。

当我运行脚本时,代码运行没有错误,然后快速退出。我希望得到事件日志。我不确定我错过了哪一步。

我已经通过调用 getOwner() 方法和控制台记录该结果来测试这个脚本,这工作正常,所以我与主网的连接正常。

我正在使用炼金术 websocket。

我的 index.js 文件

const hre = require("hardhat");
const ethers = require('ethers');
const USDT_ABI = require('../abis/USDT_ABI.json')

async function main() {

const usdt = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
const provider = new ethers.providers.WebSocketProvider("wss://eth-mainnet.ws.alchemyapi.io/v2/MY_API");
const contract = new ethers.Contract(usdt, USDT_ABI, provider)

contract.on('Transfer', (from, to, value) => console.log(from, to, value))

}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });

我的 hardhat.config.js 文件


    require("@nomiclabs/hardhat-waffle");
require('dotenv').config()

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async () => {
  const accounts = await ethers.getSigners();

  for (const account of accounts) {
    console.log(account.address);
  }
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
 module.exports = {
  paths: {
    artifacts: './src/artifacts',
  },

  networks: {
    mainnet: {
      url: "wss://eth-mainnet.ws.alchemyapi.io/v2/MY_API",
      accounts: [`0x${process.env.PRIVATE_KEY}`]
    },
    hardhat: {
      chainId: 1337
    },
  },
  solidity: "0.4.8"
};`

【问题讨论】:

  • 老兄!你已经展示了你的炼金术 API!现在每个人都可以使用它!改成MY_API什么的
  • 完成!无论如何,这只是一个刻录机帐户。不过谢谢!

标签: javascript node.js ethers.js hardhat


【解决方案1】:

我通过删除解决了这个问题

.then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });

并且只是调用 main。在 hardhat 文档中建议使用 .then 和 .catch 代码,但是当运行像这个脚本这样使用 contract.on() 的长时间运行的进程时,它会导致脚本退出。

【讨论】:

    【解决方案2】:

    我这样做:

    const ethers = require('ethers');
    const abi = [{...}]
    const contractAddress = '0x000...'
    
    const webSocketProvider = new ethers.providers.WebSocketProvider(process.env.ETHEREUM_NODE_URL, process.env.NETWORK_NAME);
    const contract = new ethers.Contract(contractAddress, abi, webSocketProvider);
    
    contract.on("Transfer", (from, to, value, event) => {
            console.log({
                from: from,
                to: to,
                value: value.toNumber(),
                data: event
            });
        });
    

    事件返回所有与事件和交易相关的数据。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2022-12-17
    • 2020-10-02
    • 2021-12-01
    • 2018-08-01
    • 2021-10-31
    • 2021-08-31
    • 2022-10-13
    • 2022-08-10
    • 1970-01-01
    相关资源
    最近更新 更多