forked from tyzoo/poap-discord-feed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
284 lines (255 loc) · 9.24 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
278
279
280
281
282
283
284
require("dotenv").config();
const API_KEY = process.env.POAP_API_KEY;
const DISCORD_CHANNELS = JSON.parse(process.env.DISCORD_CHANNELS_NAMES);
//set the enviornment variables in a .env file
const {
DISCORD_TOKEN,
DISCORD_CHANNEL_NAME,
XDAI_WS_PROVIDER,
MAINNET_WS_PROVIDER,
} = process.env;
//Initial xDai/blockchain code by @brunitob
const Web3 = require("web3");
const PoapAbi = require("./poap.json");
const POAP_XDAI_CONTRACT = "0x22C1f6050E56d2876009903609a2cC3fEf83B415";
const ZEROX = "0x0000000000000000000000000000000000000000";
const { default: axios } = require("axios");
const Discord = require("discord.js");
// Networks availables
const XDAI_NETWORK = "XDAI";
const MAINNET_NETWORK = "MAINNET";
const MINT_ACTION = "MINT";
const TRANSFER_ACTION = "TRANSFER";
const BURN_ACTION = "BURN";
const options = {
// Enable auto reconnection
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 20,
onTimeout: false,
},
};
const axiosRetry = require('axios-retry');
const sleep = ms => new Promise(r => setTimeout(r, ms));
const bot = new Discord.Client();
bot.login(DISCORD_TOKEN);
bot.on("ready", () => {
console.info(`Discord Bot logged in: ${bot.user.tag}!`);
});
const start = () => {
console.log("+*+*+*+*+*+*+*+*+*+*+*+*+*+*+");
console.log("Starting to listen POAP events...");
console.log("+*+*+*+*+*+*+*+*+*+*+*+*+*+*+");
const web3xDai = new Web3(
new Web3.providers.WebsocketProvider(XDAI_WS_PROVIDER, options)
);
const web3Mainnet = new Web3(
new Web3.providers.WebsocketProvider(MAINNET_WS_PROVIDER, options)
);
axiosRetry(axios, {
retries: 6, // number of retries
retryDelay: (retryCount) => {
console.log(`Retry attempt: ${retryCount}`);
return retryCount * 5000; // time interval between retries
},
retryCondition: (error) => {
// if retry condition is not specified, by default idempotent requests are retried
return error.response.status >= 400;
},
});
subscribeToTransfer(web3xDai, POAP_XDAI_CONTRACT, XDAI_NETWORK);
subscribeToTransfer(web3Mainnet, POAP_XDAI_CONTRACT, MAINNET_NETWORK);
};
const subscribeToTransfer = (web3, address, network) => {
let lastHash = ""
console.log(`Subscribing to ${network} - ${address} `);
const PoapContract = new web3.eth.Contract(PoapAbi, address);
PoapContract.events
.Transfer(null)
.on("data", async (result) => {
// console.log(result)
try {
const tokenId = result.returnValues.tokenId;
const fromAddress = result.returnValues.from;
const toAddress = result.returnValues.to;
const txHash = result.transactionHash;
console.log(`TokenId: ${tokenId}, to: ${toAddress}, tx: ${txHash}`);
const action = fromAddress === ZEROX ? MINT_ACTION : toAddress === ZEROX ? BURN_ACTION : TRANSFER_ACTION;
const tokenInfo = await getTokenById(tokenId);
if (tokenInfo && tokenInfo.image_url && lastHash !== txHash) {
await sendPoapEmbeddedMessage(
tokenInfo.image_url,
action,
tokenId,
tokenInfo.id,
tokenInfo.name,
toAddress,
tokenInfo.poapPower,
tokenInfo.ens,
network
);
await sendPoapSlackMessage(
tokenInfo.image_url,
action,
tokenId,
tokenInfo.id,
tokenInfo.name,
toAddress,
tokenInfo.poapPower,
tokenInfo.ens,
network
);
lastHash = txHash;
}
} catch (e){
console.error("ERROR SENDING DISCORD MESSAGE");
console.error(e);
}
})
.on("connected", (subscriptionId) => {
console.log(`Connected to ${network} - ${subscriptionId} `);
})
.on("changed", (log) => {
console.log(`Changed to ${network} - ${log} `);
})
.on("error", (error) => {
console.error(`Error to ${network} - ${error} `);
});
};
const getTokenById = async (tokenId) => {
let event, ens, address, poapPower = undefined;
try{
await sleep(5000);
const tokenData = await axios.get(`https://api.poap.tech/token/${tokenId}`, {
headers: {
'x-api-key': API_KEY,
}
});
event = tokenData.data.event;
address = tokenData.data.owner;
const addressPoaps = await axios.get(`https://api.poap.tech/actions/scan/${address}`, {
headers: {
'x-api-key': API_KEY,
}
});
poapPower = (addressPoaps.data.length) > 0 ? addressPoaps.data.length : 0;
const ensLookup = await axios.get(`https://api.poap.tech/actions/ens_lookup/${address}`, {
headers: {
'x-api-key': API_KEY,
}
});
ens = ensLookup.data.ens;
} catch (e) {
console.error("ERROR FETCHING API:")
console.error(e);
}
if(!(event && event.id && event.name && event.image_url)){
return undefined;
}
poapPower = (poapPower !== undefined)? poapPower : 0;
const { id, name, image_url } = event;
return {
id,
name,
address,
image_url,
poapPower,
ens,
};
}
const sendPoapSlackMessage = async (
imageUrl,
actionType,
poapId,
dropId,
dropName,
plainAddress,
poapPower,
ens,
network
) => {
if(actionType !== MINT_ACTION){
return;
}
const address = ens ? ens : plainAddress;
const familyUrl = `https://poap.family/event/${dropId}?utm_share=slackfeed`;
const galleryUrl = `https://poap.gallery/event/${dropId}?utm_share=slackfeed`;
const scanUrl = `https://app.poap.xyz/scan/${address}/?utm_share=slackfeed`;
const data = { imageUrl, actionType, poapId, dropId, dropName, address, poapPower, ens, network, galleryUrl, scanUrl, familyUrl };
try {
await axios.post("https://hooks.slack.com/triggers/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", data);
} catch (e) {
console.log("Error while trying to send slack message");
console.error(e);
}
};
const sendPoapEmbeddedMessage = async (
imageUrl,
actionType,
tokenId,
eventId,
eventName,
address,
poapPower,
ens,
network
) => {
// Create the embedded message using the Discord MessageEmbed API
const messageEmbed = new Discord.MessageEmbed()
.setTitle(`${actionType}: ${eventName} `)
.setColor(network === MAINNET_NETWORK ? "#5762cf" : "#48A9A9")
.addFields(
{ name: "POAP Power", value: `${emoji(poapPower)} ${poapPower}`, inline: true },
{ name: "Token ID", value: `#${tokenId}`, inline: true },
{ name: "Event ID", value: `#${eventId}`, inline: true }
)
.setURL(`https://poap.gallery/event/${eventId}/?utm_share=discordfeed`)
.setTimestamp()
.setAuthor(
ens ? ens : address.toLowerCase(),
``,
`https://app.poap.xyz/scan/${address}/?utm_share=discordfeed`
)
.setThumbnail(imageUrl);
// Call the broadcastMessage function to send the message to all relevant channels
broadcastMessage(actionType, messageEmbed);
};
const broadcastMessage = async (actionType, messageEmbed) => {
// Iterate through all the channels
DISCORD_CHANNELS.forEach((channelConfig) => {
// Check if specific actions are defined for this channel
const hasDefinedActions = Array.isArray(channelConfig.actions) && channelConfig.actions.length;
const uppercaseActions = (Array.isArray(channelConfig.actions)? channelConfig.actions : []).map((action) => action.toUpperCase());
// If the channel has defined actions and the current action is not one of them, we skip this channel
if (hasDefinedActions && !uppercaseActions.includes(actionType.toUpperCase())) {
return;
}
// Send message to the specific channel
sendMessageToChannel(channelConfig, messageEmbed);
});
};
const sendMessageToChannel = async (channelConfig, messageEmbed) => {
// Find the corresponding Discord channel by name
const discordChannel = bot.channels.cache.find(
(channel) => channel.name === channelConfig.name
);
// If the channel does not exist, we return
if (!discordChannel) return;
//console.log("Sending message to channel: " + channelConfig.name);
//console.log(messageEmbed);
// Send the message to the channel
discordChannel.send(messageEmbed);
};
const emoji = (poapPower) => {
return poapPower <= 5
? "🆕 "
: poapPower <= 10
? "🟢 "
: poapPower <= 20
? "🟡 "
: poapPower <= 50
? "🔴 "
: "🔥 ";
};
start();