Skip to content

Block Tag should default to "pending" and not to "latest" in gas estimation called from within writeContract #3641

@LogvinovLeon

Description

@LogvinovLeon

Check existing issues

Viem Version

2.22.10

Current Behavior

When sending transaction using writeContract - there is no way to specify - should gas estimation happen on the latest or pending block.

Expected Behavior

Gas estimation should default to pending block as it's a more "correct" environment. There is no way transaction will get executed in the latest block as it's already mined.

Steps To Reproduce

contract BlockHashVerifier {
    function verifyBlockHash(uint256 blockNumber, bytes32 providedHash) external view returns (bool isMatch) {
        // blockhash only works for the 256 most recent blocks (excluding current)
        require(block.number > blockNumber, "Block number must be less than current block");
        require(block.number <= blockNumber + 256, "Blockhash unavailable for this block number");

        bytes32 actualHash = blockhash(blockNumber);
        assert(actualHash == providedHash);
    }
}

If you estimate gas for this contract setting blockTag to latest - it will fail. But it will succeed with pending.
There is no way to configure it from the level of writeContract, so a good default will help

Link to Minimal Reproducible Example

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions