Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minBlockTimestamp not working with contract.watchEvents #36

Open
sjaiswal1701 opened this issue Apr 14, 2020 · 0 comments
Open

minBlockTimestamp not working with contract.watchEvents #36

sjaiswal1701 opened this issue Apr 14, 2020 · 0 comments

Comments

@sjaiswal1701
Copy link

When you supply additional optional like minBlockTimeStamp it will not work because of extra call to the eventWatcher function at line no 129.

Under file src/lib/core/Contract.js

async watchEvent(contractAddress, eventName, options = {}, callback = false) {
        let listener = false;
        let lastBlock = false;
        let since = Date.now() - 1000;

        if (utils.isFunction(options)) {
            callback = options;
            options = {};
        }

        if(!utils.isFunction(callback)) {
            throw new Error('Invalid callback function provided');
        }

        const eventWatcher = async () => {
            try {
                options = Object.assign({
                    eventName,
                    minBlockTimestamp: since,
                    orderBy: 'timestamp,desc',
                    // TODO:
                    // add filters => eventron is already equipped for them
                    // filters: options.filters
                }, options)

                let events;

                if(options.only_data_and_fingerprint) {
                    events = await this.getEvents(contractAddress, options);
                } else {
                    const response = await this.getEvents(contractAddress, options);
                    events = response.data;
                }

                const [latestEvent] = events.sort((a, b) => b.block_timestamp - a.block_timestamp);

                const newEvents = events.filter((event, index) => {
                    const duplicate = events.slice(0, index).some(priorEvent => (
                        JSON.stringify(priorEvent) == JSON.stringify(event)
                    ));

                    if (duplicate) return false;

                    if (!lastBlock) return true;

                    return event.block_timestamp > lastBlock;
                });

                if (latestEvent) lastBlock = latestEvent.block_timestamp;
                return newEvents;

            } catch (ex) {
                return Promise.reject(ex);
            }
        };

        const bindListener = () => {
            if (listener)
                clearInterval(listener);

            listener = setInterval(() => {
                eventWatcher().then(events => events.forEach(event => {
                    callback(null, event)
                })).catch(err => callback(err));
            }, 3000);
        };

        await eventWatcher(); // here it will load the previous events but did not call the callback with those events.
        bindListener();

        return {
            start: bindListener(),
            stop: () => {
                if (!listener)
                    return;

                clearInterval(listener);
                listener = false;
            }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant