-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirwox.js
287 lines (272 loc) · 10.1 KB
/
virwox.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
'use strict';
// ---------------------------------------------------------------------------
const Exchange = require ('./base/Exchange');
const { ExchangeError } = require ('./base/errors');
// ---------------------------------------------------------------------------
module.exports = class virwox extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'virwox',
'name': 'VirWoX',
'countries': [ 'AT', 'EU' ],
'rateLimit': 1000,
'has': {
'CORS': true,
},
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/27766894-6da9d360-5eea-11e7-90aa-41f2711b7405.jpg',
'api': {
'public': 'http://api.virwox.com/api/json.php',
'private': 'https://www.virwox.com/api/trading.php',
},
'www': 'https://www.virwox.com',
'doc': 'https://www.virwox.com/developers.php',
},
'requiredCredentials': {
'apiKey': true,
'secret': false,
'login': true,
'password': true,
},
'api': {
'public': {
'get': [
'getInstruments',
'getBestPrices',
'getMarketDepth',
'estimateMarketOrder',
'getTradedPriceVolume',
'getRawTradeData',
'getStatistics',
'getTerminalList',
'getGridList',
'getGridStatistics',
],
'post': [
'getInstruments',
'getBestPrices',
'getMarketDepth',
'estimateMarketOrder',
'getTradedPriceVolume',
'getRawTradeData',
'getStatistics',
'getTerminalList',
'getGridList',
'getGridStatistics',
],
},
'private': {
'get': [
'cancelOrder',
'getBalances',
'getCommissionDiscount',
'getOrders',
'getTransactions',
'placeOrder',
],
'post': [
'cancelOrder',
'getBalances',
'getCommissionDiscount',
'getOrders',
'getTransactions',
'placeOrder',
],
},
},
});
}
async fetchMarkets () {
let markets = await this.publicGetGetInstruments ();
let keys = Object.keys (markets['result']);
let result = [];
for (let p = 0; p < keys.length; p++) {
let market = markets['result'][keys[p]];
let id = market['instrumentID'];
let symbol = market['symbol'];
let base = market['longCurrency'];
let quote = market['shortCurrency'];
result.push ({
'id': id,
'symbol': symbol,
'base': base,
'quote': quote,
'info': market,
});
}
return result;
}
async fetchBalance (params = {}) {
await this.loadMarkets ();
let response = await this.privatePostGetBalances ();
let balances = response['result']['accountList'];
let result = { 'info': balances };
for (let b = 0; b < balances.length; b++) {
let balance = balances[b];
let currency = balance['currency'];
let total = balance['balance'];
let account = {
'free': total,
'used': 0.0,
'total': total,
};
result[currency] = account;
}
return this.parseBalance (result);
}
async fetchMarketPrice (symbol, params = {}) {
await this.loadMarkets ();
let response = await this.publicPostGetBestPrices (this.extend ({
'symbols': [ symbol ],
}, params));
let result = response['result'];
return {
'bid': this.safeFloat (result[0], 'bestBuyPrice'),
'ask': this.safeFloat (result[0], 'bestSellPrice'),
};
}
async fetchOrderBook (symbol, limit = undefined, params = {}) {
await this.loadMarkets ();
let request = {
'symbols': [ symbol ],
};
if (typeof limit !== 'undefined') {
request['buyDepth'] = limit; // 100
request['sellDepth'] = limit; // 100
}
let response = await this.publicPostGetMarketDepth (this.extend (request, params));
let orderbook = response['result'][0];
return this.parseOrderBook (orderbook, undefined, 'buy', 'sell', 'price', 'volume');
}
async fetchTicker (symbol, params = {}) {
await this.loadMarkets ();
let end = this.milliseconds ();
let start = end - 86400000;
let response = await this.publicGetGetTradedPriceVolume (this.extend ({
'instrument': symbol,
'endDate': this.ymdhms (end),
'startDate': this.ymdhms (start),
'HLOC': 1,
}, params));
let marketPrice = await this.fetchMarketPrice (symbol, params);
let tickers = response['result']['priceVolumeList'];
let keys = Object.keys (tickers);
let length = keys.length;
let lastKey = keys[length - 1];
let ticker = tickers[lastKey];
let timestamp = this.milliseconds ();
return {
'symbol': symbol,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'high': parseFloat (ticker['high']),
'low': parseFloat (ticker['low']),
'bid': marketPrice['bid'],
'ask': marketPrice['ask'],
'vwap': undefined,
'open': parseFloat (ticker['open']),
'close': parseFloat (ticker['close']),
'first': undefined,
'last': undefined,
'change': undefined,
'percentage': undefined,
'average': undefined,
'baseVolume': parseFloat (ticker['longVolume']),
'quoteVolume': parseFloat (ticker['shortVolume']),
'info': ticker,
};
}
parseTrade (trade, symbol = undefined) {
let sec = this.safeInteger (trade, 'time');
let timestamp = sec * 1000;
return {
'id': trade['tid'],
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'order': undefined,
'symbol': symbol,
'type': undefined,
'side': undefined,
'price': this.safeFloat (trade, 'price'),
'amount': this.safeFloat (trade, 'vol'),
'fee': undefined,
'info': trade,
};
}
async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) {
await this.loadMarkets ();
let response = await this.publicGetGetRawTradeData (this.extend ({
'instrument': symbol,
'timespan': 3600,
}, params));
let result = response['result'];
let trades = result['data'];
return this.parseTrades (trades, symbol);
}
async createOrder (market, type, side, amount, price = undefined, params = {}) {
await this.loadMarkets ();
let order = {
'instrument': this.symbol (market),
'orderType': side.toUpperCase (),
'amount': amount,
};
if (type === 'limit')
order['price'] = price;
let response = await this.privatePostPlaceOrder (this.extend (order, params));
return {
'info': response,
'id': response['orderID'].toString (),
};
}
async cancelOrder (id, symbol = undefined, params = {}) {
return await this.privatePostCancelOrder (this.extend ({
'orderID': id,
}, params));
}
sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let url = this.urls['api'][api];
let auth = {};
if (api === 'private') {
this.checkRequiredCredentials ();
auth['key'] = this.apiKey;
auth['user'] = this.login;
auth['pass'] = this.password;
}
let nonce = this.nonce ();
if (method === 'GET') {
url += '?' + this.urlencode (this.extend ({
'method': path,
'id': nonce,
}, auth, params));
} else {
headers = { 'Content-Type': 'application/json' };
body = this.json ({
'method': path,
'params': this.extend (auth, params),
'id': nonce,
});
}
return { 'url': url, 'method': method, 'body': body, 'headers': headers };
}
handleErrors (code, reason, url, method, headers, body) {
if (code === 200) {
if ((body[0] === '{') || (body[0] === '[')) {
let response = JSON.parse (body);
if ('result' in response) {
let result = response['result'];
if ('errorCode' in result) {
let errorCode = result['errorCode'];
if (errorCode !== 'OK') {
throw new ExchangeError (this.id + ' error returned: ' + body);
}
}
} else {
throw new ExchangeError (this.id + ' malformed response: no result in response: ' + body);
}
} else {
// if not a JSON response
throw new ExchangeError (this.id + ' returned a non-JSON reply: ' + body);
}
}
}
};