1

Let's say I have a uint public myvar in my contract that is initialized to some value.

Then, myvar gets set to 3 in the next block.

How can I retrieve the old value? Is this possible with web3?

Edit

Let's assume there are no Events that get emitted when myvar is changed.

1

If the function that changes myvar's value fires an event, you could retrieve the event log from web3 to get the data.

event MyEvent(uint indexed dateModified, uint prevValue, uint newValue);

function changeValue(uint newValue){

  uint prevValue = myvar;
  myVar = newValue;

  MyEvent(now, prevValue, newValue);

}

(Indexing dateModified will let you filter by date for example)

Each time the function is called, the

Then in web3 you would watch (get new events as they happen) or get them (get all past events).

From web3 docs:

var MyContract = web3.eth.contract(abi);
var myContractInstance = MyContract.at('0x78e97bcc5b5dd9ed228fed7a4887c0d7287344a9');

// watch for an event with {some: 'args'}
var myEvent = myContractInstance.MyEvent({some: 'args'}, {fromBlock: 0, toBlock: 'latest'});
myEvent.watch(function(error, result){
   ...
});

// would get all past logs again.
var myResults = myEvent.get(function(error, logs){ ... });

...

// would stop and uninstall the filter
myEvent.stopWatching();

One more thing: Event logs can only be accessed from outside Solidity, so if you wanted to get your variable's previous values from inside solidity you would need to code your own logic that stores the previous values in an array, probably.

  • I see, that's a very practical solution. Let's say that the function did not emit a log- how could that old value be accessible? – will_durant Dec 6 '17 at 16:28
  • 1
    It's not possible. Or rather, extremely complicated. See this answer: ethereum.stackexchange.com/a/1061/20200 – pabloruiz55 Dec 6 '17 at 16:42

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.