简体   繁体   中英

Persistent event state in smart contract with web3

When an event (Something) is triggered in a contract, the page console logs the output as intended (through somethingEvent.watch()), but when I reload the page, it console logs the same output. Is the most recent event meant to persist or am I not using it properly? Thanks.

// Solidity
event Something(string message);

function doSomething(string newMessage) payable {
    Something(newMessage);
}

// Web3
var somethingEvent = contractInstance.Something();

somethingEvent.watch(function(error, result) {
  console.log(result.args.message);
});

This will happen whenever you call watch() and the latest block number is the same as the previous execution. When an event is "fired", all the EVM is doing is writing out the event information to the log once the transaction is successfully mined.

When you add a watch to a contract, you can specify which block you want to start listening on. If you don't pass in anything (as your code is doing), it defaults to the latest block (see filter API docs ). So, when you reload the page and a new block hasn't been added to the chain, you will get the same event.

If you want to avoid duplicate events, use result.blockNumber to remember the last block read and increment it by 1 whenever establishing a new watch.

let blockNumber = 'latest'; // should come from some state initially set to 'latest'

...

var somethingEvent = contractInstance.Something({}, {fromBlock: blockNumber);

somethingEvent.watch(function(error, result) {
  blockNumber = result.blockNumber + 1;
  console.log(result.args.message);
});

Note that watch may not be the best thing to use in this case. It depends on how stateful your client is. You should always call stopWatching whenever you're done listening for events. If you don't want to set up a watch, you can also use get which will send back all events that match your filter criteria.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM