1

I have seen similar questions here, but the answers didn't help so far.

I have a JS function in a React component that should parse all the past events of the contract. The function is invoked in componentWillMount(), and my intention is to get all the event results as objects, store them in the state and then render.

The function:

getAllEvents() {
    const contract = require("truffle-contract");
    const contract= contract(ContractOne);
    contract.setProvider(this.state.web3.currentProvider);

    var contractInstance;

    this.state.web3.eth.getAccounts((error, accounts) => {
      contract.deployed().then(instance => {
        contractInstance = instance;

        var events = contractInstance.EventResults({
          fromBlock: 0,
          toBlock: "latest"
        });
        events.watch(
          function(err, result) {
            if (err) {
              console.log("error!");
            } else {
              this.setState(prevState => ({
                eventData: [...prevState.eventData, result.args]
              }));
            }
          }.bind(this)
        );
      });
    });
  }

However, I get only the very last event object rendered with this function, instead having shown quite a bunch. What could be the issue here?

1

This may or may not be of help but if you use this endpoint:

https://web3api.io/api/v1/addresses/<address>/logs

Api keys are free and easy to obtain. You can get all past events of any address. In this case it would the contract address.

Example:

const axios = require('axios')

let headers = {
    'x-api-key': 'UAK000000000000000000000000demo0001'
}
let url = 'https://web3api.io/api/v1/addresses/0x06012c8cf97bead5deae237070f9587f8e7a266d/logs'
let getCryptokittiesEvents = () => {
    axios.get(url, {'headers': headers})
    .then((response) => {
        console.log(response.data.payload.records)
    })
}

There are also options to add filters to the query. You can find more details here.

Please let me know if that is helpful at all! 🙂

  • It is helpful, but not quite what I was looking for! – Ruham Jan 14 at 22:40

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.