· 6 years ago · Apr 04, 2020, 04:36 PM
1var CACHE = CacheService.getDocumentCache();
2
3function myUrlFetch(url, headers){
4 var fetch = UrlFetchApp.fetch(url, {"headers":headers, "muteHttpExceptions":true});
5 if (fetch.getResponseCode() == 200) {
6 return fetch.getContentText();
7 } else {
8 return '{"error":"can not fetch api endpoint"}';
9 }
10}
11
12
13function getBittrexTicker(f, t){
14 var response = CACHE.get("trex:" + f + "-" + t);
15 if (response == null){
16 var response = myUrlFetch("https://api.bittrex.com/api/v1.1/public/getticker?market=" + f + "-" + t);
17 CACHE.put("trex:" + f + "-" + t, response, 300); // cache for 5 minute
18 }
19 return JSON.parse(response).result.Last;
20}
21
22
23function toHexString(byteArray) {
24 return Array.from(byteArray, function(byte) {
25 return ('0' + (byte & 0xFF).toString(16)).slice(-2);
26 }).join('')
27}
28
29
30function getBittrexBalances(){
31 // Bittrex API key and secret here
32 var TREX_KEY = "YOUR_BITTREX_API_KEY_HEX_STRING";
33 var TREX_SEC = "YOUR_BITTREX_API_SEC_HEX_STRING";
34
35 var response = CACHE.get("trex:balances");
36 if (response == null){
37 var nonce = new Date().getTime();
38 var url = "https://api.bittrex.com/api/v1.1/account/getbalances?nonce=" + nonce + "&apikey=" + TREX_KEY;
39 var response = myUrlFetch(
40 url, {
41 "Content-type": "application/json",
42 "apisign": toHexString(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, url, TREX_SEC))
43 }
44 );
45 CACHE.put("trex:balances", response, 300); // cache for 5 minute
46 }
47 return JSON.parse(response).result;
48}
49
50
51function getBittrexBalance(token)
52{
53 return getBittrexBalances().filter(data => data.Currency == token)[0].Balance;
54}
55
56
57function getArkBalance(address, tick)
58{
59 var response = CACHE.get(address);
60 if (response == null) {
61 var response = myUrlFetch("https://explorer.ark.io:8443/api/wallets/"+address);
62 CACHE.put(address, response, 300); // cache for 5 minute
63 }
64 var myjson = JSON.parse(response);
65 if (myjson.data){return parseFloat(myjson.data.balance)/100000000;}
66 else{return 0;}
67}