Skip to content
Open
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
2 changes: 2 additions & 0 deletions pytempo/contracts/_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def _normalize_arg(value: object) -> object:
"""Auto-checksum bare hex addresses so callers don't have to."""
if isinstance(value, str) and is_hex_address(value):
return to_checksum_address(value)
if isinstance(value, dict):
return {k: _normalize_arg(v) for k, v in value.items()}
if isinstance(value, (list, tuple)):
return type(value)(_normalize_arg(v) for v in value)
return value
Expand Down
24 changes: 18 additions & 6 deletions pytempo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import attrs
import rlp
from eth_account import Account
from eth_utils import keccak, to_checksum_address
from eth_utils import is_hex_address, keccak, to_checksum_address

from .types import (
Address,
Expand Down Expand Up @@ -556,14 +556,14 @@ def sign_access_key(

def to_estimate_gas_request(
self,
sender: str,
key_id: str | None = None,
sender: BytesLike,
key_id: BytesLike | None = None,
key_authorization: dict | SignedKeyAuthorization | None = None,
) -> dict:
"""Build an eth_estimateGas request dict from this transaction.

Args:
sender: Address of the sender (hex string).
sender: Address of the sender (hex string or bytes).
key_id: Optional access key address for keychain signature gas estimation.
key_authorization: Optional :class:`SignedKeyAuthorization` or
pre-built JSON dict.
Expand All @@ -578,8 +578,14 @@ def to_estimate_gas_request(

data_hex = "0x" + data.hex() if data else "0x"

from_addr = (
to_checksum_address(bytes(sender))
if isinstance(sender, (bytes, bytearray, memoryview))
else to_checksum_address(sender)
)

request: dict = {
"from": sender,
"from": from_addr,
"data": data_hex,
}

Expand All @@ -590,7 +596,13 @@ def to_estimate_gas_request(
request["value"] = hex(value)

if key_id is not None:
request["keyId"] = key_id
request["keyId"] = (
to_checksum_address(bytes(key_id))
if isinstance(key_id, (bytes, bytearray, memoryview))
else to_checksum_address(key_id)
if is_hex_address(key_id)
else key_id
)

if key_authorization is not None:
if isinstance(key_authorization, dict):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_typed_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,16 @@ def test_immutability_preserved(self):
assert tx1.gas_limit == 21000
assert tx2.gas_limit == 200000
assert tx1 is not tx2

def test_to_estimate_gas_request_with_address_and_bytes(self):
tx = TempoTransaction.create(
chain_id=42429,
calls=(Call.create(to="0xF0109fC8DF283027b6285cc889F5aA624EaC1F55", value=100),),
)
sender_bytes = as_address("0xF0109fC8DF283027b6285cc889F5aA624EaC1F55")
req = tx.to_estimate_gas_request(sender=sender_bytes, key_id=sender_bytes)

assert req["from"] == "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55"
assert req["to"] == "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55"
assert req["keyId"] == "0xF0109fC8DF283027b6285cc889F5aA624EaC1F55"
assert req["value"] == hex(100)