Skip to content

Commit

Permalink
Merge pull request #6 from DanielSchiavini/revert-5-revert-4-main
Browse files Browse the repository at this point in the history
Update to vyper 0.4
  • Loading branch information
DanielSchiavini authored Aug 14, 2024
2 parents 521d515 + c465c16 commit 2b01517
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 46 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ constructor_args, address = [], "0x1234..."

boa_zksync.set_zksync_test_env() # configure the environment, see previous section

# Compile a contract from source file
boa_zksync.ZksyncDeployer.create_compiler_data("source code")

# Load a contract from source code and deploy
boa.loads("contract source code", *constructor_args)

Expand Down
6 changes: 2 additions & 4 deletions boa_zksync/compiler_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import textwrap

import vyper.ast as vy_ast
from vyper.ast.utils import parse_to_ast
from vyper.ast.parse import parse_to_ast
from vyper.exceptions import InvalidType
from vyper.semantics.analysis.utils import get_exact_type_from_node

Expand Down Expand Up @@ -65,9 +65,7 @@ def __boa_debug__() {return_sig}:


def detect_expr_type(source_code, contract):
ast = parse_to_ast(source_code)
vy_ast.folding.fold(ast)
ast = ast.body[0]
ast = parse_to_ast(source_code).body[0]
if isinstance(ast, vy_ast.Expr):
with contract.override_vyper_namespace():
try:
Expand Down
7 changes: 3 additions & 4 deletions boa_zksync/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ def internal(self):
def internal():
return None

for fn in self.compiler_data.global_ctx.functions:
typ = fn._metadata["type"]
if typ.is_internal:
setattr(internal, fn.name, ZksyncInternalFunction(typ, self))
for fn_name, fn in self.compiler_data.global_ctx.functions.items():
if fn.is_internal:
setattr(internal, fn_name, ZksyncInternalFunction(fn, self))
return internal

def get_logs(self):
Expand Down
35 changes: 19 additions & 16 deletions boa_zksync/deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from boa import Env
from boa.contracts.abi.abi_contract import ABIContractFactory, ABIFunction
from boa.util.abi import Address
from vyper.compiler import CompilerData

from boa_zksync.compile import compile_zksync, compile_zksync_source
from boa_zksync.contract import ZksyncContract
Expand All @@ -16,32 +17,33 @@

class ZksyncDeployer(ABIContractFactory):

def __init__(self, compiler_data: ZksyncCompilerData, filename=None):
super().__init__(compiler_data.contract_name, compiler_data.abi, filename)
self.compiler_data = compiler_data
def __init__(self, compiler_data: CompilerData, filename=None):
contract_name = Path(compiler_data.contract_path).stem
self.zkvyper_data = self._compile(compiler_data, contract_name, filename)
super().__init__(
contract_name, self.zkvyper_data.abi, compiler_data.contract_path
)

@staticmethod
def create_compiler_data(
source_code: str,
contract_name: str = None,
filename: str = None,
def _compile(
compiler_data: CompilerData,
contract_name: str,
filename: str,
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)
if filename in ("", None, "<unknown>"):
return compile_zksync_source(
compiler_data.file_input.source_code, contract_name, compiler_args
)
return compile_zksync(contract_name, filename, compiler_args)

@classmethod
def from_abi_dict(cls, abi, name="<anonymous contract>", filename=None):
raise NotImplementedError("ZksyncDeployer does not support loading from ABI")

def deploy(self, *args, value=0, **kwargs) -> ZksyncContract:
address, _ = self.env.deploy_code(
bytecode=self.compiler_data.bytecode,
bytecode=self.zkvyper_data.bytecode,
value=value,
constructor_calldata=(
self.constructor.prepare_calldata(*args, **kwargs)
Expand All @@ -57,7 +59,7 @@ def at(self, address: Address | str) -> ZksyncContract:
"""
address = Address(address)
contract = ZksyncContract(
self.compiler_data,
self.zkvyper_data,
self._name,
self.abi,
self.functions,
Expand Down Expand Up @@ -92,6 +94,7 @@ def env(self) -> "ZksyncEnv":
"""
env = Env.get_singleton()
from boa_zksync.environment import ZksyncEnv

assert isinstance(
env, ZksyncEnv
), "ZksyncDeployer can only be used in zkSync environments"
Expand Down
13 changes: 7 additions & 6 deletions boa_zksync/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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, Settings
from vyper.compiler import CompilerData
from vyper.compiler.settings import OptimizationLevel

_EIP712_TYPE = bytes.fromhex("71")
Expand Down Expand Up @@ -181,14 +181,15 @@ def global_ctx(self):

@cached_property
def vyper(self) -> CompilerData:
# TODO: Return the compile_data already created by boa.
return compiler_data(
self.source_code, self.contract_name, VyperDeployer, settings=self.settings
self.source_code,
self.contract_name,
"<unknown>",
VyperDeployer,
optimize=OptimizationLevel.NONE,
)

@cached_property
def settings(self):
return Settings(optimize=OptimizationLevel.NONE)


@dataclass
class ZksyncMessage:
Expand Down
14 changes: 10 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
[project]
name = "titanoboa-zksync"
version = "0.1.0"
version = "0.2.0"
description = "A Zksync plugin for the Titanoboa Vyper interpreter"
license = { file = "LICENSE" }
readme = "README.md"
keywords = ["ethereum", "evm", "smart contract", "development", "vyper", "zksync"]
keywords = [
"ethereum",
"evm",
"smart contract",
"development",
"vyper",
"zksync",
]
classifiers = ["Topic :: Software Development"]

# Requirements
dependencies = ["titanoboa>=0.1,<0.2"]
dependencies = ["titanoboa>=0.2.0"]

[project.optional-dependencies]
forking-recommended = ["ujson"]
Expand Down
18 changes: 9 additions & 9 deletions tests/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def simple_contract(zksync_env):
totalSupply: public(uint256)
balances: HashMap[address, uint256]
@external
@deploy
def __init__(t: uint256):
self.totalSupply = t
self.balances[self] = t
Expand All @@ -39,7 +39,7 @@ def test_blueprint(zksync_env):
blueprint_code = """
val: public(uint256)
@external
@deploy
def __init__(val: uint256):
self.val = val
Expand Down Expand Up @@ -74,7 +74,7 @@ def test_blueprint_immutable(zksync_env):
blueprint_code = """
VAL: immutable(uint256)
@external
@deploy
def __init__(val: uint256):
VAL = val
Expand Down Expand Up @@ -140,7 +140,7 @@ def name() -> String[32]: view
@external
@view
def get_name_of(addr: HasName) -> String[32]:
return addr.name()
return staticcall addr.name()
""",
name="CallerContract",
)
Expand All @@ -153,14 +153,14 @@ def get_name_of(addr: HasName) -> String[32]:
assert trace == StackTrace(
[
" Test an error(<CalledContract interface at "
f"{called_contract.address}>.name() -> ['string'])",
f"{called_contract.address}> (file CalledContract).name() -> ['string'])",
" Test an error(<CallerContract interface at "
f"{caller_contract.address}>.get_name_of(address) -> "
"['string'])",
f"{caller_contract.address}> (file "
"CallerContract).get_name_of(address) -> ['string'])",
" <Unknown contract 0x0000000000000000000000000000000000008009>",
" <Unknown contract 0x0000000000000000000000000000000000008002>",
" Test an error(<CallerContract interface at "
f"{caller_contract.address}>.get_name_of(address) -> "
f"{caller_contract.address}> (file CallerContract).get_name_of(address) -> "
"['string'])",
]
)
Expand Down Expand Up @@ -198,7 +198,7 @@ def test_logs(zksync_env):
receiver: indexed(address)
value: uint256
@external
@deploy
def __init__(supply: uint256):
log Transfer(empty(address), msg.sender, supply)
Expand Down

0 comments on commit 2b01517

Please sign in to comment.