2

I need calcualete balance on the date to acconut, but web3 balance and etherscan.com api is missmath. How correct to calculate balance from the available data of api.

const web3 = new Web3("https://ropsten.infura.io/erKFM1jCovzvFHE1wBah");

BigNumber.config({ ERRORS: false });

const url = "http://ropsten.etherscan.io/api?";
const key = "apikey=HJ583R13XWNWUZVYB1H8J7XVGRDKEPJK9U";
const address = "0x5360486ef76c4c857675c8212f885f7757510d9f";

axios.get(`${url}module=account&action=txlist&address=${address}&startblock=0&endblock=99999999&sort=asc&${key}`)
.then(res => {
  const result = res.data.result;

  const input = [];
  const output = [];
  let balance = new BigNumber(0);

  result.forEach(item => {
    const value = new BigNumber(item.value);
    const gasUsed = +item.gasUsed;
    const gasPrice = +item.gasPrice;
    const cumGasUsed = +item.cumulativeGasUsed;


    if(item.isError === "0" && item.txreceipt_status !== "0") {
      if(item.to === address) {
        input.push({
          value: value,
          gasUsed: 0,
          time: moment.unix(item.timeStamp).format("DD-MM-YYYY").toString(),
        });

        balance = balance.plus(value);
      }
      if(item.from === address) {
        output.push({
          value: value,
          gasUsed: gasUsed,
          time: moment.unix(item.timeStamp).format("DD-MM-YYYY").toString(),
        });

        balance = balance.minus(value);
        balance = balance.minus(gasUsed * gasPrice)
      }
      if(item.from === item.to) {
        console.log("self", item.value);
      }
    }
  });
  console.log("api balance", web3.utils.fromWei(balance.toString(), "ether"));
  web3.eth.getBalance(address)
  .then(balance => console.log("web3 balance", web3.utils.fromWei(balance, "ether")));
})
.catch(console.log);
1

A couple of things to consider

  • txlist From the documentation will only list transaction from 'normal' accounts, ie it will not list 'internal transactions', it will also not list mined blocks.

  • txlist will only return the last 10000 transactions.

| improve this answer | |
0

There are several types of transactions that can impacte the balance of an account returned by web3js.eth.getBalance() function. Check different rpc in the api list of etherscan https://etherscan.io/apis#accounts

internal transactions (gas used to interact with contracts) and address you look for may also have been used for mining, thus it should have earned ether from mined blocks (which will not be in the list of normal transactions returned by etherscan's first api. Try the one dedicated for it)

as ismael already mentionned, you may also consider paging, since the account may have more than 10000 transactions in his history.

Once you include all this in your calculation, the results should match with web3js, in theory :)

Let us know if you resolved the mistery

| improve this answer | |

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.