Skip to content

Instantly share code, notes, and snippets.

@uneasyguy
Created September 11, 2018 19:55
Show Gist options
  • Save uneasyguy/b28623807715bfaa2b2e96e4fb66a504 to your computer and use it in GitHub Desktop.
Save uneasyguy/b28623807715bfaa2b2e96e4fb66a504 to your computer and use it in GitHub Desktop.
function BinanceAccountInfo() {
Logger.clear()
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("account_info");
var key = sheet.getRange(1,2).getValue() ;
var secret = sheet.getRange(2,2).getValue();
var curTime = new Date().getTime();
var timestamp = "timestamp=" + curTime;
var signature = Utilities.computeHmacSha256Signature(timestamp, secret);
signature = signature.map(function(e) {
var v = (e < 0 ? e + 256 : e).toString(16);
return v.length == 1 ? "0" + v : v;
}).join("");
var params = {
'method': 'get',
'accept': 'application/json',
'headers': {'X-MBX-APIKEY': key},
'muteHttpExceptions': true
};
var url = "https://api.binance.com/api/v3/account?" + timestamp + "&signature=" + signature;
var data = UrlFetchApp.fetch(url, params);
var json = data.getContentText();
Logger.log(json)
var data= JSON.parse(data)
var balances = data.balances;
var currency = sheet.getRange(3,2).getValue();
for (var i = 0; i < balances.length; i++) {
if (balances[i]["asset"] == currency){
var available_balance = balances[i]["free"];
sheet.getRange(4,2).setValue(available_balance);
}
}
}
@danicuki
Copy link

danicuki commented Oct 5, 2021

I also got this message all the time "Way too much request weight used; IP banned until 1628850703938"
The solution I found was to create a node.js proxy using Google Cloud Functions.

const Binance = require('node-binance-api');
const API_KEY = 'XXXX'
const binance = new Binance().options({
  APIKEY: API_KEY,
  APISECRET: 'XXX'
});

exports.binanceBalance = (req, res) => {
  binance.balance((error, balances) => {
    result = []
    console.log(balances)
    for (b of Object.keys(balances)) {
      result.push({
        ticker: b,
        value: Number(balances[b].available)
      });
    };
    result = result.filter(b => b.value > 0.000000000)
    res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
    res.send(JSON.stringify(result));
  });
};

Then I put this on my AppScript:

function BinanceAccountInfo(ticker) {
  var result;

  var cache = CacheService.getScriptCache();
  var cached = cache.get(ticker);
  if (cached != null) {
    return Number(cached);
  } else {
     result = binanceDataViaHttp()
     if (result && result.length > 0) {
       for (r of result) {
         cache.put(r.ticker, r.value, 1800);
       } 
     }
  }

  Logger.log(JSON.stringify(result))
  return result.filter(b => b.ticker === ticker)[0].value
}

function binanceDataViaHttp() {  
  url = "https://us-central1-MYPROJECT.cloudfunctions.net/binanceBalance"
  var data = UrlFetchApp.fetch(url);
  Logger.log(data)
  return JSON.parse(data);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment