如何在节点js中使用tronweb不受限制地获取tron网络中合同的所有事件日志?

问题描述

如何在js节点中使用tronweb来获取tron网络中合同的所有事件日志?还是需要任何中间件存储(例如redis等)? 加载dapp主页之前需要一次获取所有数据。 dApp是在react js中制作的。而且Trongrid api在单个请求中有200条记录的限制。

解决方法

您可以使用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;
        }
    },

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...