-
Notifications
You must be signed in to change notification settings - Fork 19
/
mex.gs
115 lines (99 loc) · 3.83 KB
/
mex.gs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// MEXC API Exchange Collectoir
// https://sites.google.com/site/moosyresearch/projects/cryptos/doc/ctrxl/collector
// **
// april 22, 2022
// https://MEXdevelop.github.io/apidocs
// The following lines can be used to test only
// var EXKEY = '*********';
// var EXSECRET = '*********';
// var ADATTRIB = '';
// function DebugLog(a) { Logger.log(a); }
function MEX_Settings() {
var stn = {
'id' : 'MEX',
'name' : 'MEXC',
'apikey' : EXKEY,
'secret' : EXSECRET,
'thirdattrib': ADATTRIB,
'uri' : 'https://api.mexc.com',
'command' : '/api/v3/account', // https://MEXdevelop.github.io/apidocs/#all-orders
'method' : 'GET',
'payload' : '',
'body' : ''
};
if (typeof ADATTRIB != 'undefined') stn.thirdattrib = ADATTRIB;
return stn;
}
function MEX_GetBalances() {
var stn = MEX_Settings(),
request = MEX_PrivateRequest(stn);
DebugLog("URL ....",JSON.stringify(request));
var response = UrlFetchApp.fetch(request.uri,request.params);
DebugLog("Receiving data from "+stn.name, response);
if (stn.thirdattrib.toLowerCase().indexOf('demo') >= 0){
response = {
"makerCommission": 20,
"takerCommission": 20,
"buyerCommission": 0,
"sellerCommission": 0,
"canTrade": true,
"canWithdraw": true,
"canDeposit": true,
"updateTime": null,
"accountType": "SPOT",
"balances": [{
"asset": "NBNTEST",
"free": "1111078",
"locked": "33"
}, {
"asset": "MAIN",
"free": "1020000",
"locked": "0"
}],
"permissions": ["SPOT"]
}
} else {
try { var response = JSON.parse(response); } catch(e) {Logger.log("No valid JSON data received"); return false;}
}
if (stn.thirdattrib.toLowerCase().indexOf('debug') >= 0) { Browser.msgBox(stn.name+" Connector DEBUG Mode:"); Browser.msgBox(JSON.stringify(response)); }
try { Logger.log("Validating datatype "+response.balances); } catch(e) { Logger.log(response); Logger.log("no or empty response"); return null;}
var array = [];
var num = 0;
Logger.log("******************************");
for (r in response.balances){
response.balances[r].asset;
num = Number(response.balances[r].free) + Number(response.balances[r].locked);
if (num * 100000 > 0) {
Logger.log("push "+num);
array.push({
curcodeEX: response.balances[r].asset,
balance: num
}); }
//Logger.log(r+" "+response.response_data.balance[r].total);
}
DebugLog(stn.name+" GetBalance", JSON.stringify(array));
if (stn.thirdattrib.toLowerCase().indexOf('debug') >= 0) { Browser.msgBox(JSON.stringify(array)); }
DebugLog(array);
return array;
}
function MEX_PrivateRequest(stn) {
function HMACSHA256HEX(s, secret) { return ToHex(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_256, s, secret)).toString(); }
function ToHex(s) { return s.map(function(byte) { return ('0' + (byte & 0xFF).toString(16)).slice(-2);}).join(''); }
var timestamp = new Date().getTime().toString(),
payld = 'recvWindow=5000×tamp='+timestamp,
sign = HMACSHA256HEX(payld,stn.secret),
payld = payld + "&signature="+sign
params = {
'method' : stn.method,
'muteHttpExceptions': true,
'headers': {
'X-MEXC-APIKEY' : stn.apikey,
'timestamp' : timestamp,
'sign' : sign,
'Content-Type' :'application/json',
},
//'payload' : bybrequest.command
}
payld = stn.command+"?"+payld+"&signature="+sign;
return { uri: stn.uri + payld, params: params};
}