-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
105 lines (87 loc) · 3.37 KB
/
app.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const Promise = require('bluebird');
const Gdax = require('gdax');
const bunyan = require('bunyan');
const bformat = require('bunyan-format');
const formatOut = bformat({ outputMode: 'long' });
const logger = bunyan.createLogger({ name: 'app', stream: formatOut, level: 'debug' });
const btcClient = new Gdax.PublicClient('BTC-USD');
const ethClient = new Gdax.PublicClient('ETH-USD');
const ltcClient = new Gdax.PublicClient('LTC-USD');
const config = require('./config');
// mail gun settings
const mailgun = config.mailgun.apiKey.length ? require('mailgun-js')({
apiKey: config.mailgun.apiKey,
domain: config.mailgun.domain,
}) : null;
const SCRIPT_TIME_INTERVAL = 60 * 1000; // 1 hour
const apiURI = config.sandboxMode ? 'https://api-public.sandbox.gdax.com' : 'https://api.gdax.com';
const authedClient = new Gdax.AuthenticatedClient(
config.gdax.apiKey,
config.gdax.secret,
config.gdax.passPhrase,
apiURI,
);
function _getDifferenceInDays(lastFillUTCString) {
return ((new Date().getTime() - new Date(lastFillUTCString).getTime()) / (1000 * 60 * 60 * 24)).toFixed(2);
}
async function _executeBuy(marketPrice, product) {
const buyParams = {
price: marketPrice, // USD
size: 0.01,
product_id: product,
};
const buyOrder = await authedClient.buy(buyParams);
logger.info(buyOrder);
const data = {
from: config.email,
to: config.email,
subject: `Order executed for ${product}`,
text: `A buy order has been made for ${buyParams.size} ${buyParams.product_id} at $${parseFloat(marketPrice).toFixed(2)}\n\nDetails:\n${JSON.stringify(buyOrder, null, 2)}`,
};
return mailgun ? mailgun.messages().send(data) : Promise.resolve();
}
async function _confirmCoin(coin) {
const fills = await authedClient.getFills({ product_id: coin.id });
logger.info(`Last ${coin.name} fill was ${_getDifferenceInDays(fills[0].created_at)} days ago...`);
if (!fills.length || (fills.length && _getDifferenceInDays(fills[0].created_at) >= config.btc.occurrence)) {
logger.info(`Executing order for ${config.eth.size} ${coin.name}s at ${parseFloat(coin.ticker.price).toFixed(2)}`);
await _executeBuy(coin.ticker.price, coin.id);
}
}
async function main() {
try {
const coins = [];
if (config.btc && config.btc.size && config.btc.occurrence) {
coins.push({
id: 'BTC-USD',
name: 'Bitcoin',
ticker: await btcClient.getProductTicker(),
size: config.btc.size,
});
}
if (config.eth && config.eth.size && config.eth.occurrence) {
coins.push({
id: 'ETH-USD',
name: 'Ethereum',
ticker: await ethClient.getProductTicker(),
size: config.eth.size,
});
}
if (config.ltc && config.ltc.size && config.ltc.occurrence) {
coins.push({
id: 'LTC-USD',
name: 'Litecoin',
ticker: await ltcClient.getProductTicker(),
size: config.ltc.size,
});
}
logger.info('Getting gdax information...');
await Promise.mapSeries(coins, coin => _confirmCoin(coin));
setTimeout(() => {
main();
}, SCRIPT_TIME_INTERVAL);
} catch (err) {
logger.error(err);
}
}
main();