Skip to content
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: abi method factory #260

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions boa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
load_partial,
loads,
loads_abi,
loads_fn_sigs,
loads_partial,
)
from boa.network import NetworkEnv
Expand Down
12 changes: 12 additions & 0 deletions boa/contracts/abi/abi_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from warnings import warn

from eth.abc import ComputationAPI
from vyper.ast import parse_to_ast
from vyper.semantics.analysis.base import FunctionVisibility, StateMutability
from vyper.semantics.types.function import ContractFunctionT
from vyper.utils import method_id

from boa.contracts.base_evm_contract import (
Expand Down Expand Up @@ -353,6 +355,16 @@ def functions(self):
def from_abi_dict(cls, abi, name="<anonymous contract>", filename=None):
return cls(name, abi, filename)

@classmethod
def from_sigs(cls, *fn_sigs, name="<anonymous contract function>", filename=None):
abi = [
ContractFunctionT.from_FunctionDef(
parse_to_ast(fn_sig).body[0], is_interface=True
).to_toplevel_abi_dict()
for fn_sig in fn_sigs
]
return cls(name, abi, filename)

def at(self, address: Address | str) -> ABIContract:
"""
Create an ABI contract object for a deployed contract at `address`.
Expand Down
4 changes: 4 additions & 0 deletions boa/interpret.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ def loads_abi(json_str: str, *args, name: str = None, **kwargs) -> ABIContractFa
return ABIContractFactory.from_abi_dict(json.loads(json_str), name, *args, **kwargs)


def loads_fn_sigs(*fn_sigs, **kwargs) -> ABIContractFactory:
return ABIContractFactory.from_sigs(*fn_sigs, **kwargs)


def loads_partial(
source_code: str,
name: str = None,
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/fork/test_abi_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ def tricrypto(get_filepath):
return abi.at("0x7F86Bf177Dd4F3494b841a37e810A34dD56c829B")


def test_load_fn_sig():
fn = boa.loads_fn_sigs(["def balanceOf(a: address) -> uint256: view"])
crv = fn.at("0xD533a949740bb3306d119CC777fa900bA034cd52")
assert (
crv.balanceOf("0x5f3b5DfEb7B28CDbD7FAba78963EE202a494e2A2")
== 778928018935537455445214842
)


def test_tricrypto(tricrypto):
assert tricrypto.fee_receiver() == "0xeCb456EA5365865EbAb8a2661B0c503410e9B347"
assert tricrypto.get_virtual_price() == 1003146380129683788
Expand Down
Loading