Last active
June 2, 2019 01:18
-
-
Save learntheropes/ee51e015ed5149995c4e9b8ce7394432 to your computer and use it in GitHub Desktop.
Poloniex private API for Google Apps Script (Google Sheet)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have the same issue as citadella