Search code examples
javascriptnode.jsweb3jsetherscan

How to get data regarding random smart contract addresses. Such as creation date, chain, holders (how many wallets are holding)


Basically, the node.js or javascript function will pull data from ETH or BSC scan using web3 or whatever, analyze it and display by the following steps:

  1. System will take the smart contract address of the ETH or BSC chain.
  2. The system will find chains via a platform like Etherscan.io, BSC scan.
  3. After knowing the chain, the system will check how many wallets are holding the token "Holders"
  4. Get Contract creation date via ETH or BSC scan.

How can I achieve the required results?


Solution

    1. The system will find chains via a platform like Etherscan.io, BSC scan.

    Answer: Create accounts on ETH and BSC scan, get API key then in the response of following 2 requests, if you get status code = 1, it means the contract is deployed on that particular chain.

    Using Etherscan dev API https://api.etherscan.io/api?module=contract&action=getabi&address=0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413&apikey=xxx

    Similarly for BSC, using BSC scan dev API https://api.bscscan.com/api?module=contract&action=getabi&address=0x0000000000000000000000000000000000001004&apikey=xxx

    1. After knowing the chain, the system will check how many wallets are holding the token "Holders"

    Well, for this I tried to find etherscan free API. However, I think they provide API in the PRO plan. So the quick solution I found using node.js and web scraping npm library cheerio is:

         const options1 = {
                    method: 'GET',
                    url: `https://etherscan.io/token/${req.query.tokenAddress}`,
                    headers: { 'content-type': 'application/json' },
                    json: true
                };
                request(options1, (error, response, html) => {
                    if (!error && response.statusCode == 200) {
                        const $ = cheerio.load(html);
    
                        const holders = $('div[id = "ContentPlaceHolder1_tr_tokenHolders"] > div > div').text().trim();
    
                        console.log(holders);
                        res.send({ chain, holders });
                    } else if (error) throw new Error(error);
                });
    
    1. Get Contract creation date via ETH or BSC scan.

    The very first transaction of any contract is for the deployment of the smart contract. So, we can get the contract creation date and time by getting the execution timestamp of the first transaction for that particular smart contract.

    For EthScan, we can get transactions of any contract via their API call https://api.etherscan.io/api?module=account&action=txlist&address=0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae&startblock=0&endblock=99999999&page=1&offset=10&sort=asc&apikey=xxx

    and for BSC it's https://api.bscscan.com/api?module=account&action=txlist&address=0x0000000000000000000000000000000000001004&startblock=1&endblock=99999999&page=1&offset=10&sort=asc&apikey=xxx

    For more information Please consult BSC scan docs and ETH scan docs