Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Apr 11, 2024
1 parent 4f10378 commit 4859f38
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 54 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ mypy:
--follow-imports=silent \
--ignore-missing-imports \
--implicit-optional \
-p vyper
-p boa_zksync

black:
black -C -t py311 vyper/ tests/ setup.py --force-exclude=vyper/version.py
black -C -t py311 boa_zksync/ tests/

flake8: black
flake8 vyper/ tests/
flake8 boa_zksync/ tests/

isort: black
isort vyper/ tests/ setup.py
isort boa_zksync/ tests/ setup.py

build:
pip install .
Expand Down
41 changes: 27 additions & 14 deletions boa_zksync/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,35 @@
from collections import namedtuple
from shutil import which

ZksyncCompilerData = namedtuple("ZksyncCompilerData", [
'method_identifiers', 'abi', 'bytecode', 'bytecode_runtime', 'warnings', 'factory_deps',
])
ZksyncCompilerData = namedtuple(
"ZksyncCompilerData",
[
"method_identifiers",
"abi",
"bytecode",
"bytecode_runtime",
"warnings",
"factory_deps",
],
)


def compile_zksync(file_name: str, compiler_args = None) -> ZksyncCompilerData:
output = json.loads(_call_zkvyper(
# make sure zkvyper uses the same vyper as boa
"--vyper", which("vyper"),
# request JSON output
"-f", "combined_json",
# pass any extra compiler args
*(compiler_args or []),
# pass the file name
"--", file_name,
))
def compile_zksync(file_name: str, compiler_args=None) -> ZksyncCompilerData:
output = json.loads(
_call_zkvyper(
# make sure zkvyper uses the same vyper as boa
"--vyper",
which("vyper"),
# request JSON output
"-f",
"combined_json",
# pass any extra compiler args
*(compiler_args or []),
# pass the file name
"--",
file_name,
)
)
return ZksyncCompilerData(**output[file_name])


Expand Down
15 changes: 11 additions & 4 deletions boa_zksync/deployer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from functools import cached_property

from boa import Env
from boa.contracts.abi.abi_contract import ABIContractFactory, ABIFunction, ABIContract
from boa.contracts.abi.abi_contract import ABIContract, ABIContractFactory, ABIFunction
from boa.util.abi import Address

from boa_zksync.compile import ZksyncCompilerData
Expand All @@ -13,7 +13,8 @@ def __init__(self, compiler_data: ZksyncCompilerData, name: str, filename: str):
name,
compiler_data.abi,
functions=[
ABIFunction(item, name) for item in compiler_data.abi
ABIFunction(item, name)
for item in compiler_data.abi
if item.get("type") == "function"
],
filename=filename,
Expand All @@ -24,9 +25,15 @@ def deploy(self, *args, value=0, **kwargs):
env = Env.get_singleton()

initcode = bytes.fromhex(self.compiler_data.bytecode.removeprefix("0x"))
constructor_calldata = self.constructor.prepare_calldata(*args, **kwargs) if args or kwargs else b""
constructor_calldata = (
self.constructor.prepare_calldata(*args, **kwargs)
if args or kwargs
else b""
)

address, _ = env.deploy_code(bytecode=initcode, value=value, constructor_calldata=constructor_calldata)
address, _ = env.deploy_code(
bytecode=initcode, value=value, constructor_calldata=constructor_calldata
)
return ABIContract(
self._name,
self.abi,
Expand Down
52 changes: 35 additions & 17 deletions boa_zksync/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@
from eth_account import Account
from eth_account.datastructures import SignedMessage
from eth_account.messages import encode_defunct
from rlp.sedes import Binary, BigEndianInt, List
from rlp.sedes import BigEndianInt, Binary, List
from vyper.utils import keccak256

from boa_zksync.rpc import ZksyncRPC

CONTRACT_DEPLOYER_ADDRESS = "0x0000000000000000000000000000000000008006"
with open(Path(__file__).parent / "IContractDeployer.json") as f:
CONTRACT_DEPLOYER = ABIContractFactory.from_abi_dict(json.load(f), "ContractDeployer", )
CONTRACT_DEPLOYER = ABIContractFactory.from_abi_dict(
json.load(f), "ContractDeployer"
)

_EIP712_TYPE = bytes.fromhex("71")
_EIP712_DOMAIN_ABI_TYPE = "EIP712Domain(string name,string version,uint256 chainId)"
_EIP712_DOMAIN_ABI_TYPE_SPEC = [
{"name": "name", "type": "string"},
{"name": "version", "type": "string"},
{"name": "chainId", "type": "uint256"}
{"name": "chainId", "type": "uint256"},
]
_EIP712_TRANSACTION_ABI_TYPE = (
"Transaction(uint256 txType,uint256 from,uint256 to,uint256 gasLimit,uint256 "
Expand All @@ -44,7 +46,7 @@
{"name": "value", "type": "uint256"},
{"name": "data", "type": "bytes"},
{"name": "factoryDeps", "type": "bytes32[]"},
{"name": "paymasterInput", "type": "bytes"}
{"name": "paymasterInput", "type": "bytes"},
]
_EIP712_TYPES_SPEC = {"Transaction": _EIP712_TRANSACTION_SPEC}
_GAS_PER_PUB_DATA_DEFAULT = 50000
Expand All @@ -55,20 +57,32 @@ def __init__(self, rpc: ZksyncRPC, accounts: dict[str, Account] = None):
super().__init__(rpc, accounts)
self.contract_deployer = CONTRACT_DEPLOYER.at(CONTRACT_DEPLOYER_ADDRESS)

def deploy_code(self, sender=None, gas=None, value=0, bytecode=b"", constructor_calldata=b"", salt=b"\0" * 32,
**kwargs):
def deploy_code(
self,
sender=None,
gas=None,
value=0,
bytecode=b"",
constructor_calldata=b"",
salt=b"\0" * 32,
**kwargs,
):
bytecode_hash = _hash_code(bytecode)
calldata = self.contract_deployer.create.prepare_calldata(salt, bytecode_hash, constructor_calldata)
calldata = self.contract_deployer.create.prepare_calldata(
salt, bytecode_hash, constructor_calldata
)

sender = str(Address(sender or self.eoa))
gas = gas or 0 # unknown at this state
account = self._accounts[sender]

nonce, chain_id, gas_price = self._rpc.fetch_multi([
("eth_getTransactionCount", [sender, "latest"]),
("eth_chainId", []),
("eth_gasPrice", [])
])
nonce, chain_id, gas_price = self._rpc.fetch_multi(
[
("eth_getTransactionCount", [sender, "latest"]),
("eth_chainId", []),
("eth_gasPrice", []),
]
)
chain_id = int(chain_id, 16)
gas_price = int(gas_price, 16)

Expand All @@ -86,7 +100,7 @@ def deploy_code(self, sender=None, gas=None, value=0, bytecode=b"", constructor_
"eip712Meta": {
"gasPerPubdata": f"0x{_GAS_PER_PUB_DATA_DEFAULT:0x}",
"factoryDeps": [[int(b) for b in bytecode]],
}
},
}

estimated_gas = self._rpc.fetch("eth_estimateGas", [tx_data])
Expand Down Expand Up @@ -190,7 +204,7 @@ def _sign_typed_data(account, estimated_gas, tx_data) -> SignedMessage:
],
"paymaster": 0,
"paymasterInput": b"",
}
},
}
if hasattr(account, "sign_typed_data"):
return account.sign_typed_data(full_message=typed_data)
Expand All @@ -200,8 +214,12 @@ def _sign_typed_data(account, estimated_gas, tx_data) -> SignedMessage:
encoded_body = _encode_struct(_EIP712_TRANSACTION_SPEC, typed_data["message"])
msg = [
b"\x19\x01",
keccak256(keccak256(_EIP712_DOMAIN_ABI_TYPE.encode()) + b"".join(encoded_domain)),
keccak256(keccak256(_EIP712_TRANSACTION_ABI_TYPE.encode()) + b"".join(encoded_body))
keccak256(
keccak256(_EIP712_DOMAIN_ABI_TYPE.encode()) + b"".join(encoded_domain)
),
keccak256(
keccak256(_EIP712_TRANSACTION_ABI_TYPE.encode()) + b"".join(encoded_body)
),
]
singable_message = encode_defunct(b"".join(msg))
msg_hash = keccak256(singable_message.body)
Expand Down Expand Up @@ -229,7 +247,7 @@ def _hash_code(bytecode: bytes) -> bytes:
bytecode_size = int(bytecode_len / 32)
if bytecode_len % 32 != 0:
raise RuntimeError("Bytecode length in 32-byte words must be odd")
if bytecode_size > 2 ** 16:
if bytecode_size > 2**16:
raise OverflowError("hash_byte_code, bytecode length must be less than 2^16")
bytecode_hash = sha256(bytecode).digest()
encoded_len = bytecode_size.to_bytes(2, byteorder="big")
Expand Down
14 changes: 3 additions & 11 deletions boa_zksync/interpret.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from tempfile import TemporaryDirectory

from boa.contracts.abi.abi_contract import ABIContract

from boa_zksync.compile import compile_zksync
from boa_zksync.deployer import ZksyncDeployer

Expand All @@ -12,22 +13,13 @@ def load_zksync(filename: str, *args, compiler_args=None, **kwargs) -> ABIContra
return deployer.deploy(*args, **kwargs)


def loads_zksync(
source_code,
*args,
name=None,
compiler_args=None,
**kwargs,
):
def loads_zksync(source_code, *args, name=None, compiler_args=None, **kwargs):
d = loads_zksync_partial(source_code, name, compiler_args=compiler_args)
return d.deploy(*args, **kwargs)


def loads_zksync_partial(
source_code: str,
name: str = None,
dedent: bool = True,
compiler_args: dict = None,
source_code: str, name: str = None, dedent: bool = True, compiler_args: dict = None
) -> ZksyncDeployer:
name = name or "VyperContract" # TODO handle this upstream in CompilerData
if dedent:
Expand Down
4 changes: 2 additions & 2 deletions boa_zksync/ipython.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from IPython.core.magic import magics_class, line_cell_magic, cell_magic, line_magic, Magics
from IPython.core.magic import Magics, cell_magic, line_cell_magic, line_magic, magics_class

from boa_zksync.interpret import eval_zksync, loads_zksync_partial, loads_zksync
from boa_zksync.interpret import eval_zksync, loads_zksync, loads_zksync_partial


@magics_class
Expand Down
2 changes: 1 addition & 1 deletion boa_zksync/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ZksyncRPC(EthereumRPC):

_disabled_methods = {
# zkSync Era does nothing with the max fee parameters.
"maxPriorityFeePerGas": 0,
"maxPriorityFeePerGas": 0
}

def fetch_uncached(self, method, params):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_zksync.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ def stop_subprocess(proc: Popen[bytes]):

@pytest.fixture(scope="module")
def account():
return Account.from_key("0x3d3cbc973389cb26f657686445bcc75662b415b656078503592ac8c1abb8810e")
return Account.from_key(
"0x3d3cbc973389cb26f657686445bcc75662b415b656078503592ac8c1abb8810e"
)


@pytest.fixture(scope="module", autouse=True)
Expand Down

0 comments on commit 4859f38

Please sign in to comment.