1

如何在节点js中使用tronweb无限制地获取tron网络中合约的所有事件日志?或者是否需要任何中间件存储,如 redis 等?需要在加载 dapp 主页之前一次性获取所有数据。dApp 是用 react js 制作的。并且 Trongrid api 在单个请求中有 200 条记录的限制。

4

1 回答 1

3

你可以使用fingerprint(它像继续令牌一样工作)

async getContractTransferEventsByUser(eventName, userId) {
        let result = [];
        let tronGrid = new TronGrid(this.tronWeb);
        try {
            let continueToken = '';
            while (true) {
                let res = await tronGrid.contract.getEvents(YOUR_CONTRACT_ADDRESS, {
                    only_confirmed: true,
                    event_name: eventName,
                    limit: 200,
                    fingerprint: continueToken,
                    order_by: "timestamp,asc",
                    min_timestamp: minTime, //remove if you don't need it
                    filters: { id: userId.toString() } //if you need to filter events by one or more values, for example, by user id (if this information is presented in event log), remove if you don't need it.
                });

                if (!res.success) {
                    console.warn("Can't get events for the contract");
                    break;
                }

                result = result.concat(res.data);

                if (typeof res.meta.fingerprint !== 'undefined') {
                    continueToken = res.meta.fingerprint;
                } else {
                    break;
                }
            }
        } catch (error) {
            console.error(error);
        } finally {
            return result;
        }
    },
于 2020-10-19T16:24:10.067 回答