manniru
11/24/2018 - 10:27 AM

For google sheets (script). Get your balance from passing in the address for ETH and symbol for ETH tokens

For google sheets (script). Get your balance from passing in the address for ETH and symbol for ETH tokens

function getETHBalance(ethAddress) {
  var response = UrlFetchApp.fetch('https://api.etherscan.io/api?module=account&action=balance&address=' + ethAddress + '&tag=latest&apikey=UMBMA6SZ3DUAWW9C3NGRMRASSNNWRIPV51');
  var json = response.getContentText();
  var data = JSON.parse(json);
  return data.result * Math.pow(10,-18);
}

function getTokenBalance(symbol, ethAddress) {
  var response = UrlFetchApp.fetch('https://api.ethplorer.io/getAddressInfo/' + ethAddress + '?apiKey=freekey');
  var json = response.getContentText();
  var data = JSON.parse(json);
  var tokenData = {};
  for each (var token in data.tokens) {
    if (token.tokenInfo.symbol == symbol) {
      tokenData = token;
    }
  }
  
  var decimals = parseInt(tokenData.tokenInfo.decimals, 10);
  var divisor = Math.pow(10, decimals);
  var balance = parseFloat(parseFloat(tokenData.balance).toFixed());
  var result = balance/divisor;
  return result;
}