Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Identifier default change #2777

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/2777.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When calling a contract, use ``w3.eth.default_block`` if no block_identifier is specified instead of ``latest``.
33 changes: 33 additions & 0 deletions tests/core/contracts/test_contract_call_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,20 @@ def test_call_revert_contract(revert_contract):
revert_contract.functions.revertWithMessage().call({"gas": 100000})


def test_changing_default_block_identifier(w3, math_contract):
assert math_contract.caller.counter() == 0
assert w3.eth.default_block == "latest"

math_contract.functions.increment(7).transact()
assert math_contract.caller.counter() == 7

assert math_contract.functions.counter().call(block_identifier=1) == 0
w3.eth.default_block = 1
assert math_contract.functions.counter().call(block_identifier=None) == 0
w3.eth.default_block = 0x2
assert math_contract.functions.counter().call(block_identifier=None) == 7


@pytest.mark.asyncio
async def test_async_invalid_address_in_deploy_arg(
AsyncWithConstructorAddressArgumentsContract,
Expand Down Expand Up @@ -1885,3 +1899,22 @@ async def test_async_returns_data_from_specified_block(async_w3, async_math_cont

assert output1 == 1
assert output2 == 2


@pytest.mark.asyncio
async def test_async_changing_default_block_identifier(async_w3, async_math_contract):
assert await async_math_contract.caller.counter() == 0
assert async_w3.eth.default_block == "latest"

await async_math_contract.functions.increment(7).transact()
assert await async_math_contract.caller.counter() == 7

assert await async_math_contract.functions.counter().call(block_identifier=1) == 0
async_w3.eth.default_block = 1
assert (
await async_math_contract.functions.counter().call(block_identifier=None) == 0
)
async_w3.eth.default_block = 0x2
assert (
await async_math_contract.functions.counter().call(block_identifier=None) == 7
)
19 changes: 11 additions & 8 deletions web3/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> "ContractFunction":
def call(
self,
transaction: Optional[TxParams] = None,
block_identifier: BlockIdentifier = "latest",
block_identifier: BlockIdentifier = None,
state_override: Optional[CallOverride] = None,
ccip_read_enabled: Optional[bool] = None,
) -> Any:
Expand Down Expand Up @@ -1308,7 +1308,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> "AsyncContractFunction":
async def call(
self,
transaction: Optional[TxParams] = None,
block_identifier: BlockIdentifier = "latest",
block_identifier: BlockIdentifier = None,
state_override: Optional[CallOverride] = None,
ccip_read_enabled: Optional[bool] = None,
) -> Any:
Expand Down Expand Up @@ -1346,8 +1346,7 @@ async def call(
self._return_data_normalizers,
self.function_identifier,
call_transaction,
# BlockIdentifier does have an Awaitable type in types.py
block_id, # type: ignore
block_id,
self.contract_abi,
self.abi,
state_override,
Expand Down Expand Up @@ -2177,6 +2176,8 @@ async def async_call_contract_function(
def parse_block_identifier(
w3: "Web3", block_identifier: BlockIdentifier
) -> BlockIdentifier:
if block_identifier is None:
return w3.eth.default_block
if isinstance(block_identifier, int):
return parse_block_identifier_int(w3, block_identifier)
elif block_identifier in {"latest", "earliest", "pending", "safe", "finalized"}:
Expand All @@ -2191,11 +2192,13 @@ def parse_block_identifier(

async def async_parse_block_identifier(
w3: "Web3", block_identifier: BlockIdentifier
) -> Awaitable[BlockIdentifier]:
) -> BlockIdentifier:
if block_identifier is None:
return w3.eth.default_block
if isinstance(block_identifier, int):
return await async_parse_block_identifier_int(w3, block_identifier)
elif block_identifier in {"latest", "earliest", "pending", "safe", "finalized"}:
return block_identifier # type: ignore
return block_identifier
elif isinstance(block_identifier, bytes) or is_hex_encoded_block_hash(
block_identifier
):
Expand All @@ -2218,7 +2221,7 @@ def parse_block_identifier_int(w3: "Web3", block_identifier_int: int) -> BlockNu

async def async_parse_block_identifier_int(
w3: "Web3", block_identifier_int: int
) -> Awaitable[BlockNumber]:
) -> BlockNumber:
if block_identifier_int >= 0:
block_num = block_identifier_int
else:
Expand All @@ -2227,7 +2230,7 @@ async def async_parse_block_identifier_int(
block_num = last_block_num + block_identifier_int + 1
if block_num < 0:
raise BlockNumberOutofRange
return BlockNumber(block_num) # type: ignore
return BlockNumber(block_num)


def transact_with_contract_function(
Expand Down