-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
61 lines (55 loc) · 2.53 KB
/
main.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
const Gdax = require('gdax')
const PushBullet = require('pushbullet')
var mongo = require('mongodb')
var Details = require('./details.json')
const pusher = new PushBullet(Details.pushbullet.key)
const URL = 'mongodb://localhost:27017/gdax'
const key = Details.gdax.key
const b64secret = Details.gdax.secret
const passphrase = Details.gdax.passphrase
const apiURI = 'https://api.gdax.com';
const authedClient = new Gdax.AuthenticatedClient(key, b64secret, passphrase, apiURI);
mongo.connect(URL, function(err, db) {
setInterval(() => {
authedClient.getFills((err, res, body) => {
if(!err) {
let cursor = db.collection('orders').find()
let fills = []
cursor.forEach((doc, err) => {
fills.push(doc)
}, () => {
// After we have the updated list of fills we check if any of the NEW ones already exist
JSON.parse(res.body).forEach(fill => {
let exists = false
fills.forEach((f) => {
if(f.order.order_id !== fill.order_id) {
exists = true
return
}
})
if(!exists) {
db.collection('orders').insertOne({ order: fill }, function(err, result) {
let product = 'unknown';
switch (fill.product_id) {
case 'ETH-EUR':
product = 'Ethereum';
break;
case 'LTC-EUR':
product = 'Litecoin';
break;
case 'BTC-EUR':
product = 'Bitcoin';
break;
}
pusher.note(Details.pushbullet.device_id,
`${fill.side=='sell' ? 'Sold' : 'Bought'} ${product}!`,
`${fill.side=='sell' ? 'Sold' : 'Bought'} ${fill.size} ${product} @ ${fill.price}`)
console.log(`[Notification] ${product}`)
})
}
})
})
}
})
}, Details.timeout)
})