Skip to content

Commit cc5eb7f

Browse files
newtectonicskclowes
authored andcommitted
contract block_identifier default change
Changed default block_identifier in contract.call() to None. Changed parse_block_identifier to use web3.eth.defaultBlock if None is passed in
1 parent d786101 commit cc5eb7f

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

newsfragments/2777.breaking.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
When calling a contract, use ``w3.eth.default_block`` if no block_identifier is specified instead of ``latest``.

tests/core/contracts/test_contract_call_interface.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,20 @@ def test_call_revert_contract(revert_contract):
10211021
revert_contract.functions.revertWithMessage().call({"gas": 100000})
10221022

10231023

1024+
def test_changing_default_block_identifier(w3, math_contract):
1025+
assert math_contract.caller.counter() == 0
1026+
assert w3.eth.default_block == "latest"
1027+
1028+
math_contract.functions.increment(7).transact()
1029+
assert math_contract.caller.counter() == 7
1030+
1031+
assert math_contract.functions.counter().call(block_identifier=1) == 0
1032+
w3.eth.default_block = 1
1033+
assert math_contract.functions.counter().call(block_identifier=None) == 0
1034+
w3.eth.default_block = 0x2
1035+
assert math_contract.functions.counter().call(block_identifier=None) == 7
1036+
1037+
10241038
@pytest.mark.asyncio
10251039
async def test_async_invalid_address_in_deploy_arg(
10261040
AsyncWithConstructorAddressArgumentsContract,
@@ -1885,3 +1899,22 @@ async def test_async_returns_data_from_specified_block(async_w3, async_math_cont
18851899

18861900
assert output1 == 1
18871901
assert output2 == 2
1902+
1903+
1904+
@pytest.mark.asyncio
1905+
async def test_async_changing_default_block_identifier(async_w3, async_math_contract):
1906+
assert await async_math_contract.caller.counter() == 0
1907+
assert async_w3.eth.default_block == "latest"
1908+
1909+
await async_math_contract.functions.increment(7).transact()
1910+
assert await async_math_contract.caller.counter() == 7
1911+
1912+
assert await async_math_contract.functions.counter().call(block_identifier=1) == 0
1913+
async_w3.eth.default_block = 1
1914+
assert (
1915+
await async_math_contract.functions.counter().call(block_identifier=None) == 0
1916+
)
1917+
async_w3.eth.default_block = 0x2
1918+
assert (
1919+
await async_math_contract.functions.counter().call(block_identifier=None) == 7
1920+
)

web3/contract.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> "ContractFunction":
11971197
def call(
11981198
self,
11991199
transaction: Optional[TxParams] = None,
1200-
block_identifier: BlockIdentifier = "latest",
1200+
block_identifier: BlockIdentifier = None,
12011201
state_override: Optional[CallOverride] = None,
12021202
ccip_read_enabled: Optional[bool] = None,
12031203
) -> Any:
@@ -1308,7 +1308,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> "AsyncContractFunction":
13081308
async def call(
13091309
self,
13101310
transaction: Optional[TxParams] = None,
1311-
block_identifier: BlockIdentifier = "latest",
1311+
block_identifier: BlockIdentifier = None,
13121312
state_override: Optional[CallOverride] = None,
13131313
ccip_read_enabled: Optional[bool] = None,
13141314
) -> Any:
@@ -1346,8 +1346,7 @@ async def call(
13461346
self._return_data_normalizers,
13471347
self.function_identifier,
13481348
call_transaction,
1349-
# BlockIdentifier does have an Awaitable type in types.py
1350-
block_id, # type: ignore
1349+
block_id,
13511350
self.contract_abi,
13521351
self.abi,
13531352
state_override,
@@ -2177,6 +2176,8 @@ async def async_call_contract_function(
21772176
def parse_block_identifier(
21782177
w3: "Web3", block_identifier: BlockIdentifier
21792178
) -> BlockIdentifier:
2179+
if block_identifier is None:
2180+
return w3.eth.default_block
21802181
if isinstance(block_identifier, int):
21812182
return parse_block_identifier_int(w3, block_identifier)
21822183
elif block_identifier in {"latest", "earliest", "pending", "safe", "finalized"}:
@@ -2191,11 +2192,13 @@ def parse_block_identifier(
21912192

21922193
async def async_parse_block_identifier(
21932194
w3: "Web3", block_identifier: BlockIdentifier
2194-
) -> Awaitable[BlockIdentifier]:
2195+
) -> BlockIdentifier:
2196+
if block_identifier is None:
2197+
return w3.eth.default_block
21952198
if isinstance(block_identifier, int):
21962199
return await async_parse_block_identifier_int(w3, block_identifier)
21972200
elif block_identifier in {"latest", "earliest", "pending", "safe", "finalized"}:
2198-
return block_identifier # type: ignore
2201+
return block_identifier
21992202
elif isinstance(block_identifier, bytes) or is_hex_encoded_block_hash(
22002203
block_identifier
22012204
):
@@ -2218,7 +2221,7 @@ def parse_block_identifier_int(w3: "Web3", block_identifier_int: int) -> BlockNu
22182221

22192222
async def async_parse_block_identifier_int(
22202223
w3: "Web3", block_identifier_int: int
2221-
) -> Awaitable[BlockNumber]:
2224+
) -> BlockNumber:
22222225
if block_identifier_int >= 0:
22232226
block_num = block_identifier_int
22242227
else:
@@ -2227,7 +2230,7 @@ async def async_parse_block_identifier_int(
22272230
block_num = last_block_num + block_identifier_int + 1
22282231
if block_num < 0:
22292232
raise BlockNumberOutofRange
2230-
return BlockNumber(block_num) # type: ignore
2233+
return BlockNumber(block_num)
22312234

22322235

22332236
def transact_with_contract_function(

0 commit comments

Comments
 (0)