|
| 1 | +"""Generated wrapper for ERC165 Solidity contract.""" |
| 2 | + |
| 3 | +# pylint: disable=too-many-arguments |
| 4 | + |
| 5 | +import json |
| 6 | +from typing import ( # pylint: disable=unused-import |
| 7 | + Any, |
| 8 | + List, |
| 9 | + Optional, |
| 10 | + Tuple, |
| 11 | + Union, |
| 12 | +) |
| 13 | + |
| 14 | +from eth_utils import to_checksum_address |
| 15 | +from mypy_extensions import TypedDict # pylint: disable=unused-import |
| 16 | +from hexbytes import HexBytes |
| 17 | +from web3 import Web3 |
| 18 | +from web3.contract import ContractFunction |
| 19 | +from web3.datastructures import AttributeDict |
| 20 | +from web3.providers.base import BaseProvider |
| 21 | + |
| 22 | +from zero_ex.contract_wrappers.bases import ContractMethod, Validator |
| 23 | +from zero_ex.contract_wrappers.tx_params import TxParams |
| 24 | + |
| 25 | + |
| 26 | +# Try to import a custom validator class definition; if there isn't one, |
| 27 | +# declare one that we can instantiate for the default argument to the |
| 28 | +# constructor for ERC165 below. |
| 29 | +try: |
| 30 | + # both mypy and pylint complain about what we're doing here, but this |
| 31 | + # works just fine, so their messages have been disabled here. |
| 32 | + from . import ( # type: ignore # pylint: disable=import-self |
| 33 | + ERC165Validator, |
| 34 | + ) |
| 35 | +except ImportError: |
| 36 | + |
| 37 | + class ERC165Validator( # type: ignore |
| 38 | + Validator |
| 39 | + ): |
| 40 | + """No-op input validator.""" |
| 41 | + |
| 42 | + |
| 43 | +try: |
| 44 | + from .middleware import MIDDLEWARE # type: ignore |
| 45 | +except ImportError: |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +class SupportsInterfaceMethod(ContractMethod): # pylint: disable=invalid-name |
| 53 | + """Various interfaces to the supportsInterface method.""" |
| 54 | + |
| 55 | + def __init__(self, web3_or_provider: Union[Web3, BaseProvider], contract_address: str, contract_function: ContractFunction, validator: Validator=None): |
| 56 | + """Persist instance data.""" |
| 57 | + super().__init__(web3_or_provider, contract_address, validator) |
| 58 | + self._underlying_method = contract_function |
| 59 | + |
| 60 | + def validate_and_normalize_inputs(self, interface_id: Union[bytes, str]): |
| 61 | + """Validate the inputs to the supportsInterface method.""" |
| 62 | + self.validator.assert_valid( |
| 63 | + method_name='supportsInterface', |
| 64 | + parameter_name='interfaceId', |
| 65 | + argument_value=interface_id, |
| 66 | + ) |
| 67 | + return (interface_id) |
| 68 | + |
| 69 | + def call(self, interface_id: Union[bytes, str], tx_params: Optional[TxParams] = None) -> bool: |
| 70 | + """Execute underlying contract method via eth_call. |
| 71 | +
|
| 72 | + :param tx_params: transaction parameters |
| 73 | + :returns: the return value of the underlying method. |
| 74 | + """ |
| 75 | + (interface_id) = self.validate_and_normalize_inputs(interface_id) |
| 76 | + tx_params = super().normalize_tx_params(tx_params) |
| 77 | + returned = self._underlying_method(interface_id).call(tx_params.as_dict()) |
| 78 | + return bool(returned) |
| 79 | + |
| 80 | + def send_transaction(self, interface_id: Union[bytes, str], tx_params: Optional[TxParams] = None) -> Union[HexBytes, bytes]: |
| 81 | + """Execute underlying contract method via eth_sendTransaction. |
| 82 | +
|
| 83 | + :param tx_params: transaction parameters |
| 84 | + """ |
| 85 | + (interface_id) = self.validate_and_normalize_inputs(interface_id) |
| 86 | + tx_params = super().normalize_tx_params(tx_params) |
| 87 | + return self._underlying_method(interface_id).transact(tx_params.as_dict()) |
| 88 | + |
| 89 | + def build_transaction(self, interface_id: Union[bytes, str], tx_params: Optional[TxParams] = None) -> dict: |
| 90 | + """Construct calldata to be used as input to the method.""" |
| 91 | + (interface_id) = self.validate_and_normalize_inputs(interface_id) |
| 92 | + tx_params = super().normalize_tx_params(tx_params) |
| 93 | + return self._underlying_method(interface_id).buildTransaction(tx_params.as_dict()) |
| 94 | + |
| 95 | + def estimate_gas(self, interface_id: Union[bytes, str], tx_params: Optional[TxParams] = None) -> int: |
| 96 | + """Estimate gas consumption of method call.""" |
| 97 | + (interface_id) = self.validate_and_normalize_inputs(interface_id) |
| 98 | + tx_params = super().normalize_tx_params(tx_params) |
| 99 | + return self._underlying_method(interface_id).estimateGas(tx_params.as_dict()) |
| 100 | + |
| 101 | +# pylint: disable=too-many-public-methods,too-many-instance-attributes |
| 102 | +class ERC165: |
| 103 | + """Wrapper class for ERC165 Solidity contract.""" |
| 104 | + supports_interface: SupportsInterfaceMethod |
| 105 | + """Constructor-initialized instance of |
| 106 | + :class:`SupportsInterfaceMethod`. |
| 107 | + """ |
| 108 | + |
| 109 | + |
| 110 | + def __init__( |
| 111 | + self, |
| 112 | + web3_or_provider: Union[Web3, BaseProvider], |
| 113 | + contract_address: str, |
| 114 | + validator: ERC165Validator = None, |
| 115 | + ): |
| 116 | + """Get an instance of wrapper for smart contract. |
| 117 | +
|
| 118 | + :param web3_or_provider: Either an instance of `web3.Web3`:code: or |
| 119 | + `web3.providers.base.BaseProvider`:code: |
| 120 | + :param contract_address: where the contract has been deployed |
| 121 | + :param validator: for validation of method inputs. |
| 122 | + """ |
| 123 | + # pylint: disable=too-many-statements |
| 124 | + |
| 125 | + self.contract_address = contract_address |
| 126 | + |
| 127 | + if not validator: |
| 128 | + validator = ERC165Validator(web3_or_provider, contract_address) |
| 129 | + |
| 130 | + web3 = None |
| 131 | + if isinstance(web3_or_provider, BaseProvider): |
| 132 | + web3 = Web3(web3_or_provider) |
| 133 | + elif isinstance(web3_or_provider, Web3): |
| 134 | + web3 = web3_or_provider |
| 135 | + else: |
| 136 | + raise TypeError( |
| 137 | + "Expected parameter 'web3_or_provider' to be an instance of either" |
| 138 | + + " Web3 or BaseProvider" |
| 139 | + ) |
| 140 | + |
| 141 | + # if any middleware was imported, inject it |
| 142 | + try: |
| 143 | + MIDDLEWARE |
| 144 | + except NameError: |
| 145 | + pass |
| 146 | + else: |
| 147 | + try: |
| 148 | + for middleware in MIDDLEWARE: |
| 149 | + web3.middleware_onion.inject( |
| 150 | + middleware['function'], layer=middleware['layer'], |
| 151 | + ) |
| 152 | + except ValueError as value_error: |
| 153 | + if value_error.args == ("You can't add the same un-named instance twice",): |
| 154 | + pass |
| 155 | + |
| 156 | + self._web3_eth = web3.eth |
| 157 | + |
| 158 | + functions = self._web3_eth.contract(address=to_checksum_address(contract_address), abi=ERC165.abi()).functions |
| 159 | + |
| 160 | + self.supports_interface = SupportsInterfaceMethod(web3_or_provider, contract_address, functions.supportsInterface, validator) |
| 161 | + |
| 162 | + |
| 163 | + @staticmethod |
| 164 | + def abi(): |
| 165 | + """Return the ABI to the underlying contract.""" |
| 166 | + return json.loads( |
| 167 | + '[{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"}]' # noqa: E501 (line-too-long) |
| 168 | + ) |
| 169 | + |
| 170 | +# pylint: disable=too-many-lines |
0 commit comments