-1

now that I've finally switched to websocket in Binance, I cant parse data from the response anymore like I used to do with my old script. I am using Javascript with Google Apps Script

function balanceBinance() {
  
  var key = '';
  var secret = '';

  var curTime = Number(new Date().getTime()).toFixed(0);
  var string = "type=SPOT&timestamp=" + curTime;
  var sKey = Utilities.computeHmacSha256Signature(string, secret);
  sKey = sKey.map(function(e) {
      var v = (e < 0 ? e + 256 : e).toString(16);
      return v.length == 1 ? "0" + v : v;
  }).join("");
  var params = {
    'type':'SPOT',
    'method': 'GET',
    'headers': {'X-MBX-APIKEY': key},
    'muteHttpExceptions': true
  };

  var url = "https://api.binance.com/sapi/v1/accountSnapshot?" + string + "&signature=" + sKey;
  var data = UrlFetchApp.fetch(url, params);
  var data = JSON.parse(data.getContentText());  
  var data = data.snapshotVos; 

Logger.log(data);
}

it returns:

[{data={totalAssetOfBtc=0.0000117, balances=[{free=0.0000117, asset=BNB, locked=0}, {asset=BTC, locked=0, free=0.00000093}, {free=0.00026, asset=BTCDOWN, locked=0}, {locked=0, free=0.00, asset=EUR}, {asset=USDT, locked=0, free=0.06716916}]}, type=spot, updateTime=1.622851199E12}, {updateTime=1.622937599E12, data={totalAssetOfBtc=0.0000117, balances=[{asset=BNB, locked=0, free=0.0000117}, {locked=0, asset=BTC, free=0.00000093}, {locked=0, free=0.00026, asset=BTCDOWN}, {locked=0, free=0.34585555, asset=EUR}, {free=0.06716916, locked=0, asset=USDT}]}, type=spot}, {type=spot, updateTime=1.623023999E12, data={balances=[{locked=0, asset=BNB, free=0.0000117}, {asset=BTC, locked=0, free=0.00000093}, {asset=BTCDOWN, locked=0, free=0.00026}, {free=0.34585555, locked=0, asset=EUR}, {free=411.06716916, locked=0, asset=USDT}], totalAssetOfBtc=0.11298258}}, {updateTime=1.623110399E12, type=spot, data={balances=[{free=0.0000117, locked=0, asset=BNB}, {asset=BTC, locked=0, free=0.00000093}, {locked=0, asset=BTCDOWN, free=0.00026}, {asset=ETHDOWN, free=0.00376, locked=0}, {free=0.34585555, asset=EUR, locked=0}, {free=0.40578148, locked=0, asset=USDT}], totalAssetOfBtc=0.0000117}}, {data={totalAssetOfBtc=0.0000117, balances=[{free=0.0000117, locked=0, asset=BNB}, {free=0.00000093, asset=BTC, locked=0}, {free=0.00026, asset=BTCDOWN, locked=0}, {free=0.00376, asset=ETHDOWN, locked=0}, {free=0.34585555, asset=EUR, locked=0}, {free=0.40578148, locked=0, asset=USDT}]}, type=spot, updateTime=1.623196799E12}]

in the previous version of the script I was able to extract the free paramater for any corresponding asset, with the following code:

var data = data.filter(function (el) {
     return el.free > 0; 
});
  
  var i;
  for (i=0; i < data.length; i++) {
 
  var asset = data[i].asset;
  var free  = data[i].free; 
    if (free  < 1)                {var a = parseFloat(free);   var a = a.toFixed(8);   var free = parseFloat(a);} 
    if (free  > 1 && free < 10)   {var a = parseFloat(free);   var a = a.toFixed(4);   var free = parseFloat(a);}
    if (free  >=10)               {var a = parseFloat(free);   var a = a.toFixed(2);   var free = parseFloat(a);}
    
  var row=rows.indexOf(asset)+2;   
     if (row>0){
       sheet.getRange(row, 13).setValue(free);}    
  }
John Galassi
  • 309
  • 2
  • 16

1 Answers1

1

Your JSON structure is like this

snapshotVos = [
  { data: { balances: [{asset,free}, {asset,free}] } },
  { data: { balances: [{asset,free}, {asset,free}] } },
  { data: { balances: [{asset,free}, {asset,free}] } } ]

Sample Code:

function parseJson() {
  var sampleJson ={msg:'', snapshotVos:[{updateTime:1.622851199E12, data:{totalAssetOfBtc:0.10958809, balances:[{asset:'BNB', free:0.0000, locked:0}, {asset:'BTC', locked:0, free:0.00000093}, {asset:'BTCDOWN', free:0.00026, locked:0}, {free:2981.34585555, asset:'EUR', locked:0}, {free:411.06716916, asset:'USDT', locked:0}]}, type:'spot'}, {data:{balances:[{asset:'BNB', locked:0, free:0.0000117}, {asset:'BTC', locked:0, free:0.00000093}, {asset:'BTCDOWN', locked:0, free:0.00026}, {locked:0, free:2981.34585555, asset:'EUR'}, {asset:'USDT', free:411.06716916, locked:0}], totalAssetOfBtc:0.11403012}, updateTime:1.622937599E12, type:'spot'}], code:200.0}; 

  var sampleJsonStr = JSON.stringify(sampleJson);
  var data = JSON.parse(sampleJsonStr);
  var balances = []

  data.snapshotVos.forEach( snap => {
    balances.push(snap.data.balances);
  });
  Logger.log(balances);
  Logger.log(balances.flat());
  
  var data = balances.flat().filter(function (el) {
      return el.free > 0; 
  });

  Logger.log(data);
  for (i=0; i < data.length; i++) {
 
    var asset = data[i].asset;
    var free  = data[i].free; 
    Logger.log("asset: "+asset+" "+"free: "+free);
  }

}

What it does?

  1. I created a JSON object from your sample data, convert it to string using JSON.stringify() then convert it back to a json object using JSON.parse()
  2. I combined all data.balances array within snapshotVos array into a single balances array. I used array.flat() to change 2-d array into 1-d array before filtering it based on your given criteria.
  3. The data variable should already match your for-loop.
Ron M
  • 5,791
  • 1
  • 4
  • 16
  • 1
    please let me know if you still encountered some issues – Ron M Jun 09 '21 at 19:45
  • thanks very much for your answer and help. It seems fine at the moment, and definitely something I couldnt be able to do on my own: let me just test it in the script within the next day or two and I'll get back to you and in case mark your answer right. In the meanwhile, I hope I can ask you in case I need extra explanation. Thanks again – John Galassi Jun 09 '21 at 21:39
  • sure, no problem – Ron M Jun 09 '21 at 21:40
  • thanks. Provided that I will need to dive a bit deeper into it, I just wanted to ask you one thing out of curiosity for the time being, and that simply is: why do we need to convert the data to string and then back to Json? Wasn't it in the right form to begin with? thanks – John Galassi Jun 10 '21 at 08:15
  • Oh its just for the sake of reproduction from my side :) I showed you that from a string json, it could be successfully parsed using json.parse() – Ron M Jun 10 '21 at 13:46