Skip to content

Instantly share code, notes, and snippets.

@svasva
Last active December 21, 2015 20:58
Show Gist options
  • Save svasva/6364860 to your computer and use it in GitHub Desktop.
Save svasva/6364860 to your computer and use it in GitHub Desktop.
Meteor.startup ->
Meteor.publish 'hashrates', ->
return unless @userId
Hashrates.find({userId: @userId})
Meteor.publish 'top_hrates', (currId) ->
sel = { wrkName: '__total__', hashrate: {$gt: 0} }
sel.currId = currId if currId
opt =
sort: { hashrate: -1 }
limit: 15
Hashrates.find(sel, opt)
Meteor.publish 'settings', ->
return unless @userId
if Meteor.users.findOne(@userId)?.profile?.admin
Settings.find()
Meteor.publish 'blocks', (currId) ->
Blocks.find({currId: currId}, {limit: 15, sort: {time: -1}})
Meteor.publish 'block_stats', (currId) ->
return unless @userId
sel =
userId: @userId
currId: currId
BlockStats.find(sel, {sort: {time: -1}, limit: 15})
Meteor.publish 'workers', ->
return unless @userId
Workers.find({userId: @userId})
Meteor.publish 'tradeCount', ->
return false
init = true
count = 0
id = Random.id()
h = Trades.find({}).observeChanges
added: =>
count++
@changed('tradeCount', id, {count: count}) unless init
removed: =>
count--
@changed('tradeCount', id, {count: count})
init = false
@added('tradeCount', id, {count: count})
@ready()
@onStop -> h.stop()
Meteor.publish 'orders', (pairId) ->
return unless @userId
Orders.find({pairId: pairId, userId: @userId, complete: false, cancelled: false})
Meteor.publish 'last_trades', (pairId) ->
Trades.find({pairId: pairId}, {sort: {timestamp: -1}, limit: 20})
Meteor.publish 'notifications', ->
return unless @userId
Notifications.find({userId: @userId}, {sort: {time: -1}, limit: 50})
Meteor.publish 'balances', ->
return false unless @userId
Balances.find({userId: @userId})
Meteor.publish 'order_book', (pairId) ->
return unless pairId
Meteor._orderBook ||= {}
if ob = Meteor._orderBook[pairId]
idx = ob.newPub(@)
else
Meteor._orderBook[pairId] = new OrderBook(pairId, @)
idx = 0
@ready()
@onStop -> Meteor._orderBook?[pairId]?.removePub(idx)
Meteor.publish 'graph_data', (pairId) ->
return unless pairId
Meteor._graphData ||= {}
if ob = Meteor._graphData[pairId]
idx = ob.newPub(@)
else
Meteor._graphData[pairId] = new GraphData(pairId, @)
idx = 0
@ready()
@onStop -> Meteor._graphData?[pairId]?.removePub(idx)
Meteor.publish 'wallets', ->
return false unless @userId
Wallets.find({userId: @userId})
Meteor.publish 'transactions', (currId) ->
return false unless @userId
Transactions.find({currId: currId, userId: @userId}, {sort: {time: -1}, limit: 100})
Meteor.publish 'trade_pairs', -> TradePairs.find()
Meteor.publish 'currencies', ->
sel = {public: true}
fields =
miningTable: 0
host: 0
user: 0
pass: 0
port: 0
if @userId
user = Meteor.users.findOne(@userId)
if user?.profile?.admin
Currencies.find(sel)
else Currencies.find(sel, {fields: fields})
Meteor.publish 'graph_data', (pairId) ->
return unless pairId
Meteor._graphData ||= {}
if ob = Meteor._graphData[pairId]
idx = ob.newPub(@)
else
Meteor._graphData[pairId] = new GraphData(pairId, @)
idx = 0
@ready()
@onStop -> Meteor._graphData?[pairId]?.removePub(idx)
class @GraphData
constructor: (pairId, pub) ->
@data = {}
@pubs = [pub]
Trades.find({pairId: pairId}, {sort: {timestamp: 1}}).forEach (t) =>
@added(t)
newPub: (pub) ->
idx = @pubs.push(pub) - 1
for minute, stats of @data
pub.added('graph_data', minute, stats)
removePub: (idx) -> @pubs.splice(idx, 1)
round: (amount, precision = 8) ->
Math.round(amount * Math.pow(10, precision)) / Math.pow(10, precision)
roundMin: (time, min = 30) ->
Math.round(time / (60 * 1000 * min)) * (60 * 1000 * min)
added: (trade) ->
try
return unless trade.rate
rate = @round(trade.rate)
minute = @roundMin(trade.timestamp)
unless @data[minute]?.o
newRec = true
@data[minute] = {}
@data[minute].time = minute
@data[minute].o = rate
@data[minute].h = rate unless @data[minute].h > rate
@data[minute].l = rate unless @data[minute].l < rate
@data[minute].c = rate
if newRec
pub.added('graph_data', minute.toString(), @data[minute]) for pub in @pubs
else
pub.changed('graph_data', minute.toString(), @data[minute]) for pub in @pubs
catch e
console.log e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment