Get started with Trade class
The reason for isolating the trader class is testability. It's hard to properly test code with as much global state as files in `lib/api` had. With a `Trader` class we can easily instantiate multiple objects, with different configs, without the need for seeding the database with test configs, effectively enabling us to do unit tests as well as integration tests. Besides that, this will allow easier reconfiguration, thanks to `Trader#configure` method.
This commit is contained in:
parent
b2d643cf1d
commit
d6a0f17e80
2 changed files with 179 additions and 49 deletions
|
|
@ -1,49 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
var _transferExchange;
|
||||
var _tradeExchange;
|
||||
var _api;
|
||||
var _config;
|
||||
var _balance = null;
|
||||
var _balanceTriggers = [];
|
||||
|
||||
var winston = require('winston');
|
||||
var logger = new (winston.Logger)({transports:[new (winston.transports.Console)()]});
|
||||
|
||||
var async = require('async');
|
||||
|
||||
exports.init = function(config, api, transferExchange, tradeExchange) {
|
||||
_api = api;
|
||||
_config = config;
|
||||
|
||||
_transferExchange = transferExchange;
|
||||
_tradeExchange = tradeExchange;
|
||||
|
||||
_balanceTriggers = [function (cb) { _transferExchange.balance(cb); }];
|
||||
|
||||
if (tradeExchange)
|
||||
_balanceTriggers.push(function(cb) { _tradeExchange.balance(cb); });
|
||||
|
||||
_pollBalance();
|
||||
setInterval(_pollBalance, 60 * 1000);
|
||||
};
|
||||
|
||||
exports.balance = function balance() {
|
||||
return _balance;
|
||||
};
|
||||
|
||||
exports.triggerBalance = _pollBalance;
|
||||
|
||||
function _pollBalance() {
|
||||
logger.info('collecting balance');
|
||||
async.parallel(_balanceTriggers, function(err, results) {
|
||||
if (err) return;
|
||||
|
||||
_balance = {
|
||||
transferBalance: results[0],
|
||||
tradeBalance: results.length === 2 ? results[1] : null,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
logger.info('Balance update:', _balance);
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue