From f19ff414c7c1cced6bc0ba88e1f102c91db32c52 Mon Sep 17 00:00:00 2001 From: NotPeopling2day <32708219+NotPeopling2day@users.noreply.github.com> Date: Tue, 19 Dec 2023 15:58:02 -0500 Subject: [PATCH] refactor: updates for 0.7.0 --- ape_frame/accounts.py | 33 ++++++++++++++++++--------------- ape_frame/providers.py | 9 +++++---- setup.py | 2 +- tests/test_accounts.py | 1 + 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/ape_frame/accounts.py b/ape_frame/accounts.py index 0f9cc1a..33a1093 100644 --- a/ape_frame/accounts.py +++ b/ape_frame/accounts.py @@ -1,3 +1,5 @@ +import json +from importlib.metadata import version from typing import Any, Iterator, Optional, Union from ape.api.accounts import AccountAPI, AccountContainerAPI, TransactionAPI @@ -28,8 +30,8 @@ class FrameAccount(AccountAPI): def web3(self) -> Web3: headers = { "Origin": "Ape", - "User-Agent": "ape-frame/0.1.0", - "Content-Type": "application/json" + "User-Agent": f"ape-frame/{version('ape-frame')}", + "Content-Type": "application/json", } return Web3(HTTPProvider("http://127.0.0.1:1248", request_kwargs={"headers": headers})) @@ -41,7 +43,7 @@ def alias(self) -> str: def address(self) -> AddressType: return self.web3.eth.accounts[0] - def sign_message(self, msg: Any) -> Optional[MessageSignature]: + def sign_message(self, msg: Any, **signer_options: Any) -> Optional[MessageSignature]: raw_signature = None if isinstance(msg, str): @@ -76,18 +78,19 @@ def sign_message(self, msg: Any) -> Optional[MessageSignature]: return None if isinstance(msg, EIP712Message): + json_message = json.dumps(msg._body_) try: - raw_signature = self.web3.eth.sign_typed_data(self.address, msg._body_) + raw_signature = self.web3.eth.sign_typed_data(self.address, json_message) except ValueError as e: if not e.args[0]["message"] == "User declined transaction": raise return None return ( - MessageSignature( # type: ignore[call-arg] - v=raw_signature[64], - r=raw_signature[0:32], - s=raw_signature[32:64], + MessageSignature( + v=int(raw_signature[64]), + r=HexBytes(raw_signature[0:32]), + s=HexBytes(raw_signature[32:64]), ) if raw_signature else None @@ -95,8 +98,8 @@ def sign_message(self, msg: Any) -> Optional[MessageSignature]: def sign_transaction(self, txn: TransactionAPI, **signer_options) -> Optional[TransactionAPI]: # TODO: need a way to deserialized from raw bytes - # raw_signed_txn_bytes = self.web3.eth.sign_transaction(txn.dict()) - txn_data = txn.dict(exclude={"sender"}) + # raw_signed_txn_bytes = self.web3.eth.sign_transaction(txn.model_dump()) + txn_data = txn.model_dump_json(by_alias=True, exclude={"sender"}) unsigned_txn = serializable_unsigned_transaction_from_dict(txn_data) try: raw_signature = self.web3.eth.sign(self.address, hexstr=keccak(unsigned_txn).hex()) @@ -106,16 +109,16 @@ def sign_transaction(self, txn: TransactionAPI, **signer_options) -> Optional[Tr return None - txn.signature = TransactionSignature( # type: ignore[call-arg] - v=raw_signature[64], # type: ignore[arg-type] - r=raw_signature[0:32], # type: ignore[arg-type] - s=raw_signature[32:64], # type: ignore[arg-type] + txn.signature = TransactionSignature( + v=int(raw_signature[64]), + r=HexBytes(raw_signature[0:32]), + s=HexBytes(raw_signature[32:64]), ) return txn def check_signature( self, - data: Union[SignableMessage, TransactionAPI, EIP712Message, str], + data: Union[SignableMessage, TransactionAPI, EIP712Message, str, int], signature: Optional[MessageSignature] = None, ) -> bool: if isinstance(data, str): diff --git a/ape_frame/providers.py b/ape_frame/providers.py index 32e0bc1..62c9fe7 100644 --- a/ape_frame/providers.py +++ b/ape_frame/providers.py @@ -1,10 +1,11 @@ +from importlib.metadata import version from typing import Any from ape.api import UpstreamProvider -from ape_ethereum.provider import Web3Provider from ape.exceptions import ProviderError +from ape_ethereum.provider import Web3Provider from eth_utils import to_hex -from requests import HTTPError # type: ignore[import] +from requests import HTTPError from web3 import HTTPProvider, Web3 from web3.gas_strategies.rpc import rpc_gas_price_strategy from web3.middleware import geth_poa_middleware @@ -27,8 +28,8 @@ def connection_str(self) -> str: def connect(self): headers = { "Origin": "Ape", - "User-Agent": "ape-frame/0.1.0", - "Content-Type": "application/json" + "User-Agent": f"ape-frame/{version('ape-frame')}", + "Content-Type": "application/json", } self._web3 = Web3(HTTPProvider(self.uri, request_kwargs={"headers": headers})) diff --git a/setup.py b/setup.py index b67b76f..6b5050e 100644 --- a/setup.py +++ b/setup.py @@ -59,7 +59,7 @@ url="https://github.com/ApeWorX/ape-frame", include_package_data=True, install_requires=[ - "eth-ape>=0.6.0,<0.7.0", + "eth-ape>=0.7.0,<0.8.0", ], python_requires=">=3.8,<4", extras_require=extras_require, diff --git a/tests/test_accounts.py b/tests/test_accounts.py index d2c42cc..03ed20d 100644 --- a/tests/test_accounts.py +++ b/tests/test_accounts.py @@ -11,3 +11,4 @@ def test_account_container(self): container = AccountContainer(data_folder=data_path, account_type=FrameAccount) for acct in container.accounts: assert type(acct) is FrameAccount + assert acct.alias == "frame"