-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
133 lines (105 loc) · 4.4 KB
/
handler.js
File metadata and controls
133 lines (105 loc) · 4.4 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB.DocumentClient();
const axios = require('axios');
const cheerio = require('cheerio');
const { App } = require('@slack/bolt');
const { get } = require('cheerio/lib/api/traversing');
function getTeamAlerts(teamid, channelid) {
const params = {
TableName: 'gasbot',
Key: {
teamid,
channelid,
},
};
return ddb.scan(params).promise();
}
function getGasbotEnabled(teamid) {
const params = {
TableName: 'gasbotteams',
Key: {
teamid,
},
};
return ddb.scan(params).promise();
}
// prices {"standard":225,"fast":225,"instant":225}
// source "zapper"
function gasPricesFormatter(prices, source) {
return `*${source}* \n ${prices.instant} | ${prices.fast} | ${prices.standard}`
}
module.exports.run = async (event, context) => {
// Initialize slack app
const app = new App({
signingSecret: process.env.SLACK_SIGNING_SECRET,
token: process.env.SLACK_BOT_TOKEN,
});
// Zapper API
const zapperGasEth = await axios.get('https://api.zapper.fi/v1/gas-price?api_key=5d1237c2-3840-4733-8e92-c5a58fe81b88&network=ethereum&eip1559=false')
const zapperGasPolygon = await axios.get('https://api.zapper.fi/v1/gas-price?api_key=5d1237c2-3840-4733-8e92-c5a58fe81b88&network=polygon&eip1559=false')
const zapperGasAvalanche = await axios.get('https://api.zapper.fi/v1/gas-price?api_key=5d1237c2-3840-4733-8e92-c5a58fe81b88&network=avalanche&eip1559=false')
console.log(zapperGasAvalanche.data)
// API for this guy
const gasNowRequest = await axios.get('https://www.gasnow.org/api/v3/gas/price?utm_source=yolo');
const gasNowData = gasNowRequest.data.data;
const gasNowLow = Math.trunc(gasNowData.standard / 1000000000);
const gasNowFast = Math.trunc(gasNowData.fast / 1000000000);
const gasNowTrader = Math.trunc(gasNowData.rapid / 1000000000);
const gasNowPrices = {
standard: gasNowLow,
fast: gasNowFast,
instant: gasNowTrader,
}
// Scrape the Polygon prices
const respPolygon = await axios.get('https://polygonscan.com/gastracker');
const $2 = cheerio.load(respPolygon.data);
const gasStandardPolygon = parseInt($2('#standardgas').text().trim().replace(' Gwei', ''))
const gasFastPolygon = parseInt($2('#fastgas').text().trim().replace(' Gwei', ''))
const gasRapidPolygon = parseInt($2('#rapidgas').text().trim().replace(' Gwei', ''))
const polyscanPolygonPrices = {
standard: gasStandardPolygon,
fast: gasFastPolygon,
instant: gasRapidPolygon,
}
const gasNowMessage = gasPricesFormatter(gasNowPrices, 'Gas Now')
const polygonscanMessage = gasPricesFormatter(polyscanPolygonPrices, 'Polygon Scan')
const zapperAvalancheMessage = gasPricesFormatter(zapperGasAvalanche.data, 'Zapper')
const zapperPolygonMessage = gasPricesFormatter(zapperGasPolygon.data, 'Zapper')
const zapperEthMessage = gasPricesFormatter(zapperGasEth.data, 'Zapper')
// Need to send teamid into this
const teamResponse = await app.client.team.info()
const teamid = teamResponse.team.id
let gasbotEnabledResponse = {};
let isGasbotEnabled = false;
gasbotEnabledResponse = await getGasbotEnabled(teamid);
isGasbotEnabled = gasbotEnabledResponse.Items && gasbotEnabledResponse.Items[0] &&
gasbotEnabledResponse.Items[0].enabled;
// don't do anything if gasbot isn't enabled
if (!isGasbotEnabled) {
return
}
// Build notification strings
const baseText = `----- *ERC20 L1 GAS* ----- \n ${gasNowMessage} \n ${zapperEthMessage}`
const baseTextPolygon = `----- *POLYGON L2* ----- \n ${polygonscanMessage} \n ${zapperPolygonMessage}`
const baseTextAvalanche = `----- *AVALANCHE L2* ----- \n ${zapperAvalancheMessage}`
// const channels = gasbotEnabledResponse.Items[0].channels
const channels = gasbotEnabledResponse.Items.map(item => item.channelid)
for (const index in channels) {
const channel = channels[index];
// Get alerts for members of that channel
const teamAlertsResponse = await getTeamAlerts(teamid, channel)
const isTeamAlertExistant = teamAlertsResponse.Items && teamAlertsResponse.Items[0] &&
teamAlertsResponse.Items[0].subscribers && teamAlertsResponse.Items[0].subscribers.length;
let subsString = '';
if (isTeamAlertExistant) {
teamAlertsResponse.Items[0].subscribers.forEach(sub => subsString += ` <@${sub}>`)
subsString += '\n'
}
const text = `${subsString}${baseText}\n${baseTextPolygon}\n${baseTextAvalanche}\n===================`
// Post message to chat
await app.client.chat.postMessage({
text,
channel,
})
}
};