Web3 BSC 配对合约返回 -32602 错误

问题描述 投票:0回答:1

我正在尝试加载 Pancake Swap 上 BSC 对的所有同步事件。我正在使用免费的 quiknode BSC 节点和 nodejs web3 库之一。

请求如下所示:

const blocksPerRequest = 10;
    const reserves = await pairContract.getPastEvents('Sync', {
      filter: {},
      fromBlock: blockNumber - BigInt((i +1) * blocksPerRequest),
      toBlock: blockNumber - BigInt(i * blocksPerRequest),
    });

i 变量设置为 0。

我收到此错误,请求限制为 10000,但我只加载 10 个块的这些事件:

innerError: {
    code: -32602,
    message: 'getMultipleAccounts, eth_getLogs, and eth_newFilter are limited to a 10000 range'
  },
  code: 101,
  data: undefined,
  request: {
    jsonrpc: '2.0',
    id: '96ec05fe-b8ad-4cfa-8abf-9eaf7fb22c69',
    method: 'eth_getLogs',
    params: [
      {
        fromBlock: '0x20793fc',
        toBlock: '0x2079406',
        topics: [
          '0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1'
        ],
        address: '0x8ff7115defd48165aa3f70153a644e8f58e71f42'
      }
    ]
  }```

This was working a few months ago.
node.js web3js bsc
1个回答
0
投票

该问题指出请求限制为 10,000,这可能是由于您正在使用的 QuikNode BSC 节点施加的限制造成的。节点通常对一次调用中可以返回的记录数量有限制,以防止过载。此限制与您正在查询的块数无关,而是与节点愿意在单个请求中返回的事件日志(在您的情况下为“同步”事件)总数有关。

const Web3 = require('web3');
const web3 = new Web3('YOUR_QUICKNODE_BSC_URL');

// Assuming you have already initialized the pairContract
// ...

async function loadSyncEvents(pairContract, startBlock, endBlock, maxBlocksPerRequest) {
    let events = [];
    while (startBlock <= endBlock) {
        let toBlock = Math.min(startBlock + maxBlocksPerRequest - 1, endBlock);
        try {
            let batchEvents = await pairContract.getPastEvents('Sync', {
                fromBlock: startBlock,
                toBlock: toBlock,
            });
            events = events.concat(batchEvents);
            startBlock = toBlock + 1;
        } catch (error) {
            console.error(`Error fetching events for blocks ${startBlock} to ${toBlock}: ${error}`);
            if (maxBlocksPerRequest > 1) {
                // If error, try with fewer blocks
                maxBlocksPerRequest = Math.floor(maxBlocksPerRequest / 2);
            } else {
                // If already at minimum block range, log and skip ahead
                console.error(`Skipping blocks ${startBlock} to ${toBlock}`);
                startBlock = toBlock + 1;
            }
        }
    }
    return events;
}

// Example usage:
const startBlock = /* Your start block number */;
const endBlock = /* Your end block number */;
const maxBlocksPerRequest = 10; // You can adjust this based on your requirements

loadSyncEvents(pairContract, startBlock, endBlock, maxBlocksPerRequest)
    .then(events => {
        console.log(`Loaded ${events.length} Sync events`);
        // Process events...
    })
    .catch(error => {
        console.error('Failed to load events:', error);
    });

上面是一个演示代码,您可以在其中添加动态块范围调整。希望对您有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.