/
expBackoff.js
47 lines (41 loc) · 1.45 KB
/
expBackoff.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
const apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
const apiSecret = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
function generateSugnature(string) {
let signature = Utilities.computeHmacSha256Signature(string, apiSecret)
return signature.map(function(x) {
const val = (x < 0 ? x + 256 : x).toString(16)
return val.length == 1 ? "0" + val : val
}).join("")
}
function makeApiCall(endpoint, string) {
const timestamp = Number(new Date().getTime()).toFixed(0)
string = string + timestamp
const signature = generateSugnature(string)
const query = "?" + string + "&signature=" + signature
const params = {
'method': 'get',
'headers': {'X-MBX-APIKEY': apiKey},
}
const response = UrlFetchApp.fetch(`${endpoint}${query}`, params)
console.log(response.getContentText())
}
function exponentialBackoff(url, string) {
for (let count = 0; count < 50; count++) {
try {
Utilities.sleep((count * count) + (Math.random() * 1000))
makeApiCall(url, string)
}
catch(e) {
console.info(e)
continue
}
}
}
function allOrders() {
// makeApiCall(Binance.Endpoint.ALL_ORDERS, "symbol=LTCBTC×tamp=")
exponentialBackoff(Binance.Endpoint.ALL_ORDERS, "symbol=LTCBTC×tamp=")
}
function accountInformation() {
// makeApiCall(Binance.Endpoint.ACCOUNT_INFORMATION, "")
exponentialBackoff(Binance.Endpoint.ACCOUNT_INFORMATION, "timestamp=")
}