This repository has been archived by the owner on Feb 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first working attempt
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const moment = require('moment'); | ||
const util = require('../../core/util.js'); | ||
const _ = require('lodash'); | ||
const retry = require('../../exchange/exchangeUtils').retry; | ||
|
||
const config = util.getConfig(); | ||
const dirs = util.dirs(); | ||
const Fetcher = require(dirs.exchanges + 'therocktrading'); | ||
|
||
Fetcher.prototype.getTrades = function(since, to, page, callback, descending) { | ||
const handle = (err, data) => { | ||
if (err) return callback(err); | ||
var trades = []; | ||
if (_.isArray(data.trades)) { | ||
trades = _.map(data.trades, function(trade) { | ||
return { | ||
tid: trade.id, | ||
price: trade.price, | ||
amount: trade.amount, | ||
date: moment.utc(trade.date).format('X') | ||
}; | ||
}); | ||
} | ||
|
||
callback(null, descending ? trades : trades.reverse()); | ||
}; | ||
|
||
if (moment.isMoment(since)) since = since.format(); | ||
if (moment.isMoment(to)) to = to.format(); | ||
|
||
let options = { | ||
before: to, | ||
after: since, | ||
page: page, | ||
order: "ASC", | ||
per_page: 200 | ||
} | ||
const fetch = cb => this.therocktrading.trades(this.pair, options, this.processResponse('getTrades', cb)); | ||
retry(null, fetch, handle); | ||
}; | ||
|
||
util.makeEventEmitter(Fetcher); | ||
|
||
var end = false; | ||
var done = false; | ||
var from = false; | ||
|
||
var page = 1; | ||
|
||
var fetcher = new Fetcher(config.watch); | ||
|
||
var fetch = () => { | ||
fetcher.import = true; | ||
|
||
console.log("IMPORTER: starting fetcher") | ||
|
||
fetcher.getTrades(from, end, page, handleFetch); | ||
} | ||
|
||
var handleFetch = (err, trades) => { | ||
if(!err && !trades.length) { | ||
console.log('no more trades'); | ||
fetcher.emit('done'); | ||
} | ||
|
||
if (err) { | ||
log.error(`There was an error importing from TheRockTrading ${err}`); | ||
fetcher.emit('done'); | ||
return fetcher.emit('trades', []); | ||
} | ||
|
||
if (trades.length > 0) { | ||
page = page + 1; | ||
} | ||
|
||
console.log("IMPORTER handleFetch: emitting trades: ", trades) | ||
fetcher.emit('trades', trades); | ||
} | ||
|
||
module.exports = function (daterange) { | ||
|
||
from = daterange.from.clone(); | ||
end = daterange.to.clone(); | ||
|
||
return { | ||
bus: fetcher, | ||
fetch: fetch | ||
} | ||
} | ||
|
||
|