Last active
October 31, 2019 08:29
-
-
Save helloluis/6fc5150bb51a005ba0375346d6e3a3b6 to your computer and use it in GitHub Desktop.
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
var Bitstamp = function(){ | |
this.host = "https://www.bitstamp.net"; | |
this.path = "/api/"; | |
this.balances = {}; | |
this.initialize = function() { | |
var pp = PropertiesService.getScriptProperties(); | |
this.key = pp.getProperties()['bitstamp_key']; | |
this.secret = pp.getProperties()['bitstamp_secret']; | |
this.customer_id = pp.getProperties()['bitstamp_id']; | |
}; | |
this.createSignature = function(nonce) { | |
var message = nonce + this.customer_id + this.key; | |
var res = Utilities.computeHmacSha256Signature(message, this.secret).map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2)}).join(""); | |
return res.toUpperCase(); | |
}; | |
this.getBalancesV2 = function() { | |
var url = this.host + this.path; | |
var sub_path = "v2/balance/"; | |
url += sub_path; | |
var nonce = this.generateUUID(); | |
var timestamp = ""+new Date().getTime(); | |
var signature = this.createSignatureV2("post", sub_path, "", nonce, timestamp, ""); | |
var headers = { | |
'X-Auth' : "BITSTAMP " + this.key, | |
'X-Auth-Signature' : signature, | |
'X-Auth-Nonce' : nonce, | |
'X-Auth-Timestamp' : timestamp, | |
'X-Auth-Version' : 'v2', | |
'contentType' : "application/x-www-form-urlencoded" | |
}; | |
var options = { | |
"method" : "post", | |
"muteHttpExceptions" : true, | |
"headers" : headers, | |
"payload" : {} | |
}; | |
var response = UrlFetchApp.fetch(url, options); | |
var balances = JSON.parse(response.getContentText()); | |
Logger.log(response); | |
this.balances = { | |
'usd' : balances['usd_balance'], | |
'btc' : balances['btc_balance'], | |
'eth' : balances['eth_balance'], | |
'ltc' : balances['ltc_balance'], | |
'bch' : balances['bch_balance'], | |
'eur' : balances['eur_balance'] | |
} | |
return balances; | |
}; | |
this.generateUUID = function() { | |
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | |
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); | |
return v.toString(16); | |
}); | |
}; | |
this.createSignatureV2 = function(verb, sub_path, query, nonce, timestamp, request_body) { | |
var message = "BITSTAMP " + this.key + verb + this.host + this.path + sub_path + query + "application/x-www-form-urlencoded" + nonce + timestamp + 'v2' + request_body; | |
var res = Utilities.computeHmacSha256Signature(message, this.secret).map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2)}).join(""); | |
return res.toUpperCase(); | |
}; | |
// run this every time the object is created | |
this.initialize(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment