Skip to content

Commit a5452e9

Browse files
Merge pull request #17 from hukkin/mypy
Introduce mypy type checking
2 parents 9d2ee36 + b2794d8 commit a5452e9

File tree

15 files changed

+142
-131
lines changed

15 files changed

+142
-131
lines changed

Makefile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
SHELL := /bin/bash
22

3-
.PHONY: test test37 test38 test39 mypy coverage
3+
.PHONY: test test38 test39 mypy coverage
44

55
TOX := docker-compose run --rm app tox
66

77
test:
88
$(TOX)
99

1010

11-
test37:
12-
$(TOX) -e py37
13-
1411
test38:
1512
$(TOX) -e py38
1613

@@ -27,4 +24,4 @@ coverage: .coverage
2724
docker-compose run --rm app coverage report
2825

2926
coverage.xml: .coverage
30-
docker-compose run --rm app coverage xml
27+
docker-compose run --rm app coverage xml

setup.cfg

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ install_requires =
1313
web3 >= 5.16
1414
httpx >= 0.16
1515
pydantic >= 1.7
16-
16+
python_requires = >=3.8
1717
setup_requires =
1818
setuptools_scm>=3.5.0
1919

@@ -36,8 +36,21 @@ zksync_sdk.contract_abi =
3636
[tox:tox]
3737
envlist = py{38,39},mypy
3838

39-
[testenv]
39+
[testenv:py{38,39}]
4040
deps = coverage
41-
extras = test
4241
setenv = ZK_SYNC_LIBRARY_PATH=/lib/zks-crypto-linux-x64.so
4342
commands = coverage run -m unittest
43+
44+
[testenv:mypy]
45+
extras = test
46+
commands = mypy .
47+
48+
[mypy]
49+
show_error_codes = True
50+
no_implicit_optional = True
51+
52+
[mypy-setuptools.*]
53+
ignore_missing_imports = True
54+
55+
[mypy-eth_account.*]
56+
ignore_missing_imports = True

tests/test_wallet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async def test_deposit(self):
6262
async def test_change_pubkey(self):
6363
trans = await self.wallet.set_signing_key("ETH", eth_auth_data=ChangePubKeyEcdsa())
6464
try:
65-
status = await trans.await_committed()
65+
status = await trans.await_committed(attempts=1000, attempts_timeout=1000)
6666
self.assertEqual(status, TransactionStatus.COMMITTED)
6767
except Exception as ex:
6868
assert False, str(ex)

zksync_sdk/contract_utils.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1+
import importlib.resources as pkg_resources
12
import json
23

3-
try:
4-
import importlib.resources as pkg_resources
5-
except ImportError:
6-
# Try backported to PY<37 `importlib_resources`.
7-
import importlib_resources as pkg_resources
84
from . import contract_abi
95

106
zksync_abi_cache = None

zksync_sdk/ethereum_provider.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from decimal import Decimal
2+
from typing import Optional
23

34
from web3 import Web3
45

@@ -13,7 +14,7 @@ def __init__(self, web3: Web3, zksync: ZkSync):
1314
self.web3 = web3
1415
self.zksync = zksync
1516

16-
async def approve_deposit(self, token: Token, limit: Decimal = None):
17+
async def approve_deposit(self, token: Token, limit: Decimal):
1718
contract = ERC20Contract(self.web3, self.zksync.contract_address, token.address,
1819
self.zksync.account)
1920
return contract.approve_deposit(token.from_decimal(limit))
@@ -30,7 +31,7 @@ async def full_exit(self, token: Token, account_id: int):
3031
async def set_auth_pubkey_hash(self, pubkey_hash: bytes, nonce: int):
3132
return self.zksync.set_auth_pub_key_hash(pubkey_hash, nonce)
3233

33-
async def is_deposit_approved(self, token: Token, threshold: int = None) -> bool:
34+
async def is_deposit_approved(self, token: Token, threshold: int) -> bool:
3435
contract = ERC20Contract(self.web3, self.zksync.contract_address, token.address,
3536
self.zksync.account)
3637
return contract.is_deposit_approved(threshold)

zksync_sdk/lib.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ctypes
22
from ctypes import (Structure, c_ubyte, cdll)
33
import os
4+
from typing import Optional
45

56
PRIVATE_KEY_LEN = 32
67
PUBLIC_KEY_LEN = 32
@@ -48,9 +49,9 @@ class ZksOrders(Structure):
4849

4950
class ZkSyncLibrary:
5051

51-
def __init__(self, library_path: str = None):
52+
def __init__(self, library_path: Optional[str] = None):
5253
if library_path is None:
53-
library_path = os.getenv("ZK_SYNC_LIBRARY_PATH")
54+
library_path = os.environ["ZK_SYNC_LIBRARY_PATH"]
5455
self.lib = cdll.LoadLibrary(library_path)
5556

5657
def private_key_from_seed(self, seed: bytes):
@@ -68,17 +69,17 @@ def get_public_key(self, private_key: bytes):
6869
def get_pubkey_hash(self, public_key: bytes):
6970
assert len(public_key) == PUBLIC_KEY_LEN
7071
public_key_hash = ctypes.pointer(ZksPubkeyHash())
71-
public_key = ctypes.pointer(
72+
public_key_ptr = ctypes.pointer(
7273
ZksPackedPublicKey(data=(c_ubyte * PUBLIC_KEY_LEN)(*public_key)))
73-
self.lib.zks_crypto_public_key_to_pubkey_hash(public_key, public_key_hash)
74+
self.lib.zks_crypto_public_key_to_pubkey_hash(public_key_ptr, public_key_hash)
7475
return bytes(public_key_hash.contents.data)
7576

7677
def sign(self, private_key: bytes, message: bytes):
7778
assert len(private_key) == PRIVATE_KEY_LEN
7879
signature = ctypes.pointer(ZksSignature())
79-
private_key = ctypes.pointer(
80+
private_key_ptr = ctypes.pointer(
8081
ZksPrivateKey(data=(c_ubyte * PRIVATE_KEY_LEN)(*private_key)))
81-
self.lib.zks_crypto_sign_musig(private_key, message, len(message), signature)
82+
self.lib.zks_crypto_sign_musig(private_key_ptr, message, len(message), signature)
8283
return bytes(signature.contents.data)
8384

8485
def hash_orders(self, orders: bytes):

zksync_sdk/serializers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ def remove_address_prefix(address: str) -> str:
203203
if address.startswith('sync:'):
204204
return address[5:]
205205

206+
return address
207+
206208

207209
def serialize_address(address: str) -> bytes:
208210
address = remove_address_prefix(address)

zksync_sdk/transport/http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from http.client import OK
2-
from typing import List
2+
from typing import List, Optional
33

44
import httpx
55

@@ -11,7 +11,7 @@ class HttpJsonRPCTransport(JsonRPCTransport):
1111
def __init__(self, network: Network):
1212
self.network = network
1313

14-
async def request(self, method: str, params: List):
14+
async def request(self, method: str, params: Optional[List]):
1515
async with httpx.AsyncClient() as client:
1616
response = await client.post(self.network.zksync_url,
1717
json=self.create_request(method, params))

zksync_sdk/types/responses.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Config:
5858
alias_generator = to_camel
5959

6060
def get_nonce(self) -> int:
61+
assert self.committed is not None, "`get_nonce` needs `committed` to be set"
6162
return self.committed.nonce
6263

6364

zksync_sdk/types/transactions.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def find_by_symbol(self, symbol: str) -> Optional[Token]:
127127
else:
128128
return None
129129

130-
def find(self, token: TokenLike) -> Token:
130+
def find(self, token: TokenLike) -> Optional[Token]:
131131
result = None
132132
if isinstance(token, int):
133133
result = self.find_by_id(token)
@@ -167,9 +167,9 @@ class ChangePubKey(EncodedTx):
167167
nonce: int
168168
valid_from: int
169169
valid_until: int
170-
eth_auth_data: Union[ChangePubKeyCREATE2, ChangePubKeyEcdsa] = None
171-
eth_signature: TxEthSignature = None
172-
signature: TxSignature = None
170+
eth_auth_data: Union[ChangePubKeyCREATE2, ChangePubKeyEcdsa, None] = None
171+
eth_signature: Optional[TxEthSignature] = None
172+
signature: Optional[TxSignature] = None
173173

174174
def human_readable_message(self) -> str:
175175
message = f"Set signing key: {self.new_pk_hash.replace('sync:', '').lower()}"
@@ -240,7 +240,7 @@ class Transfer(EncodedTx):
240240
nonce: int
241241
valid_from: int
242242
valid_until: int
243-
signature: TxSignature = None
243+
signature: Optional[TxSignature] = None
244244

245245
def tx_type(self) -> int:
246246
return 5
@@ -297,7 +297,7 @@ class Withdraw(EncodedTx):
297297
valid_from: int
298298
valid_until: int
299299
token: Token
300-
signature: TxSignature = None
300+
signature: Optional[TxSignature] = None
301301

302302
def tx_type(self) -> int:
303303
return 3
@@ -351,7 +351,7 @@ class ForcedExit(EncodedTx):
351351
nonce: int
352352
valid_from: int
353353
valid_until: int
354-
signature: TxSignature = None
354+
signature: Optional[TxSignature] = None
355355

356356
def tx_type(self) -> int:
357357
return 8
@@ -387,7 +387,7 @@ def dict(self):
387387
}
388388

389389
@dataclass
390-
class Order:
390+
class Order(EncodedTx):
391391
account_id: int
392392
recipient: str
393393
nonce: int
@@ -397,8 +397,11 @@ class Order:
397397
ratio: Fraction
398398
valid_from: int
399399
valid_until: int
400-
signature: TxSignature = None
401-
ethSignature: TxEthSignature = None
400+
signature: Optional[TxSignature] = None
401+
eth_signature: Optional[TxEthSignature] = None
402+
403+
def tx_type(self) -> int:
404+
raise NotImplementedError
402405

403406
def msg_type(self) -> int:
404407
return b'o'[0]
@@ -446,7 +449,7 @@ def dict(self):
446449
"validFrom": self.valid_from,
447450
"validUntil": self.valid_until,
448451
"signature": self.signature.dict() if self.signature else None,
449-
"ethSignature": self.ethSignature.dict() if self.ethSignature else None,
452+
"ethSignature": self.eth_signature.dict() if self.eth_signature else None,
450453
}
451454

452455
@dataclass
@@ -458,7 +461,7 @@ class Swap(EncodedTx):
458461
fee_token: Token
459462
fee: int
460463
nonce: int
461-
signature: TxSignature = None
464+
signature: Optional[TxSignature] = None
462465

463466
def tx_type(self) -> int:
464467
return 11
@@ -511,7 +514,7 @@ class MintNFT(EncodedTx):
511514
fee: int
512515
fee_token: Token
513516
nonce: int
514-
signature: TxSignature = None
517+
signature: Optional[TxSignature] = None
515518

516519
def tx_type(self) -> int:
517520
return 9
@@ -557,7 +560,7 @@ class WithdrawNFT(EncodedTx):
557560
valid_from: int
558561
valid_until: int
559562
token_id: int
560-
signature: TxSignature = None
563+
signature: Optional[TxSignature] = None
561564

562565
def tx_type(self) -> int:
563566
return 10

0 commit comments

Comments
 (0)