Skip to content

Suggest: make gas limit configurable in Python SDK signer #1846

@HeadyZhang

Description

@HeadyZhang

Summary

The reference Web3EvmSigner in the Python SDK hardcodes gas: 300000 for both write_contract() and send_transaction(). Making this configurable would improve flexibility for different contract call complexities and network conditions.

Affected file

python/x402/mechanisms/evm/signers.py

  • Line 479: write_contract()"gas": 300000
  • Line 505: send_transaction()"gas": 300000

Current behavior

# write_contract (L475-482)
tx = func(*args).build_transaction(
    {
        "from": self._account.address,
        "nonce": self._w3.eth.get_transaction_count(self._account.address),
        "gas": 300000,
        "gasPrice": self._w3.eth.gas_price,
    }
)

# send_transaction (L500-507)
tx = {
    "from": self._account.address,
    "to": Web3.to_checksum_address(to),
    "data": data,
    "nonce": self._w3.eth.get_transaction_count(self._account.address),
    "gas": 300000,
    "gasPrice": self._w3.eth.gas_price,
}

Suggested change

Add a configurable default with optional per-call override:

class Web3EvmSigner:
    DEFAULT_GAS_LIMIT = 300_000

    def __init__(self, w3, account, *, gas_limit: int = DEFAULT_GAS_LIMIT):
        self._w3 = w3
        self._account = account
        self._gas_limit = gas_limit

    def send_transaction(self, to: str, data: bytes, *, gas: int | None = None) -> str:
        tx = {
            "from": self._account.address,
            "to": Web3.to_checksum_address(to),
            "data": data,
            "nonce": self._w3.eth.get_transaction_count(self._account.address),
            "gas": gas or self._gas_limit,
            "gasPrice": self._w3.eth.gas_price,
        }
        # ...

Alternatively, using eth.estimate_gas() as a default would adapt to actual call complexity:

"gas": gas or self._w3.eth.estimate_gas({"to": to, "data": data, "from": self._account.address}),

This is a minor ergonomics improvement — the current hardcoded value works fine for most cases. Happy to open a PR if helpful.

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