-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (60 loc) · 1.86 KB
/
index.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
'use strict';
require('dotenv').config();
const axios = require('axios');
const aws = require('aws-sdk');
const ses = new aws.SES({ region: 'us-east-1' });
const handler = async (event, context, callback) => {
try {
// validate env
const walletAddress = process.env.WALLET_ADDRESS;
const alertThreshold = Number(process.env.ALERT_THRESHOLD_PERCENTAGE);
if (!walletAddress || alertThreshold < 0 || alertThreshold > 100) {
console.error('environment configured incorrectly');
return;
} else {
console.log(`Alert Threshold: ${alertThreshold}%`);
}
// query Compound API
let response = await axios.get(
`http://api.compound.finance/api/v2/account?addresses=${process.env.WALLET_ADDRESS}`
);
// ensure account accuracy
const account = response.data.accounts[0];
if (
account.address.toUpperCase() != process.env.WALLET_ADDRESS.toUpperCase()
) {
console.error('Account not found');
return;
}
// calculate utilization
const borrowValue = account.total_borrow_value_in_eth.value;
const collateralValue = account.total_collateral_value_in_eth.value;
const utilization = ((borrowValue / collateralValue) * 100).toFixed(2);
if (utilization < alertThreshold) {
console.log(`Utilization Okay: ${utilization}%`);
} else {
console.log('Sending Mail....');
return sendAlertMail(utilization);
}
} catch (err) {
console.error(JSON.stringify(err));
}
};
const sendAlertMail = (utilization) => {
let params = {
Destination: {
ToAddresses: [process.env.GMAIL_USER],
},
Message: {
Body: {
Text: {
Data: `UTILIZATION: ${utilization}%`,
},
},
Subject: { Data: '!!COMPOUND ALERT!!' },
},
Source: process.env.GMAIL_USER,
};
return ses.sendEmail(params).promise();
};
exports.handler = handler;