def test_method_id_type_error(): """Test that passing in wrong types raises TypeError.""" with pytest.raises(TypeError): method_id(123, []) with pytest.raises(TypeError): method_id("ERC20Token", 123)
def decode_erc721_asset_data(asset_data: str) -> ERC721AssetData: """Decode an ERC721 asset data hex string. >>> decode_erc721_asset_data('0x025717920000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c480000000000000000000000000000000000000000000000000000000000000001') ERC721AssetData(asset_proxy_id='0x02571792', token_address='0x1dc4c1cefef38a777b15aa20260a54e584b16c48', token_id=1) """ # noqa: E501 (line too long) assert_is_string(asset_data, "asset_data") if len(asset_data) < ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH: raise ValueError( "Could not decode ERC721 Asset Data. Expected length of encoded" + f"data to be at least {ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH}. " + f"Got {len(asset_data)}.") asset_proxy_id: str = asset_data[0:SELECTOR_LENGTH] if asset_proxy_id != abi_utils.method_id("ERC721Token", ["address", "uint256"]): raise ValueError( "Could not decode ERC721 Asset Data. Expected Asset Proxy Id to be" + f" ERC721 (" + f"{abi_utils.method_id('ERC721Token', ['address', 'uint256'])}" + f"), but got {asset_proxy_id}") (token_address, token_id) = eth_abi.decode_abi( ["address", "uint256"], bytes.fromhex(asset_data[SELECTOR_LENGTH:])) return ERC721AssetData( asset_proxy_id=asset_proxy_id, token_address=token_address, token_id=token_id, )
def decode_erc20_asset_data(asset_data: str) -> ERC20AssetData: """Decode an ERC20 asset data hex string. :param asset_data: String produced by prior call to encode_erc20_asset_data() >>> decode_erc20_asset_data("0xf47261b00000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48") ERC20AssetData(asset_proxy_id='0xf47261b0', token_address='0x1dc4c1cefef38a777b15aa20260a54e584b16c48') """ # noqa: E501 (line too long) assert_is_string(asset_data, "asset_data") if len(asset_data) < ERC20_ASSET_DATA_BYTE_LENGTH: raise ValueError( "Could not decode ERC20 Proxy Data. Expected length of encoded" + f" data to be at least {str(ERC20_ASSET_DATA_BYTE_LENGTH)}." + f" Got {str(len(asset_data))}.") asset_proxy_id: str = asset_data[0:SELECTOR_LENGTH] if asset_proxy_id != abi_utils.method_id("ERC20Token", ["address"]): raise ValueError( "Could not decode ERC20 Proxy Data. Expected Asset Proxy Id to be" + f" ERC20 ({abi_utils.method_id('ERC20Token', ['address'])})" + f" but got {asset_proxy_id}.") # workaround for https://github.com/PyCQA/pylint/issues/1498 # pylint: disable=unsubscriptable-object token_address = eth_abi.decode_abi(["address"], bytes.fromhex( asset_data[SELECTOR_LENGTH:]))[0] return ERC20AssetData(asset_proxy_id=asset_proxy_id, token_address=token_address)
def decode_erc721_asset_data(asset_data: str) -> ERC721AssetData: # docstring considered all one line by pylint: disable=line-too-long """Decode an ERC721 asset data hex string. >>> decode_erc721_asset_data('0x025717920000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c480000000000000000000000000000000000000000000000000000000000000001') {'asset_proxy_id': '0x02571792', 'token_address': '0x1dc4c1cefef38a777b15aa20260a54e584b16c48', 'token_id': 1} """ # noqa: E501 (line too long) assert_is_string(asset_data, "asset_data") if len(asset_data) < ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH: raise ValueError( "Could not decode ERC721 Asset Data. Expected length of encoded" + f"data to be at least {ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH}. " + f"Got {len(asset_data)}." ) asset_proxy_id: str = asset_data[0:SELECTOR_LENGTH] # prefer `black` formatting. pylint: disable=C0330 if asset_proxy_id != abi_utils.method_id( "ERC721Token", ["address", "uint256"] ): raise ValueError( "Could not decode ERC721 Asset Data. Expected Asset Proxy Id to be" + f" ERC721 (" + f"{abi_utils.method_id('ERC721Token', ['address', 'uint256'])}" + f"), but got {asset_proxy_id}" ) (token_address, token_id) = eth_abi.decode_abi( ["address", "uint256"], bytes.fromhex(asset_data[SELECTOR_LENGTH:]) ) return { "asset_proxy_id": asset_proxy_id, "token_address": token_address, "token_id": token_id, }