3

is there a way to compare the balance of an address at two different times or in general check the balance of an address at a specific time?

I would like to do something like...

uint256 public startTime
address public exampleAddress

function exampleFunction () public constant returns (uint value) {
    if (balanceOf(exampleAddress) < balanceOf_atStartTime(exampleAddress)) {
        returns X;
    }
}

My preferred input would be a variable like defined above, but an Unix Timestamp or an event would be suitable as well.

Does anyone know a way to do this kind of operation?

Thank you very much in advance

2

Short answer: No you can't get an address's past balance through Solidity. But there is a solution using web3js.

Long answer: You could do something like this to get the balance of an address at a certain time as seen here: Get Balance History of address

with the following code.

let blockNum = web3.eth.blockNumber;
const historicTimestamp = new Date(historicDate).getTime();
while(true) {
  const block = web3.eth.getBlock(blockNum);
  if(block.timestamp < historicTimestamp) break;
  --blockNum;
}

//The blockNumber here is your required block number
web3.eth.getBalance(address, blockNumber).then(balance => `Balance at block number is ${balance}`);

Then you could pass it to your smart contract and compare the two balances there or compare in web3 completely.

0

I don't think this can be done from the smart contract itself.

The best bet you could have is to try using the blocknumber parameter from web3 and estimate the block number as close as possible to your desired timestamp.

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.