[Ethereum] Get Balance History of address

addressesetherexternal-apihistory

In building an IONIC app, I am attempting to retrieve the historical balance of a single Ethereum address. I am currently not sure how far back I will need to go. I know that Etherscan.IO offers a web tool to get balance from a certain date at https://etherscan.io/balancecheck-tool , however I cant find any API's that allow me to do this programmatically. I am also unable to get web3 to install in my current project. Any advise would be appreciated.

Best Answer

If you observe the balance check tool, you can see there are two options

  1. Date
  2. Block Number

If you know the block number, you can use the following function to get the historic balance

web3.eth.getBalance(address, blockNumber).then(balance => `Balance at block number is ${balance}

If you dont know the block number, but want to get it by date/time. You first need to find the block that is mined during that time.

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}`);
Related Topic