Skip to content

Commit 790c73a

Browse files
billbsingkclowes
authored andcommitted
setter for default_account and core tests
1 parent df5a093 commit 790c73a

File tree

9 files changed

+99
-58
lines changed

9 files changed

+99
-58
lines changed

tests/core/contracts/test_contract_transact_interface.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,29 +155,29 @@ def test_transacting_with_contract_with_arguments(web3,
155155
def test_deploy_when_default_account_is_set(web3,
156156
wait_for_transaction,
157157
STRING_CONTRACT):
158-
web3.eth.defaultAccount = web3.eth.accounts[1]
159-
assert web3.eth.defaultAccount is not empty
158+
web3.eth.default_account = web3.eth.accounts[1]
159+
assert web3.eth.default_account is not empty
160160

161161
StringContract = web3.eth.contract(**STRING_CONTRACT)
162162

163163
deploy_txn = StringContract.constructor("Caqalai").transact()
164164
web3.eth.waitForTransactionReceipt(deploy_txn)
165165
txn_after = web3.eth.getTransaction(deploy_txn)
166-
assert txn_after['from'] == web3.eth.defaultAccount
166+
assert txn_after['from'] == web3.eth.default_account
167167

168168

169169
def test_transact_when_default_account_is_set(web3,
170170
wait_for_transaction,
171171
math_contract,
172172
transact):
173-
web3.eth.defaultAccount = web3.eth.accounts[1]
174-
assert web3.eth.defaultAccount is not empty
173+
web3.eth.default_account = web3.eth.accounts[1]
174+
assert web3.eth.default_account is not empty
175175

176176
txn_hash = transact(contract=math_contract,
177177
contract_function='increment')
178178
wait_for_transaction(web3, txn_hash)
179179
txn_after = web3.eth.getTransaction(txn_hash)
180-
assert txn_after['from'] == web3.eth.defaultAccount
180+
assert txn_after['from'] == web3.eth.default_account
181181

182182

183183
def test_transacting_with_contract_with_string_argument(web3, string_contract, transact, call):

tests/core/eth-module/test_defaultAccount_api.py

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import pytest
2+
3+
4+
@pytest.fixture(autouse=True)
5+
def wait_for_first_block(web3, wait_for_block):
6+
wait_for_block(web3)
7+
8+
9+
def test_uses_default_account_when_set(web3, extra_accounts,
10+
wait_for_transaction):
11+
web3.eth.default_account = extra_accounts[2]
12+
assert web3.eth.default_account == extra_accounts[2]
13+
14+
txn_hash = web3.eth.sendTransaction({
15+
"to": extra_accounts[1],
16+
"value": 1234,
17+
})
18+
19+
wait_for_transaction(web3, txn_hash)
20+
21+
txn = web3.eth.getTransaction(txn_hash)
22+
assert txn['from'] == extra_accounts[2]
23+
24+
25+
def test_uses_defaultAccount_when_set_with_warning(web3, extra_accounts,
26+
wait_for_transaction):
27+
with pytest.warns(DeprecationWarning):
28+
web3.eth.defaultAccount = extra_accounts[2]
29+
assert web3.eth.defaultAccount == extra_accounts[2]
30+
31+
txn_hash = web3.eth.sendTransaction({
32+
"to": extra_accounts[1],
33+
"value": 1234,
34+
})
35+
36+
wait_for_transaction(web3, txn_hash)
37+
38+
txn = web3.eth.getTransaction(txn_hash)
39+
assert txn['from'] == extra_accounts[2]
40+
41+
42+
def test_uses_given_from_address_when_provided(web3, extra_accounts,
43+
wait_for_transaction):
44+
web3.eth.default_account = extra_accounts[2]
45+
txn_hash = web3.eth.sendTransaction({
46+
"from": extra_accounts[5],
47+
"to": extra_accounts[1],
48+
"value": 1234,
49+
})
50+
51+
wait_for_transaction(web3, txn_hash)
52+
53+
txn = web3.eth.getTransaction(txn_hash)
54+
assert txn['from'] == extra_accounts[5]
55+
56+
57+
def test_uses_given_from_address_when_provided_with_warning(web3, extra_accounts,
58+
wait_for_transaction):
59+
with pytest.warns(DeprecationWarning):
60+
web3.eth.defaultAccount = extra_accounts[2]
61+
txn_hash = web3.eth.sendTransaction({
62+
"from": extra_accounts[5],
63+
"to": extra_accounts[1],
64+
"value": 1234,
65+
})
66+
67+
wait_for_transaction(web3, txn_hash)
68+
69+
txn = web3.eth.getTransaction(txn_hash)
70+
assert txn['from'] == extra_accounts[5]

tests/core/pm-module/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def setup_w3():
4242
pyevm_backend = PyEVMBackend(genesis_parameters=custom_genesis_params)
4343
t = EthereumTester(backend=pyevm_backend)
4444
w3 = Web3(Web3.EthereumTesterProvider(ethereum_tester=t))
45-
w3.eth.defaultAccount = w3.eth.accounts[0]
45+
w3.eth.default_account = w3.eth.accounts[0]
4646
w3.eth.defaultContractFactory = LinkableContract
4747
w3.enable_unstable_package_management_api()
4848
return w3

tests/core/pm-module/test_ens_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def ens_setup(deployer):
116116
@pytest.fixture
117117
def ens(ens_setup, mocker):
118118
mocker.patch('web3.middleware.stalecheck._isfresh', return_value=True)
119-
ens_setup.web3.eth.defaultAccount = ens_setup.web3.eth.coinbase
119+
ens_setup.web3.eth.default_account = ens_setup.web3.eth.coinbase
120120
ens_setup.web3.enable_unstable_package_management_api()
121121
return ens_setup
122122

tests/core/pm-module/test_registry_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
@pytest.fixture
2525
def fresh_w3():
2626
w3 = Web3(Web3.EthereumTesterProvider())
27-
w3.eth.defaultAccount = w3.eth.accounts[0]
27+
w3.eth.default_account = w3.eth.accounts[0]
2828
w3.eth.defaultContractFactory = LinkableContract
2929
w3.enable_unstable_package_management_api()
3030
return w3

tests/ens/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def ens_setup():
220220
@pytest.fixture
221221
def ens(ens_setup, mocker):
222222
mocker.patch('web3.middleware.stalecheck._isfresh', return_value=True)
223-
ens_setup.web3.eth.defaultAccount = ens_setup.web3.eth.coinbase
223+
ens_setup.web3.eth.default_account = ens_setup.web3.eth.coinbase
224224
return ens_setup
225225

226226

tests/ethpm/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def fetch_manifest_path(name, version):
7878
@pytest.fixture
7979
def w3():
8080
w3 = Web3(Web3.EthereumTesterProvider())
81-
w3.eth.defaultAccount = w3.eth.accounts[0]
81+
w3.eth.default_account = w3.eth.accounts[0]
8282
return w3
8383

8484

web3/eth.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
cast,
1212
overload,
1313
)
14+
import warnings
1415

1516
from eth_account import (
1617
Account,
@@ -40,6 +41,7 @@
4041
Literal,
4142
)
4243
from web3._utils.empty import (
44+
Empty,
4345
empty,
4446
)
4547
from web3._utils.encoding import (
@@ -104,7 +106,7 @@
104106

105107
class Eth(ModuleV2, Module):
106108
account = Account()
107-
_default_account = empty
109+
_default_account: Union[Empty, ChecksumAddress] = empty
108110
defaultBlock: Literal["latest"] = "latest" # noqa: E704
109111
defaultContractFactory: Type[Union[Contract, ConciseContract, ContractCaller]] = Contract # noqa: E704,E501
110112
iban = Iban
@@ -198,17 +200,29 @@ def chainId(self) -> int:
198200
return self.web3.manager.request_blocking(RPC.eth_chainId, [])
199201

200202
@property
201-
def default_account(self) -> str:
202-
return self.default_account
203+
def default_account(self) -> Union[Empty, ChecksumAddress]:
204+
return self._default_account
205+
206+
@default_account.setter
207+
def default_account(self, account: Union[Empty, ChecksumAddress]) -> None:
208+
self._default_account = account
203209

204210
@property
205-
def defaultAccount(self) -> str:
211+
def defaultAccount(self) -> Union[Empty, ChecksumAddress]:
206212
warnings.warn(
207213
'defaultAccount is deprecated in favor of default_account',
208214
category=DeprecationWarning,
209215
)
210216
return self._default_account
211217

218+
@defaultAccount.setter
219+
def defaultAccount(self, account: Union[Empty, ChecksumAddress]) -> None:
220+
warnings.warn(
221+
'defaultAccount is deprecated in favor of default_account',
222+
category=DeprecationWarning,
223+
)
224+
self._default_account = account
225+
212226
def block_id_munger(
213227
self,
214228
account: Union[Address, ChecksumAddress, ENS],
@@ -443,7 +457,6 @@ def call_munger(
443457
if block_identifier is None:
444458
block_identifier = self.defaultBlock
445459

446-
<<<<<<< HEAD
447460
return (transaction, block_identifier)
448461

449462
call: Method[Callable[..., Union[bytes, bytearray]]] = Method(
@@ -456,14 +469,8 @@ def estimate_gas_munger(
456469
transaction: TxParams,
457470
block_identifier: Optional[BlockIdentifier] = None
458471
) -> Sequence[Union[TxParams, BlockIdentifier]]:
459-
if 'from' not in transaction and is_checksum_address(self.defaultAccount):
460-
transaction = assoc(transaction, 'from', self.defaultAccount)
461-
=======
462-
def estimateGas(self, transaction: TxParams, block_identifier: BlockIdentifier=None) -> Wei:
463-
# TODO: move to middleware
464472
if 'from' not in transaction and is_checksum_address(self.default_account):
465473
transaction = assoc(transaction, 'from', self.default_account)
466-
>>>>>>> 1b792773... eth.defaultAccount to eth.default_account
467474

468475
if block_identifier is None:
469476
params: Sequence[Union[TxParams, BlockIdentifier]] = [transaction]

0 commit comments

Comments
 (0)