Skip to content

Commit

Permalink
Move compilation to deployer
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Apr 29, 2024
1 parent 161afc5 commit 7a68cad
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 40 deletions.
5 changes: 5 additions & 0 deletions boa_zksync/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import boa

from boa_zksync.deployer import ZksyncDeployer
from boa_zksync.environment import ZksyncEnv
from boa_zksync.node import EraTestNode


def set_zksync_env(url):
boa.set_env(ZksyncEnv.from_url(url))
boa.set_deployer_class(ZksyncDeployer)


def set_zksync_test_env():
boa.set_env(ZksyncEnv(rpc=EraTestNode()))
boa.set_deployer_class(ZksyncDeployer)


def set_zksync_fork(url):
boa.set_env(ZksyncEnv.from_url(url))
boa.set_deployer_class(ZksyncDeployer)
boa.env.fork()


Expand All @@ -22,6 +26,7 @@ def set_zksync_browser_env(address=None):
from boa_zksync.browser import ZksyncBrowserEnv

boa.set_env(ZksyncBrowserEnv(address))
boa.set_deployer_class(ZksyncDeployer)


boa.set_zksync_env = set_zksync_env
Expand Down
54 changes: 38 additions & 16 deletions boa_zksync/deployer.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
from functools import cached_property
from pathlib import Path

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

from boa_zksync.compile import compile_zksync, compile_zksync_source
from boa_zksync.contract import ZksyncContract
from boa_zksync.types import ZksyncCompilerData


class ZksyncDeployer(ABIContractFactory):

def __init__(self, compiler_data: ZksyncCompilerData, filename=None):
super().__init__(compiler_data.contract_name, compiler_data.abi, filename, compiler_data)

@staticmethod
def create_compiler_data(
source_code: str,
contract_name: str = None,
filename: str = None,
compiler_args: dict = None,
**kwargs
) -> ZksyncCompilerData:
if not contract_name:
contract_name = Path(filename).stem if filename else "<anonymous contract>"

if filename:
return compile_zksync(contract_name, filename, compiler_args)
return compile_zksync_source(source_code, contract_name, compiler_args)

def deploy(self, *args, value=0, **kwargs) -> ZksyncContract:
env = Env.get_singleton()
from boa_zksync.environment import ZksyncEnv
Expand All @@ -28,22 +50,6 @@ def deploy(self, *args, value=0, **kwargs) -> ZksyncContract:
)
return self.at(address)

def deploy_as_blueprint(self, *args, **kwargs) -> ZksyncContract:
"""
In zkSync, any contract can be used as a blueprint.
Note that we do need constructor arguments for this.
"""
return self.deploy(*args, **kwargs)

@cached_property
def constructor(self) -> ABIFunction:
"""
Get the constructor function of the contract.
:raises: StopIteration if the constructor is not found.
"""
ctor_abi = next(i for i in self.abi if i["type"] == "constructor")
return ABIFunction(ctor_abi, contract_name=self._name)

def at(self, address: Address | str) -> ZksyncContract:
"""
Create an ABI contract object for a deployed contract at `address`.
Expand All @@ -61,3 +67,19 @@ def at(self, address: Address | str) -> ZksyncContract:
)
env.register_contract(address, contract)
return contract

def deploy_as_blueprint(self, *args, **kwargs) -> ZksyncContract:
"""
In zkSync, any contract can be used as a blueprint.
Note that we do need constructor arguments for this.
"""
return self.deploy(*args, **kwargs)

@cached_property
def constructor(self) -> ABIFunction:
"""
Get the constructor function of the contract.
:raises: StopIteration if the constructor is not found.
"""
ctor_abi = next(i for i in self.abi if i["type"] == "constructor")
return ABIFunction(ctor_abi, contract_name=self._name)
22 changes: 0 additions & 22 deletions boa_zksync/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
from eth.exceptions import VMError
from eth_account import Account

from boa_zksync.compile import compile_zksync, compile_zksync_source
from boa_zksync.deployer import ZksyncDeployer
from boa_zksync.node import EraTestNode
from boa_zksync.types import DeployTransaction, ZksyncComputation, ZksyncMessage

Expand Down Expand Up @@ -202,26 +200,6 @@ def get_code(self, address: Address) -> bytes:
def set_code(self, address: Address, bytecode: bytes):
return self._rpc.fetch("hardhat_setCode", [address, list(bytecode)])

def create_deployer(
self,
source_code: str,
name: str = None,
filename: str = None,
dedent: bool = True,
compiler_args: dict = None,
) -> "ZksyncDeployer":
if not name:
name = Path(filename).stem if filename else "<anonymous contract>"

if filename:
compiler_data = compile_zksync(name, filename, compiler_args)
else:
compiler_data = compile_zksync_source(source_code, name, compiler_args)

return ZksyncDeployer.from_abi_dict(
compiler_data.abi, name, filename, compiler_data
)

def generate_address(self, alias: Optional[str] = None) -> _AddressType:
"""
Generates a new address for the zkSync environment.
Expand Down
10 changes: 8 additions & 2 deletions boa_zksync/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from functools import cached_property

import rlp
from boa.contracts.vyper.vyper_contract import VyperDeployer
from boa.interpret import compiler_data
from boa.rpc import fixup_dict, to_bytes, to_hex
from boa.util.abi import Address
Expand All @@ -10,6 +11,7 @@
from eth_account.datastructures import SignedMessage
from eth_account.messages import encode_typed_data
from rlp.sedes import BigEndianInt, Binary, List
from vyper.compiler import CompilerData

_EIP712_TYPE = bytes.fromhex("71")
_EIP712_TYPES_SPEC = {
Expand Down Expand Up @@ -171,8 +173,12 @@ def global_ctx(self):
return self.vyper.global_ctx

@cached_property
def vyper(self):
return compiler_data(self.source_code, self.contract_name)
def vyper(self) -> CompilerData:
return compiler_data(self.source_code, self.contract_name, VyperDeployer)

@cached_property
def settings(self):
return self.vyper.settings


@dataclass
Expand Down

0 comments on commit 7a68cad

Please sign in to comment.