-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpair.js
258 lines (208 loc) · 6.94 KB
/
pair.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
'use strict';
var Storage = require('./storage');
var _ = require('lodash');
var co = require('co');
var Currencies = require('./currencies');
var Transaction = require('./transaction');
var events = require('events');
var util = require('util');
function onerror(err) {
// log any uncaught errors
// co will not throw any errors you do not handle!!!
// HANDLE ALL YOUR ERRORS!!!
console.error(err.stack);
}
var Pair = function(name, sourceCurrency, destCurrency, tradeOperator) {
this.name = name;
/**
*
* @type {BTCE|TradeAPI|null}
*/
this.tradeOperator = tradeOperator || null;
/**
*
* @param {TradeAPI} tradeOperator
*/
this.setTradeOperator = function(tradeOperator) {
this.tradeOperator = tradeOperator;
};
this.sourceCurrency = sourceCurrency;
this.destCurrency = destCurrency;
this.tradePairName = sourceCurrency.toLocaleLowerCase()+'_'+destCurrency.toLocaleLowerCase();
if((this.sourceCurrency == Currencies.BTC && this.destCurrency == Currencies.RUR) ||
(this.sourceCurrency == Currencies.RUR && this.destCurrency == Currencies.BTC))
{
this.tradePairName = 'btc_rur';
this.sourceCurrency = sourceCurrency;
this.sourceCurrency = sourceCurrency;
}
/**
*
* @type {Storage}
*/
this.storage = null;
this.save = function*() {
yield this.storage.saveAsync('fee', this.fee);
yield this.storage.saveAsync('bids', JSON.stringify(this.bids));
yield this.storage.saveAsync('asks', JSON.stringify(this.asks));
yield this.storage.saveAsync('status', this.status);
yield this.storage.saveAsync('lastUpdate', this.lastUpdate);
yield this.storage.emitAsync('refreshed');
};
this.load = function*() {
this.fee = yield this.storage.loadAsync('fee');
this.bids = JSON.parse(yield this.storage.loadAsync('bids'));
this.asks = JSON.parse(yield this.storage.loadAsync('asks'));
this.status = yield this.storage.loadAsync('status');
this.lastUpdate = yield this.storage.loadAsync('lastUpdate');
this.emit('refreshed');
};
this.initialize = function() {
this.storage = new Storage(this.tradePairName);
};
this.startListening = function() {
var self = this;
this.storage.on('refreshed', function() {
co(self.load()).catch(onerror);
});
};
this.stopListening = function() {
this.storage.off('refreshed');
};
this.fee = null;
this.bids = [];
this.asks = [];
this.status = null;
this.lastUpdate = null;
this.getSourceAmount = function() {
return _.reduce(this.bids, function(total, bid) {
return total = bid[1];
})
};
this.getDestTreshold = function() {
return (this.bids[0]|| {})[0];
};
this.getSourceTreshold = function() {
return (this.asks[0]|| {})[0];
};
this.getDestAmount = function() {
return _.reduce(this.bids, function(total, ask) {
return total = ask[1];
})
};
this.refreshAsync = function*() {
var self = this;
// Trade API method call.
var info = yield this.tradeOperator.depthAsync(this.tradePairName);
var info2 = yield this.tradeOperator.feeAsync(this.tradePairName);
self.bids =_.sortBy(info.bids, function(bid) {
return -bid[0];
});
self.asks =_.sortBy(info.asks, function(ask) {
return ask[0];
});
self.status = 'ok';
self.lastUpdate = Math.floor(Date.now() / 1000);
self.fee = info2.trade/100;
yield this.save();
return 3;
};
this.calculateFee = function(amount) {
};
/**
*
* @param {Number} amount
* @returns {Transaction}
*/
this.calculateSell = function(amount) {
console.log('selling');
var self = this;
var currencyAmount = amount;
var transactions = new Transaction(self.destCurrency, self.sourceCurrency);
_.map(this.asks, function(ask) {
if(ask[1] >= currencyAmount && currencyAmount>0) {
let localAmount = (currencyAmount / ask[0]);
let fee = localAmount * self.fee;
transactions.add(new Transaction(
self.destCurrency,
self.sourceCurrency,
ask[0],
(localAmount - fee),
currencyAmount,
fee));
currencyAmount = 0;
} else if(currencyAmount>0) {
let localAmount = (ask[1] / ask[0]);
let fee = localAmount * self.fee;
transactions.add(new Transaction(
self.destCurrency,
self.sourceCurrency,
ask[0],
(localAmount - fee),
ask[1],
fee));
currencyAmount -= ask[1];
}
});
return transactions;
};
/**
*
* @param {Number} amount
* @returns {Transaction}
*/
this.calculateBuy = function(amount) {
console.log('buying');
var self = this;
var currencyAmount = amount;
var transactions = new Transaction(self.sourceCurrency, self.destCurrency);
_.map(this.bids, function(bid) {
if(bid[1] >= currencyAmount && currencyAmount>0) {
let localAmount = (currencyAmount * bid[0]);
let fee = localAmount * self.fee;
transactions.add(new Transaction(
self.sourceCurrency,
self.destCurrency,
bid[0],
(localAmount - fee),
currencyAmount,
fee));
currencyAmount = 0;
} else if(currencyAmount>0) {
let localAmount = (bid[1] * bid[0]);
let fee = localAmount * self.fee;
transactions.add(new Transaction(
self.sourceCurrency,
self.destCurrency,
bid[0],
(localAmount - fee),
bid[1],
fee));
currencyAmount -= bid[1];
}
});
return transactions;
};
/**
*
* @param {String} currency
* @param {Number} amount
* @returns {Transaction|null}
*/
this.calculate = function(currency, amount) {
var self = this;
var calculatedAmount = 0;
var currencyAmount = amount;
var transactions = [];
if(currency == this.sourceCurrency) {
return this.calculateBuy(amount);
} else if(currency == this.destCurrency) {
return this.calculateSell(amount);
} else {
return null;
}
};
this.initialize();
};
util.inherits(Pair, events.EventEmitter);
module.exports = Pair;