-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
69 lines (57 loc) 路 2.2 KB
/
Copy pathindex.ts
File metadata and controls
69 lines (57 loc) 路 2.2 KB
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
import dotenv from 'dotenv';
dotenv.config();
import colors from 'colors';
import cron from 'node-schedule';
import cronstrue from 'cronstrue';
import config from './config';
import QuidaxService from './src/services/Quidax';
import MailgunService from './src/services/Mailgun';
import { IOrder } from './src/@types/Order';
const mailer = new MailgunService(config.mailgun_scret, config.mailgun_domain);
async function placeOrder(order: IOrder) {
try {
const { data: response } = await QuidaxService.instantBuy(order.asset, order.currency, order.amount, order.quantity);
const { data: confirmation } = await QuidaxService.confirmInstandBuy(response.data.id);
if (confirmation.status === 'success') {
await mailer.notify(
config.notifications.to,
config.notifications.from,
`Order for ${order.asset}/${order.currency} was completed`,
JSON.stringify(confirmation.data, undefined, 2)
);
}
} catch (error: any) {
await mailer.notify(
config.notifications.to,
config.notifications.from,
`Order for ${order.asset}/${order.currency} failed`,
JSON.stringify(error?.response?.data as string, undefined, 2)
);
console.log('could not place order'.red, error.response?.data);
}
}
async function runBot() {
console.log(colors.magenta('Starting Bot'));
for (const order of config.orders) {
const { asset, currency, amount, quantity, schedule } = order;
if (amount && quantity) {
throw new Error('Error: You cannot supply both amount and quantity, you have to pick one... check the docs for the difference');
}
if (amount) {
console.log(
colors.yellow(`Setting up CRON to buy ${amount} ${currency} worth of ${asset} ${schedule ? cronstrue.toString(schedule, { verbose: true }) : 'Immediately'}`)
);
} else {
console.log(
colors.yellow(`Setting up CRON to buy ${quantity} ${asset} with ${currency} ${schedule ? cronstrue.toString(schedule, { verbose: true }) : 'Immediately'}`)
);
}
if (!schedule) {
console.log('no schedule yet');
await placeOrder(order);
} else {
cron.scheduleJob(schedule, async () => await placeOrder(order));
}
}
}
runBot();