尝试从 Nodejs 中的 Uniswap V2 Factory 订阅 PairCreated 事件

Trying to susbcribe to PairCreated events from UniswapV2 Factory in Nodejs

我之前使用此代码从币安智能链上的 PancakeSwapV2 工厂获取事件。我现在想使用此代码从以太坊区块链上的 UniswapV2 工厂获取事件,但出现以下错误:

(node:3544) UnhandledPromiseRejectionWarning: Error: resolver or addr is not configured for ENS name (argument="name", value="0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f ", code=INVALID_ARGUMENT, version=contracts/5.4.0)
    at Logger.makeError (C:\Users\aaaa\WebstormProjects\web3test\node_modules\@ethersproject\logger\lib\index.js:187:21)
    at Logger.throwError (C:\Users\aaaa\WebstormProjects\web3test\node_modules\@ethersproject\logger\lib\index.js:196:20)
    at Logger.throwArgumentError (C:\Users\aaaa\WebstormProjects\web3test\node_modules\@ethersproject\logger\lib\index.js:199:21)
    at C:\Users\aaaa\WebstormProjects\web3test\node_modules\@ethersproject\contracts\lib\index.js:101:32
    at step (C:\Users\aaaa\WebstormProjects\web3test\node_modules\@ethersproject\contracts\lib\index.js:48:23)
    at Object.next (C:\Users\aaaa\WebstormProjects\web3test\node_modules\@ethersproject\contracts\lib\index.js:29:53)
    at fulfilled (C:\Users\aaaa\WebstormProjects\web3test\node_modules\@ethersproject\contracts\lib\index.js:20:58)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3544) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To
 terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:3544) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Process finished with exit code 0

这是我尝试重用的源代码:

const Web3 = require('web3');
const ethers = require('ethers');

const INFURA_BASE_URL = 'https://mainnet.infura.io/v3/';
const INFURA_API_KEY = 'REPLACE';
web3 = new Web3(new Web3.providers.HttpProvider(INFURA_BASE_URL + INFURA_API_KEY));

const privateKey = "REPLACE";
const account = web3.eth.accounts.privateKeyToAccount(privateKey)
console.log(account.address)

const provider = new ethers.providers.JsonRpcProvider('https://mainnet.infura.io/v3/REPLACE');
const wallet = new ethers.Wallet(privateKey);
const account2 = wallet.connect(provider);

const addresses = {
    WETH: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
    factory: '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f ',
    router: '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ',
    recipient: account.address
};

//console.log(provider)

const factory = new ethers.Contract(
    addresses.factory,
    [
        'event PairCreated(address indexed token0, address indexed token1, address pair, uint)',
        'function getPair(address tokenA, address tokenB) external view returns (address pair)'
    ],
    account2
);

factory.on('PairCreated', async (token0, token1, pairAddress) => { }

你知道我做错了什么吗?谢谢。

看来我正在用这段代码接近解决方案:

const Web3 = require("web3");

let web3 = new Web3(
    new Web3.providers.WebsocketProvider("wss://mainnet.infura.io/ws/v3/cc44823998a0412294a47680xxxxxxxx")
);

let abi = JSON.parse('[{"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token0","type":"address"},{"indexed":true,"internalType":"address","name":"token1","type":"address"},{"indexed":false,"internalType":"address","name":"pair","type":"address"},{"indexed":false,"internalType":"uint256","name":"","type":"uint256"}],"name":"PairCreated","type":"event"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"allPairs","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"allPairsLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"}],"name":"createPair","outputs":[{"internalType":"address","name":"pair","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"feeTo","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"feeToSetter","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"getPair","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_feeTo","type":"address"}],"name":"setFeeTo","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_feeToSetter","type":"address"}],"name":"setFeeToSetter","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]')

const instance = new web3.eth.Contract(abi, '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f');

//web3.eth.getBlockNumber().then(console.log)

    instance.getPastEvents(
        "allEvents",
        {fromBlock: "12908000", toBlock: "12908094"},
        (errors, events) => {
            if (!errors) {
                //console.log('it is ok')
                //console.log(events)
             }
        }
    ).then(r => {
        console.log(r)
    });