Skip to content

Commit

Permalink
Merge pull request #250 from bancorprotocol/249-issue-with-bancor-pol…
Browse files Browse the repository at this point in the history
…-balance-updating-after-trades

249 issue with bancor pol balance updating after trades
  • Loading branch information
NIXBNT authored Dec 11, 2023
2 parents b4afac1 + f396ace commit 4201fd0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
19 changes: 16 additions & 3 deletions fastlane_bot/config/multicaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -152,7 +151,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
Expand All @@ -177,4 +177,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
24 changes: 4 additions & 20 deletions fastlane_bot/events/multicall_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions resources/NBTest/NBTest_899_CustomMulticall.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"id": "64050694894fae74",
"metadata": {
"ExecuteTime": {
Expand All @@ -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()"
Expand All @@ -273,7 +274,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"id": "e2347c4c",
"metadata": {
"ExecuteTime": {
Expand Down Expand Up @@ -312,7 +313,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"id": "3bae18bc82ad3ccc",
"metadata": {
"ExecuteTime": {
Expand Down Expand Up @@ -369,7 +370,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.17"
"version": "3.10.1"
}
},
"nbformat": 4,
Expand Down
3 changes: 2 additions & 1 deletion resources/NBTest/NBTest_899_CustomMulticall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -153,6 +153,7 @@ class MockContract:

with patch.object(multicaller, 'multicall') as mock_multicall:
with multicaller:
multicaller.multicall()
pass

mock_multicall.assert_called_once()
Expand Down

0 comments on commit 4201fd0

Please sign in to comment.