zeta-chain / protocol-contracts

Protocol contracts implementing the core logic of the protocol, deployed on ZetaChain and on connected chains
MIT License
70 stars 58 forks source link

Check if erc20 and zrc20 are token contracts in v2 contracts #239

Open skosito opened 4 months ago

skosito commented 4 months ago
          Here we're assuming token is an ERC20 compliant address always. To code this defensively we'd check always if it's a token contract.

Also we need to check if to != address(0).

    function isERC20(address token) internal view returns (bool) {
        return token.code.length > 0 && IERC20(token).totalSupply() > 0;
    }

    function withdrawAndRevert(address token, address to, uint256 amount, bytes calldata data) public nonReentrant {
        require(isERC20(token), "Provided address is not an ERC20 token");
        require(token != address(0), "Provided address is invalid");
        IERC20(token).safeTransfer(address(gateway), amount);

_Originally posted by @fbac in https://github.com/zeta-chain/protocol-contracts/pull/217#discussion_r1682561476_