forked from dsgriffin/opensea-sales-x-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
132 lines (101 loc) · 3.05 KB
/
app.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
require('dotenv').config();
const axios = require('axios');
const tweet = require('./tweet');
const discord = require("./discord");
const { ethers } = require('ethers');
const { db } = require('./db');
const processDelayMS = parseInt(process.env.PROCESS_DELAY);
const baseUri = process.env.BASE_URI;
const abi = ['event Fused(address sender, uint fusedId, uint burnedId, bytes32 fusionReceiptIPFSHash)'];
const address = process.env.CONTRACT_ADDRESS;
const provider = new ethers.providers.InfuraProvider(process.env.CONTRACT_NETWORK, process.env.INFURA_APIKEY);
const contract = new ethers.Contract(address, abi, provider);
const setLatestBlockNumber = (blockNumber) => {
db.json.latestBlockHash = blockNumber;
db.save();
}
/**
*
* @param {{getBlock: () => any, blockHash: string, args: {sender: string, fusedId: any, burnedId: any, fusionReceiptIPFSHash: string}}[]} events
* @return {Promise<void>}
*/
const setBlock = async (events) => {
if (events.length === 0) {
return;
}
if (!db.json.blocks) {
db.json.blocks = {};
}
if (db.json.blocks[events[0].blockHash]) {
return;
}
const block = await events[0].getBlock();
const timestamp = block.timestamp;
db.json.blocks[block.hash] = {
events: events.map(event => ({
sender: event.args.sender,
fusedId: event.args.fusedId.toNumber(),
burnedId: event.args.burnedId.toNumber()
})),
timestamp,
hash: block.hash,
posted: false
}
db.save();
}
const filterUnPosted = () => {
const now = Date.now() / 1000;
return Object.values(db.json.blocks || []).filter(block => (!block.posted && block.timestamp - processDelayMS < now));
}
/**
*
* @param {{sender: string, fusedId: number, burnedId: number}} event
*/
const prepareFusion = async event => ({
toBurn: event.burnedId,
toFuse: event.fusedId,
sender: event.sender,
imageUrl: (await axios.get(`${baseUri}/${event.fusedId}`)).data.image
})
const postEvents = async () => {
const unPosted = filterUnPosted();
for await (const block of unPosted) {
for await (const event of block.events) {
try {
const payload = await prepareFusion(event);
await tweet.tweet(payload)
await discord.send(payload);
console.log(`Successfully posted #${payload.toFuse} and #${payload.toBurn} to Discord!`);
} catch (e) {
console.error(e);
return;
}
}
block.posted = true;
db.save();
}
}
const main = () => {
setInterval(async () => {
const events = await contract.queryFilter('Fused', db.json.latestBlockHash || undefined);
if (events.length === 0) {
return;
}
setLatestBlockNumber(events[events.length - 1].blockNumber);
const blockEvents = {};
for (const event of events) {
if (blockEvents[event.blockHash]) {
blockEvents[event.blockHash].push(event);
} else {
blockEvents[event.blockHash] = [event];
}
}
Object.values(blockEvents).forEach(setBlock);
try {
await postEvents();
} catch (e) {
console.log(e);
}
}, processDelayMS);
}
main();