-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: refactor for zksync abstraction #195
Changes from 43 commits
3be0754
ac35cda
cc5746c
3921a08
8f4f660
1ff3c18
16de424
32deb48
80b3e20
ab90622
a29bd5c
20ab1f5
2c85b4d
ddf553c
ce99613
37e0997
9565ce0
6035c0c
3d335d4
853d550
debb3c8
ae7e6ad
1ff4e28
b43481b
309a688
08fe81d
2dfdf53
ee260b4
1c83d70
f09af27
79ff1b7
517e7df
571889e
f0a2539
d788747
821d856
0631a5d
16da97a
f96fff3
cf54096
e0a8644
330dda0
bb840ca
ac57b4d
2c4e55b
9ec7a09
e7909cd
92e29aa
433e76c
5f48854
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
from collections import defaultdict | ||
from copy import deepcopy | ||
from functools import cached_property | ||
from os.path import basename | ||
from typing import Any, Optional, Union | ||
from warnings import warn | ||
|
||
|
@@ -34,7 +33,8 @@ def __init__(self, abi: dict, contract_name: str): | |
|
||
@property | ||
def name(self) -> str: | ||
return self._abi["name"] | ||
# note: the `constructor` definition does not have a name | ||
return self._abi.get("name", "") | ||
|
||
@cached_property | ||
def argument_types(self) -> list: | ||
|
@@ -62,6 +62,8 @@ def pretty_signature(self) -> str: | |
|
||
@cached_property | ||
def method_id(self) -> bytes: | ||
if self._abi["type"] == "constructor": | ||
return b"" # constructors don't have method IDs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this should be handled at the caller really (as is done in vyper_contract.py) |
||
return method_id(self.name + self.signature) | ||
|
||
def __repr__(self) -> str: | ||
|
@@ -215,13 +217,16 @@ class ABIContract(_BaseEVMContract): | |
def __init__( | ||
self, | ||
name: str, | ||
abi: dict, | ||
abi: list[dict], | ||
functions: list[ABIFunction], | ||
address: Address, | ||
filename: Optional[str] = None, | ||
env=None, | ||
compiler_data: Optional[Any] = None, | ||
): | ||
super().__init__(env, filename=filename, address=address) | ||
super().__init__( | ||
env, filename=filename, address=address, compiler_data=compiler_data | ||
) | ||
self._name = name | ||
self._abi = abi | ||
self._functions = functions | ||
|
@@ -241,6 +246,7 @@ def __init__( | |
setattr(self, name, ABIOverload.create(group, self)) | ||
|
||
self._address = Address(address) | ||
self._computation: Optional[ComputationAPI] = None | ||
|
||
@property | ||
def abi(self): | ||
|
@@ -260,6 +266,7 @@ def marshal_to_python(self, computation, abi_type: list[str]) -> tuple[Any, ...] | |
:param computation: the computation object returned by `execute_code` | ||
:param abi_type: the ABI type of the return value. | ||
""" | ||
self._computation = computation | ||
# when there's no contract in the address, the computation output is empty | ||
if computation.is_error: | ||
return self.handle_error(computation) | ||
|
@@ -274,13 +281,18 @@ def stack_trace(self, computation: ComputationAPI) -> StackTrace: | |
""" | ||
Create a stack trace for a failed contract call. | ||
""" | ||
reason = "" | ||
# revert without reason has args=[None], so we don't want to include that | ||
if computation.is_error and any(computation.error.args): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case of a revert from an assert without reason, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be better to filter those on the next line?
|
||
reason = " ".join(str(arg) for arg in computation.error.args) | ||
|
||
calldata_method_id = bytes(computation.msg.data[:4]) | ||
if calldata_method_id in self.method_id_map: | ||
function = self.method_id_map[calldata_method_id] | ||
msg = f" ({self}.{function.pretty_signature})" | ||
msg = f" {reason}({self}.{function.pretty_signature})" | ||
else: | ||
# Method might not be specified in the ABI | ||
msg = f" (unknown method id {self}.0x{calldata_method_id.hex()})" | ||
msg = f" {reason}(unknown method id {self}.0x{calldata_method_id.hex()})" | ||
|
||
return_trace = StackTrace([msg]) | ||
return _handle_child_trace(computation, self.env, return_trace) | ||
|
@@ -290,7 +302,12 @@ def deployer(self) -> "ABIContractFactory": | |
""" | ||
Returns a factory that can be used to retrieve another deployed contract. | ||
""" | ||
return ABIContractFactory(self._name, self._abi, self._functions) | ||
return ABIContractFactory( | ||
self._name, | ||
self._abi, | ||
filename=self.filename, | ||
compiler_data=self.compiler_data, | ||
) | ||
|
||
def __repr__(self): | ||
file_str = f" (file {self.filename})" if self.filename else "" | ||
|
@@ -308,33 +325,40 @@ class ABIContractFactory: | |
def __init__( | ||
self, | ||
name: str, | ||
abi: dict, | ||
functions: list[ABIFunction], | ||
abi: list[dict], | ||
filename: Optional[str] = None, | ||
compiler_data: Optional[Any] = None, | ||
): | ||
self._name = name | ||
self._abi = abi | ||
self._functions = functions | ||
self._filename = filename | ||
self._functions = [ | ||
ABIFunction(item, name) for item in abi if item.get("type") == "function" | ||
] | ||
self.filename = filename | ||
self.compiler_data = compiler_data | ||
|
||
@cached_property | ||
def abi(self): | ||
return deepcopy(self._abi) | ||
|
||
@classmethod | ||
def from_abi_dict(cls, abi, name="<anonymous contract>"): | ||
functions = [ | ||
ABIFunction(item, name) for item in abi if item.get("type") == "function" | ||
] | ||
return cls(basename(name), abi, functions, filename=name) | ||
def from_abi_dict( | ||
cls, abi, name="<anonymous contract>", filename=None, compiler_data=None | ||
): | ||
return cls(name, abi, filename, compiler_data) | ||
|
||
def at(self, address: Address | str) -> ABIContract: | ||
""" | ||
Create an ABI contract object for a deployed contract at `address`. | ||
""" | ||
address = Address(address) | ||
contract = ABIContract( | ||
self._name, self._abi, self._functions, address, self._filename | ||
self._name, | ||
self._abi, | ||
self._functions, | ||
address, | ||
self.filename, | ||
compiler_data=self.compiler_data, | ||
) | ||
contract.env.register_contract(address, contract) | ||
return contract | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea i don't think we should use
""
as a default here, i think we should return Optional and just handle the None case at the call sites. like we will get stuff like""
as a key in the overloads dictionary, which seems funky.or if we really want a name, we can use
"__init__"
(or"constructor"
as i think you originally had).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah constructor was the first workaround I thought of, because it's just clear and simple.
handling it at the call sites is fine, but it creates a lot of boiler plate code