2

I have my sample contract as:-

 pragma solidity ^0.4.24;

contract Storage {
  uint256 storedData;

  event print(uint256 store);

  function set(uint256 data) public {
    emit print(data);
    storedData = data;
  }

  function get() constant returns (uint256) {
    return storedData;   
  }
}

After setting the value from geth console and mining it, when I use get function to check the value it returns the correct value which was set earlier. However when I try to get the event logs with the following code:-

var eve = instance.print({sender:eth.coinbase},{fromBlock: 0, toBlock: "latest"});
var allValues = eve.get();allValues[allValues.length - 1].args;
console.log(allValues[allValues.length - 1].args.store);

Here instance is the contract instance, It shows the error as cannot access member args of undefined.

  • 1
    Hi and welcome to Ethereum StackExchange.You can't call events from outside the contract - they are meant only for the contract to emit internally. So you can only trigger the event by calling your set function. – Lauri Peltonen Aug 22 '18 at 5:26
  • @LauriPeltonen He's trying to read the event from outside the contract (after calling set). – smarx Aug 22 '18 at 5:26
  • I believe the issue is {sender: eth.coinbase}. I'm not sure what that's supposed to be doing or if it's having any harmful effect, but I would try removing it. (Just use {} instead as the first parameter, since you don't want to filter at all.) – smarx Aug 22 '18 at 5:28
  • Hm, ok thanks for the heads-up, I'm not so familiar with web3 so disregard my comment :) – Lauri Peltonen Aug 22 '18 at 5:29
  • @smarx I have tried that as well, still no luck. – Abhinav Arora Aug 22 '18 at 5:45
2

Your code looks fine for me. I tried to match your code as closely as possible using Node.js and ganache. Here's my full (working) test code:

const Web3 = require('web3');
const fs = require('fs');
const solc = require('solc');
const ganache = require('ganache-cli');

const web3 = new Web3(ganache.provider());

const compiled = solc.compile(`
pragma solidity ^0.4.24;

contract Storage {
  uint256 storedData;

  event print(uint256 store);

  function set(uint256 data) public {
    emit print(data);
    storedData = data;
  }

  function get() constant returns (uint256) {
    return storedData;
  }
}
`, 0).contracts[':Storage'];

web3.eth.getAccounts((err, accounts) => {
  web3.eth.defaultAccount = accounts[0];

  const contract = web3.eth.contract(JSON.parse(compiled.interface));
  contract.new({ data: compiled.bytecode, gas: 3000000 }, (err, instance) => {
    if (instance.address) {
      instance.set(42, (err, txHash) => {
        instance.print().get((err, logs) => {
          console.log(logs[0].args.store.toString()); // OUTPUT: 42
        });
      });
    }
  });
});
  • thanks for the answer. Your code looks pretty close to what I am trying. I still don't know why mine gives the above error. – Abhinav Arora Aug 22 '18 at 6:23
  • 2
    I was hoping you could compare your code to mine and try to find a difference. If that's not helping, perhaps you can share the rest of your code/methodology. (E.g. share how you're compiling and deploying the code, your calls to set and get, etc. Everything someone would need to replicate your test.) – smarx Aug 22 '18 at 6:30
  • For deploying contract, we are using .js file as follows: var interfaceContent = eth.contract(ABI) var byteCode = "0x" + "BYTE_CODE" var c = interfaceContent.new({from:eth.accounts[0],data:byteCode,gas: 4700000}, function(e, contract){ if(e) { console.error(e); return; } if(!contract.address) { console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined..."); } else { console.log("Contract mined! Address: " + contract.address); console.log(contract); } }) – Abhinav Arora Aug 22 '18 at 9:37
  • Then in the geth console I am deploying the contract as loadScript(filenam.js) and waiting till the contract gets mined to get the address. After that I am getting contract instance as instance = eth.contract(ABI).at("ADDRESS") For setting a value, instance.set(4123) and waiting for the transaction to get mined. Now after this if I check the value as var v = instance.get() and console.log(v), it shows me the new value. However if I try to read the event as mentioned above , it says :- TypeError: Cannot access member 'args' of undefined at <anonymous>:1:1 – Abhinav Arora Aug 22 '18 at 9:47

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.