Skip to content

Commit ffe59da

Browse files
pacrobpacrob
pacrob
authored andcommitted
move default_account and default_block properties and setters to BaseEth so Eth and AsyncEth can access
1 parent c7c9b1a commit ffe59da

File tree

5 files changed

+114
-44
lines changed

5 files changed

+114
-44
lines changed

docs/providers.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ Eth
408408
- :meth:`web3.eth.block_number <web3.eth.Eth.block_number>`
409409
- :meth:`web3.eth.chain_id <web3.eth.Eth.chain_id>`
410410
- :meth:`web3.eth.coinbase <web3.eth.Eth.coinbase>`
411+
- :meth:`web3.eth.default_account <web3.eth.Eth.default_account>`
412+
- :meth:`web3.eth.default_block <web3.eth.Eth.default_block>`
411413
- :meth:`web3.eth.gas_price <web3.eth.Eth.gas_price>`
412414
- :meth:`web3.eth.hashrate <web3.eth.Eth.hashrate>`
413415
- :meth:`web3.eth.max_priority_fee <web3.eth.Eth.max_priority_fee>`

docs/web3.eth.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ The following properties are available on the ``web3.eth`` namespace.
4949
.. py:attribute:: Eth.default_account
5050
5151
The ethereum address that will be used as the default ``from`` address for
52-
all transactions.
52+
all transactions. Defaults to empty.
5353

5454

5555
.. py:attribute:: Eth.defaultAccount
@@ -61,7 +61,7 @@ The following properties are available on the ``web3.eth`` namespace.
6161
.. py:attribute:: Eth.default_block
6262
6363
The default block number that will be used for any RPC methods that accept
64-
a block identifier. Defaults to ``'latest'``.
64+
a block identifier. Defaults to ``'latest'``.
6565

6666

6767
.. py:attribute:: Eth.defaultBlock

newsfragments/2315.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add Async access to `default_account` and `default_block`

web3/_utils/module_testing/eth_module.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
HexBytes,
3636
)
3737

38+
from web3._utils.empty import (
39+
empty,
40+
)
3841
from web3._utils.ens import (
3942
ens_addresses,
4043
)
@@ -845,6 +848,41 @@ async def test_async_eth_accounts(self, async_w3: "Web3") -> None:
845848
))
846849
assert await async_w3.eth.coinbase in accounts # type: ignore
847850

851+
def test_async_provider_default_account(
852+
self,
853+
async_w3: "Web3",
854+
unlocked_account_dual_type: ChecksumAddress
855+
) -> None:
856+
857+
# check defaults to empty
858+
default_account = async_w3.eth.default_account
859+
assert default_account is empty
860+
861+
# check setter
862+
async_w3.eth.default_account = unlocked_account_dual_type
863+
default_account = async_w3.eth.default_account
864+
assert default_account == unlocked_account_dual_type
865+
866+
# reset to default
867+
async_w3.eth.default_account = empty
868+
869+
def test_async_provider_default_block(
870+
self,
871+
async_w3: "Web3",
872+
) -> None:
873+
874+
# check defaults to 'latest'
875+
default_block = async_w3.eth.default_block
876+
assert default_block == 'latest'
877+
878+
# check setter
879+
async_w3.eth.default_block = BlockNumber(12345)
880+
default_block = async_w3.eth.default_block
881+
assert default_block == BlockNumber(12345)
882+
883+
# reset to default
884+
async_w3.eth.default_block = 'latest'
885+
848886

849887
class EthModuleTest:
850888
def test_eth_protocol_version(self, web3: "Web3") -> None:
@@ -2919,3 +2957,38 @@ def test_eth_get_raw_transaction_by_block_raises_error_block_identifier(
29192957
)
29202958
):
29212959
web3.eth.get_raw_transaction_by_block(unknown_identifier, 0) # type: ignore
2960+
2961+
def test_default_account(
2962+
self,
2963+
web3: "Web3",
2964+
unlocked_account_dual_type: ChecksumAddress
2965+
) -> None:
2966+
2967+
# check defaults to empty
2968+
default_account = web3.eth.default_account
2969+
assert default_account is empty
2970+
2971+
# check setter
2972+
web3.eth.default_account = unlocked_account_dual_type
2973+
default_account = web3.eth.default_account
2974+
assert default_account == unlocked_account_dual_type
2975+
2976+
# reset to default
2977+
web3.eth.default_account = empty
2978+
2979+
def test_default_block(
2980+
self,
2981+
web3: "Web3",
2982+
) -> None:
2983+
2984+
# check defaults to 'latest'
2985+
default_block = web3.eth.default_block
2986+
assert default_block == 'latest'
2987+
2988+
# check setter
2989+
web3.eth.default_block = BlockNumber(12345)
2990+
default_block = web3.eth.default_block
2991+
assert default_block == BlockNumber(12345)
2992+
2993+
# reset to default
2994+
web3.eth.default_block = 'latest'

web3/eth.py

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ class BaseEth(Module):
117117
mungers=None,
118118
)
119119

120-
""" property default_block """
121120
@property
122121
def default_block(self) -> BlockIdentifier:
123122
return self._default_block
@@ -126,10 +125,46 @@ def default_block(self) -> BlockIdentifier:
126125
def default_block(self, value: BlockIdentifier) -> None:
127126
self._default_block = value
128127

128+
@property
129+
def defaultBlock(self) -> BlockIdentifier:
130+
warnings.warn(
131+
'defaultBlock is deprecated in favor of default_block',
132+
category=DeprecationWarning,
133+
)
134+
return self._default_block
135+
136+
@defaultBlock.setter
137+
def defaultBlock(self, value: BlockIdentifier) -> None:
138+
warnings.warn(
139+
'defaultBlock is deprecated in favor of default_block',
140+
category=DeprecationWarning,
141+
)
142+
self._default_block = value
143+
129144
@property
130145
def default_account(self) -> Union[ChecksumAddress, Empty]:
131146
return self._default_account
132147

148+
@default_account.setter
149+
def default_account(self, account: Union[ChecksumAddress, Empty]) -> None:
150+
self._default_account = account
151+
152+
@property
153+
def defaultAccount(self) -> Union[ChecksumAddress, Empty]:
154+
warnings.warn(
155+
'defaultAccount is deprecated in favor of default_account',
156+
category=DeprecationWarning,
157+
)
158+
return self._default_account
159+
160+
@defaultAccount.setter
161+
def defaultAccount(self, account: Union[ChecksumAddress, Empty]) -> None:
162+
warnings.warn(
163+
'defaultAccount is deprecated in favor of default_account',
164+
category=DeprecationWarning,
165+
)
166+
self._default_account = account
167+
133168
def send_transaction_munger(self, transaction: TxParams) -> Tuple[TxParams]:
134169
if 'from' not in transaction and is_checksum_address(self.default_account):
135170
transaction = assoc(transaction, 'from', self.default_account)
@@ -551,52 +586,11 @@ def chainId(self) -> int:
551586
)
552587
return self.chain_id
553588

554-
""" property default_account """
555-
@property
556-
def default_account(self) -> Union[ChecksumAddress, Empty]:
557-
return self._default_account
558-
559-
@default_account.setter
560-
def default_account(self, account: Union[ChecksumAddress, Empty]) -> None:
561-
self._default_account = account
562-
563-
@property
564-
def defaultAccount(self) -> Union[ChecksumAddress, Empty]:
565-
warnings.warn(
566-
'defaultAccount is deprecated in favor of default_account',
567-
category=DeprecationWarning,
568-
)
569-
return self._default_account
570-
571-
@defaultAccount.setter
572-
def defaultAccount(self, account: Union[ChecksumAddress, Empty]) -> None:
573-
warnings.warn(
574-
'defaultAccount is deprecated in favor of default_account',
575-
category=DeprecationWarning,
576-
)
577-
self._default_account = account
578-
579589
get_balance: Method[Callable[..., Wei]] = Method(
580590
RPC.eth_getBalance,
581591
mungers=[BaseEth.block_id_munger],
582592
)
583593

584-
@property
585-
def defaultBlock(self) -> BlockIdentifier:
586-
warnings.warn(
587-
'defaultBlock is deprecated in favor of default_block',
588-
category=DeprecationWarning,
589-
)
590-
return self._default_block
591-
592-
@defaultBlock.setter
593-
def defaultBlock(self, value: BlockIdentifier) -> None:
594-
warnings.warn(
595-
'defaultBlock is deprecated in favor of default_block',
596-
category=DeprecationWarning,
597-
)
598-
self._default_block = value
599-
600594
@property
601595
def max_priority_fee(self) -> Wei:
602596
return self._max_priority_fee()

0 commit comments

Comments
 (0)