Skip to content

Commit

Permalink
Merge pull request #2 from zksync-sdk/v0.1.0
Browse files Browse the repository at this point in the history
V0.1.0
  • Loading branch information
vyastrebovvareger authored Dec 14, 2022
2 parents f8c3b58 + 7cd19f6 commit e93a25d
Show file tree
Hide file tree
Showing 9 changed files with 709 additions and 490 deletions.
519 changes: 357 additions & 162 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "zksync2"
version = "0.0.2"
version = "0.1.0"
authors = [
{ name="Viktor Yastrebov", email="[email protected]" },
]
Expand Down
6 changes: 5 additions & 1 deletion tests/contracts/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import importlib.resources as pkg_resources
import json

from eth_typing import HexStr

from tests import contracts
from eth_utils import remove_0x_prefix


def get_binary(name: str) -> bytes:
Expand All @@ -15,7 +19,7 @@ def get_hex_binary(name: str) -> bytes:
with p.open(mode='r') as contact_file:
lines = contact_file.readlines()
data = "".join(lines)
return bytes.fromhex(data)
return bytes.fromhex(remove_0x_prefix(HexStr(data)))


def get_abi(name: str):
Expand Down
386 changes: 177 additions & 209 deletions tests/test_zksync_web3.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion zksync2/manage_contracts/erc20_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ def __init__(self, web3_eth: Web3, abi: Optional[dict] = None):
abi = _erc_20_abi_default()
self.contract = web3_eth.eth.contract(address=None, abi=abi)

def encode_method(self, fn_name, args):
def encode_method(self, fn_name, args) -> HexStr:
return self.contract.encodeABI(fn_name=fn_name, args=args)
111 changes: 11 additions & 100 deletions zksync2/module/request_types.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from enum import Enum
from dataclasses import dataclass
from typing import TypedDict, List, Optional
from typing import List, Optional
from web3._utils.compat import (
TypedDict,
)

from eth_typing import HexStr
from web3 import Web3
from web3.types import AccessList
from zksync2.core.types import PaymasterParams
from zksync2.manage_contracts.contract_deployer import ContractDeployer
from zksync2.manage_contracts.deploy_addresses import ZkSyncAddresses
from enum import Enum


@dataclass
Expand All @@ -19,111 +20,21 @@ class EIP712Meta:
paymaster_params: Optional[PaymasterParams] = None


Transaction = TypedDict(
"Transaction",
{
Transaction = TypedDict("Transaction", {
"chain_id": int,
"nonce": int,
"from": HexStr,
"to": HexStr,
"gas": int,
"gasPrice": int,
"maxPriorityFeePerGas": int,
"value": int,
"data": HexStr,
"transactionType": int,
"accessList": Optional[AccessList],
"eip712Meta": EIP712Meta,
}, total=False)
}, total=False)


class TransactionType(Enum):
EIP_712_TX_TYPE = 113


def create_function_call_transaction(from_: HexStr,
to: HexStr,
ergs_price: int,
ergs_limit: int,
data: HexStr,
value: int = 0):
eip712_meta_default = EIP712Meta()
tx: Transaction = {
"from": from_,
"to": to,
"gas": ergs_limit,
"gasPrice": ergs_price,
"value": value,
"data": data,
"transactionType": TransactionType.EIP_712_TX_TYPE.value,
"eip712Meta": eip712_meta_default
}
return tx


def create2_contract_transaction(web3: Web3,
from_: HexStr,
ergs_price: int,
ergs_limit: int,
bytecode: bytes,
deps: List[bytes] = None,
call_data: Optional[bytes] = None,
value: int = 0,
salt: Optional[bytes] = None):
contract_deployer = ContractDeployer(web3)
call_data = contract_deployer.encode_create2(bytecode=bytecode,
call_data=call_data,
salt=salt)
factory_deps = []
if deps is not None:
for dep in deps:
factory_deps.append(dep)
factory_deps.append(bytecode)

eip712_meta = EIP712Meta(ergs_per_pub_data=EIP712Meta.ERGS_PER_PUB_DATA_DEFAULT,
custom_signature=None,
factory_deps=factory_deps,
paymaster_params=None)
tx: Transaction = {
"from": from_,
"to": Web3.toChecksumAddress(ZkSyncAddresses.CONTRACT_DEPLOYER_ADDRESS.value),
"gas": ergs_limit,
"gasPrice": ergs_price,
"value": value,
"data": HexStr(call_data),
"transactionType": TransactionType.EIP_712_TX_TYPE.value,
"eip712Meta": eip712_meta
}
return tx


def create_contract_transaction(web3: Web3,
from_: HexStr,
ergs_price: int,
ergs_limit: int,
bytecode: bytes,
deps: List[bytes] = None,
call_data: Optional[bytes] = None,
value: int = 0,
salt: Optional[bytes] = None):
contract_deployer = ContractDeployer(web3)
call_data = contract_deployer.encode_create(bytecode=bytecode,
call_data=call_data,
salt_data=salt)
factory_deps = []
if deps is not None:
for dep in deps:
factory_deps.append(dep)
factory_deps.append(bytecode)
eip712_meta = EIP712Meta(ergs_per_pub_data=EIP712Meta.ERGS_PER_PUB_DATA_DEFAULT,
custom_signature=None,
factory_deps=factory_deps,
paymaster_params=None)
tx: Transaction = {
"from": from_,
"to": Web3.toChecksumAddress(ZkSyncAddresses.CONTRACT_DEPLOYER_ADDRESS.value),
"gas": ergs_limit,
"gasPrice": ergs_price,
"value": value,
"data": HexStr(call_data),
"transactionType": TransactionType.EIP_712_TX_TYPE.value,
"eip712Meta": eip712_meta
}
return tx
16 changes: 4 additions & 12 deletions zksync2/module/zksync_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

zks_estimate_fee_rpc = RPCEndpoint("zks_estimateFee")
zks_main_contract_rpc = RPCEndpoint("zks_getMainContract")
zks_get_l1_withdraw_tx_rpc = RPCEndpoint("zks_getL1WithdrawalTx")
zks_get_confirmed_tokens_rpc = RPCEndpoint("zks_getConfirmedTokens")
zks_get_token_price_rpc = RPCEndpoint("zks_getTokenPrice")
zks_l1_chain_id_rpc = RPCEndpoint("zks_L1ChainId")
Expand Down Expand Up @@ -76,12 +75,13 @@ def meta_formatter(eip712: EIP712Meta) -> dict:
'from': to_checksum_address,
'gas': to_hex_if_integer,
'gasPrice': to_hex_if_integer,
'maxPriorityFeePerGas': to_hex_if_integer,
'nonce': to_hex_if_integer,
'to': to_checksum_address,
'value': to_hex_if_integer,
'chainId': to_hex_if_integer,
'transactionType': to_hex_if_integer,
'eip712Meta': meta_formatter
'eip712Meta': meta_formatter,
}

zks_transaction_request_formatter = apply_formatters_to_dict(ZKS_TRANSACTION_PARAMS_FORMATTERS)
Expand Down Expand Up @@ -182,11 +182,6 @@ class ZkSync(Eth, ABC):
mungers=None
)

_zks_get_l1_withdraw_tx: Method[Callable[[L2WithdrawTxHash], TransactionHash]] = Method(
zks_get_l1_withdraw_tx_rpc,
mungers=[default_root_munger]
)

_zks_get_confirmed_tokens: Method[Callable[[From, Limit], ZksTokens]] = Method(
zks_get_confirmed_tokens_rpc,
mungers=[default_root_munger],
Expand Down Expand Up @@ -221,7 +216,7 @@ class ZkSync(Eth, ABC):
request_formatters=zksync_get_request_formatters
)

_eth_estimate_gas: Method[Callable[[Transaction], str]] = Method(
_eth_estimate_gas: Method[Callable[[Transaction], int]] = Method(
eth_estimate_gas_rpc,
mungers=[default_root_munger],
request_formatters=zksync_get_request_formatters
Expand Down Expand Up @@ -258,9 +253,6 @@ def zks_estimate_fee(self, transaction: Transaction) -> Fee:
def zks_main_contract(self) -> HexStr:
return self._zks_main_contract()

def zks_get_l1_withdraw_tx(self, withdraw_hash: L2WithdrawTxHash) -> TransactionHash:
return self._zks_get_l1_withdraw_tx(withdraw_hash)

def zks_get_confirmed_tokens(self, offset: From, limit: Limit) -> List[Token]:
return self._zks_get_confirmed_tokens(offset, limit)

Expand All @@ -286,7 +278,7 @@ def zks_get_l2_to_l1_msg_proof(self,
def zks_get_testnet_paymaster_address(self) -> HexStr:
return self._zks_get_testnet_paymaster_address()

def eth_estimate_gas(self, tx: Transaction) -> str:
def eth_estimate_gas(self, tx: Transaction) -> int:
return self._eth_estimate_gas(tx)

def wait_for_transaction_receipt(self,
Expand Down
4 changes: 2 additions & 2 deletions zksync2/signer/eth_signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from abc import abstractmethod, ABC
from eip712_structs import make_domain, EIP712Struct
from eth_account.datastructures import SignedMessage
from eth_account.signers.base import BaseAccount
from eth_typing import ChecksumAddress, HexStr
from eth_utils import keccak
from eth_account.signers.local import LocalAccount
from eth_account.messages import encode_defunct, SignableMessage


Expand All @@ -23,7 +23,7 @@ class PrivateKeyEthSigner(EthSignerBase, ABC):
_NAME = "zkSync"
_VERSION = "2"

def __init__(self, creds: LocalAccount, chain_id: int):
def __init__(self, creds: BaseAccount, chain_id: int):
self.credentials = creds
self.chain_id = chain_id
self.default_domain = make_domain(name=self._NAME,
Expand Down
Loading

0 comments on commit e93a25d

Please sign in to comment.