Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Adding summary model and starting to add logic, refs #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Genar Trias Ortiz committed Dec 5, 2017
1 parent 9ab57d9 commit a7b87e5
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 5 deletions.
66 changes: 65 additions & 1 deletion server/exchanges/bittrex.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bittrex from 'node-bittrex-api'
import debug from 'debug'

module.exports = class BittrexExchange {
constructor (bitrexOpts) {
Expand All @@ -21,12 +22,75 @@ module.exports = class BittrexExchange {
if (market) {
options.market = market
}

bittrex.getorderhistory(options, function (data, err) {
if (err) {
debug('goby-api:bittrex')('An error getting orderhistory happened')
return cb(err)
}

if (!data) {
return cb(null, [])
}

return cb(null, data.result)
})
}

processOrderHistory (orders) {
let total = 0
orders.forEach((order) => {
const price = (order.Price - order.Commission)
if (order.OrderType === 'LIMIT_BUY') {
total += price
}
if (order.OrderType === 'LIMIT_SELL') {
total -= price
}
})

return {
benfit: total
}
}

getSummary (cb) {
this.getBalances((err, balances) => {
if (err) {
cb(err)
}

return cb(null, data)
const orderHistories = []

balances.forEach((balance) => {
const balanceSummary = new Promise((resolve, reject) => {
const market = 'BTC-' + balance.Currency
this.getOrders(market, (err, data) => {
if (err) {
return reject(err)
}

if (!data) {
return resolve()
}

return resolve({
exchange: market,
benfit: this.processOrderHistory(data).benfit
})
})
})

orderHistories.push(balanceSummary)
})

Promise.all(orderHistories)
.then((result) => {
cb(null, result)
})
.catch((err) => {
cb(err)
})
})
}
}
4 changes: 4 additions & 0 deletions server/model-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@
"order": {
"dataSource": null,
"public": true
},
"summary": {
"dataSource": null,
"public": true
}
}
21 changes: 21 additions & 0 deletions server/models/summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import BittrexExchange from '../exchanges/bittrex'

module.exports = function (Summary) {
Summary.getSummarys = (cb) => {
const bitrexOpts = Summary.app.get('bittrex')
const exchange = new BittrexExchange(bitrexOpts)

exchange.getSummary((err, data) => {
if (err) {
cb(err)
}

cb(null, data)
})
}

Summary.remoteMethod('getSummarys', {
http: { verb: 'get', path: '/' },
returns: {type: 'array', root: true}
})
}
14 changes: 14 additions & 0 deletions server/models/summary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "summary",
"plural": "summaries",
"base": "Model",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
7 changes: 3 additions & 4 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import crons from './crons'

const app = module.exports = loopback()


app.start = function() {
app.start = function () {
// start the web server
return app.listen(function() {
return app.listen(function () {
app.emit('started')
const baseUrl = app.get('url').replace(/\/$/, '')
console.info('Web server listening at: %s', baseUrl)
Expand All @@ -20,7 +19,7 @@ app.start = function() {

// Bootstrap the application, configure models, datasources and middleware.
// Sub-apps like REST API are mounted via boot scripts.
boot(app, __dirname, function(err) {
boot(app, __dirname, function (err) {
if (err) throw err

// start the server if `$ node server.js`
Expand Down

0 comments on commit a7b87e5

Please sign in to comment.