-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
336 lines (315 loc) · 9.51 KB
/
bot.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
require("dotenv").config()
const { ethers, constants } = require("ethers");
const { Client, Intents } = require('discord.js');
// const Twit = require('twit');
// var T = new Twit({
// consumer_key: '...',
// consumer_secret: '...',
// access_token: '...',
// access_token_secret: '...',
// timeout_ms: 60*1000, // optional HTTP request timeout to apply to all requests.
// strictSSL: true, // optional - requires SSL certificates to be valid.
// })
let abi2 = [{
"anonymous": false,
"name": "DeedMinted",
"inputs": [
{
"internalType": "uint256",
"type": "uint256",
"indexed": false,
"name": "DeedId"
},
{
"name": "DeedOwner",
"internalType": "address",
"indexed": false,
"type": "address"
},
{
"internalType": "uint256",
"type": "uint256",
"name": "DeedLevel",
"indexed": false
}
],
"type": "event"
}]
let newAbi = [{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "Referer",
"type": "address"
},
{
"indexed": false,
"internalType": "address",
"name": "DeedOwner",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "DeedLevel",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "DeedPoints",
"type": "uint256"
}
],
"name": "NewDeed",
"type": "event"
},
{
"inputs": [],
"name": "deedIndex",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "addressToDeed",
"outputs": [
{
"internalType": "uint256",
"name": "index",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "level",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "points",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": true,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "DeedOwner",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "DeedLevel",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "DeedPoints",
"type": "uint256"
}
],
"name": "UpdateDeed",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "approved",
"type": "address"
},
{
"indexed": true,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "operator",
"type": "address"
},
{
"indexed": false,
"internalType": "bool",
"name": "approved",
"type": "bool"
}
],
"name": "ApprovalForAll",
"type": "event"
},
]
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const EXPECTED_PONG_BACK = 15000
const KEEP_ALIVE_CHECK_INTERVAL = 7500
const startConnection = async () => {
let provider = new ethers.providers.InfuraProvider("homestead", process.env.INFURA); // homestead -> mainnet
let wsprovider = ethers.providers.InfuraProvider.getWebSocketProvider("homestead", process.env.INFURA); // homestead -> mainnet
// let provider = new ethers.providers.JsonRpcProvider("http://localhost:8545")
// let wsprovider = new ethers.providers.WebSocketProvider("http://localhost:8545")
let contract = new ethers.Contract(process.env.NEW_DEED, newAbi, provider);
let wscontract = new ethers.Contract(process.env.NEW_DEED, newAbi, wsprovider);
let pingTimeout = null
let keepAliveInterval = null
const filter1 = wscontract.filters.NewDeed()
const filter2 = wscontract.filters.Transfer()
const events = await contract.queryFilter({
address: process.env.NEW_DEED,
topics: filter1.topics.concat(filter2.topics)
}, "latest") // 14379229, "latest") // TODO: switch later to LATEST only
const logs = await provider.getLogs({
fromBlock: 14379229,
toBlock: 'latest',
address: process.env.NEW_DEED,
topic: filter1.topics.concat(filter2.topics)
})
// TODO: remove after posting all missed logs
// for (let log of logs) {
// ev = contract.interface.parseLog(log)
// if (ev) {
// if (ev.name === "Transfer") {
// await new Promise(r => setTimeout(r, 2500));
// if (ev.args.from === ethers.constants.AddressZero) {
// let msg = "\nDeed #" + ev.args.tokenId.toString() + " minted by " + ev.args.to + "!"
// console.log(msg)
// client.channels.cache.get(process.env.CHAT_ID.toString()).send(msg);
// }
// }
// if (ev.name === "NewDeed") {
// if (ev.args.Referer === ethers.constants.AddressZero) {
// if (ev.args.DeedPoints.gt(1)) {
// let msg = "Owner: " + ev.args.DeedOwner + " (+2pts)"
// console.log(msg)
// client.channels.cache.get(process.env.CHAT_ID.toString()).send(msg);
// } else {
// let msg = "Owner: " + ev.args.DeedOwner + " (+1pt)"
// console.log(msg)
// client.channels.cache.get(process.env.CHAT_ID.toString()).send(msg);
// }
// } else {
// let msg = "Owner: " + ev.args.DeedOwner + " : Referred by " + ev.args.Referer + " (+2pts, +3pts)"
// console.log(msg)
// client.channels.cache.get(process.env.CHAT_ID.toString()).send(msg);
// }
// }
// }
// await new Promise(r => setTimeout(r, 500));
// }
wscontract.on(events, (ev) => {
if (ev) {
if (ev.event === "Transfer") {
if (ev.args.from === ethers.constants.AddressZero) {
let msg = "@everyone Deed #" + ev.args.tokenId.toString() + " minted by " + ev.args.to + "!"
console.log(msg)
client.channels.cache.get(process.env.CHAT_ID.toString()).send(msg);
}
}
if (ev.event === "NewDeed") {
if (ev.args.Referer === ethers.constants.AddressZero) {
if (ev.args.DeedPoints.gt(1)) {
let msg = "Owner: " + ev.args.DeedOwner + " (+2pts)"
console.log(msg)
client.channels.cache.get(process.env.CHAT_ID.toString()).send(msg);
} else {
let msg = "Owner: " + ev.args.DeedOwner + " (+1pt)"
console.log(msg)
client.channels.cache.get(process.env.CHAT_ID.toString()).send(msg);
}
} else {
let msg = "Owner: " + ev.args.DeedOwner + " : Referred by " + ev.args.Referer + " (+2pts, +3pts)"
console.log(msg)
client.channels.cache.get(process.env.CHAT_ID.toString()).send(msg);
}
}
}
})
keepAliveInterval = setInterval(() => {
console.log('Checking if the connection is alive, sending a ping')
wsprovider._websocket.ping()
pingTimeout = setTimeout(() => {
wsprovider._websocket.terminate()
}, EXPECTED_PONG_BACK)
}, KEEP_ALIVE_CHECK_INTERVAL)
wsprovider._websocket.on('close', () => {
console.log('The websocket connection was closed')
clearInterval(keepAliveInterval)
clearTimeout(pingTimeout)
startConnection()
})
wsprovider._websocket.on('pong', () => {
console.log('Received pong, so connection is alive, clearing the timeout')
clearInterval(pingTimeout)
})
}
startConnection()
client.login(process.env.BOT_TOKEN);