ERC20Helper compatible with non-standard ERC20
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user