-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
261 lines (235 loc) · 8.65 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
require('dotenv').config()
const axios = require('axios')
const Discord = require('discord.js')
const client = new Discord.Client({
partials: ['MESSAGE ']
});
const chainNames = ['eth', 'matic', 'arbitrum', 'base', 'zksync']
const subgraphs = {
eth: 'https://api.studio.thegraph.com/query/48757/harvest-mainnet-test/version/latest',
matic: 'https://api.studio.thegraph.com/query/48757/l2-polygon-test/version/latest',
arbitrum: 'https://api.studio.thegraph.com/query/48757/harvest-base/version/latest',
base: 'https://api.studio.thegraph.com/query/48757/harvest-arbitrum/version/latest',
zksync: 'https://api.studio.thegraph.com/query/48757/harvest-zksync/version/latest',
}
const myHeaders = new Headers()
myHeaders.append('Content-Type', 'application/json')
const PRICE_CHECK_INTERVAL = parseInt(process.env.PRICE_CHECK_INTERVAL) || 3600000
const REPEAT_MESSAGE_INTERVAL = parseInt(process.env.REPEAT_MESSAGE_INTERVAL) || 3600000
const FEE_LIMIT = parseFloat(process.env.FEE_LIMIT) || 0.05
const API_KEY = process.env.API_KEY || '41e90ced-d559-4433-b390-af424fdc76d6'
let isPriceChecking = false
let discordChannel = null
let quietTime = 0
const filterDataById = (vaultId, data) => {
let result = data.filter(item => item.vault.id.toLowerCase() === vaultId.toLowerCase())
result.sort((a, b) => parseInt(b.timestamp) - parseInt(a.timestamp))
return result[0]
}
const CHAIN_IDS = {
ETH_MAINNET: '1',
ETH_ROPSTEN: '3',
POLYGON_MAINNET: '137',
BASE: '8453',
ARBITRUM_ONE: '42161',
ZKSYNC: '324',
}
const getChainName = chain => {
let chainName = 'Ethereum'
switch (chain) {
case CHAIN_IDS.POLYGON_MAINNET:
chainName = 'Polygon'
break
case CHAIN_IDS.ARBITRUM_ONE:
chainName = 'Arbitrum'
break
case CHAIN_IDS.BASE:
chainName = 'Base'
break
case CHAIN_IDS.ZKSYNC:
chainName = 'Zksync'
break
default:
chainName = 'Ethereum'
break
}
return chainName
}
const priceCheck = async () => {
const graphql = JSON.stringify({
query: `{
priceFeeds(
where:{
timestamp_gt: ${Math.floor(Date.now()/1000) - 3600*2}
}
orderBy: timestamp
orderDirection: desc
) {
vault {
id
symbol
}
price
timestamp
}
}`,
variables: {},
}),
requestOptions = {
method: 'POST',
headers: myHeaders,
body: graphql,
redirect: 'follow',
}
let diffData = []
if (!discordChannel || !isPriceChecking) {
return
}
// check vault
try {
let apiData = await axios.get(`http://api.harvest.finance/vaults?key=${API_KEY}`)
const filteredApiData = Object.fromEntries(
Object.entries(apiData.data).map(([key, value]) => [
key,
Object.fromEntries(
Object.entries(value).filter(([_, innerValue]) => !innerValue.inactive)
)
])
)
const fetchPromises = chainNames.map(async (chainName) => {
const url = subgraphs[chainName];
return fetch(url, requestOptions)
.then(response => response.json())
.then(res => {
console.log('priceFeeds: ', res.data.priceFeeds)
return res.data.priceFeeds;
})
.catch(error => {
console.log('error', error);
return null;
});
});
Promise.all(fetchPromises)
.then(subgraphDataArray => {
subgraphDataArray.forEach((subgraphData, index) => {
const chainName = chainNames[index];
if (!subgraphData) return; // Skip if there was an error
Object.keys(filteredApiData[chainName]).forEach(vaultSymbol => {
let graphData = filterDataById(filteredApiData[chainName][vaultSymbol]?.vaultAddress, subgraphData);
if (parseFloat(graphData?.price) / parseFloat(filteredApiData[chainName][vaultSymbol].usdPrice) < (1 - FEE_LIMIT) || parseFloat(graphData?.price) / parseFloat(filteredApiData[chainName][vaultSymbol].usdPrice) > (1 + FEE_LIMIT)) {
let difference = parseFloat(graphData?.price) / parseFloat(filteredApiData[chainName][vaultSymbol].usdPrice)
diffData.push({
symbol: filteredApiData[chainName][vaultSymbol].id,
address: filteredApiData[chainName][vaultSymbol].vaultAddress ?? filteredApiData[chainName][vaultSymbol].tokenAddress,
chain: getChainName(filteredApiData[chainName][vaultSymbol].chain),
api_price: parseFloat(filteredApiData[chainName][vaultSymbol].usdPrice).toFixed(2),
subgraph_price: parseFloat(graphData.price).toFixed(2),
difference: difference.toFixed(2) * 100,
subgraph_price_timestamp: graphData.timestamp
});
}
});
});
})
.then( () => {
let bSendMessage = true
quietTime += PRICE_CHECK_INTERVAL
if (quietTime > REPEAT_MESSAGE_INTERVAL) {
quietTime = 0
bSendMessage = true
}
if (bSendMessage) {
console.log(quietTime)
const embed = new Discord.MessageEmbed()
.setTitle('Price alerts')
.setColor(diffData?.length === 0 ? '#5AA27C' : '#D0342C')
if(diffData.length === 0) {
embed.addField('Price Status: ', 'OK', true)
discordChannel.send(embed)
} else if(diffData.length < 5) {
diffData.forEach(entry => {
embed.addField('Symbol', entry.symbol, true)
.addField('Chain Name', entry.chain, true)
.addField('Address', entry.address, true)
.addField('API Price', entry.api_price, true)
.addField('Subgraph Price', entry.subgraph_price, true)
.addField('Price Difference', entry.difference +'%', true)
.addField('Subgraph Price Time', new Date(entry.subgraph_price_timestamp * 1000).toUTCString(), true)
.addField('\u200B', '\u200B') // Adding a blank field to create space between entries
})
discordChannel.send(embed)
} else {
const chunkedData = [];
for (let i = 0; i < diffData.length; i += 3) {
chunkedData.push(diffData.slice(i, i + 3))
}
chunkedData.forEach((chunk, index) => {
const chunkEmbed = new Discord.MessageEmbed()
.setTitle(index === 0 ? 'Price alerts' : '')
.setColor(diffData?.length === 0 ? '#5AA27C' : '#D0342C')
if (chunk.length === 0) {
chunkEmbed.addField('Price Status: ', 'OK', true)
}
chunk.forEach(entry => {
chunkEmbed.addField('Symbol', entry.symbol, true)
.addField('Chain Name', entry.chain, true)
.addField('Address', entry.address, true)
.addField('API Price', entry.api_price, true)
.addField('Subgraph Price', entry.subgraph_price, true)
.addField('Price Difference', entry.difference.toFixed(2) +'%', true)
.addField('Subgraph Price Time', new Date(entry.subgraph_price_timestamp * 1000).toUTCString(), true)
.addField('\u200B', '\u200B') // Adding a blank field to create space between entries
});
discordChannel.send(chunkEmbed)
});
}
}
})
} catch (e) {
console.log(e)
const embed = new Discord.MessageEmbed()
.setTitle('Price Check alerts')
.setColor('#D0342C')
.setDescription("There was an error while checking the price check.")
discordChannel.send(embed)
}
if (isPriceChecking) {
setTimeout(priceCheck, PRICE_CHECK_INTERVAL);
}
}
const startPriceCheck = (channel) => {
discordChannel = channel
isPriceChecking = true
priceCheck()
}
const stopPriceCheck = () => {
isPriceChecking = false
}
client.on("ready", () => {
console.log("Harvest Price check bot is ready")
const channel = client.channels.cache.get(process.env.CHANNEL_ID)
if (channel) {
startPriceCheck(channel)
} else {
console.log("Channel not found")
}
})
client.on("message", msg => {
try {
if (msg.content === "/price-check-test") {
msg.reply("Harvest price check bot is active now!");
}
else if (msg.content == "/price-check-start") {
startPriceCheck(msg.channel)
msg.channel.send('Price check started!')
}
else if (msg.content == "/price-check-stop") {
stopPriceCheck()
msg.channel.send('Price check stopped!')
}
else if (msg.content == "/help") {
msg.channel.send('/price-check-start, /price-check-stop')
}
} catch (exception) { console.error(exception) }
})
client.login(process.env.BOT_TOKEN)