4

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.

1
3

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}`);
3
  • I really appreciate the assistance on this. However, as I stated, I am unable to get web3 for use in my project, do you know of another way other than web3?
    – Kontra
    Feb 20 '18 at 20:40
  • Can you be more specific on what is not working with web3?
    – Sumanth
    Feb 21 '18 at 14:59
  • There was an issue with the package itself. I was able to get it to install. Thanks!
    – Kontra
    Feb 21 '18 at 17:17
2

You can use the defaultBlock parameter to get the historic balance. For example, using web3.js v1.0:

web3.eth.getBalance(account, 10000).then(balance => {console.log(`balance for ${account} at block 10000 is ${balance}`)});

Regarding the fact that you can't install web3 in your project, you should create another question, giving more details with what you tried and the errors you encounter.

0

You'll have to iterate the chain to do this as you only have the timestamp and the Etherscan API only has this service for PRO users

I recently had to do this and I had to traverse the whole chain, to optimize it, I implemented a Binary Search algo that significantly reduced the time complexity

async function getHistoricBlockByTimestamp(historicTimestamp, provider) {

let lowerBound = 0;
let upperBound = await provider.getBlockNumber();

let midPoint = 0;

while (lowerBound <= upperBound) {
    midPoint = Math.floor(lowerBound + ((upperBound - lowerBound) / 2));
    // Get the block details using midPoint
    let blockDetails = await provider.getBlock(midPoint);

    // Compare timestamp from block with given timestamp
    if (blockDetails.timestamp === historicTimestamp) {
        continue;
    } else if (blockDetails.timestamp > historicTimestamp) {
        upperBound = midPoint - 1;
    } else {
        lowerBound = midPoint + 1;
    }
}

return midPoint;

}

Then you can use the block number of the closest block which is your midpoint in this case, this way

let oldBlock = await provider.getBlock(blockAtTimestamp);

let balanceAtTimestampBigNumber = await provider.getBalance(address, blockAtTimestamp);
balanceAtTimestamp = balanceAtTimestampBigNumber.toString() / 10 ** 18;

Note

I used https://www.docs.ethers.io here, taking advantage of the EtherscanProvider API

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.