From 18ff2242be09e13656603ce66f48905462fb3c75 Mon Sep 17 00:00:00 2001 From: tracy <25892474+traceurl@users.noreply.github.com> Date: Tue, 27 Feb 2024 11:01:15 +0800 Subject: [PATCH] ERC20Helper compatible with non-standard ERC20 --- contracts/helper/ERC20Helper.sol | 37 +++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/contracts/helper/ERC20Helper.sol b/contracts/helper/ERC20Helper.sol index 624ef65..42ba37e 100644 --- a/contracts/helper/ERC20Helper.sol +++ b/contracts/helper/ERC20Helper.sol @@ -16,6 +16,15 @@ interface IERC20ForCheck { function allowance(address owner, address spender) external view returns (uint256); } +interface IOldERC20ForCheck { + function decimals() external view returns (uint); + function name() external view returns (bytes32); + function symbol() external view returns (bytes32); + + function balanceOf(address account) external view returns (uint256); + function allowance(address owner, address spender) external view returns (uint256); +} + contract ERC20Helper { function isERC20(address token, address user, address spender) external view returns(bool isOk, string memory symbol, string memory name, uint decimals, uint256 balance, uint256 allownance) { @@ -27,16 +36,38 @@ contract ERC20Helper { allownance = _allownance; isOk = true; } catch { - isOk = false; + try this.judgeOldERC20(token, user, spender) returns (bytes32 _symbol, bytes32 _name, uint _decimals, uint256 _balance, uint256 _allownance) { + symbol = bytes32ToString(_symbol); + name = bytes32ToString(_name); + decimals = _decimals; + balance = _balance; + allownance = _allownance; + isOk = true; + } catch { + isOk = false; + } } } - function judgeERC20(address token, address user, address spender) external view returns(string memory symbol, string memory name, uint decimals, uint256 balance, uint256 allownance) { + function judgeERC20(address token, address user, address spender) external view returns(string memory symbol, string memory name, uint decimals, uint256 balance, uint256 allownance) { name = IERC20ForCheck(token).name(); symbol = IERC20ForCheck(token).symbol(); decimals = IERC20ForCheck(token).decimals(); balance = IERC20ForCheck(token).balanceOf(user); allownance = IERC20ForCheck(token).allowance(user,spender); - } + } + + function judgeOldERC20(address token, address user, address spender) external view returns(bytes32 symbol, bytes32 name, uint decimals, uint256 balance, uint256 allownance) { + name = IOldERC20ForCheck(token).name(); + symbol = IOldERC20ForCheck(token).symbol(); + decimals = IOldERC20ForCheck(token).decimals(); + + balance = IOldERC20ForCheck(token).balanceOf(user); + allownance = IOldERC20ForCheck(token).allowance(user,spender); + } + + function bytes32ToString(bytes32 _bytes) public pure returns (string memory _string) { + _string = string(abi.encodePacked(_bytes)); + } } \ No newline at end of file