I have a piece of code that is scanning for new ERC-20 eth contracts via Infura API each 5 seconds and saving the addresses to the JSON file.
Then I have a second function which is not working how it should, the problem with that function (check Contract) is using etherscan API to check if the contract has https://link to find social media and if it's found, bot will send me a message and then address and it's data should be removed from the JSON. Also when source code exists but there is no URL, but should also remove this contract from the JSON.
//Some const set up from the top
const infura = process.env.Infura;
const myself = process.env.MYTELEGRAM;
const ETHScanAPI = process.env.EtherscanAPI;
const axios = require('axios');
const fs = require('fs');
const Web3 = require('web3').default;
const web3 = new Web3(`wss://mainnet.infura.io/ws/v3/${infura}`);
const path = require('path');
const filePath = path.resolve(process.cwd(), 'latestblock.json');
const contracts = path.resolve(process.cwd(), 'foundcontracts.json');
//Part of the funcion that is adding ERC20-tokens to my JSON file
if (tokenDetails.isERC20 === true) {
let newContract = {
"contractAddress": receipt.contractAddress,
"name": tokenDetails.name,
"ticker": tokenDetails.symbol,
"timestamp": Date.now()
};
//Get Real supply and convert it to the BigInt and then devide to get real number
const realSupply = Number(tokenDetails.totalSupply / BigInt(10) ** BigInt(tokenDetails.decimals));
console.log(realSupply);
// Regex to make number like this ex: 1,000
const parsedSupply = parseFloat(realSupply.toFixed(0)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
console.log(parsedSupply);
// Add the new contract to the array
foundContracts.foundcontracts.push(newContract);
// Write the updated JSON back to the file
fs.writeFileSync(contracts, JSON.stringify(foundContracts));
// Send a message on telegram
bot.sendMessage(myself, ` ⚙️ New ERC20-Token deployed ⚙️ \n\nName: <b>${tokenDetails.name}</b> \nSymbol: $${tokenDetails.symbol} \nTotal Supply: <b>${parsedSupply}</b> \n\nContract: <a href='https://etherscan.io/address/${receipt.contractAddress}'>${receipt.contractAddress}</a>`, {parse_mode: 'HTML'});
// Check async function (causing problems)
async function checkContracts() {
// Read the contracts from the JSON file
let readContracts = fs.readFileSync(contracts, 'utf8');
let foundContracts = JSON.parse(readContracts);
console.log(foundContracts);
// Get the current timestamp
const now = Date.now();
// Iterate over the contracts
for (let i = 0; i < foundContracts.foundcontracts.length; i++) {
let contract = foundContracts.foundcontracts[i];
// Check if the contract has been scanned for more than two hours
if (now - contract.timestamp > 2 * 60 * 60 * 1000) {
// If it has, remove it from the array
foundContracts.foundcontracts.splice(i, 1);
i--; // Decrement the index to account for the removed element
continue; // Skip the rest of this iteration
}
// Get the source code of the contract
const url = `https://api.etherscan.io/api?module=contract&action=getsourcecode&address=${contract.contractAddress}&apikey=${ETHScanAPI}`;
const response = await axios.get(url);
// Check if the contract has a URL
if (response.data.status === '1') {
const sourceCode = response.data.result[0].SourceCode;
// Check for socials regardless of whether sourceCode is truthy or falsy
const socials = extractSocials(sourceCode);
if (!socials) {
// If the contract doesn't have a URL, remove it from the array
foundContracts.foundcontracts.splice(i, 1);
i--; // Decrement the index to account for the removed element
} else {
// If the contract has a URL, check for socials
if (socials) {
// If socials are found, send a message to the Telegram chat
bot.sendMessage(myself, ` Found socials for contract <a href='https://etherscan.com/address/${contract.contractAddress}'>${contract.contractAddress}</a> \n\nName: ${contract.name} \nSymbol: $${contract.ticker} \n\n ${socials}`, {parse_mode: 'HTML'});
// Remove the contract from the array after sending the message
foundContracts.foundcontracts.splice(i, 1);
i--; // Decrement the index to account for the removed element
}
}
} else {
console.log('Source code not found for contract:', contract.contractAddress);
}
}
// Write the updated contracts back to the JSON file
fs.writeFileSync(contracts, JSON.stringify(foundContracts, null, 2));
};
// Call the function every 60 seconds
setInterval(checkContracts, 60000);
This function async function checkContracts() is not properly removing smart concratcs from the JSON, what I found is that the duplicated messages stop after 2 hours, which is thanks to this statement.
// Check if the contract has been scanned for more than two hours
if (now - contract.timestamp > 2 * 60 * 60 * 1000) {
// If it has, remove it from the array
foundContracts.foundcontracts.splice(i, 1);
i--; // Decrement the index to account for the removed element
continue; // Skip the rest of this iteration
}
But if the bot finds social media and sends me a message, or when the contract is found but there is no media, the contracts aren't removed from the JSON and I don't understand why. I have tried many approaches but nothing works, I was not able to fix this problem, still the bot repeats the same contract socials, will just stop after 2 hours.
JSON:
// empty
{
"foundcontracts": []
}
//with addresses
{"foundcontracts":[{
"contractAddress":"0xeb428d640a6b122a881bbd4409a3a18b3d8fc560",
"name":"Greedy Snake",
"ticker":"Snake",
"timestamp":1690632939778},
{
"contractAddress":"0xe2cb34fc7d4e4484afd1dd4bd76260645eac0e7e",
"name":"EASYFEEDBACK",
"ticker":"EASYF",
"timestamp":1690633385061},
{
"contractAddress":"0x3d4a25855ec4f97f2daf08a9ae025f149338fd58",
"name":"ELONX",
"ticker":
"ELONX",
"timestamp":1690633770683},
{
"contractAddress":"0x97816f7711a70a8671a381b5617029d2ce56d509",
"name":"Xbaby",
"ticker":"XBABY",
"timestamp":1690633815812
}]}
Any ideas would be much appreciated, I tried everything including awaiting fs promises but that didn't help either. Not sure what am I missing but I think I'll feel stupid when will find out.