This repository was archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoinBuyerHelper.js
More file actions
390 lines (325 loc) · 12.4 KB
/
Copy pathcoinBuyerHelper.js
File metadata and controls
390 lines (325 loc) · 12.4 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
const { ethers } = require('ethers');
const fs = require('fs');
const { Mutex } = require('async-mutex');
const fileMutex = new Mutex();
const coinMutex = new Mutex();
const transferSig = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
/**
* @param {import('alchemy-sdk').Log} log
* @returns {boolean}
* */
function isTransferLog(log) {
if (log.topics.length === 0) return false;
return log.topics[0].toLowerCase() === transferSig;
}
function parseLogAddress(s) {
return ethers.utils.getAddress(`0x${s.slice(-40)}`)
}
/**
*
* @typedef {Object} TransferLogInfo
* @property {string} sender - Who Sent the tokens
* @property {string} receiver - Who received the tokens
* @property {string} asset - Which token was transfered
* @property {string} amount - Amount transfered
*/
/**
* @param {import('alchemy-sdk').Log} log
* @returns {TransferLogInfo}
* */
function parseLog(log) {
const [_, sender, receiver] = log.topics
const asset = log.address
const amount = ethers.BigNumber.from(log.data).toString()
return {
sender: parseLogAddress(sender),
receiver: parseLogAddress(receiver),
asset,
amount
}
}
/**
* @param {import('alchemy-sdk').Log[]} logs
* @param {string} address
* @returns {string}
* */
function isValidBuyTransaction(logs, address) {
if (logs.length === 0) return false
if(!address) return false
// Check if the wallet is receiving any asset
const transferLogs = logs
.filter(isTransferLog)
.map(parseLog)
.filter(l => l.receiver === ethers.utils.getAddress(address));
if(transferLogs.length > 0){
return transferLogs[transferLogs.length-1].sender.toLowerCase();
}
return undefined;
}
async function addObjectToFile(chatId, groupId) {
const filePath = "./groupData.json"
const object = {
"chatId": chatId,
"group": groupId
}
try {
const release = await fileMutex.acquire();
// Read existing data from the file
const existingData = await fs.promises.readFile(filePath, 'utf8');
const existingObjects = JSON.parse(existingData);
// Add the new object to the array
existingObjects.push(object);
// Write the updated data back to the file
await fs.promises.writeFile(filePath, JSON.stringify(existingObjects, null, 2));
release();
} catch (error) {
console.error('Error adding object to file:', error);
}
}
// Function to remove an object from a file by chatId
async function removeObjectFromFile(chatId) {
const filePath = "./groupData.json"
try {
// Read existing data from the file
const release = await fileMutex.acquire();
const existingData = await fs.promises.readFile(filePath, 'utf8');
const existingObjects = JSON.parse(existingData);
// Find the index of the object with the provided chatId
const indexToRemove = existingObjects.findIndex(obj => obj.chatId === chatId);
if (indexToRemove !== -1) {
// Remove the object from the array
existingObjects.splice(indexToRemove, 1);
// Write the updated data back to the file
await fs.promises.writeFile(filePath, JSON.stringify(existingObjects, null, 2));
} else {
console.log('Object with specified chatId not found in the file.');
}
release();
} catch (error) {
console.error('Error removing object from file:', error);
}
}
async function getChatIdsFromFile() {
const filePath = "./groupData.json"
try {
// Read existing data from the file
const existingData = await fs.promises.readFile(filePath, 'utf8');
const existingObjects = JSON.parse(existingData);
return existingObjects;
} catch (error) {
console.error('Error getting chatIds from file:', error);
return [];
}
}
async function updateOptions(chatId, newAddress, newName) {
try {
// Read the file and parse its content
const release = await fileMutex.acquire();
const data = await fs.promises.readFile("./groupData.json", 'utf8');
const jsonArray = JSON.parse(data);
// Find the object with the matching chatId
const index = jsonArray.findIndex(item => item.chatId === chatId);
if (index !== -1) {
// If options object doesn't exist, create it
if (!jsonArray[index].options) {
jsonArray[index].options = {};
}
// Add or update the 'address' and 'newName' properties in options
jsonArray[index].options[newAddress] = newName;
}
// Write the updated content back to the file
await fs.promises.writeFile("./groupData.json", JSON.stringify(jsonArray, null, 2));
release();
} catch (error) {
console.error('Error:', error);
}
}
async function updateAgeFilter(chatId, pronoun, time) {
try {
// Read the file and parse its content
const release = await fileMutex.acquire();
const data = await fs.promises.readFile("./groupData.json", 'utf8');
const jsonArray = JSON.parse(data);
// Find the object with the matching chatId
const index = jsonArray.findIndex(item => item.chatId === chatId);
if (index !== -1) {
// If options object doesn't exist, create it
if (!jsonArray[index].options) {
jsonArray[index].options = {};
}
// Add or update the 'address' and 'newName' properties in options
jsonArray[index].options["filterPronoun"] = pronoun;
jsonArray[index].options["time"] = time;
}
// Write the updated content back to the file
await fs.promises.writeFile("./groupData.json", JSON.stringify(jsonArray, null, 2));
release();
} catch (error) {
console.error('Error:', error);
}
}
async function updateMarketcapFilter(chatId, pronoun, amount) {
try {
// Read the file and parse its content
const release = await fileMutex.acquire();
const data = await fs.promises.readFile("./groupData.json", 'utf8');
const jsonArray = JSON.parse(data);
// Find the object with the matching chatId
const index = jsonArray.findIndex(item => item.chatId === chatId);
if (index !== -1) {
// If options object doesn't exist, create it
if (!jsonArray[index].options) {
jsonArray[index].options = {};
}
// Add or update the 'address' and 'newName' properties in options
jsonArray[index].options["filterPronounMM"] = pronoun;
jsonArray[index].options["amountMM"] = amount;
}
// Write the updated content back to the file
await fs.promises.writeFile("./groupData.json", JSON.stringify(jsonArray, null, 2));
release();
} catch (error) {
console.error('Error:', error);
}
}
async function generateFormattedList() {
try {
// Read the JSON data from the file
const data = await fs.promises.readFile('./scrapedCoins.json', 'utf8');
const coinData = JSON.parse(data);
// Initialize an empty array to store the formatted strings
const formattedList = [];
// Iterate through the object properties and construct formatted strings
let currentString = ''; // Initialize the current formatted string
for (const key in coinData) {
if (Object.hasOwnProperty.call(coinData, key)) {
const value = coinData[key].name;
const formattedString = `${value}: <a href='https://www.dextools.io/app/en/ether/pair-explorer/${key}'>DexTools</a> | <a href='https://www.dexview.com/eth/${key}'>DexView</a>`;
// Check if adding the formattedString would exceed the limit
if (currentString.length + formattedString.length > 3950) {
formattedList.push(currentString); // Push the current formatted string
currentString = formattedString; // Start a new formatted string
} else {
// Add the formattedString to the current string
currentString += (currentString ? '\n' : '') + formattedString;
}
}
}
// Push the last formatted string
if (currentString) {
formattedList.push(currentString);
}
return formattedList;
} catch (error) {
console.error('Error:', error);
return [];
}
}
async function findWalletsWithTracker(trackerValue) {
let walletsWithTracker = [];
// Read the JSON data from the file
const data = await fs.promises.readFile('./customBuyersArray.json', 'utf8');
const coinData = JSON.parse(data);
for (const key in coinData) {
if (coinData.hasOwnProperty(key)) {
const element = coinData[key];
// Check if the tracker array exists and contains the trackerValue
if (element?.tracker && element.tracker.includes(trackerValue)) {
walletsWithTracker.push(key);
}
}
}
return walletsWithTracker;
}
async function getAddressData(senderAddress) {
const data = await fs.promises.readFile('./customBuyersArray.json', 'utf8');
const buyerData = JSON.parse(data);
if (buyerData[senderAddress]) {
return buyerData[senderAddress]
}else{
return false;
}
}
async function addTrackerToKey(wallet, chatId) { //key is chatId
const release = await coinMutex.acquire();
try {
const data = await fs.promises.readFile('./customBuyersArray.json', 'utf8');
const buyerData = JSON.parse(data);
if (buyerData[wallet]) {
// If 'tracker' is an array, add the trackerValue
if (Array.isArray(buyerData[wallet]?.tracker)) {
if (!buyerData[wallet].tracker.includes(chatId)) {
buyerData[wallet].tracker.push(chatId);
}
} else {
// Initialize the tracker array if it doesn't exist
buyerData[wallet].tracker = [chatId];
}
} else {
// Initialize the key with tracker and visibility if key doesn't exist
buyerData[wallet] = {
tracker: [chatId],
visibility: 'private'
};
}
await fs.promises.writeFile('./customBuyersArray.json', JSON.stringify(buyerData, null, 2));
} catch (error) {
console.error("An error occurred:", error);
} finally {
release();
}
}
async function removeTrackerFromWallet(wallet, chatId) {
const release = await coinMutex.acquire();
try {
// Read the file
const data = await fs.promises.readFile('./customBuyersArray.json', 'utf8');
const buyerData = JSON.parse(data);
// Check if the wallet exists and if 'tracker' is an array
if (buyerData[wallet] && Array.isArray(buyerData[wallet].tracker)) {
// Filter out the chatId from the tracker array
buyerData[wallet].tracker = buyerData[wallet].tracker.filter(id => id !== chatId);
// Optionally, if the tracker array becomes empty, you might want to handle it (e.g., remove the wallet entry or keep it empty)
if (buyerData[wallet].tracker.length === 0) {
delete buyerData[wallet]; // or handle as needed
}
// Write the updated data back to the file
await fs.promises.writeFile('./customBuyersArray.json', JSON.stringify(buyerData, null, 2));
}
} catch (error) {
console.error("An error occurred:", error);
} finally {
// Release the mutex
release();
}
}
async function compareTimeValues(userValue, otherValue) {
const timeUnits = {
'm': 60 * 1000, // minutes in milliseconds
'h': 60 * 60 * 1000, // hours in milliseconds
'd': 24 * 60 * 60 * 1000 // days in milliseconds
};
const extractValueAndUnit = (value) => {
const numericValue = parseInt(value);
const unit = value.charAt(value.length - 1).toLowerCase();
return numericValue * (timeUnits[unit] || 1);
};
const userTime = extractValueAndUnit(userValue);
const otherTime = extractValueAndUnit(otherValue);
return userTime < otherTime;
}
module.exports = {
isValidBuyTransaction,
addObjectToFile,
removeObjectFromFile,
getChatIdsFromFile,
updateOptions,
generateFormattedList,
updateAgeFilter,
compareTimeValues,
findWalletsWithTracker,
addTrackerToKey,
getAddressData,
removeTrackerFromWallet,
updateMarketcapFilter
};