From 0eb991540f9ec22efdf839c610fdc532cc869619 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Thu, 20 May 2021 17:28:09 +0300 Subject: [PATCH 01/12] Gradually introduce mypy type checking --- Makefile | 7 ++----- setup.cfg | 31 ++++++++++++++++++++++++++++--- zksync_sdk/ethereum_provider.py | 2 +- zksync_sdk/serializers.py | 2 ++ zksync_sdk/types/responses.py | 1 + zksync_sdk/types/transactions.py | 2 +- zksync_sdk/wallet.py | 6 +++--- zksync_sdk/zksync.py | 2 +- zksync_sdk/zksync_provider/v01.py | 1 + 9 files changed, 40 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 35a7ac6f..2fdecd66 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ SHELL := /bin/bash -.PHONY: test test37 test38 test39 mypy coverage +.PHONY: test test38 test39 mypy coverage TOX := docker-compose run --rm app tox @@ -8,9 +8,6 @@ test: $(TOX) -test37: - $(TOX) -e py37 - test38: $(TOX) -e py38 @@ -27,4 +24,4 @@ coverage: .coverage docker-compose run --rm app coverage report coverage.xml: .coverage - docker-compose run --rm app coverage xml \ No newline at end of file + docker-compose run --rm app coverage xml diff --git a/setup.cfg b/setup.cfg index afabb01c..e755cf88 100644 --- a/setup.cfg +++ b/setup.cfg @@ -13,7 +13,7 @@ install_requires = web3 >= 5.16 httpx >= 0.16 pydantic >= 1.7 - +python_requires = >=3.8 setup_requires = setuptools_scm>=3.5.0 @@ -36,8 +36,33 @@ zksync_sdk.contract_abi = [tox:tox] envlist = py{38,39},mypy -[testenv] +[testenv:py{38,39}] deps = coverage -extras = test setenv = ZK_SYNC_LIBRARY_PATH=/lib/zks-crypto-linux-x64.so commands = coverage run -m unittest + +[testenv:mypy] +extras = test +commands = mypy . + +[mypy] +show_error_codes = True + +# TODO: These `ignore_errors` exist to gradually introduce mypy type checking. +# Fix the errors and remove these lines. +[mypy-zksync_sdk.wallet.*] +ignore_errors = True +[mypy-zksync_sdk.contract_utils.*] +ignore_errors = True +[mypy-zksync_sdk.transport.http.*] +ignore_errors = True +[mypy-zksync_sdk.lib.*] +ignore_errors = True +[mypy-zksync_sdk.types.transactions.*] +ignore_errors = True + +[mypy-setuptools.*] +ignore_missing_imports = True + +[mypy-eth_account.*] +ignore_missing_imports = True diff --git a/zksync_sdk/ethereum_provider.py b/zksync_sdk/ethereum_provider.py index 64b2b56f..857c97c2 100644 --- a/zksync_sdk/ethereum_provider.py +++ b/zksync_sdk/ethereum_provider.py @@ -13,7 +13,7 @@ def __init__(self, web3: Web3, zksync: ZkSync): self.web3 = web3 self.zksync = zksync - async def approve_deposit(self, token: Token, limit: Decimal = None): + async def approve_deposit(self, token: Token, limit: Decimal): contract = ERC20Contract(self.web3, self.zksync.contract_address, token.address, self.zksync.account) return contract.approve_deposit(token.from_decimal(limit)) diff --git a/zksync_sdk/serializers.py b/zksync_sdk/serializers.py index 61cb362f..01934e56 100644 --- a/zksync_sdk/serializers.py +++ b/zksync_sdk/serializers.py @@ -202,6 +202,8 @@ def remove_address_prefix(address: str) -> str: if address.startswith('sync:'): return address[5:] + return address + def serialize_address(address: str) -> bytes: address = remove_address_prefix(address) diff --git a/zksync_sdk/types/responses.py b/zksync_sdk/types/responses.py index e4d3117d..99b34c8b 100644 --- a/zksync_sdk/types/responses.py +++ b/zksync_sdk/types/responses.py @@ -40,6 +40,7 @@ class Config: alias_generator = to_camel def get_nonce(self) -> int: + assert self.committed is not None, "`get_nonce` needs `committed` to be set" return self.committed.nonce diff --git a/zksync_sdk/types/transactions.py b/zksync_sdk/types/transactions.py index c8eb9d75..c26fa501 100644 --- a/zksync_sdk/types/transactions.py +++ b/zksync_sdk/types/transactions.py @@ -115,7 +115,7 @@ def find_by_symbol(self, symbol: str) -> Optional[Token]: else: return None - def find(self, token: TokenLike) -> Token: + def find(self, token: TokenLike) -> Optional[Token]: result = None if isinstance(token, int): result = self.find_by_id(token) diff --git a/zksync_sdk/wallet.py b/zksync_sdk/wallet.py index d6eb840b..ba754cee 100644 --- a/zksync_sdk/wallet.py +++ b/zksync_sdk/wallet.py @@ -213,12 +213,12 @@ async def withdraw(self, eth_address: str, amount: Decimal, token: TokenLike, async def get_balance(self, token: TokenLike, type: str): account_state = await self.get_account_state() - token = await self.resolve_token(token) + token_obj = await self.resolve_token(token) if type == "committed": - token_balance = account_state.committed.balances.get(token.symbol) + token_balance = account_state.committed.balances.get(token_obj.symbol) else: - token_balance = account_state.verified.balances.get(token.symbol) + token_balance = account_state.verified.balances.get(token_obj.symbol) if token_balance is None: token_balance = 0 return token_balance diff --git a/zksync_sdk/zksync.py b/zksync_sdk/zksync.py index 9ccdee30..f8a22178 100644 --- a/zksync_sdk/zksync.py +++ b/zksync_sdk/zksync.py @@ -11,7 +11,7 @@ class Contract: def __init__(self, contract_address: str, web3: Web3, account: BaseAccount, abi): self.contract_address = contract_address self.web3 = web3 - self.contract = self.web3.eth.contract(self.contract_address, abi=abi) + self.contract = self.web3.eth.contract(self.contract_address, abi=abi) # type: ignore self.account = account def _call_method(self, method_name, *args, amount=None, **kwargs): diff --git a/zksync_sdk/zksync_provider/v01.py b/zksync_sdk/zksync_provider/v01.py index e79debbe..9312737b 100644 --- a/zksync_sdk/zksync_provider/v01.py +++ b/zksync_sdk/zksync_provider/v01.py @@ -57,6 +57,7 @@ async def get_confirmations_for_eth_op_amount(self) -> int: async def get_account_nonce(self, address: str) -> Tuple[int, int]: state = await self.get_state(address) + assert state.id is not None, "AccountState must have ID" return state.id, state.get_nonce() async def get_tx_receipt(self, address: str) -> TransactionDetails: From 16dabe8390845c7830b890f30f9bc0e045d15468 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Thu, 20 May 2021 19:38:04 +0300 Subject: [PATCH 02/12] Type check lib.py --- setup.cfg | 2 -- zksync_sdk/lib.py | 10 +++++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/setup.cfg b/setup.cfg index e755cf88..a2b3c867 100644 --- a/setup.cfg +++ b/setup.cfg @@ -56,8 +56,6 @@ ignore_errors = True ignore_errors = True [mypy-zksync_sdk.transport.http.*] ignore_errors = True -[mypy-zksync_sdk.lib.*] -ignore_errors = True [mypy-zksync_sdk.types.transactions.*] ignore_errors = True diff --git a/zksync_sdk/lib.py b/zksync_sdk/lib.py index 280d1fcb..8582a30f 100644 --- a/zksync_sdk/lib.py +++ b/zksync_sdk/lib.py @@ -36,7 +36,7 @@ class ZkSyncLibrary: def __init__(self, library_path: str = None): if library_path is None: - library_path = os.getenv("ZK_SYNC_LIBRARY_PATH") + library_path = os.environ["ZK_SYNC_LIBRARY_PATH"] self.lib = cdll.LoadLibrary(library_path) def private_key_from_seed(self, seed: bytes): @@ -54,15 +54,15 @@ def get_public_key(self, private_key: bytes): def get_pubkey_hash(self, public_key: bytes): assert len(public_key) == PUBLIC_KEY_LEN public_key_hash = ctypes.pointer(ZksPubkeyHash()) - public_key = ctypes.pointer( + public_key_ptr = ctypes.pointer( ZksPackedPublicKey(data=(c_ubyte * PUBLIC_KEY_LEN)(*public_key))) - self.lib.zks_crypto_public_key_to_pubkey_hash(public_key, public_key_hash) + self.lib.zks_crypto_public_key_to_pubkey_hash(public_key_ptr, public_key_hash) return bytes(public_key_hash.contents.data) def sign(self, private_key: bytes, message: bytes): assert len(private_key) == PRIVATE_KEY_LEN signature = ctypes.pointer(ZksSignature()) - private_key = ctypes.pointer( + private_key_ptr = ctypes.pointer( ZksPrivateKey(data=(c_ubyte * PRIVATE_KEY_LEN)(*private_key))) - self.lib.zks_crypto_sign_musig(private_key, message, len(message), signature) + self.lib.zks_crypto_sign_musig(private_key_ptr, message, len(message), signature) return bytes(signature.contents.data) From b942ffaefe4dc7c569c03484b7c46da379c8417c Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Thu, 20 May 2021 20:12:08 +0300 Subject: [PATCH 03/12] Type check types/transactions.py --- setup.cfg | 2 -- zksync_sdk/types/transactions.py | 12 ++++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/setup.cfg b/setup.cfg index a2b3c867..e2553cdc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -56,8 +56,6 @@ ignore_errors = True ignore_errors = True [mypy-zksync_sdk.transport.http.*] ignore_errors = True -[mypy-zksync_sdk.types.transactions.*] -ignore_errors = True [mypy-setuptools.*] ignore_missing_imports = True diff --git a/zksync_sdk/types/transactions.py b/zksync_sdk/types/transactions.py index c26fa501..4d5c58c2 100644 --- a/zksync_sdk/types/transactions.py +++ b/zksync_sdk/types/transactions.py @@ -155,9 +155,9 @@ class ChangePubKey(EncodedTx): nonce: int valid_from: int valid_until: int - eth_auth_data: Union[ChangePubKeyCREATE2, ChangePubKeyEcdsa] = None - eth_signature: TxEthSignature = None - signature: TxSignature = None + eth_auth_data: Union[ChangePubKeyCREATE2, ChangePubKeyEcdsa, None] = None + eth_signature: Optional[TxEthSignature] = None + signature: Optional[TxSignature] = None def human_readable_message(self) -> str: message = f"Set signing key: {self.new_pk_hash.replace('sync:', '').lower()}" @@ -227,7 +227,7 @@ class Transfer(EncodedTx): nonce: int valid_from: int valid_until: int - signature: TxSignature = None + signature: Optional[TxSignature] = None def tx_type(self) -> int: return 5 @@ -277,7 +277,7 @@ class Withdraw(EncodedTx): valid_from: int valid_until: int token: Token - signature: TxSignature = None + signature: Optional[TxSignature] = None def tx_type(self) -> int: return 3 @@ -325,7 +325,7 @@ class ForcedExit(EncodedTx): nonce: int valid_from: int valid_until: int - signature: TxSignature = None + signature: Optional[TxSignature] = None def tx_type(self) -> int: return 8 From 892435504b984bf3e121779021864ec30ca90e85 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Thu, 20 May 2021 22:09:16 +0300 Subject: [PATCH 04/12] Type check transport/http.py --- setup.cfg | 2 -- zksync_sdk/transport/http.py | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/setup.cfg b/setup.cfg index e2553cdc..0da06b9c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -54,8 +54,6 @@ show_error_codes = True ignore_errors = True [mypy-zksync_sdk.contract_utils.*] ignore_errors = True -[mypy-zksync_sdk.transport.http.*] -ignore_errors = True [mypy-setuptools.*] ignore_missing_imports = True diff --git a/zksync_sdk/transport/http.py b/zksync_sdk/transport/http.py index bc55090c..fe24dbf5 100644 --- a/zksync_sdk/transport/http.py +++ b/zksync_sdk/transport/http.py @@ -1,5 +1,5 @@ from http.client import OK -from typing import List +from typing import List, Optional import httpx @@ -11,7 +11,7 @@ class HttpJsonRPCTransport(JsonRPCTransport): def __init__(self, network: Network): self.network = network - async def request(self, method: str, params: List): + async def request(self, method: str, params: Optional[List]): async with httpx.AsyncClient() as client: response = await client.post(self.network.zksync_url, json=self.create_request(method, params)) From 51aedd18cf38d0a7415a5c73674ba813a7936734 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Thu, 20 May 2021 22:30:18 +0300 Subject: [PATCH 05/12] Type check contract_utils.py --- setup.cfg | 5 +---- zksync_sdk/contract_utils.py | 6 +----- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/setup.cfg b/setup.cfg index 0da06b9c..62c03840 100644 --- a/setup.cfg +++ b/setup.cfg @@ -48,12 +48,9 @@ commands = mypy . [mypy] show_error_codes = True -# TODO: These `ignore_errors` exist to gradually introduce mypy type checking. -# Fix the errors and remove these lines. +# TODO: Fix the errors in `zksync_sdk/wallet.py` and remove these lines. [mypy-zksync_sdk.wallet.*] ignore_errors = True -[mypy-zksync_sdk.contract_utils.*] -ignore_errors = True [mypy-setuptools.*] ignore_missing_imports = True diff --git a/zksync_sdk/contract_utils.py b/zksync_sdk/contract_utils.py index 2c801162..eb339e29 100644 --- a/zksync_sdk/contract_utils.py +++ b/zksync_sdk/contract_utils.py @@ -1,10 +1,6 @@ +import importlib.resources as pkg_resources import json -try: - import importlib.resources as pkg_resources -except ImportError: - # Try backported to PY<37 `importlib_resources`. - import importlib_resources as pkg_resources from . import contract_abi zksync_abi_cache = None From 5223f25b57541669b74c792cd6e2eff408bfce6e Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Fri, 21 May 2021 13:42:24 +0300 Subject: [PATCH 06/12] Type check wallet.py --- setup.cfg | 4 ---- zksync_sdk/wallet.py | 52 ++++++++++++++++++++++---------------------- zksync_sdk/zksync.py | 2 +- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/setup.cfg b/setup.cfg index 62c03840..f1250e34 100644 --- a/setup.cfg +++ b/setup.cfg @@ -48,10 +48,6 @@ commands = mypy . [mypy] show_error_codes = True -# TODO: Fix the errors in `zksync_sdk/wallet.py` and remove these lines. -[mypy-zksync_sdk.wallet.*] -ignore_errors = True - [mypy-setuptools.*] ignore_missing_imports = True diff --git a/zksync_sdk/wallet.py b/zksync_sdk/wallet.py index ba754cee..6add4f8e 100644 --- a/zksync_sdk/wallet.py +++ b/zksync_sdk/wallet.py @@ -72,21 +72,21 @@ async def build_change_pub_key( if fee is None: if eth_auth_type == ChangePubKeyTypes.ecdsa: - fee = await self.zk_provider.get_transaction_fee(FeeTxType.change_pub_key_ecdsa, + fee_obj = await self.zk_provider.get_transaction_fee(FeeTxType.change_pub_key_ecdsa, self.address(), fee_token) elif eth_auth_type == ChangePubKeyTypes.onchain: - fee = await self.zk_provider.get_transaction_fee(FeeTxType.change_pub_key_onchain, + fee_obj = await self.zk_provider.get_transaction_fee(FeeTxType.change_pub_key_onchain, self.address(), fee_token) - elif eth_auth_type == ChangePubKeyTypes.create2: - fee = await self.zk_provider.get_transaction_fee(FeeTxType.change_pub_key_create2, + else: # eth_auth_type == ChangePubKeyTypes.create2 + fee_obj = await self.zk_provider.get_transaction_fee(FeeTxType.change_pub_key_create2, self.address(), fee_token) - fee = fee.total_fee + fee_int = fee_obj.total_fee else: - fee = token.from_decimal(fee) + fee_int = token.from_decimal(fee) new_pubkey_hash = self.zk_signer.pubkey_hash_str() change_pub_key = ChangePubKey( @@ -94,7 +94,7 @@ async def build_change_pub_key( account_id=account_id, new_pk_hash=new_pubkey_hash, token=token, - fee=fee, + fee=fee_int, nonce=nonce, valid_until=valid_until, valid_from=valid_from, @@ -126,19 +126,19 @@ async def build_forced_exit( valid_until=DEFAULT_VALID_UNTIL ) -> Tuple[ForcedExit, TxEthSignature]: account_id, nonce = await self.zk_provider.get_account_nonce(self.address()) - token = await self.resolve_token(token) + token_obj = await self.resolve_token(token) if fee is None: - fee = await self.zk_provider.get_transaction_fee(FeeTxType.withdraw, target, token.id) - fee = fee.total_fee + fee_obj = await self.zk_provider.get_transaction_fee(FeeTxType.withdraw, target, token_obj.id) + fee_int = fee_obj.total_fee else: - fee = token.from_decimal(fee) + fee_int = token_obj.from_decimal(fee) forced_exit = ForcedExit(initiator_account_id=account_id, target=target, - fee=fee, + fee=fee_int, nonce=nonce, valid_from=valid_from, valid_until=valid_until, - token=token) + token=token_obj) eth_signature = self.eth_signer.sign_tx(forced_exit) zk_signature = self.zk_signer.sign_tx(forced_exit) forced_exit.signature = zk_signature @@ -153,19 +153,19 @@ async def build_transfer(self, to: str, amount: Decimal, token: TokenLike, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL) -> Tuple[Transfer, TxEthSignature]: account_id, nonce = await self.zk_provider.get_account_nonce(self.address()) - token = await self.resolve_token(token) + token_obj = await self.resolve_token(token) if fee is None: - fee = await self.zk_provider.get_transaction_fee(FeeTxType.transfer, to, token.id) - fee = fee.total_fee + fee_obj = await self.zk_provider.get_transaction_fee(FeeTxType.transfer, to, token_obj.id) + fee_int = fee_obj.total_fee else: - fee = token.from_decimal(fee) + fee_int = token_obj.from_decimal(fee) transfer = Transfer(account_id=account_id, from_address=self.address(), to_address=to, - amount=token.from_decimal(amount), fee=fee, + amount=token_obj.from_decimal(amount), fee=fee_int, nonce=nonce, valid_from=valid_from, valid_until=valid_until, - token=token) + token=token_obj) eth_signature = self.eth_signer.sign_tx(transfer) zk_signature = self.zk_signer.sign_tx(transfer) transfer.signature = zk_signature @@ -182,22 +182,22 @@ async def transfer(self, to: str, amount: Decimal, token: TokenLike, async def build_withdraw(self, eth_address: str, amount: Decimal, token: TokenLike, fee: Decimal = None, fast: bool = False, valid_from=DEFAULT_VALID_FROM, - valid_until=DEFAULT_VALID_UNTIL) -> (Withdraw, TxEthSignature): + valid_until=DEFAULT_VALID_UNTIL) -> Tuple[Withdraw, TxEthSignature]: account_id, nonce = await self.zk_provider.get_account_nonce(self.address()) - token = await self.resolve_token(token) + token_obj = await self.resolve_token(token) if fee is None: tx_type = FeeTxType.fast_withdraw if fast else FeeTxType.withdraw - fee = await self.zk_provider.get_transaction_fee(tx_type, eth_address, token.id) - fee = fee.total_fee + fee_obj = await self.zk_provider.get_transaction_fee(tx_type, eth_address, token_obj.id) + fee_int = fee_obj.total_fee else: - fee = token.from_decimal(fee) + fee_int = token_obj.from_decimal(fee) withdraw = Withdraw(account_id=account_id, from_address=self.address(), to_address=eth_address, - amount=token.from_decimal(amount), fee=fee, + amount=token_obj.from_decimal(amount), fee=fee_int, nonce=nonce, valid_from=valid_from, valid_until=valid_until, - token=token) + token=token_obj) eth_signature = self.eth_signer.sign_tx(withdraw) zk_signature = self.zk_signer.sign_tx(withdraw) withdraw.signature = zk_signature diff --git a/zksync_sdk/zksync.py b/zksync_sdk/zksync.py index f8a22178..f011bb52 100644 --- a/zksync_sdk/zksync.py +++ b/zksync_sdk/zksync.py @@ -11,7 +11,7 @@ class Contract: def __init__(self, contract_address: str, web3: Web3, account: BaseAccount, abi): self.contract_address = contract_address self.web3 = web3 - self.contract = self.web3.eth.contract(self.contract_address, abi=abi) # type: ignore + self.contract = self.web3.eth.contract(self.contract_address, abi=abi) # type: ignore[call-overload] self.account = account def _call_method(self, method_name, *args, amount=None, **kwargs): From 47a49b746072a6923f5a8d6b98c53eaee8ff0cd2 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Fri, 21 May 2021 14:00:13 +0300 Subject: [PATCH 07/12] Disable implicit optional for PEP compliancy --- setup.cfg | 1 + zksync_sdk/ethereum_provider.py | 3 ++- zksync_sdk/lib.py | 3 ++- zksync_sdk/wallet.py | 20 ++++++++++---------- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/setup.cfg b/setup.cfg index f1250e34..4a4837fb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -47,6 +47,7 @@ commands = mypy . [mypy] show_error_codes = True +no_implicit_optional = True [mypy-setuptools.*] ignore_missing_imports = True diff --git a/zksync_sdk/ethereum_provider.py b/zksync_sdk/ethereum_provider.py index 857c97c2..1db57010 100644 --- a/zksync_sdk/ethereum_provider.py +++ b/zksync_sdk/ethereum_provider.py @@ -1,4 +1,5 @@ from decimal import Decimal +from typing import Optional from web3 import Web3 @@ -30,7 +31,7 @@ async def full_exit(self, token: Token, account_id: int): async def set_auth_pubkey_hash(self, pubkey_hash: bytes, nonce: int): return self.zksync.set_auth_pub_key_hash(pubkey_hash, nonce) - async def is_deposit_approved(self, token: Token, threshold: int = None) -> bool: + async def is_deposit_approved(self, token: Token, threshold: int) -> bool: contract = ERC20Contract(self.web3, self.zksync.contract_address, token.address, self.zksync.account) return contract.is_deposit_approved(threshold) diff --git a/zksync_sdk/lib.py b/zksync_sdk/lib.py index 8582a30f..8f666d68 100644 --- a/zksync_sdk/lib.py +++ b/zksync_sdk/lib.py @@ -1,6 +1,7 @@ import ctypes from ctypes import (Structure, c_ubyte, cdll) import os +from typing import Optional PRIVATE_KEY_LEN = 32 PUBLIC_KEY_LEN = 32 @@ -34,7 +35,7 @@ class ZksSignature(Structure): class ZkSyncLibrary: - def __init__(self, library_path: str = None): + def __init__(self, library_path: Optional[str] = None): if library_path is None: library_path = os.environ["ZK_SYNC_LIBRARY_PATH"] self.lib = cdll.LoadLibrary(library_path) diff --git a/zksync_sdk/wallet.py b/zksync_sdk/wallet.py index 6add4f8e..79f0bd44 100644 --- a/zksync_sdk/wallet.py +++ b/zksync_sdk/wallet.py @@ -43,8 +43,8 @@ async def send_txs_batch(self, transactions: List[TransactionWithSignature], return await self.zk_provider.submit_txs_batch(transactions, signatures) async def set_signing_key(self, fee_token: TokenLike, *, - eth_auth_data: Union[ChangePubKeyCREATE2, ChangePubKeyEcdsa] = None, - fee: Decimal = None, nonce: int = None, + eth_auth_data: Union[ChangePubKeyCREATE2, ChangePubKeyEcdsa, None] = None, + fee: Optional[Decimal] = None, nonce: Optional[int] = None, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL): change_pub_key, eth_signature = await self.build_change_pub_key(fee_token, eth_auth_data=eth_auth_data, @@ -56,8 +56,8 @@ async def set_signing_key(self, fee_token: TokenLike, *, async def build_change_pub_key( self, fee_token: TokenLike, *, - fee: Decimal = None, nonce: int = None, - eth_auth_data: Union[ChangePubKeyCREATE2, ChangePubKeyEcdsa] = None, + fee: Optional[Decimal] = None, nonce: Optional[int] = None, + eth_auth_data: Union[ChangePubKeyCREATE2, ChangePubKeyEcdsa, None] = None, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL ): account_id, new_nonce = await self.zk_provider.get_account_nonce(self.address()) @@ -110,7 +110,7 @@ async def build_change_pub_key( return change_pub_key, eth_signature - async def forced_exit(self, target: str, token: TokenLike, fee: Decimal = None, + async def forced_exit(self, target: str, token: TokenLike, fee: Optional[Decimal] = None, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL) -> str: transfer, eth_signature = await self.build_forced_exit(target, token, fee, valid_from, valid_until) @@ -121,7 +121,7 @@ async def build_forced_exit( self, target: str, token: TokenLike, - fee: Decimal = None, + fee: Optional[Decimal] = None, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL ) -> Tuple[ForcedExit, TxEthSignature]: @@ -149,7 +149,7 @@ def address(self): return self.eth_signer.address() async def build_transfer(self, to: str, amount: Decimal, token: TokenLike, - fee: Decimal = None, + fee: Optional[Decimal] = None, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL) -> Tuple[Transfer, TxEthSignature]: account_id, nonce = await self.zk_provider.get_account_nonce(self.address()) @@ -172,7 +172,7 @@ async def build_transfer(self, to: str, amount: Decimal, token: TokenLike, return transfer, eth_signature async def transfer(self, to: str, amount: Decimal, token: TokenLike, - fee: Decimal = None, + fee: Optional[Decimal] = None, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL) -> str: transfer, eth_signature = await self.build_transfer(to, amount, token, fee, valid_from, valid_until) @@ -180,7 +180,7 @@ async def transfer(self, to: str, amount: Decimal, token: TokenLike, return await self.send_signed_transaction(transfer, eth_signature) async def build_withdraw(self, eth_address: str, amount: Decimal, token: TokenLike, - fee: Decimal = None, fast: bool = False, + fee: Optional[Decimal] = None, fast: bool = False, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL) -> Tuple[Withdraw, TxEthSignature]: account_id, nonce = await self.zk_provider.get_account_nonce(self.address()) @@ -204,7 +204,7 @@ async def build_withdraw(self, eth_address: str, amount: Decimal, token: TokenLi return withdraw, eth_signature async def withdraw(self, eth_address: str, amount: Decimal, token: TokenLike, - fee: Decimal = None, fast: bool = False, + fee: Optional[Decimal] = None, fast: bool = False, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL) -> str: withdraw, eth_signature = await self.build_withdraw(eth_address, amount, token, fee, fast, From 4688317912f2c42d6ac87e439c19944e2b068f47 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Fri, 21 May 2021 14:04:03 +0300 Subject: [PATCH 08/12] Add an assert --- zksync_sdk/wallet.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zksync_sdk/wallet.py b/zksync_sdk/wallet.py index 79f0bd44..6f62cf7a 100644 --- a/zksync_sdk/wallet.py +++ b/zksync_sdk/wallet.py @@ -79,7 +79,8 @@ async def build_change_pub_key( fee_obj = await self.zk_provider.get_transaction_fee(FeeTxType.change_pub_key_onchain, self.address(), fee_token) - else: # eth_auth_type == ChangePubKeyTypes.create2 + else: + assert eth_auth_type == ChangePubKeyTypes.create2, "invalid eth_auth_type" fee_obj = await self.zk_provider.get_transaction_fee(FeeTxType.change_pub_key_create2, self.address(), fee_token) From b0c7f84cbf674c3d0144e772ce8e4efa297f2e33 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Fri, 2 Jul 2021 12:14:08 +0300 Subject: [PATCH 09/12] Fix Order.ethSignature --> Order.eth_signature --- zksync_sdk/types/transactions.py | 4 ++-- zksync_sdk/wallet.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/zksync_sdk/types/transactions.py b/zksync_sdk/types/transactions.py index e3183ce6..40e1a42b 100644 --- a/zksync_sdk/types/transactions.py +++ b/zksync_sdk/types/transactions.py @@ -398,7 +398,7 @@ class Order(EncodedTx): valid_from: int valid_until: int signature: Optional[TxSignature] = None - ethSignature: Optional[TxEthSignature] = None + eth_signature: Optional[TxEthSignature] = None def tx_type(self) -> int: raise NotImplementedError @@ -449,7 +449,7 @@ def dict(self): "validFrom": self.valid_from, "validUntil": self.valid_until, "signature": self.signature.dict() if self.signature else None, - "ethSignature": self.ethSignature.dict() if self.ethSignature else None, + "ethSignature": self.eth_signature.dict() if self.eth_signature else None, } @dataclass diff --git a/zksync_sdk/wallet.py b/zksync_sdk/wallet.py index fc58eedb..de8fef09 100644 --- a/zksync_sdk/wallet.py +++ b/zksync_sdk/wallet.py @@ -371,7 +371,7 @@ async def get_order(self, token_sell: TokenLike, token_buy: TokenLike, valid_from=valid_from, valid_until=valid_until) - order.ethSignature = self.eth_signer.sign_tx(order) + order.eth_signature = self.eth_signer.sign_tx(order) order.signature = self.zk_signer.sign_tx(order) return order From 945f9e7f21e3db7a0f3a88f35415eeab6fca9976 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Fri, 10 Sep 2021 15:16:18 +0300 Subject: [PATCH 10/12] Fix mypy --- zksync_sdk/wallet.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/zksync_sdk/wallet.py b/zksync_sdk/wallet.py index 52012d01..04c5a72e 100644 --- a/zksync_sdk/wallet.py +++ b/zksync_sdk/wallet.py @@ -103,7 +103,7 @@ async def build_change_pub_key( fee_token: Token, eth_auth_data: Union[ChangePubKeyCREATE2, ChangePubKeyEcdsa, None], fee: int, - nonce: int = None, + nonce: Optional[int] = None, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL): if nonce is None: @@ -132,7 +132,7 @@ async def build_change_pub_key( return change_pub_key, eth_signature - async def forced_exit(self, target: str, token: TokenLike, fee: Decimal = None, + async def forced_exit(self, target: str, token: TokenLike, fee: Optional[Decimal] = None, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL) -> Transaction: nonce = await self.zk_provider.get_account_nonce(self.address()) token_obj = await self.resolve_token(token) @@ -142,7 +142,7 @@ async def forced_exit(self, target: str, token: TokenLike, fee: Decimal = None, else: fee_int = token_obj.from_decimal(fee) - transfer, eth_signature = await self.build_forced_exit(target, token, fee_int, nonce, + transfer, eth_signature = await self.build_forced_exit(target, token_obj, fee_int, nonce, valid_from, valid_until) return await self.send_signed_transaction(transfer, eth_signature) @@ -154,7 +154,7 @@ async def build_forced_exit( target: str, token: Token, fee: int, - nonce: int = None, + nonce: Optional[int] = None, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL) -> Tuple[ForcedExit, TxEthSignature]: if nonce is None: @@ -174,7 +174,7 @@ async def build_forced_exit( return forced_exit, eth_signature - async def mint_nft(self, content_hash: str, recipient: str,token: TokenLike, fee: Decimal = None) -> Transaction: + async def mint_nft(self, content_hash: str, recipient: str,token: TokenLike, fee: Optional[Decimal] = None) -> Transaction: token_obj = await self.resolve_token(token) nonce = await self.zk_provider.get_account_nonce(self.address()) @@ -300,7 +300,7 @@ async def build_transfer( return transfer, eth_signature async def transfer(self, to: str, amount: Decimal, token: TokenLike, - fee: Decimal = None, + fee: Optional[Decimal] = None, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL) -> Transaction: nonce = await self.zk_provider.get_account_nonce(self.address()) token_obj = await self.resolve_token(token) From 68aae53aa455d4cb0bd944b48e0939c40f3cab5d Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Fri, 10 Sep 2021 16:02:15 +0300 Subject: [PATCH 11/12] Fix mypy with up-to-date eth_account --- zksync_sdk/zksync_signer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zksync_sdk/zksync_signer.py b/zksync_sdk/zksync_signer.py index 4f6df2fb..17d761b0 100644 --- a/zksync_sdk/zksync_signer.py +++ b/zksync_sdk/zksync_signer.py @@ -9,8 +9,8 @@ def derive_private_key(library: ZkSyncLibrary, message: str, account: BaseAccoun chain_id: ChainId): if chain_id != ChainId.MAINNET: message = f"{message}\nChain ID: {chain_id}." - message = encode_defunct(message.encode()) - signature = account.sign_message(message) + signable_message = encode_defunct(message.encode()) + signature = account.sign_message(signable_message) private_key = library.private_key_from_seed(signature.signature) return private_key From b2794d87cb5cd2d95b7e28886a6f1856c5f1f39e Mon Sep 17 00:00:00 2001 From: "v.yastrebov90" Date: Fri, 10 Sep 2021 16:39:14 +0300 Subject: [PATCH 12/12] increase test_change_pubkey attempts & timeouts --- tests/test_wallet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_wallet.py b/tests/test_wallet.py index 0620a4bd..513c36f2 100644 --- a/tests/test_wallet.py +++ b/tests/test_wallet.py @@ -62,7 +62,7 @@ async def test_deposit(self): async def test_change_pubkey(self): trans = await self.wallet.set_signing_key("ETH", eth_auth_data=ChangePubKeyEcdsa()) try: - status = await trans.await_committed() + status = await trans.await_committed(attempts=1000, attempts_timeout=1000) self.assertEqual(status, TransactionStatus.COMMITTED) except Exception as ex: assert False, str(ex)