ERC20 Token Supply – Getting Total Supply of Any ERC20 Token in Solidity

erc-20marketssolidity

I'm a new developper on Solidity and maybe this is a trivial question but it is a crucial point for my project.

Is there any way to get the total supply of any ERC20 token (WBTC, and others) on Solidity ? Should I use an oracle ? Or do you have any other idea that I would not have though of ?

Thank you very much !

Best Answer

You just need to know the address of the ERC20 token and execute function totalSupply().

If you want to have that in a simple contract, you could have something like this:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";

contract Test {
    IERC20 public token;

    function getTotalSupply(address addr) public view returns(uint256) {
        return IERC20(addr).totalSupply();
    }
}

You certainly need to know the address of the ERC20 tokens you may want to check, so you have to know that beforehand and if necessary, store it in your contract within a list.

Related Topic