Skip to content

Commit

Permalink
Fix route table index
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Dec 1, 2024
1 parent f291825 commit 1e750bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
32 changes: 21 additions & 11 deletions eth_defi/vault/valuation.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ def multicall_callback(self, succeed: bool, raw_return_value: Any) -> TokenAmoun
self,
raw_return_value,
)

if token_amount in (0, None):
raise RuntimeError(f"Selling got zero amount. Route {self}, raw return value {raw_return_value}")

return token_amount

except Exception as e:
Expand All @@ -177,7 +173,8 @@ def multicall_callback(self, succeed: bool, raw_return_value: Any) -> TokenAmoun
self.quoter,
raw_return_value,
)
raise e
raise e # 0.0000673


if self.debug:
logger.info(
Expand Down Expand Up @@ -335,7 +332,7 @@ def handle_onchain_return_value(
) -> Decimal | None:
"""Convert swapExactTokensForTokens() return value to tokens we receive"""
route = wrapper.route
target_token_out = raw_return_value[0]
target_token_out = raw_return_value[-1]
return route.target_token.convert_to_decimals(target_token_out)

def get_path_combinations(
Expand Down Expand Up @@ -566,7 +563,18 @@ def create_route_diagnostics(
- Flag routes that work
- Show both best and suboptimal routes
- Show values of each portfolio position if sold with the route
Outputs:
.. code-block:: text
Asset Address Balance Router Works Value
Path
USDC USDC 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 0.35 yes 0.35
WETH -> USDC WETH 0x4200000000000000000000000000000000000006 0.000000 UniswapV2Router02Quoter yes 0.00
DINO -> USDC DINO 0x85E90a5430AF45776548ADB82eE4cD9E33B08077 547942.000069 UniswapV2Router02Quoter no -
DINO -> WETH -> USDC DINO 0x85E90a5430AF45776548ADB82eE4cD9E33B08077 547942.000069 UniswapV2Router02Quoter yes 36.69
:return:
Human-readable DataFrame.
Expand All @@ -583,10 +591,11 @@ def create_route_diagnostics(
if reserve_balance:
# Handle case where we cannot route reserve balance to itself
data.append({
"Path": self.denomination_token.symbol,
"Asset": self.denomination_token.symbol,
"Address": self.denomination_token.address,
"Balance": f"{reserve_balance:,.2f}",
"Router": "",
"Path": "",
"Works": "yes",
"Value": f"{reserve_balance:,.2f}",
})
Expand All @@ -595,22 +604,23 @@ def create_route_diagnostics(

out_balance = sell_prices[route]

if out_balance:
if out_balance is not None:
formatted_balance = f"{out_balance:,.2f}"
else:
formatted_balance = "-"

data.append({
"Path": _format_symbolic_path_uniswap_v2(self.web3, route),
"Asset": route.source_token.symbol,
"Address": route.source_token.address,
"Balance": f"{portfolio.spot_erc20[route.source_token.address]:.6f}",
"Router": route.quoter.__class__.__name__,
"Path": _format_symbolic_path_uniswap_v2(self.web3, route),
"Works": "yes" if out_balance is not None else "no",
"Value": formatted_balance,
})

df = pd.DataFrame(data)
df = df.set_index("Asset")
df = df.set_index("Path")
return df


Expand Down
13 changes: 12 additions & 1 deletion tests/lagoon/test_lagoon_valuation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def test_uniswap_v2_weth_usdc_sell_route(
- See that the logic for a single route works
- Test various ways of building the tx payload for eth_call
- Router address is 0x4752ba5dbc23f44d87826276bf6fd6b1c372ad24
- Dino amount is 547942000069182639312002
- Dino PATH is ["0x85E90a5430AF45776548ADB82eE4cD9E33B08077", "0x4200000000000000000000000000000000000006", "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"]
- Dino value 0.0000673 * 547942 = $36.876496599999996
"""

uniswap_v2_quoter_v2 = UniswapV2Router02Quoter(
Expand Down Expand Up @@ -177,4 +185,7 @@ def test_lagoon_diagnose_routes(
print()
print(routes)


assert routes.loc["USDC"]["Value"] is not None
assert routes.loc["WETH -> USDC"]["Value"] is not None
assert routes.loc["DINO -> WETH -> USDC"]["Value"] is not None
assert routes.loc["DINO -> USDC"]["Value"] == "-"

0 comments on commit 1e750bb

Please sign in to comment.