-
Notifications
You must be signed in to change notification settings - Fork 1
/
discordBot.js
218 lines (194 loc) · 5.99 KB
/
discordBot.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
const ethers = require("ethers");
const mongoose = require("mongoose");
const { Schema } = mongoose;
const {
Client,
Events,
GatewayIntentBits,
EmbedBuilder,
} = require("discord.js");
const {
NFT_CONTRACT,
ABI,
WETH_ABI,
MONGODB_URI,
DISCORD_TOKEN,
DISCORD_CHANNEL_ID,
} = require("./settings.json");
const salesSchema = new Schema({
id: Number,
fella: String,
from: String,
to: String,
tx: String,
block: Number,
logged: { type: Boolean, default: false },
});
salesSchema.index({ tx: 1, fella: 1 }, { unique: true });
const Sales = mongoose.model("sales", salesSchema);
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
db.on("error", console.error.bind(console, "MongoDB connection error:"));
db.once("open", () => {
console.log("Connected to MongoDB!");
client.once(Events.ClientReady, (c) => {
console.log(`Connected to Discord!`);
checkNFTSales();
setInterval(checkNFTSales, 10000);
});
client.login(DISCORD_TOKEN);
});
process.on("SIGINT", async () => {
console.log("Shutting down the bot...");
await finishUp();
});
let periodInterval;
const checkNFTSales = async () => {
try {
const url = `https://mainnet.base.org`;
const provider = new ethers.JsonRpcProvider(url);
const currentBlock = await provider.getBlockNumber();
const filter = {
address: NFT_CONTRACT,
fromBlock: currentBlock - 1500,
toBlock: "latest",
};
const transactions = await provider.getLogs(filter);
const resetColor = "\x1b[0m";
const greenColor = "\x1b[32m";
const addPeriod = () => process.stdout.write(".");
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(
`${resetColor}${greenColor}[${currentBlock}] ${resetColor}Checking for Fella sales.`,
);
if (periodInterval) {
clearInterval(periodInterval);
}
periodInterval = setInterval(addPeriod, 2800);
for (const log of transactions) {
try {
const { transactionHash, blockNumber } = log;
const contract = new ethers.Contract(NFT_CONTRACT, ABI, provider);
const event = contract.interface.parseLog(log);
const { value } = await provider.getTransaction(transactionHash);
const WETH = new ethers.Contract("0x4200000000000000000000000000000000000006", WETH_ABI, provider);
const WETHLOGS = await WETH.queryFilter("Transfer",blockNumber,blockNumber);
const price = await gweiToEth(value);
let WETH_PRICE = 0
for (const log of WETHLOGS) {
try {
const { transactionHash: wethTxHash } = log;
if (wethTxHash === transactionHash) {
log.args[2] ? WETH_PRICE += parseInt(log.args[2]) : null
}
} catch (error) {
console.error("Error returning WETH values:", error);
}
}
if (event.name === "Transfer" && value !== 0n || WETH_PRICE !== 0) {
const { from, to, tokenId } = event.args;
const existingSale = await Sales.findOne({
tx: transactionHash,
fella: tokenId.toString(),
});
if (!existingSale || !existingSale.logged) {
const message = {
id: tokenId,
from: from,
to: to,
tx: transactionHash,
price: WETH_PRICE === 0 ? price : (WETH_PRICE / 10 ** 18).toString(),
};
if (existingSale) {
existingSale.logged = true;
await existingSale.save();
} else {
const newSale = new Sales({
fella: tokenId.toString(),
from,
to,
tx: transactionHash,
block: blockNumber,
logged: true,
});
await newSale.save();
}
await sendMessageToDiscord(message);
process.stdout.clearLine();
process.stdout.cursorTo(0);
console.log(`Sale found @ ${message.tx}`);
}
}
} catch (error) {
console.error("Error processing transaction:", error);
}
}
} catch (error) {
console.log(error);
}
};
const sendMessageToDiscord = async (message) => {
const channel = client.channels.cache.get(DISCORD_CHANNEL_ID);
if (channel) {
const Embed = new EmbedBuilder()
.setColor(0x0099ff)
.setTitle(`Fella #${message.id}`)
.setURL(`https://basedfellas.io/collection/${message.id}`)
.setAuthor({
name: "Fella Sales Bot",
iconURL: "https://basedfellas.io/images/1.png",
url: "https://basedfellas.io",
})
.setDescription(
`Fella #${message.id} has just been sold for ${message.price} ETH`,
)
.addFields(
{
name: "From",
value: `[${shortenEthAddy(
message.from,
)}](https://basescan.org/address/${message.from})`,
},
{
name: "To",
value: `[${shortenEthAddy(
message.to,
)}](https://basescan.org/address/${message.to})`,
},
{
name: "TX",
value: `[${shortenEthAddy(message.tx)}](https://basescan.org/tx/${
message.tx
})`,
},
)
.setImage(`https://basedfellas.io/images/fellas/${message.id}.png`)
.setTimestamp()
.setFooter({
text: "Fella Sales Bot By KingSimpa",
iconURL: "https://github.com/KingSimpa69",
});
await channel.send({ embeds: [Embed] });
} else {
console.error("Discord channel not found.");
}
};
const finishUp = async () => {
await db.close();
await client.destroy();
console.log("Connections closed!");
process.exit(0);
};
const shortenEthAddy = (addy) => {
const shorty = addy.slice(0, 5) + "..." + addy.slice(37, 41);
return shorty;
};
const gweiToEth = async (gwei) => {
const result = ethers.formatEther(gwei);
return result;
};