-
Notifications
You must be signed in to change notification settings - Fork 28
/
handler.js
56 lines (47 loc) · 1.82 KB
/
handler.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
import { Coinbase, Sms } from './lib';
const coinbase = new Coinbase();
const sms = new Sms();
export const getExchangeRates = (event, context, callback) =>
Promise.all([
coinbase.getExchangeRates('Buy'),
coinbase.getExchangeRates('Sell'),
])
.then(() =>
callback(null, { statusCode: 200, body: { message: 'Collected exchange rates.' } }),
)
.catch((err) => {
console.error(err);
callback({ statusCode: 500, body: { message: 'Something went wrong' } });
});
export const exchangeRateAlarm = (event, context, callback) => {
const alarm = JSON.parse(event.Records[0].Sns.Message);
console.log(alarm);
const message = `${alarm.AlarmDescription} - ${alarm.NewStateReason}.`;
console.log(message);
return sms.sendMessage(message)
.then(() => callback(null, { statusCode: 200, body: alarm }));
};
export const buy = (event, context, callback) =>
coinbase.buy()
.then((tx) => {
const message = `Bought ${tx.amount.amount} ${tx.amount.currency} for a total of ${tx.total.amount} ${tx.total.currency}.`;
console.log(message);
return sms.sendMessage(message)
.then(() => callback(null, { statusCode: 200, body: { message } }));
})
.catch((err) => {
console.error(err);
callback({ statusCode: 500, body: { message: 'Something went wrong' } });
});
export const sell = (event, context, callback) =>
coinbase.sell()
.then((tx) => {
const message = `Sold ${tx.amount.amount} ${tx.amount.currency} for a total of ${tx.total.amount} ${tx.total.currency}.`;
console.log(message);
return sms.sendMessage(message)
.then(() => callback(null, { statusCode: 200, body: { message } }));
})
.catch((err) => {
console.error(err);
callback({ statusCode: 500, body: { message: 'Something went wrong' } });
});