Skip to content

Instantly share code, notes, and snippets.

@learntheropes
Last active June 2, 2019 01:18
Show Gist options
  • Save learntheropes/ee51e015ed5149995c4e9b8ce7394432 to your computer and use it in GitHub Desktop.
Save learntheropes/ee51e015ed5149995c4e9b8ce7394432 to your computer and use it in GitHub Desktop.
Poloniex private API for Google Apps Script (Google Sheet)
// work in progress
// you need a poloniex API key and secret with trading option enabled
// you can test it with:
// = polo("returnBalances","BTC")
// or
// = polo("returnBalances","all")
// or buy and sell:
// polo("BUY","BTC_LTC", 0.0251, 1) or polo("SELL","BTC_LTC", 0.0251, 1)
function polo(command,currencyPair,rate,amount){
// fillOrKill, immediateOrCancel, postOnly set to true are missing
poloniex_auth_(command,[{key: "currencyPair",value: currencyPair},{key: "rate",value: rate},{key: "amount",value: amount}], function(result){
Logger.log(result)
return JSON.stringify(result)
})
}
function poloniex_auth_(command,params,callback) {
// I assume that all the keys are in the "keys" spreadsheet. The key is in cell B4 and the secret in cell C4.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("keys");
var key = sheet.getRange("B4").getValue();
var secret = sheet.getRange("C4").getValue();
var nonce = 1495932972127042 + new Date().getTime();
var payload = {
"nonce": nonce,
"command": command
}
params.forEach( function(param){
payload[param.key] = param.value;
})
var payloadEncoded = Object.keys(payload).map(function(param) {
return encodeURIComponent(param) + '=' + encodeURIComponent(payload[param]);
}).join('&');
var uri = "https://poloniex.com/tradingApi";
var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, payloadEncoded, secret);
var stringSignature = signature.map(function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
var headers = {
"key": key,
"sign": stringSignature
}
var params = {
"method": "post",
"headers": headers,
"payload": payloadEncoded
}
var response = UrlFetchApp.fetch(uri, params);
var dataAll = JSON.parse(response.getContentText());
callback(dataAll)
}
@citadella
Copy link

citadella commented Aug 25, 2017

So I've gotten the trading bit to work just fine, but I can't seem to get a simple query on my balances to work. I'm trying:
polo("returnBalances","BTC")
But I get the following error message:

Request failed for https://poloniex.com/tradingApi returned code 422. Truncated server response: {"error":"Invalid currencyPair parameter."} (use muteHttpExceptions option to examine full response) (line 66).

Thoughts?

@cinderblue
Copy link

I have the same issue as citadella

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