From 56cb8f115b30b86d44c0c489dd5e83f40a4fc9c1 Mon Sep 17 00:00:00 2001 From: liquid-8 Date: Wed, 27 Oct 2021 22:54:55 +0300 Subject: [PATCH 1/2] fix/enhancement: added bytes32 name/symbol support in get_token() --- uniswap/assets/erc20b32.abi | 263 ++++++++++++++++++++++++++++++++++++ uniswap/uniswap.py | 14 +- 2 files changed, 273 insertions(+), 4 deletions(-) create mode 100644 uniswap/assets/erc20b32.abi diff --git a/uniswap/assets/erc20b32.abi b/uniswap/assets/erc20b32.abi new file mode 100644 index 0000000..267eeb0 --- /dev/null +++ b/uniswap/assets/erc20b32.abi @@ -0,0 +1,263 @@ +[ + { + "constant": true, + "inputs": [], + "name": "name", + "outputs": [ + { + "name": "", + "type": "bytes32" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "symbol", + "outputs": [ + { + "name": "", + "type": "bytes32" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "decimals", + "outputs": [ + { + "name": "", + "type": "uint8" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "spender", + "type": "address" + }, + { + "name": "value", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "totalSupply", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "from", + "type": "address" + }, + { + "name": "to", + "type": "address" + }, + { + "name": "value", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "spender", + "type": "address" + }, + { + "name": "addedValue", + "type": "uint256" + } + ], + "name": "increaseAllowance", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "owner", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "spender", + "type": "address" + }, + { + "name": "subtractedValue", + "type": "uint256" + } + ], + "name": "decreaseAllowance", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "to", + "type": "address" + }, + { + "name": "value", + "type": "uint256" + } + ], + "name": "transfer", + "outputs": [ + { + "name": "", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "constant": true, + "inputs": [ + { + "name": "owner", + "type": "address" + }, + { + "name": "spender", + "type": "address" + } + ], + "name": "allowance", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "from", + "type": "address" + }, + { + "indexed": true, + "name": "to", + "type": "address" + }, + { + "indexed": false, + "name": "value", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "name": "spender", + "type": "address" + }, + { + "indexed": false, + "name": "value", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + } +] diff --git a/uniswap/uniswap.py b/uniswap/uniswap.py index 541b943..275b2fd 100644 --- a/uniswap/uniswap.py +++ b/uniswap/uniswap.py @@ -1171,22 +1171,28 @@ def _calculate_max_output_token( # ------ Helpers ------------------------------------------------------------ - def get_token(self, address: AddressLike) -> ERC20Token: + def get_token(self, address: AddressLike, abi_name:str="erc20") -> ERC20Token: """ Retrieves metadata from the ERC20 contract of a given token, like its name, symbol, and decimals. """ # FIXME: This function should always return the same output for the same input # and would therefore benefit from caching - token_contract = _load_contract(self.w3, abi_name="erc20", address=address) + token_contract = _load_contract(self.w3, abi_name, address=address) try: - name = token_contract.functions.name().call() - symbol = token_contract.functions.symbol().call() + _name = token_contract.functions.name().call() + _symbol = token_contract.functions.symbol().call() decimals = token_contract.functions.decimals().call() except Exception as e: logger.warning( f"Exception occurred while trying to get token {_addr_to_str(address)}: {e}" ) raise InvalidToken(address) + if abi_name == "erc20b32": + name = _name.decode() + symbol = _symbol.decode() + else: + name = _name + symbol = _symbol return ERC20Token(symbol, address, name, decimals) @functools.lru_cache() From 3db89ed552baff50d1511e84193b2740cee3eb77 Mon Sep 17 00:00:00 2001 From: liquid-8 Date: Wed, 27 Oct 2021 23:14:50 +0300 Subject: [PATCH 2/2] fix/enhancement: added bytes32 name/symbol support in get_token() --- uniswap/uniswap.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/uniswap/uniswap.py b/uniswap/uniswap.py index 275b2fd..535ec09 100644 --- a/uniswap/uniswap.py +++ b/uniswap/uniswap.py @@ -1187,11 +1187,13 @@ def get_token(self, address: AddressLike, abi_name:str="erc20") -> ERC20Token: f"Exception occurred while trying to get token {_addr_to_str(address)}: {e}" ) raise InvalidToken(address) - if abi_name == "erc20b32": + try: name = _name.decode() - symbol = _symbol.decode() - else: + except: name = _name + try: + symbol = _symbol.decode() + except: symbol = _symbol return ERC20Token(symbol, address, name, decimals)