0

Now, I'm working on an Ethereum Blockchain project to transfer the ownership of stored devices by using local web3 provider at the truffle environment.

  • Truffle v5.0.27 (core: 5.0.27)
  • Solidity v0.5.0 (solc-js)
  • Node v10.15.3
  • Web3.js v1.0.0-beta.37

**Browser details

  • OS: [Windows 10]
  • Browser tried both [firefox, chrome]
  • MetaMask Version [7.0.1]

Hence, I'm attempting to query all past events that a contract has ever emitted by using the following code

App.contracts.DeviceManager.deployed().then(function(instance) {
    DeviceManagerContractInstance= instance;
    DeviceManagerContractInstance.getPastEvents('allEvents',
    { fromBlock: 0, toBlock: 'latest' })

    .then(events => console.log("events:",events))
    .catch((err) => console.error("err",err));
     })

but I was getting this error

TypeError: DeviceManagerContractInstance.getPastEvents is not a function

Also, I tried to use allEvents()

 DeviceManagerContractInstance.events.allEvents()
.on('data', (event) => {
    console.log(event);
})
.on('error', console.error);

However, it's throwing the same error.

I don't know what is missing, and if the problem belongs to localhost or, code syntax.

Any Idea,

  • What's DeviceManager and how it is constructed? – Ismael Oct 23 at 6:47
  • DeviceManager is my contract. – Byo0ona Oct 23 at 13:13
  • How is it constructed? ie it is a web3 contract or a truffle contract. Perhaps it is not being assigned correctly and DeviceManagerContractInstance is not valid. – Ismael Oct 24 at 0:44
0

1st lets clear a few things for you.

  • Past events are not stored in contract.
  • There are no built in functions like getPastEvents() and events.allEvents() to get all events.
  • Events are fired only where you emit them.

Here is an example contract

pragma solidity ^0.5.11;

contract SillyOwner {
    address owner;

    constructor() public {
        owner = msg.sender;
    }

    event OwnerChange(
        address oldOwner,
        address newOwner
    );

    function changeOwner(address _no) public { 
        require(msg.sender == owner, "Insufficient permission");
        address ow = owner;
        owner = _no;
        emit OwnerChange(ow, owner);
    }
}

Here OwnerChange event will only be emitted when changeOwner() is called and the require statement returns true.

Example frontend:

const receipt = await this.SillyOwnerInstance.changeOwner(accounts[1], { from: accounts[0] });
console.log(receipt.logs);

You will have the emited event object available inside receipt.logs.

And again after calling changeOwner() a bunch of times there is not way to determine how many times the event was emitted and what were the values.

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.