0

I'm experimenting a little bit with the blockchain, I'm currently trying to send from an oracle an address to store on the blockchain through a smart contract. The code (very simple and without checks) is the following:

function addNewContract(address _newAddress) public {
    emit LoggingStrings("New address added!");
    //require(msg.sender == oracleAddress); --> commented because I don't need this at the moment, just playing
    contractAddressArray.push(_newAddress);
    contractNumber++;
    emit NewAddressAdded(_newAddress);
}

I correctly log the _newAddress received in the Node.js console of my DAPP, but this happens only once. I have a routine that every 10 seconds repeats the same code to store a contract on the blockchain:

export const addNewContract = (contractAddress) => {
 return new Promise((resolve, reject) => {
  account().then(account => {
    console.log("Adding new contract: " + contractAddress);
    contract.addNewContract(contractAddress, {from: account}, (err, res) => {
      console.log("New contract added from frontend: " + contractAddress + "  " + err);
      if(err === null){
        resolve(res);
      } else {
        reject(err);
      }
    });
  });
 }); 
};

I see the console.log prints, but I don't see the events emitted anymore. Is that because that's how it is supposed to work, or am I doing something wrong?

Thanks!

EDIT: Here is the code of the logging

export const update = (callback) => {
  contract.LoggingString((error, result) => callback(error, result));
}

const consume = () => {
  console.log("consumer started");
  update((error, result) => {
    console.log("NEW EVENT");
    console.log("DATA: ");
    console.log(result.args);
    console.log("\n");
  });
};
  • Where's the code for how you see the first emittance of the LoggingStrings(...) event log? – gskapka Mar 21 at 15:30
  • Edited the question and added! – Masiar Mar 21 at 15:55
1

Your update function appears to only consume your event once:

export const update = (callback) => {
  contract.LoggingString((error, result) => callback(error, result));
}

Once the callback you pass into this function has run, execution is complete and so further events have no callbacks to fire.

Instead, you want to use web3 to subscribe to a stream of events from your contract via:

myInstantiatedContract.contract.events.LoggingStrings([options][, callback])

...which will fire your callback for every event that is seen.

You can find more info in the docs here.

  • I tried to follow the link on the docs without success (can't find the docs related to events). myContract.events.LoggingStrings([options][, callback]) this doesn't work as "events" appears to be undefined thus I cannot call LoggingStrings – Masiar Mar 25 at 8:57
  • I've edited the link I posted to go to the section in docs on events in web3, where that piece of code I posted was taken from (and edited to fit your code). If events are undefined, you may be using a different version of web3 where events may be found via: myInstantiatedContract.contract.events. – gskapka Mar 25 at 9:23

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.