Giter Site home page Giter Site logo

Comments (11)

UnforgivenZZZ avatar UnforgivenZZZ commented on July 4, 2024 3

@forhas
we all left tron so maybe don't expected those issue to be fixed

from tronweb.

ColonelJ avatar ColonelJ commented on July 4, 2024 1

In case it might help anyone, this is the code I'm using instead of using contract.Method().watch to discover the events due to this problem (seems to work reliably for recent events).

(Note that the 60 seconds on the first line is to allow for the fact that the newly loaded events will have timestamps in the past, and when onlyConfirmed is on, this gap will include the time it takes for the transaction to confirm on the solidity node. Even without onlyConfirmed there should still be at least 3 seconds because of waiting for the interval to trigger and this is also the maximum time since the previous block was mined.)

    let last_event_timestamp = Date.now() - 60000;
    let processing = false;
    setInterval(async () => {
        if (processing) return;
        processing = true;
        try {
            let events = [];
            let more_events =
                await tronWeb.getEventResult(CONTRACT_ADDRESS, {
                    onlyConfirmed: true,
                    sinceTimestamp: last_event_timestamp + 1,
                    sort: 'block_timestamp'
                });
            while (more_events.length
                   && more_events[more_events.length - 1].fingerprint) {
                events = events.concat(more_events);
                more_events =
                    await tronWeb.getEventResult(CONTRACT_ADDRESS, {
                        onlyConfirmed: true,
                        previousLastEventFingerprint:
                            more_events[more_events.length - 1].fingerprint,
                        sort: 'block_timestamp'
                    });
            }
            events = events.concat(more_events);
            if (events.length) {
                last_event_timestamp = events[events.length - 1].timestamp;
                for (let i = 0; i < events.length; ++i) {
                    if (events[i].name == 'Method') {
                        /*...*/
                    } else if (events[i].name == /*...*/) {
                        /*...*/
                    /*...*/
                    } else {
                        console.error('Unrecognized event name:', events[i].name);
                    }
                }
            }
        } catch (err) {
            console.error('Error getting events:', err);
        }
        processing = false;
    }, 3000);

This also has the advantage that it only does one request for all possible events that can be emitted by the contract. If you wanted you could add the eventName parameter to the options for getEventResult so you wouldn't have to check the name.

Maybe the contract.Method().watch functionality could use logic similar to this to achieve the desired effect.

from tronweb.

sullof avatar sullof commented on July 4, 2024

If you want to be sure you get all the events in a block, you can use TronGridJS API and retrieve the events by block. Look at https://github.com/TRON-US/trongrid-js/blob/master/test/unit/lib/block.test.js for examples.

from tronweb.

sullof avatar sullof commented on July 4, 2024

To solve the issue we will add web sockets in the future.

from tronweb.

sullof avatar sullof commented on July 4, 2024

I'll take a look at your code.

from tronweb.

forhas avatar forhas commented on July 4, 2024

Hi @sullof,
Do you know if this one is expected to be resolved anytime soon?

from tronweb.

forhas avatar forhas commented on July 4, 2024

@UnforgivenZZZ thanks for clarifying.
Is the project still maintained (by anyone)?

from tronweb.

sullof avatar sullof commented on July 4, 2024

It looks like last merge was 21 days ago by @jeancky.

from tronweb.

tigfamon avatar tigfamon commented on July 4, 2024

@sullof , @forhas is there any update on this issue? For our poker game, event listening is still not working correctly. Some events get lost.

from tronweb.

pryctoyogi avatar pryctoyogi commented on July 4, 2024

@tigfamon I did not get the chance to test the latest version.
I've implemented my own manual solution:

setInterval(getEvents, tronBlockTime * pullCycle);

async function getEvents() {
    if (isRunning) {
        return;
    }
    isRunning = true;
    try {
        const latestBlock = await tronWeb.trx.getBlock('latest');
        let start = latestEventsBlockNumber;
        let end = latestBlock.block_header.raw_data.number;
        const eventsArr = [];
        for (let i = start; i < end; i++) {
            eventsPullingOptions.block_number = i;
            const events = await tronGrid.contract.getEvents(playzaContractAddress, eventsPullingOptions);
            const blockEvents = await events.data;
            if (blockEvents && blockEvents.length > 0) {
                eventsArr.push(...blockEvents);
            }
        }
        latestEventsBlockNumber = end;
        handleEvents(eventsArr);
    } catch (err) {
        // handle errors
        }
    } finally {
        isRunning = false;
    }
}

from tronweb.

002love avatar 002love commented on July 4, 2024

@sullof bro, is the problem still not fixed? Can you please correct this misunderstanding, this is a lot of problems 😔

from tronweb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.