Search code examples
blockchainethereumbrowniedecentralized-applications

how can i get all ERC20 addresses in brownie to use in frontend


I want to create app like uniswap using brownie and react how can i access all tokens addresses and abi for my project and use it in frontend. How can i implement this in best optimized way ?


Solution

  • What you want to do is to take information from a Token like uniswap

    uniswap didn't save all the existing tokens, that's an impossible thing to do

    every time you write the address of a token on uniswap, it makes requests to the smart contract, calling up existing functions thanks to the ERC-20 standard

    the functions that are called are

    totalSupply() // to get the total supply
    
    decimals() // to get the number of decimals
    
    name() // to get the name of the token (e.g. Bitcoin)
    
    symbol() // to get the symbol of the token (e.g. BTC)
    

    to obtain this data you have to make a call through web3, which will return the data you request

    // initialize web3
    const Web3 = require("web3");
    
    // save only the ABI of the standard, so you can re-use them for all the tokens
    // in this ABI you can find only the function totalSupply ()
    const ABI = [
         {
             "type": "function",
             "name": "totalSupply",
             "inputs": [],
             "outputs": [{"name": "", "type": "uint256"}],
             "stateMutability": "view",
             "payable": false,
             "constant": true // for backward-compatibility
         }
    ];
    
    // run the JS function
    async function run() {
         const web3 = new Web3(<YourNodeUrl>);
    // create the web3 contract
         const contract = new web3.eth.Contract(ABI, <TokenAddress>);
    // call the function and get the totalSupply
         const totalSupply = await contract.methods.totalSupply().call();
         console.log(totalSupply);
    }