def test_two_instantiations_with_web3_objects(web3_instance): """Test that instantiating two Exchange objects doesn't raise. When instantiating an Exchange object with a web3 client (rather than a provider) there was a bug encountered where web3.py was giving an error when trying to install the rich-revert-handling middleware on the web3 client, an error saying "can't install this same middleware instance again." Test that that bug isn't occurring. """ exchange = Exchange( # pylint: disable=unused-variable web3_instance, chain_to_addresses(ChainId.GANACHE).exchange) exchange2 = Exchange( # pylint: disable=unused-variable web3_instance, chain_to_addresses(ChainId.GANACHE).exchange)
def test_local_message_signer__sign_order(): """Test signing order with the local_message_signer middleware""" expected_signature = ( "0x1c8bdfbb3ce3ed0f38c5a358a7f49ad5f21ea9857224c2fe98c458f2fa25551d4" "d6db0157d9dfe9f9fadb8dedabb7786352843357f4ec8d0fbcbeeb619b1091f5803") address = "0x5409ED021D9299bf6814279A6A1411A7e866A631" exchange = chain_to_addresses(ChainId.GANACHE).exchange private_key = ( "f2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e0164837257d") ganache = HTTPProvider("http://127.0.0.1:8545") web3_instance = Web3(ganache) web3_instance.middleware_onion.add( construct_local_message_signer(private_key)) order = { "makerAddress": "0x0000000000000000000000000000000000000000", "takerAddress": "0x0000000000000000000000000000000000000000", "senderAddress": "0x0000000000000000000000000000000000000000", "feeRecipientAddress": "0x0000000000000000000000000000000000000000", "makerAssetData": (b"\x00") * 20, "makerFeeAssetData": (b"\x00") * 20, "takerAssetData": (b"\x00") * 20, "takerFeeAssetData": (b"\x00") * 20, "salt": 0, "makerFee": 0, "takerFee": 0, "makerAssetAmount": 0, "takerAssetAmount": 0, "expirationTimeSeconds": 0, } assert (sign_hash( web3_instance, to_checksum_address(address), generate_order_hash_hex(order, exchange, chain_id=1337), ) == expected_signature)
def weth_instance(web3_eth): # pylint: disable=redefined-outer-name """Get an instance of the WrapperEther contract.""" return web3_eth.contract( address=to_checksum_address( chain_to_addresses(ChainId.GANACHE).ether_token), abi=abi_by_name("WETH9"), )
def is_valid_signature(provider: BaseProvider, data: str, signature: str, signer_address: str) -> bool: """Check the validity of the supplied signature. Check if the supplied `signature`:code: corresponds to signing `data`:code: with the private key corresponding to `signer_address`:code:. :param provider: A Web3 provider able to access the 0x Exchange contract. :param data: The hex encoded data signed by the supplied signature. :param signature: The hex encoded signature. :param signer_address: The hex encoded address that signed the data to produce the supplied signature. :returns: Tuple consisting of a boolean and a string. Boolean is true if valid, false otherwise. If false, the string describes the reason. >>> is_valid_signature( ... Web3.HTTPProvider("http://127.0.0.1:8545"), ... '0x6927e990021d23b1eb7b8789f6a6feaf98fe104bb0cf8259421b79f9a34222b0', ... '0x1B61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc3340349190569279751135161d22529dc25add4f6069af05be04cacbda2ace225403', ... '0x5409ed021d9299bf6814279a6a1411a7e866a631', ... ) True """ # noqa: E501 (line too long) assert_is_provider(provider, "provider") assert_is_hex_string(data, "data") assert_is_hex_string(signature, "signature") assert_is_address(signer_address, "signer_address") return Exchange( provider, chain_to_addresses(ChainId(1 # hard-coded to always be mainnet )).exchange, ).is_valid_hash_signature.call( bytes.fromhex(remove_0x_prefix(HexStr(data))), to_checksum_address(signer_address), bytes.fromhex(remove_0x_prefix(HexStr(signature))), )
def zrx_address(): """Get address of ZRX token for Ganache chain.""" return chain_to_addresses(ChainId.GANACHE).zrx_token
def weth_asset_data(): # pylint: disable=redefined-outer-name """Get 0x asset data for Wrapped Ether (WETH) token.""" return asset_data_utils.encode_erc20( chain_to_addresses(ChainId.GANACHE).ether_token)
def erc20_proxy_address(): """Get the 0x ERC20 Proxy address.""" return chain_to_addresses(ChainId.GANACHE).erc20_proxy
def erc20_wrapper(ganache_provider): """Get an instance of ERC20Token wrapper class for testing.""" return ERC20Token(ganache_provider, chain_to_addresses(ChainId.GANACHE).ether_token)
def exchange_wrapper(ganache_provider): """Get an Exchange wrapper instance.""" return Exchange( web3_or_provider=ganache_provider, contract_address=chain_to_addresses(ChainId.GANACHE).exchange, )
def contract_wrapper(ganache_provider): """Get a ContractMethod instance for testing.""" return ContractMethod( web3_or_provider=ganache_provider, contract_address=chain_to_addresses(ChainId.GANACHE).ether_token, )