0

I have events (fired some time ago by the contract) containing numbers. I need to load them all, and then to draw a chart.

What I do:

contract.getInitBlock(function(error, value)
{
    var init_block = value.toNumber();
    var prevTransactions = contract.updateHistory({_from: web3.eth.coinbase}, 
        {fromBlock: init_block, toBlock: 'latest'}); 
    prevTransactions.watch(function(err, result) 
    {
        if(err) 
        {
            return;
        }

        add_info_to_history(result);

    });
});

window.setTimeout(function() 
{
    chart();
}, 3000);

But this is a bad style.

Question: how can I find out that I got ALL events iterated and it is time to draw chart?

0

Instead of using .watch() which listens for new events being fired, you can use .get() to retrieve all past events fired.

Something like:

contract.getInitBlock(function(error, value)
{
    var init_block = value.toNumber();
    var events = contract.[[EVENT_NAME]]({[[INDEX_NAME]]:[[INDEX_VALUE]]},{fromBlock: 0, toBlock: 'latest'});
    events.get(function(err, result) 
    {
        console.log("Events that matched: ",result);
        // Iterate through events or do whatever you want with the resulting array
    });
});

[[EVENT_NAME]] should be the name of the event

[[INDEX_NAME]] and [[INDEX_VALUE]] should be used if you want to filter the events by some idex you used when you fired them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.