Forked from learntheropes/bittrex_private_and_public_api.gs
Created
August 20, 2017 08:24
-
-
Save kevin-chau/4704625492fdc472b744837a6d528627 to your computer and use it in GitHub Desktop.
Bittrex private and public API for Google Apps Script (Google Sheet)
This file contains 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 bittrex API key and secret with read account option enabled | |
// I assume that all the keys are in the "keys" spreadsheet. The key is in cell B5 and the secret in cell C5 | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("keys"); | |
var key = sheet.getRange("B5").getValue() | |
var secret = sheet.getRange("C5").getValue(); | |
var baseUrl = 'https://bittrex.com/api/v1.1/'; | |
var nonce = Math.floor(new Date().getTime()/1000); | |
/** | |
* Used to retrieve all balances from your account. | |
* @return the balance of any currency that is not 0. | |
*/ | |
function bittrexGetbalances() { | |
var finals = []; | |
var results = bittrexPrivateRequest("account/getbalances"); | |
Logger.log(results) | |
results.forEach(function(result,index) { | |
finals.push({'currency': result.Currency, 'balance': result.Balance}) | |
}); | |
Logger.log(finals) | |
return json2array_(finals); | |
}; | |
/** | |
* Used to retrieve the balance from your account for a specific currency. | |
* @param {string} a string literal for the currency (ex: LTC). | |
* @return the currency balance. | |
* @customfunction | |
*/ | |
function bittrexGetbalance(currency) { | |
var payload = { "currency" : currency }; | |
var results = bittrexPrivateRequest("account/getbalance",payload); | |
return results.Balance; | |
}; | |
function bittrexPublicRequest(command,payload){ | |
var uri = uriCreator_(command,payload); | |
var response = UrlFetchApp.fetch(uri); | |
var dataAll = JSON.parse(response.getContentText()); | |
if (dataAll.success = true) { | |
return dataAll.result | |
} else { | |
return dataAll.message | |
} | |
} | |
function bittrexPrivateRequest(command,payload){ | |
var uri = uriCreator_(command,payload,true) | |
var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, uri, secret); | |
// Signature copied from comments: | |
var apisign = signature.map(function(byte) { | |
return ('0' + (byte & 0xFF).toString(16)).slice(-2); | |
}).join(''); | |
var headers = { | |
"apisign": apisign | |
} | |
var params = { | |
"method": "get", | |
"headers": headers, | |
"payload": payload | |
} | |
var response = UrlFetchApp.fetch(uri, params); | |
var dataAll = JSON.parse(response.getContentText()); | |
if (dataAll.success = true) { | |
return dataAll.result | |
} else { | |
return dataAll.message | |
} | |
}; | |
function uriCreator_(command,payload,private){ | |
if (payload) { | |
var payloadEncoded = Object.keys(payload).map(function(param) { | |
return encodeURIComponent(param) + '=' + encodeURIComponent(payload[param]); | |
}).join('&'); | |
} | |
if (private) { | |
uri = baseUrl.concat(command + "?apikey=" + key + "&nonce=" + nonce + "&" + payloadEncoded) | |
} else { | |
uri = baseUrl.concat("public/" + command + "?" + payloadEncoded) | |
} | |
return uri | |
}; | |
function json2array_(data){ | |
var results = []; | |
var keys = []; | |
var values = []; | |
for (var i in data){ | |
for (var key in data[i]){ | |
if (i == 0) keys.push(key); | |
values.push(data[i][key]); | |
} | |
if (i == 0){ | |
results.push(keys); | |
keys = []; | |
} | |
results.push(values); | |
values = []; | |
} | |
return results; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment