From 708de9395c0ecedccd23f493e8e675601f2cf9d4 Mon Sep 17 00:00:00 2001 From: Ben Hauser Date: Mon, 12 Feb 2024 16:08:48 +0400 Subject: [PATCH 1/5] feat: multicall verbosity --- brownie/network/multicall.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/brownie/network/multicall.py b/brownie/network/multicall.py index c62718d00..c13c39899 100644 --- a/brownie/network/multicall.py +++ b/brownie/network/multicall.py @@ -24,6 +24,7 @@ class Call: calldata: Tuple[str, bytes] decoder: FunctionType + readable: str class Result(ObjectProxy): @@ -48,6 +49,7 @@ class Multicall: def __init__(self) -> None: self.address = None self._block_number = defaultdict(lambda: None) # type: ignore + self._verbose = defaultdict(lambda: None) # type: ignore self._contract = None self._pending_calls: Dict[int, List[Call]] = defaultdict(list) @@ -61,10 +63,14 @@ def block_number(self) -> int: return self._block_number[get_ident()] def __call__( - self, address: Optional[str] = None, block_identifier: Union[str, bytes, int, None] = None + self, + address: Optional[str] = None, + block_identifier: Union[str, bytes, int, None] = None, + verbose: bool = False, ) -> "Multicall": self.address = address # type: ignore self._block_number[get_ident()] = block_identifier # type: ignore + self._verbose[get_ident()] = verbose return self def _flush(self, future_result: Result = None) -> Any: @@ -76,6 +82,11 @@ def _flush(self, future_result: Result = None) -> Any: # or this result has already been retrieved return future_result with self._lock: + if self._verbose[get_ident()]: + print(f"Multicall: total {len(pending_calls)} calls") + for item in pending_calls: + print(f" {item.readable}") + print() ContractCall.__call__.__code__ = getattr(ContractCall, "__original_call_code") results = self._contract.tryAggregate( # type: ignore False, @@ -96,7 +107,8 @@ def flush(self) -> Any: def _call_contract(self, call: ContractCall, *args: Tuple, **kwargs: Dict[str, Any]) -> Proxy: """Add a call to the buffer of calls to be made""" calldata = (call._address, call.encode_input(*args, **kwargs)) # type: ignore - call_obj = Call(calldata, call.decode_output) # type: ignore + readable = f"{call._name}({', '.join(str(i) for i in args)})" + call_obj = Call(calldata, call.decode_output, readable) # type: ignore # future result result = Result(call_obj) self._pending_calls[get_ident()].append(result) From f3977e7b1eb704168007a86f08748b4ff2942421 Mon Sep 17 00:00:00 2001 From: Ben Hauser Date: Mon, 12 Feb 2024 18:11:46 +0400 Subject: [PATCH 2/5] fix: clear block_number and verbose on exit --- brownie/network/multicall.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/brownie/network/multicall.py b/brownie/network/multicall.py index c13c39899..9b65ff603 100644 --- a/brownie/network/multicall.py +++ b/brownie/network/multicall.py @@ -162,6 +162,9 @@ def __exit__(self, exc_type: Exception, exc_val: Any, exc_tb: TracebackType) -> self.flush() getattr(ContractCall, "__multicall")[get_ident()] = None + self._block_number.pop(get_ident(), None) + self._verbose.pop(get_ident(), None) + @staticmethod def deploy(tx_params: Dict) -> Contract: """Deploy an instance of the `Multicall2` contract. From a4641ec996cbd689c0ef3a446177061a15a4b67c Mon Sep 17 00:00:00 2001 From: Ben Hauser Date: Tue, 13 Feb 2024 01:40:02 +0400 Subject: [PATCH 3/5] fix: improve verbosity formatting --- brownie/network/multicall.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/brownie/network/multicall.py b/brownie/network/multicall.py index 9b65ff603..4a44e07d2 100644 --- a/brownie/network/multicall.py +++ b/brownie/network/multicall.py @@ -13,6 +13,7 @@ from brownie.network import accounts, web3 from brownie.network.contract import Contract, ContractCall from brownie.project import compile_source +from brownie.utils import color DATA_DIR = BROWNIE_FOLDER.joinpath("data") MULTICALL2_ABI = json.loads(DATA_DIR.joinpath("interfaces", "Multicall2.json").read_text()) @@ -48,6 +49,7 @@ class Multicall: def __init__(self) -> None: self.address = None + self.default_verbosity = False self._block_number = defaultdict(lambda: None) # type: ignore self._verbose = defaultdict(lambda: None) # type: ignore self._contract = None @@ -66,11 +68,11 @@ def __call__( self, address: Optional[str] = None, block_identifier: Union[str, bytes, int, None] = None, - verbose: bool = False, + verbose: Optional[bool] = None, ) -> "Multicall": self.address = address # type: ignore self._block_number[get_ident()] = block_identifier # type: ignore - self._verbose[get_ident()] = verbose + self._verbose[get_ident()] = verbose if verbose is not None else self.default_verbosity return self def _flush(self, future_result: Result = None) -> Any: @@ -82,11 +84,18 @@ def _flush(self, future_result: Result = None) -> Any: # or this result has already been retrieved return future_result with self._lock: - if self._verbose[get_ident()]: - print(f"Multicall: total {len(pending_calls)} calls") - for item in pending_calls: - print(f" {item.readable}") - print() + if self._verbose.get(get_ident(), self.default_verbosity): + message = ( + "Multicall:" + f"\n Thread ID: {get_ident()}" + f"\n Block number: {self._block_number[get_ident()]}" + f"\n Calls: {len(pending_calls)}" + ) + for c, item in enumerate(pending_calls, start=1): + u = "\u2514" if c == len(pending_calls) else "\u251c" + message = f"{message}\n {u}\u2500{item.readable}" + print(color.highlight(f"{message}\n")) + ContractCall.__call__.__code__ = getattr(ContractCall, "__original_call_code") results = self._contract.tryAggregate( # type: ignore False, From 756e2cd532db0f3468f3a8912c1bdee424cdacc9 Mon Sep 17 00:00:00 2001 From: Ben Hauser Date: Tue, 13 Feb 2024 01:46:41 +0400 Subject: [PATCH 4/5] docs: add multicall verbose to network API docs --- docs/api-network.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/api-network.rst b/docs/api-network.rst index 439f8ca2b..0d2efa2a1 100644 --- a/docs/api-network.rst +++ b/docs/api-network.rst @@ -1900,6 +1900,21 @@ Multicall Attributes ... brownie.multicall.block_number 12733683 +.. py:attribute:: Multicall.default_verbosity + + Default verbosity setting for multicall. Set to ``False`` by default. If set to ``True``, the content of each batched call is printed to the console. This is useful for debugging, to ensure a multicall is performing as expected. + + .. code-block:: python + + >>> multicall.default_verbosity = True + + You can also enable verbosity for individual multicalls by setting the `verbose` keyword: + + .. code-block:: python + + >>> with brownie.multicall(verbose=True): + ... + Multicall Methods ***************** From 0dc1d76dd79da0c94f950d253d3b3ca526a118bc Mon Sep 17 00:00:00 2001 From: Ben Hauser Date: Tue, 13 Feb 2024 01:50:08 +0400 Subject: [PATCH 5/5] chore: default_verbosity -> default_verbose --- brownie/network/multicall.py | 6 +++--- docs/api-network.rst | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/brownie/network/multicall.py b/brownie/network/multicall.py index 4a44e07d2..f56bf9a92 100644 --- a/brownie/network/multicall.py +++ b/brownie/network/multicall.py @@ -49,7 +49,7 @@ class Multicall: def __init__(self) -> None: self.address = None - self.default_verbosity = False + self.default_verbose = False self._block_number = defaultdict(lambda: None) # type: ignore self._verbose = defaultdict(lambda: None) # type: ignore self._contract = None @@ -72,7 +72,7 @@ def __call__( ) -> "Multicall": self.address = address # type: ignore self._block_number[get_ident()] = block_identifier # type: ignore - self._verbose[get_ident()] = verbose if verbose is not None else self.default_verbosity + self._verbose[get_ident()] = verbose if verbose is not None else self.default_verbose return self def _flush(self, future_result: Result = None) -> Any: @@ -84,7 +84,7 @@ def _flush(self, future_result: Result = None) -> Any: # or this result has already been retrieved return future_result with self._lock: - if self._verbose.get(get_ident(), self.default_verbosity): + if self._verbose.get(get_ident(), self.default_verbose): message = ( "Multicall:" f"\n Thread ID: {get_ident()}" diff --git a/docs/api-network.rst b/docs/api-network.rst index 0d2efa2a1..802a3e5f1 100644 --- a/docs/api-network.rst +++ b/docs/api-network.rst @@ -1900,13 +1900,13 @@ Multicall Attributes ... brownie.multicall.block_number 12733683 -.. py:attribute:: Multicall.default_verbosity +.. py:attribute:: Multicall.default_verbose Default verbosity setting for multicall. Set to ``False`` by default. If set to ``True``, the content of each batched call is printed to the console. This is useful for debugging, to ensure a multicall is performing as expected. .. code-block:: python - >>> multicall.default_verbosity = True + >>> multicall.default_verbose = True You can also enable verbosity for individual multicalls by setting the `verbose` keyword: