forked from emmtte/list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
API_Cryptopia.js
54 lines (42 loc) · 2 KB
/
API_Cryptopia.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// I assume that key and secret API are in the "Config" spreadsheet. The key is in cell B18 and the secret in cell B19
function Cryptopia () {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Config");
var key = sheet.getRange("B18").getValue()
var secret = sheet.getRange("B19").getValue();
var digest = "{}";
var rawHash = Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5,digest));
Logger.log(rawHash+" "+rawHash);
url="https://www.cryptopia.co.nz/Api/GetBalance"
var url_encoded = encodeURIComponent(url).toLowerCase()
Logger.log(url_encoded);
var nonce = Math.floor(new Date().getTime()/1000);
signature=key+"POST"+url_encoded+nonce+rawHash
Logger.log("signature "+signature);
var shaObj = new jsSHA("SHA-256", "TEXT");
shaObj.setHMACKey(secret, "B64");
shaObj.update(signature);
var hmac = shaObj.getHMAC("B64");
Logger.log("signed: "+hmac);
header_value="amx "+key+":"+hmac+":"+nonce
Logger.log("header_value: "+header_value);
var options = {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': header_value },
payload: '{}'
}
var response = UrlFetchApp.fetch (url, options);
var data = JSON.parse(response.getContentText());
Logger.log(data)
var array = [];
//{Error=null, Data=[{Status=OK, Address=null, HeldForTrades=0, Symbol=1337, Unconfirmed=0, PendingWithdraw=0, Total=0, Available=0, BaseAddress=null, CurrencyId=331, StatusMessage=null}, {Status=OK, Address=null, HeldForTrades=0, Symbol=21M, Unconfirmed=0, PendingWithdraw=0, Total=0, Available=0, BaseAddress=, CurrencyId=573, StatusMessage=null}
for(var x in data.Data){ balance=parseFloat(data.Data[x].Total);
if (balance > 0) {
asset=data.Data[x].Symbol
array.push({'currency': asset, 'balance': balance, 'market': "Cryptopia"})
}
}
Logger.log(array);
return array;
}