美文网首页
如何监听交易池txpool(mempool)?

如何监听交易池txpool(mempool)?

作者: 梁帆 | 来源:发表于2022-12-12 16:05 被阅读0次

使用hardhat或者ethers.js

import { ethers } from "hardhat";

async function listen() {
    const provider = ethers.getDefaultProvider("http://localhost:8545");
    provider.on("pending", async (tx) => {
        console.log("tx detected: ", tx);
    })
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
listen().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

这样就能得到正在pending中的tx了,如:

监听到交易池中新的交易
你就可以得到相关信息,比如得到gasPrice,那我们就可以自建新交易超出这个gasPrice,来进行抢先交易。

相关文章

网友评论

      本文标题:如何监听交易池txpool(mempool)?

      本文链接:https://www.haomeiwen.com/subject/vxhcqdtx.html