forked from Tetu-Community/tetu-discord-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
277 lines (229 loc) · 7.33 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const axios = require('axios')
const delay = require('delay')
const ms = require('ms')
const { Client } = require('discord.js')
const StatusUpdater = require('@tmware/status-rotate')
const BigNumber = require('bignumber.js')
const ethers = require('ethers')
const reject = require('lodash.reject')
const dotenv = require('dotenv');
dotenv.config();
const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000'
function truncateErr (err) {
return (err || '').toString().substring(0, 128)
}
function getProvider (network) {
const urlStr = process.env[`${network.toUpperCase()}_RPC_HTTP`]
if (!urlStr) {
throw new Error(`no RPC provider for network ${network} found!`)
}
return jsonRpcProviderFromUrl(urlStr)
}
function jsonRpcProviderFromUrl (urlStr) {
const u = new URL(urlStr)
return new ethers.providers.JsonRpcProvider({
url: u.origin + u.pathname,
user: u.username,
password: u.password,
})
}
async function balancerPriceQuote ({ provider, poolId, inputToken, outputToken, inputAmount }) {
const contract = new ethers.Contract(
'0xBA12222222228d8Ba445958a75a0704d566BF2C8',
require('./abis/BalancerVault.json'),
provider,
)
const swap0 = {
poolId,
assetInIndex: 0,
assetOutIndex: 1,
amount: inputAmount,
userData: '0x',
}
const assets = [
inputToken,
outputToken,
]
const funds = {
sender: ADDRESS_ZERO,
fromInternalBalance: false,
recipient: ADDRESS_ZERO,
toInternalBalance: false,
}
const quote = await contract.callStatic.queryBatchSwap(0, [swap0], assets, funds)
return BigNumber(quote[1].toString()).absoluteValue().toFixed(0)
}
async function tetuSwapPriceQuote ({ provider, router, pair, fee, inputToken, outputToken, inputAmount }) {
const routerContract = new ethers.Contract(
router,
require('./abis/TetuSwapRouter.json'),
provider,
)
const pairContract = new ethers.Contract(
pair,
require('./abis/UniswapV2Pair.json'),
provider,
)
const reserves = await pairContract.getReserves()
const inputTokenIsReserveZero = ethers.BigNumber.from(inputToken).lt(outputToken)
const res = await routerContract.getAmountOut(
inputAmount,
inputTokenIsReserveZero ? reserves[0] : reserves[1],
inputTokenIsReserveZero ? reserves[1] : reserves[0],
fee,
)
return res.toString()
}
async function newClientWithStatusUpdater (key) {
const bot = new Client({ intents: [] })
bot.statusUpdater = new StatusUpdater(bot)
bot.login(key)
const guilds = []
for (const guildId of process.env.GUILD_IDS.split(',')) {
try {
const guild = await bot.guilds.fetch(guildId)
if (!guild) {
throw new Error('cannot find guild with id', guildId)
}
guilds.push(guild)
} catch (err) {
console.log('could not find guild', truncateErr(err))
}
}
return [bot, guilds]
}
async function updateStatus (bot, guilds, nickname, statusMsg) {
if (statusMsg.length > 128) {
statusMsg = statusMsg.substring(0, 128)
}
if (process.env.VERBOSE) {
console.log('updating bot', nickname, statusMsg)
}
if (nickname) {
for (const guild of guilds) {
const botUser = await guild.members.fetch(bot.user.id)
botUser.setNickname(nickname)
}
}
const s = { type: 3, name: statusMsg }
await bot.statusUpdater.addStatus(s)
await bot.statusUpdater.updateStatus(s)
}
async function runTetuPriceBot () {
const [bot, guilds] = await newClientWithStatusUpdater(process.env.TETU_PRICE_BOT_KEY)
bot.once('ready', loop)
async function loop () {
try {
const resp = await axios.get('https://api.coingecko.com/api/v3/coins/tetu')
await updateStatus(
bot,
guilds,
`TETU $${resp.data.market_data.current_price.usd}`,
`24h: ${BigNumber(resp.data.market_data.price_change_percentage_24h_in_currency.usd).toFixed(1)}%`,
)
} catch (err) {
console.log('error in runTetuPriceBot', truncateErr(err))
}
await delay(ms('2m'))
loop()
}
}
async function runTetuCirculatingSupplyBot () {
const [bot, guilds] = await newClientWithStatusUpdater(process.env.TETU_CIRCULATING_SUPPLY_BOT_KEY)
bot.once('ready', loop)
async function loop () {
try {
const resp = await axios.get('https://api.tetu.io/api/v1/info/circulationSupply')
await updateStatus(
bot,
guilds,
BigNumber(resp.data).toFormat(2),
'Circulating Supply',
)
} catch (err) {
console.log('error in runTetuCirculatingSupplyBot', truncateErr(err))
}
await delay(ms('5m'))
loop()
}
}
async function runTetuBalDiscountBot () {
const [bot, guilds] = await newClientWithStatusUpdater(process.env.TETU_BAL_DISCOUNT_BOT_KEY)
bot.once('ready', loop)
async function loop () {
try {
const resp = await balancerPriceQuote({
provider: getProvider('polygon'),
poolId: '0x7af62c1ebf97034b7542ccec13a2e79bbcf34380000000000000000000000c13',
inputToken: '0x7fc9e0aa043787bfad28e29632ada302c790ce33',
outputToken: '0x3d468ab2329f296e1b9d8476bb54dd77d8c2320f',
inputAmount: ethers.utils.parseEther('1'),
})
const bptForTetuBal = BigNumber(resp.toString()).shiftedBy(-18)
const discount = BigNumber(1).minus(bptForTetuBal)
await updateStatus(
bot,
guilds,
`${bptForTetuBal.toFixed(4)} (${discount.times(100).toFixed(1)}%)`,
'tetuBAL discount',
)
} catch (err) {
console.log('error in runTetuBalDiscountBot', truncateErr(err))
}
await delay(ms('5m'))
loop()
}
}
async function runTetuQiDiscountBot () {
const [bot, guilds] = await newClientWithStatusUpdater(process.env.TETU_QI_DISCOUNT_BOT_KEY)
bot.once('ready', loop)
async function loop () {
try {
const resp = await balancerPriceQuote({
provider: getProvider('polygon'),
poolId: '0xd80ef9fabfdc3b52e17f74c383cf88ee2efbf0b6000000000000000000000a65',
inputToken: '0x4cd44ced63d9a6fef595f6ad3f7ced13fceac768',
outputToken: '0x580a84c73811e1839f75d86d75d88cca0c241ff4',
inputAmount: ethers.utils.parseEther('1'),
})
const qiForTetuQi = BigNumber(resp.toString()).shiftedBy(-18)
const discount = BigNumber(1).minus(qiForTetuQi)
await updateStatus(
bot,
guilds,
`${qiForTetuQi.toFixed(4)} (${discount.times(100).toFixed(1)}%)`,
'tetuQI discount',
)
} catch (err) {
console.log('error in runTetuQiDiscountBot', truncateErr(err))
}
await delay(ms('5m'))
loop()
}
}
async function runTetuTvlBot () {
const [bot, guilds] = await newClientWithStatusUpdater(process.env.TETU_TVL_BOT_KEY)
bot.once('ready', loop)
async function loop () {
try {
const resp = await axios.get('https://api.llama.fi/protocol/tetu')
let tvl = 0;
Object.values(resp.data.currentChainTvls).forEach(t => tvl += Number(t));
await updateStatus(
bot,
guilds,
'$' + BigNumber(tvl).toFormat(0),
'TVL',
)
} catch (err) {
console.log('error in runTetuCirculatingSupplyBot', truncateErr(err))
}
await delay(ms('5m'))
loop()
}
}
setTimeout(runTetuPriceBot, 0)
setTimeout(runTetuCirculatingSupplyBot, 0)
setTimeout(runTetuBalDiscountBot, 0)
//setTimeout(runTetuQiDiscountBot, 0)
setTimeout(runTetuTvlBot, 0)