forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wex.nz.js.old
301 lines (247 loc) · 9.7 KB
/
wex.nz.js.old
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
var WEX = require('node-wex');
var moment = require('moment');
var util = require('../core/util');
var _ = require('lodash');
var log = require('../core/log')
var Trader = function(config) {
this.key = config.key;
this.secret = config.secret;
this.asset = config.asset;
this.currency = config.currency;
this.pair = [config.asset, config.currency].join('_').toLowerCase();
this.name = 'wex.nz';
_.bindAll(this);
this.wex = new WEX(this.key, this.secret);
_.bindAll(this.wex, ['trade', 'trades', 'getInfo', 'ticker', 'orderList']);
// see @link https://github.com/askmike/gekko/issues/302
this.wexHistorocial = new WEX(false, false, {public_url: 'https://wex.nz/api/3/'});
_.bindAll(this.wexHistorocial, 'trades');
}
Trader.prototype.round = function(amount) {
// Prevent "You incorrectly entered one of fields."
// because of more than 8 decimals.
amount *= 100000000;
amount = Math.floor(amount);
amount /= 100000000;
return amount;
}
Trader.prototype.buy = function(amount, price, callback) {
amount = this.round(amount);
var set = function(err, data) {
if(err || !data || !data.return)
return log.error('unable to buy:', err);
callback(null, data.return.order_id);
}.bind(this);
// workaround for nonce error
setTimeout(function() {
this.wex.trade(this.pair, 'buy', price, amount, _.bind(set, this));
}.bind(this), 1000);
}
Trader.prototype.sell = function(amount, price, callback) {
amount = this.round(amount);
var set = function(err, data) {
if(err || !data || !data.return)
return log.error('unable to sell:\n\n', err);
callback(null, data.return.order_id);
};
// workaround for nonce error
setTimeout(function() {
this.wex.trade(this.pair, 'sell', price, amount, _.bind(set, this));
}.bind(this), 1000);
}
// if the exchange errors we try the same call again after
// waiting 10 seconds
Trader.prototype.retry = function(method, args) {
var wait = +moment.duration(10, 'seconds');
log.debug(this.name, 'returned an error, retrying..');
var self = this;
// run the failed method again with the same
// arguments after wait
setTimeout(
function() { method.apply(self, args) },
wait
);
}
Trader.prototype.getPortfolio = function(callback) {
var args = _.toArray(arguments);
var calculate = function(err, data) {
if(err) {
if(err.message === 'invalid api key')
util.die('Your ' + this.name + ' API keys are invalid');
return this.retry(this.getPortfolio, args);
}
if(_.isEmpty(data))
err = 'no data';
if (err || !data.return || !data.return.funds)
return this.retry(this.getPortfolio, args);
var assetAmount = parseFloat( data.return.funds[this.asset.toLowerCase()] );
var currencyAmount = parseFloat( data.return.funds[this.currency.toLowerCase()] );
if(!_.isNumber(assetAmount) || _.isNaN(assetAmount)) {
log.error(`wex.nz did not return portfolio for ${this.asset}, assuming 0.`);
assetAmount = 0;
}
if(!_.isNumber(currencyAmount) || _.isNaN(currencyAmount)) {
log.error(`wex.nz did not return portfolio for ${this.currency}, assuming 0.`);
currencyAmount = 0;
}
var portfolio = [
{ name: this.asset, amount: assetAmount },
{ name: this.currency, amount: currencyAmount }
];
callback(err, portfolio);
}.bind(this);
this.wex.getInfo(calculate);
}
Trader.prototype.getTicker = function(callback) {
// wex.nz doesn't state asks and bids in its
// ticker
var set = function(err, data) {
if(err)
return this.retry(this.wex.ticker, [this.pair, set]);
var ticker = {
ask: data.ticker.buy,
bid: data.ticker.sell
};
callback(err, ticker);
}.bind(this);
this.wex.ticker(this.pair, set);
}
Trader.prototype.getFee = function(callback) {
// wex.nz doesn't have different fees based on orders
// at this moment it is always 0.2%
callback(false, 0.002);
}
Trader.prototype.checkOrder = function(order, callback) {
var check = function(err, result) {
// wex returns an error when you have no open trades
// right now we assume on every error that the order
// was filled.
//
// TODO: check whether the error states that there are no
// open trades or that there is something else.
if(err)
callback(false, true);
else
callback(err, !result[order]);
}.bind(this);
this.wex.orderList({}, check);
}
Trader.prototype.getOrder = function(orderId, callback) {
console.log('getOrder', orderId);
var args = _.toArray(arguments);
var check = function(err, result) {
if(err) {
log.error('error on getOrder', err);
return this.retry(this.getOrder, args);
}
var order = null;
_.each(result.return, o => {
if(o.order_id === +orderId)
order = o;
});
if(!order)
return log.error('wex.nz did not provide the order');
var price = parseFloat(order.rate);
var amount = parseFloat(order.amount);
var date = moment.unix(order.timestamp);
callback(undefined, {price, amount, date});
}.bind(this);
this.wex.tradeHistory({pair: this.pair}, check);
}
Trader.prototype.cancelOrder = function(order, callback) {
this.wex.cancelOrder(order, (err, result) => {
callback();
});
}
Trader.prototype.getTrades = function(since, callback, descending) {
var args = _.toArray(arguments);
var process = function(err, trades) {
if(err)
return this.retry(this.getTrades, args);
if(descending)
callback(false, trades);
else
callback(false, trades.reverse());
}.bind(this);
// see @link https://github.com/askmike/gekko/issues/302
if(since) {
this.wexHistorocial.makePublicApiRequest(
'trades',
this.pair + '?limit=2000',
this.processAPIv3Trades(process)
)
} else
this.wex.trades(this.pair, process);
}
Trader.prototype.processAPIv3Trades = function(cb) {
return function(err, data) {
var trades = _.map(data[this.pair], function(t) {
return {
price: t.price,
amount: t.amount,
tid: t.tid,
date: t.timestamp
}
})
cb(err, trades);
}.bind(this)
}
Trader.getCapabilities = function () {
return {
name: 'wex.nz',
slug: 'wex.nz',
currencies: ['USD', 'RUR', 'EUR', 'BTC', 'LTC', 'ETH', 'NMC', 'NMC', 'NVC', 'PPC', 'DSH', 'BCH'],
assets: [
'BTC', 'LTC', 'NMC', 'NVC', 'USD', 'EUR', 'PPC', 'DSH', 'ETH',
'USDET', 'RURET', 'EURET', 'BTCET', 'LTCET', 'ETHET', 'NMCET', 'NVCET', 'PPCET', 'DSHET', 'BCHET' // Token
],
markets: [
{ pair: ['USD', 'BTC'], minimalOrder: { amount: 0.01, unit: 'asset' } },
{ pair: ['RUR', 'BTC'], minimalOrder: { amount: 0.01, unit: 'asset' } },
{ pair: ['EUR', 'BTC'], minimalOrder: { amount: 0.01, unit: 'asset' } },
{ pair: ['BTC', 'LTC'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['USD', 'LTC'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['RUR', 'LTC'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['EUR', 'LTC'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['BTC', 'NMC'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['USD', 'NMC'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['BTC', 'NVC'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['USD', 'NVC'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['RUR', 'USD'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['USD', 'EUR'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['RUR', 'EUR'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['BTC', 'PPC'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['USD', 'PPC'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['BTC', 'DSH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['USD', 'DSH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['RUR', 'DSH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['EUR', 'DSH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['LTC', 'DSH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['ETH', 'DSH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['BTC', 'ETH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['USD', 'ETH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['EUR', 'ETH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['LTC', 'ETH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['RUR', 'ETH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['USD', 'BCH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['BTC', 'BCH'], minimalOrder: { amount: 0.1, unit: 'asset' } },
// Token pairs
{ pair: ['USD', 'USDET'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['RUR', 'RURET'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['EUR', 'EURET'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['BTC', 'BTCET'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['LTC', 'LTCET'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['ETH', 'ETHET'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['NMC', 'NMCET'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['NVC', 'NVCET'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['PPC', 'PPCET'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['DSH', 'DSHET'], minimalOrder: { amount: 0.1, unit: 'asset' } },
{ pair: ['BCH', 'BCHET'], minimalOrder: { amount: 0.1, unit: 'asset' } },
],
requires: ['key', 'secret'],
providesHistory: false,
tid: 'tid',
tradable: true
};
}
module.exports = Trader;