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

refactor: migrate to ape with Pydantic changes #18

Merged
merged 13 commits into from
Mar 11, 2022
4 changes: 3 additions & 1 deletion .github/workflows/commitlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ jobs:
python-version: 3.8

- name: Install Dependencies
run: pip install .[dev]
run: |
python -m pip install --upgrade pip
pip install .[lint]

- name: Check commit history
run: cz check --rev-range $(git rev-list --all --reverse | head -1)..HEAD
12 changes: 9 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ jobs:
python-version: 3.8

- name: Install Dependencies
run: pip install .[lint]
run: |
python -m pip install --upgrade pip
pip install .[lint]

- name: Run Black
run: black --check .
Expand All @@ -38,7 +40,9 @@ jobs:
python-version: 3.8

- name: Install Dependencies
run: pip install .[lint,test] # Might need test deps
run: |
python -m pip install --upgrade pip
pip install .[lint,test]

- name: Run MyPy
run: mypy .
Expand All @@ -60,7 +64,9 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Install Dependencies
run: pip install .[test]
run: |
python -m pip install --upgrade pip
pip install .[test]

- name: Run Tests
run: pytest -m "not fuzzing" -n 0 -s --cov
Expand Down
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ repos:
hooks:
- id: check-yaml

- repo: https://github.com/asottile/seed-isort-config
rev: v2.2.0
hooks:
- id: seed-isort-config

- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.9.3
hooks:
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ ape ledger delete <alias>

## Development

This project is in development and should be considered a beta.
Things might not be in their final state and breaking changes may occur.
Please see the [contributing guide](CONTRIBUTING.md) to learn more how to contribute to this project.
Comments, questions, criticisms and pull requests are welcomed.

## License
Expand Down
35 changes: 20 additions & 15 deletions ape_ledger/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import rlp # type: ignore
from ape.api import AccountAPI, AccountContainerAPI, TransactionAPI, TransactionType
from ape.convert import to_address
from ape.logging import logger
from ape.types import AddressType, MessageSignature, TransactionSignature
from ape.utils import to_address
from eth_account.messages import SignableMessage
from hexbytes import HexBytes

Expand All @@ -17,7 +17,16 @@


class AccountContainer(AccountContainerAPI):
_usb_device = None
@property
def accounts(self) -> Iterator[AccountAPI]:
for account_file in self._account_files:
yield LedgerAccount(container=self, account_file_path=account_file) # type: ignore

def __setitem__(self, address: AddressType, account: AccountAPI):
raise NotImplementedError()

def __delitem__(self, address: AddressType):
raise NotImplementedError()

@property
def _account_files(self) -> Iterator[Path]:
Expand All @@ -31,10 +40,6 @@ def aliases(self) -> Iterator[str]:
def __len__(self) -> int:
return len([*self._account_files])

def __iter__(self) -> Iterator[AccountAPI]:
for account_file in self._account_files:
yield LedgerAccount(self, account_file) # type: ignore

def save_account(self, alias: str, address: str, hd_path: str):
"""
Save a new Ledger account to your ape configuration.
Expand All @@ -51,14 +56,14 @@ def delete_account(self, alias: str):


class LedgerAccount(AccountAPI):
_account_file_path: Path
account_file_path: Path

# Optional because it's lazily loaded
_account_client: Optional[LedgerEthereumAccountClient] = None
account_client: Optional[LedgerEthereumAccountClient] = None

@property
def alias(self) -> str:
return self._account_file_path.stem
return self.account_file_path.stem

@property
def address(self) -> AddressType:
Expand All @@ -71,13 +76,13 @@ def hdpath(self) -> HDAccountPath:

@property
def account_file(self) -> dict:
return json.loads(self._account_file_path.read_text())
return json.loads(self.account_file_path.read_text())

@property
def _client(self) -> LedgerEthereumAccountClient:
if self._account_client is None:
self._account_client = connect_to_ethereum_account(self.address, self.hdpath)
return self._account_client
if self.account_client is None:
self.account_client = connect_to_ethereum_account(self.address, self.hdpath)
return self.account_client

def sign_message(self, msg: SignableMessage) -> Optional[MessageSignature]:
version = msg.version
Expand Down Expand Up @@ -115,10 +120,10 @@ def sign_message(self, msg: SignableMessage) -> Optional[MessageSignature]:
def sign_transaction(self, txn: TransactionAPI) -> Optional[TransactionSignature]:
txn_type = TransactionType(txn.type) # In case it is not enum
if txn_type == TransactionType.STATIC:
serializable_txn = StaticFeeTransaction(**txn.as_dict())
serializable_txn = StaticFeeTransaction(**txn.dict())
txn_bytes = rlp.encode(serializable_txn, StaticFeeTransaction)
else:
serializable_txn = DynamicFeeTransaction(**txn.as_dict())
serializable_txn = DynamicFeeTransaction(**txn.dict())
version_byte = bytes(HexBytes(TransactionType.DYNAMIC.value))
txn_bytes = version_byte + rlp.encode(serializable_txn, DynamicFeeTransaction)

Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,5 @@ markers = "fuzzing: Run Hypothesis fuzz test suite"
line_length = 100
force_grid_wrap = 0
include_trailing_comma = true
known_third_party = ["ape", "click", "eip712", "eth_account", "eth_typing", "eth_utils", "hid", "pytest", "rlp", "setuptools"]
known_first_party = ["ape_ledger", "conftest"]
multi_line_output = 3
use_parentheses = true
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"hypothesis>=6.2.0,<7.0", # Strategy-based fuzzer
],
"lint": [
"commitizen>=2.19,<2.20", # Manage commits and publishing releases
"black>=21.10b0,<22.0", # auto-formatter and linter
"mypy>=0.910,<1.0", # Static type analyzer
"flake8>=3.8.3,<4.0", # Style linter
Expand All @@ -23,7 +24,6 @@
"twine", # Package upload tool
],
"dev": [
"commitizen", # Manage commits and publishing releases
"pre-commit", # Ensure that linters are run prior to commiting
"pytest-watch", # `ptw` test watcher/runner
"IPython", # Console for interacting
Expand Down Expand Up @@ -57,7 +57,7 @@
install_requires=[
"click>=8.0.0",
"hidapi==0.10.1",
"eth-ape>=0.1.0b1",
"eth-ape>=0.1.0,<0.2.0",
"eth-account>=0.5.6,<0.6.0",
"eth-typing>=2.2.2",
"eth-utils>=1.10.0",
Expand All @@ -70,7 +70,7 @@
"ape_ledger=ape_ledger._cli:cli",
],
},
python_requires=">=3.7,<4",
python_requires=">=3.7.2,<4",
extras_require=extras_require,
py_modules=["ape_ledger"],
license="Apache-2.0",
Expand Down
Empty file added tests/__init__.py
Empty file.
18 changes: 6 additions & 12 deletions tests/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

from ape_ledger.accounts import AccountContainer, LedgerAccount
from ape_ledger.exceptions import LedgerSigningError
from conftest import TEST_ADDRESS, TEST_ALIAS, TEST_HD_PATH, assert_account

from .conftest import TEST_ADDRESS, TEST_ALIAS, TEST_HD_PATH, assert_account


class Person(EIP712Type):
Expand Down Expand Up @@ -89,11 +90,6 @@ def account_connection(mocker, ledger_account):
return patch


@pytest.fixture
def mock_config_manager(mocker):
return mocker.MagicMock()


@pytest.fixture(autouse=True)
def isolated_file_system(runner):
with runner.isolated_filesystem():
Expand All @@ -103,10 +99,8 @@ def isolated_file_system(runner):
@pytest.fixture
def account(mock_container):
create_account("account.json", TEST_HD_PATH)
account = LedgerAccount(mock_container, Path("account.json"))
with networks.parse_network_choice(f"ethereum:{LOCAL_NETWORK_NAME}:test") as provider:
account.provider = provider
yield account
with networks.parse_network_choice(f"ethereum:{LOCAL_NETWORK_NAME}:test"):
yield LedgerAccount(container=mock_container, account_file_path=Path("account.json"))


@pytest.fixture
Expand All @@ -117,8 +111,8 @@ def sign_txn_spy(mocker):


class TestAccountContainer:
def test_save_account(self, mock_container, mock_config_manager):
container = AccountContainer(Path("."), LedgerAccount, mock_config_manager)
def test_save_account(self, mock_container):
container = AccountContainer(data_folder=Path("."), account_type=LedgerAccount)
container.save_account(TEST_ALIAS, TEST_ADDRESS, TEST_HD_PATH)
assert_account(f"{TEST_ALIAS}.json", expected_hdpath=TEST_HD_PATH)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_choices.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ape_ledger.choices import AddressPromptChoice
from conftest import TEST_ADDRESS

from .conftest import TEST_ADDRESS


class TestAddressPromptChoice:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from ape_ledger import LedgerAccount
from ape_ledger.hdpath import HDBasePath
from conftest import TEST_ADDRESS, TEST_HD_PATH, assert_account

from .conftest import TEST_ADDRESS, TEST_HD_PATH, assert_account


def _get_container():
Expand Down