emmtte / list

Google Sheets automatic creation with Google Apps Script (GAS) for managing a cryptocurrency tracking spreadsheet with multi exchanges

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kucoin.js is blank

Popcaughan opened this issue · comments

Your other code is looking good but the kucoin code is blank.

Thanks!

Poloniex and Kucoin are blank because I have still some problems with the code.
Need to work on it

Well, here's the code I use for Polo and it works fine.

// THIS IS THE PART THAT SHOWS YOU HOW TO DO PRIVATE CALLS with the POST METHOD and your Poloniex secret key

function getPoloBalances(key, secret){

var api_key = key;
var api_secret = secret;

// First you create a nonce, which is an incremental random number, to do so, we can use the current date time summed with a number
var nonce = 9465426902234426 + new Date().getTime();

// First choose a command from here https://poloniex.com/support/api/ seeing if it require specific options
// Then, we set the variable 'p' as a string which combine the command & any related parameter (if any) & the nonce.
// in this case we specified the account=all parameter for the command=returnCompleteBalances as it gives us also loans and on orders balances

var p = "command=returnCompleteBalances&account=all&nonce="+nonce;

// Then, we sign this variable 'p' with our secret key (taken from the API of Poloniex) to obtain a new string 'signature'
var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, p, api_secret);

// Then, we convert the resulting string: from an array of byte (which is a standard output) to HEX
signature = signature.map(function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')

// Then we create the variable 'headers' and we specify in the object "Key" the API key associated with the secret key (always taken from the API of Poloniex), and we pass the 'signature' output string to the object "Sign"
var headers = {
"Key" : api_key,
"Sign" : signature
};

// then we define 'options' as POST method, specifying the headers and the payload

var options = {
"method" : "POST",
"headers": headers,
"payload": p
};

// Then we fetch the url passing the 'options' which make us call the command and sign it

var url = "https://poloniex.com/tradingApi";
var response2 = UrlFetchApp.fetch(url, options);

// Then we parse the fetched url in the var 'json2'
var json2 = JSON.parse(response2.getContentText());

@ManuCart I am working on a similar project but with a focus on getting the order history maybe we help each other a bit. I am not a very experienced developer but I am trying to adapt this rest client for Google Apps: https://github.com/Satoshinaire/kucoin-api

@jceelen Good to see that, I was first focused too with order history but it was to complex for me because we need to work with ledger, BNB fees, ETH market... Too much solution to implement but have no much time. Good luck!

@Popcaughan thanks for the code added Poloniex
Need to work on Kucoin

Hi, below is what I've got for kucoin. Any chance you could take a look at Bitstamp? At the moment my Google Sheet connects to a VPS running node.js code to download stamp balances, but I'd love to do it directly.

Please note that Bitstamp requires API key, secret and customer number to create your signature.

Anyway, here's the kucoin code:

function kucoin(){

 var host = 'https://api.kucoin.com';
 var endpoint ='/v1/account/balance';
 var nonce = '' + Date.parse(new Date());
 var strForSign = endpoint + '/' + nonce + '/';
 var signatureStr = Utilities.base64Encode(strForSign, Utilities.Charset.UTF_8);
 var digest = Utilities.computeHmacSha256Signature(signatureStr, priv_key, Utilities.Charset.UTF_8);

 // https://pthree.org/2016/02/26/digest-algorithms-in-google-spreadsheets/
 var hexstr = '';
 for (i = 0; i < digest.length; i++) {
   var val = (digest[i]+256) % 256;
   hexstr += ('0'+val.toString(16)).slice(-2);
 }

 var url = host + endpoint;
 var options = {
   'headers' : {
     'KC-API-KEY': pub_key,
     'KC-API-NONCE': nonce,
     'KC-API-SIGNATURE': hexstr
    }
 }

 var jsondata = UrlFetchApp.fetch(url, options);
 var data   = JSON.parse(jsondata.getContentText());

// {msg=Operation succeeded., code=OK, data=[{coinType=KCS, balance=0, freezeBalanceStr=0.0, balanceStr=0.0, freezeBalance=0}, {coinType=XRB, balance=0, freezeBalanceStr=0.0, balanceStr=0.0, freezeBalance=0},
for(var x in data.data){
var balance = parseFloat(data.data[x].balance);
if (balance > 0) {
var asset = data.data[x].coinType
array.push({'currency': asset, 'balance': balance, 'market': "Kucoin"});
}
}
}

Hi @Popcaughan
Thank you for your help for Poloniex and Kucoin.
I try to find something for Bitstamp.
I will work on it.
I close this issue, feel free to open an other about bitstamp