Skip to content

Commit

Permalink
move default_account and default_block properties and setters to Base…
Browse files Browse the repository at this point in the history
…Eth so Eth and AsyncEth can access
  • Loading branch information
pacrob committed Jan 20, 2022
1 parent c7c9b1a commit 2bf8e58
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 42 deletions.
1 change: 1 addition & 0 deletions newsfragments/2315.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add Async access to `default_account` and `default_block`
75 changes: 75 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
HexBytes,
)

from web3._utils.empty import (
empty,
)
from web3._utils.ens import (
ens_addresses,
)
Expand Down Expand Up @@ -845,6 +848,43 @@ async def test_async_eth_accounts(self, async_w3: "Web3") -> None:
))
assert await async_w3.eth.coinbase in accounts # type: ignore

@pytest.mark.asyncio
async def test_async_provider_default_account(
self,
async_w3: "Web3",
unlocked_account_dual_type: ChecksumAddress
) -> None:

# check defaults to empty
default_account = async_w3.eth.default_account
assert default_account is empty

# check setter
async_w3.eth.default_account = unlocked_account_dual_type
default_account = async_w3.eth.default_account
assert default_account == unlocked_account_dual_type

# reset to default
async_w3.eth.default_account = empty

@pytest.mark.asyncio
async def test_async_provider_default_block(
self,
async_w3: "Web3",
) -> None:

# check defaults to 'latest'
default_block = async_w3.eth.default_block
assert default_block == 'latest'

# check setter
async_w3.eth.default_block = BlockNumber(12345)
default_block = async_w3.eth.default_block
assert default_block == BlockNumber(12345)

# reset to default
async_w3.eth.default_block = 'latest'


class EthModuleTest:
def test_eth_protocol_version(self, web3: "Web3") -> None:
Expand Down Expand Up @@ -2919,3 +2959,38 @@ def test_eth_get_raw_transaction_by_block_raises_error_block_identifier(
)
):
web3.eth.get_raw_transaction_by_block(unknown_identifier, 0) # type: ignore

def test_default_account(
self,
web3: "Web3",
unlocked_account_dual_type: ChecksumAddress
) -> None:

# check defaults to empty
default_account = web3.eth.default_account
assert default_account is empty

# check setter
web3.eth.default_account = unlocked_account_dual_type
default_account = web3.eth.default_account
assert default_account == unlocked_account_dual_type

# reset to default
web3.eth.default_account = empty

async def test_default_block(
self,
web3: "Web3",
) -> None:

# check defaults to 'latest'
default_block = web3.eth.default_block
assert default_block == 'latest'

# check setter
web3.eth.default_block = BlockNumber(12345)
default_block = web3.eth.default_block
assert default_block == BlockNumber(12345)

# reset to default
web3.eth.default_block = 'latest'
78 changes: 36 additions & 42 deletions web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class BaseEth(Module):
mungers=None,
)

""" property default_block """
@property
def default_block(self) -> BlockIdentifier:
return self._default_block
Expand All @@ -126,10 +125,46 @@ def default_block(self) -> BlockIdentifier:
def default_block(self, value: BlockIdentifier) -> None:
self._default_block = value

@property
def defaultBlock(self) -> BlockIdentifier:
warnings.warn(
'defaultBlock is deprecated in favor of default_block',
category=DeprecationWarning,
)
return self._default_block

@defaultBlock.setter
def defaultBlock(self, value: BlockIdentifier) -> None:
warnings.warn(
'defaultBlock is deprecated in favor of default_block',
category=DeprecationWarning,
)
self._default_block = value

@property
def default_account(self) -> Union[ChecksumAddress, Empty]:
return self._default_account

@default_account.setter
def default_account(self, account: Union[ChecksumAddress, Empty]) -> None:
self._default_account = account

@property
def defaultAccount(self) -> Union[ChecksumAddress, Empty]:
warnings.warn(
'defaultAccount is deprecated in favor of default_account',
category=DeprecationWarning,
)
return self._default_account

@defaultAccount.setter
def defaultAccount(self, account: Union[ChecksumAddress, Empty]) -> None:
warnings.warn(
'defaultAccount is deprecated in favor of default_account',
category=DeprecationWarning,
)
self._default_account = account

def send_transaction_munger(self, transaction: TxParams) -> Tuple[TxParams]:
if 'from' not in transaction and is_checksum_address(self.default_account):
transaction = assoc(transaction, 'from', self.default_account)
Expand Down Expand Up @@ -551,52 +586,11 @@ def chainId(self) -> int:
)
return self.chain_id

""" property default_account """
@property
def default_account(self) -> Union[ChecksumAddress, Empty]:
return self._default_account

@default_account.setter
def default_account(self, account: Union[ChecksumAddress, Empty]) -> None:
self._default_account = account

@property
def defaultAccount(self) -> Union[ChecksumAddress, Empty]:
warnings.warn(
'defaultAccount is deprecated in favor of default_account',
category=DeprecationWarning,
)
return self._default_account

@defaultAccount.setter
def defaultAccount(self, account: Union[ChecksumAddress, Empty]) -> None:
warnings.warn(
'defaultAccount is deprecated in favor of default_account',
category=DeprecationWarning,
)
self._default_account = account

get_balance: Method[Callable[..., Wei]] = Method(
RPC.eth_getBalance,
mungers=[BaseEth.block_id_munger],
)

@property
def defaultBlock(self) -> BlockIdentifier:
warnings.warn(
'defaultBlock is deprecated in favor of default_block',
category=DeprecationWarning,
)
return self._default_block

@defaultBlock.setter
def defaultBlock(self, value: BlockIdentifier) -> None:
warnings.warn(
'defaultBlock is deprecated in favor of default_block',
category=DeprecationWarning,
)
self._default_block = value

@property
def max_priority_fee(self) -> Wei:
return self._max_priority_fee()
Expand Down

0 comments on commit 2bf8e58

Please sign in to comment.