-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
202 lines (183 loc) · 5.91 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
require("dotenv").config();
//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 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)
);
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)
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 tokenInfo = await getTokenById(tokenId);
// mint
// transfer
// burn
const action =
fromAddress == ZEROX
? MINT_ACTION
: toAddress == ZEROX
? BURN_ACTION
: TRANSFER_ACTION;
if (tokenInfo && tokenInfo.image_url && lastHash != txHash) {
logPoap(
tokenInfo.image_url,
action,
tokenId,
tokenInfo.id,
tokenInfo.name,
toAddress,
tokenInfo.poapPower,
tokenInfo.ens,
network
);
lastHash = txHash
}
})
.on("connected", (subscriptionId) => {
console.log(`Connected to ${network} - ${subscriptionId} `);
})
.on("changed", (log) => {
console.log(`Changed to ${network} - ${log} `);
})
.on("error", (error) => {
console.log(`Error to ${network} - ${error} `);
});
};
const getTokenById = async (tokenId) => {
const tokenInfoCompleted = await axios
.get(`https://api.poap.xyz/token/${tokenId}`)
.then(async (response) => {
// {"event":{"id":1710,"fancy_id":"avastars-birthday-party-winner-poap-2021","name":"Avastars Birthday Party WINNER POAP","event_url":"https://avastars.io/","image_url":"https://storage.googleapis.com/poapmedia/avastars-birthday-party-winner-poap-2021-logo-1618590848145.png","country":"","city":"","description":"Poap for winners of giveaways","year":2021,"start_date":"20-Apr-2021","end_date":"20-Apr-2021"},"tokenId":"168570","owner":"0x4af37e995eb4fadc77a5ee355ae0a80edc5d1f04","layer":"Layer2"}
const { id, name, image_url } = response.data.event;
const address = response.data.owner;
const tokenWithEns = await axios
.get(`https://api.poap.xyz/actions/ens_lookup/${address}`)
.then(async (ensResponse) => {
//ens is null if it is not valid
const { ens } = ensResponse.data;
const tokenInfoWithPower = await axios
.get(`https://api.poap.xyz/actions/scan/${address}`)
.then(async (scanResponse) => {
const poapPower = scanResponse.data.length;
return {
id,
name,
address,
image_url,
poapPower,
ens,
};
})
.catch((e) => console.log(e));
return tokenInfoWithPower;
})
.catch((e) => console.log(e));
return tokenWithEns;
})
.catch((e) => console.log(e));
return tokenInfoCompleted;
};
const logPoap = async (
imageUrl,
action,
tokenId,
eventId,
eventName,
address,
poapPower,
ens,
network
) => {
const channel = bot.channels.cache.find(
(ch) => ch.name === DISCORD_CHANNEL_NAME
);
if (!channel) return;
const embed = new Discord.MessageEmbed() // Ver 12.2.0 of Discord.js
.setTitle(`${action}: ${eventName} `)
.setColor(network == MAINNET_NETWORK ? "#5762cf" : "#48A9A9")
// removed, maybe we can show mainnet etherscan link
// .setDescription(
// `POAP Power: ${poapPower} ${emoji(poapPower)} | Token ID# ${tokenId} | Event ID#: ${eventId}`
// )
.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);
channel.send(embed);
};
const emoji = (poapPower) => {
return poapPower <= 5
? "🆕 "
: poapPower <= 10
? "🟢 "
: poapPower <= 20
? "🟡 "
: poapPower <= 50
? "🔴 "
: "🔥 ";
};
start();