Skip to content

Commit 405ff76

Browse files
dbfreempacrob
authored and
pacrob
committed
Feature/asyncify contract (ethereum#2404)
* setting default contract factory in AsyncEth to AsyncContract
1 parent 1814e11 commit 405ff76

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

ens/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def resolver(self, normal_name: str) -> Optional['Contract']:
263263
if is_none_or_zero_address(resolver_addr):
264264
return None
265265
# TODO: look at possibly removing type ignore when AsyncENS is written
266-
return self._resolverContract(address=resolver_addr) # type: ignore
266+
return self._resolverContract(address=resolver_addr)
267267

268268
def reverser(self, target_address: ChecksumAddress) -> Optional['Contract']:
269269
reversed_domain = address_to_reverse_domain(target_address)
@@ -394,7 +394,7 @@ def _set_resolver(
394394
resolver_addr
395395
).transact(transact)
396396
# TODO: look at possibly removing type ignore when AsyncENS is written
397-
return self._resolverContract(address=resolver_addr) # type: ignore
397+
return self._resolverContract(address=resolver_addr)
398398

399399
def _setup_reverse(
400400
self, name: str, address: ChecksumAddress, transact: Optional["TxParams"] = None

ethpm/package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def get_contract_instance(self, name: ContractName, address: Address) -> Contrac
310310
address=address, **contract_kwargs
311311
)
312312
# TODO: type ignore may be able to be removed after more of AsynContract is finished
313-
return contract_instance # type: ignore
313+
return contract_instance
314314

315315
#
316316
# Build Dependencies

web3/eth.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ class BaseEth(Module):
118118
_default_block: BlockIdentifier = "latest"
119119
_default_chain_id: Optional[int] = None
120120
gasPriceStrategy = None
121-
defaultContractFactory: Type[Union[Contract, AsyncContract,
122-
ConciseContract, ContractCaller, AsyncContractCaller]] = Contract
123121

124122
_gas_price: Method[Callable[[], Wei]] = Method(
125123
RPC.eth_gasPrice,
@@ -374,6 +372,7 @@ def set_contract_factory(
374372

375373
class AsyncEth(BaseEth):
376374
is_async = True
375+
defaultContractFactory: Type[Union[AsyncContract, AsyncContractCaller]] = AsyncContract
377376

378377
@property
379378
async def accounts(self) -> Tuple[ChecksumAddress]:
@@ -586,10 +585,29 @@ async def call(
586585
) -> Union[bytes, bytearray]:
587586
return await self._call(transaction, block_identifier, state_override)
588587

588+
@overload
589+
def contract(self, address: None = None, **kwargs: Any) -> Type[AsyncContract]: ... # noqa: E704,E501
590+
591+
@overload # noqa: F811
592+
def contract(self, address: Union[Address, ChecksumAddress, ENS], **kwargs: Any) -> AsyncContract: ... # noqa: E704,E501
593+
594+
def contract( # noqa: F811
595+
self, address: Optional[Union[Address, ChecksumAddress, ENS]] = None, **kwargs: Any
596+
) -> Union[Type[AsyncContract], AsyncContract]:
597+
ContractFactoryClass = kwargs.pop('ContractFactoryClass', self.defaultContractFactory)
598+
599+
ContractFactory = ContractFactoryClass.factory(self.w3, **kwargs)
600+
601+
if address:
602+
return ContractFactory(address)
603+
else:
604+
return ContractFactory
605+
589606

590607
class Eth(BaseEth):
591608
account = Account()
592609
iban = Iban
610+
defaultContractFactory: Type[Union[Contract, ConciseContract, ContractCaller]] = Contract
593611

594612
def namereg(self) -> NoReturn:
595613
raise NotImplementedError()
@@ -997,6 +1015,24 @@ def getCompilers(self) -> NoReturn:
9971015
is_property=True,
9981016
)
9991017

1018+
@overload
1019+
def contract(self, address: None = None, **kwargs: Any) -> Type[Contract]: ... # noqa: E704,E501
1020+
1021+
@overload # noqa: F811
1022+
def contract(self, address: Union[Address, ChecksumAddress, ENS], **kwargs: Any) -> Contract: ... # noqa: E704,E501
1023+
1024+
def contract( # noqa: F811
1025+
self, address: Optional[Union[Address, ChecksumAddress, ENS]] = None, **kwargs: Any
1026+
) -> Union[Type[Contract], Contract]:
1027+
ContractFactoryClass = kwargs.pop('ContractFactoryClass', self.defaultContractFactory)
1028+
1029+
ContractFactory = ContractFactoryClass.factory(self.w3, **kwargs)
1030+
1031+
if address:
1032+
return ContractFactory(address)
1033+
else:
1034+
return ContractFactory
1035+
10001036
@deprecated_for("generate_gas_price")
10011037
def generateGasPrice(self, transaction_params: Optional[TxParams] = None) -> Optional[Wei]:
10021038
return self._generate_gas_price(transaction_params)

0 commit comments

Comments
 (0)