From 4823aacac8772d6c727a89f5c610984fdbc1ecaa Mon Sep 17 00:00:00 2001 From: Lesigh-3100 Date: Mon, 11 Dec 2023 14:46:29 +0200 Subject: [PATCH 1/3] Fix to bancor POL multicall - combines tkn balance & price --- fastlane_bot/config/multicaller.py | 16 +++++++++++++++- fastlane_bot/events/multicall_utils.py | 24 ++++-------------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/fastlane_bot/config/multicaller.py b/fastlane_bot/config/multicaller.py index 18f94bdbd..4087d792a 100644 --- a/fastlane_bot/config/multicaller.py +++ b/fastlane_bot/config/multicaller.py @@ -152,7 +152,8 @@ def multicall(self) -> List[Any]: w3 = self.contract.web3 _encoded_data = [] - for fn_list in _calls_for_aggregate.keys(): + function_keys = _calls_for_aggregate.keys() + for fn_list in function_keys: _encoded_data.append(w3.eth.contract( abi=MULTICALL_ABI, address=self.MULTICALL_CONTRACT_ADDRESS @@ -177,4 +178,17 @@ def multicall(self) -> List[Any]: return_data = [i[0] for i in decoded_data_list if len(i) == 1] return_data += [i[1] for i in decoded_data_list if len(i) > 1] + + # Handling for Bancor POL - combine results into a Tuple + if "tokenPrice" in function_keys and "amountAvailableForTrading" in function_keys: + new_return = [] + returned_items = int(len(return_data)) + total_pools = int(returned_items / 2) + assert returned_items % 2 == 0, f"[multicaller.py multicall] non-even number of returned calls for Bancor POL {returned_items}" + total_pools = int(total_pools) + + for idx in range(total_pools): + new_return.append((return_data[idx][0], return_data[idx][1], return_data[idx + total_pools])) + return_data = new_return + return return_data diff --git a/fastlane_bot/events/multicall_utils.py b/fastlane_bot/events/multicall_utils.py index 6d03b16ac..9fc84daba 100644 --- a/fastlane_bot/events/multicall_utils.py +++ b/fastlane_bot/events/multicall_utils.py @@ -285,27 +285,11 @@ def _extract_pol_params_for_multicall(result: Any, pool_info: Dict, mgr: Any) -> """ tkn0_address = pool_info["tkn0_address"] - if type(result) != int: - prices = result - p0, p1 = prices - token_price = Decimal(p1) / Decimal(p0) - - if mgr.cfg.ARB_CONTRACT_VERSION < 10: - tkn_contract = mgr.token_contracts.get(tkn0_address, mgr.web3.eth.contract(abi=ERC20_ABI, address=tkn0_address)) if tkn0_address not in mgr.cfg.ETH_ADDRESS else None - if tkn_contract is not None: - if tkn0_address not in mgr.token_contracts: - mgr.token_contracts[tkn0_address] = tkn_contract - tkn_balance = tkn_contract.functions.balanceOf(mgr.cfg.BANCOR_POL_ADDRESS).call() - else: - tkn_balance = 0 - - else: - tkn_balance = pool_info["y_0"] - token_price= int(str(encode_token_price(token_price))) - else: - tkn_balance = result - token_price = pool_info["B_0"] + p0, p1, tkn_balance = result + token_price = Decimal(p1) / Decimal(p0) + token_price= int(str(encode_token_price(token_price))) + result = { "fee": "0.000", "fee_float": 0.000, From 0ce0a3e5d9aa3eae6f7d748fbafcd6d2ed8c7e02 Mon Sep 17 00:00:00 2001 From: Lesigh-3100 Date: Mon, 11 Dec 2023 15:02:40 +0200 Subject: [PATCH 2/3] Remove duplicate multicall --- fastlane_bot/config/multicaller.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fastlane_bot/config/multicaller.py b/fastlane_bot/config/multicaller.py index 4087d792a..754555177 100644 --- a/fastlane_bot/config/multicaller.py +++ b/fastlane_bot/config/multicaller.py @@ -119,8 +119,7 @@ def __enter__(self) -> 'MultiCaller': return self def __exit__(self, exc_type, exc_val, exc_tb): - if exc_type is None: - self.multicall() + pass def add_call(self, fn: Callable, *args, **kwargs) -> None: self._contract_calls.append(partial(fn, *args, **kwargs)) From f396acedad1a7d061c2db9ce130d74a0d7e8b34e Mon Sep 17 00:00:00 2001 From: Lesigh-3100 Date: Mon, 11 Dec 2023 15:24:07 +0200 Subject: [PATCH 3/3] Fix multicall tests --- resources/NBTest/NBTest_899_CustomMulticall.ipynb | 9 +++++---- resources/NBTest/NBTest_899_CustomMulticall.py | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/resources/NBTest/NBTest_899_CustomMulticall.ipynb b/resources/NBTest/NBTest_899_CustomMulticall.ipynb index 40d79f306..31656d3ad 100644 --- a/resources/NBTest/NBTest_899_CustomMulticall.ipynb +++ b/resources/NBTest/NBTest_899_CustomMulticall.ipynb @@ -243,7 +243,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "id": "64050694894fae74", "metadata": { "ExecuteTime": { @@ -258,6 +258,7 @@ "\n", "with patch.object(multicaller, 'multicall') as mock_multicall:\n", " with multicaller:\n", + " multicaller.multicall()\n", " pass\n", "\n", " mock_multicall.assert_called_once()" @@ -273,7 +274,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "e2347c4c", "metadata": { "ExecuteTime": { @@ -312,7 +313,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "id": "3bae18bc82ad3ccc", "metadata": { "ExecuteTime": { @@ -369,7 +370,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.17" + "version": "3.10.1" } }, "nbformat": 4, diff --git a/resources/NBTest/NBTest_899_CustomMulticall.py b/resources/NBTest/NBTest_899_CustomMulticall.py index 6c14ccdcc..9848c63c9 100644 --- a/resources/NBTest/NBTest_899_CustomMulticall.py +++ b/resources/NBTest/NBTest_899_CustomMulticall.py @@ -6,7 +6,7 @@ # extension: .py # format_name: light # format_version: '1.5' -# jupytext_version: 1.15.2 +# jupytext_version: 1.14.5 # kernelspec: # display_name: Python 3 (ipykernel) # language: python @@ -153,6 +153,7 @@ class MockContract: with patch.object(multicaller, 'multicall') as mock_multicall: with multicaller: + multicaller.multicall() pass mock_multicall.assert_called_once()