From 258e48707f4c4218a1d1170652b4b2343bf8ffea Mon Sep 17 00:00:00 2001 From: Hieu Nguyen Date: Sun, 10 Nov 2024 02:51:56 +0200 Subject: [PATCH] Handle executed price --- .../generic-router/test_generic_router_multihops.py | 8 +++++--- tradeexecutor/ethereum/uniswap_v3/uniswap_v3_routing.py | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/ethereum/polygon_forked/generic-router/test_generic_router_multihops.py b/tests/ethereum/polygon_forked/generic-router/test_generic_router_multihops.py index 30029e87b..dc8351c85 100644 --- a/tests/ethereum/polygon_forked/generic-router/test_generic_router_multihops.py +++ b/tests/ethereum/polygon_forked/generic-router/test_generic_router_multihops.py @@ -110,7 +110,7 @@ def strategy_universe( return TradingStrategyUniverse.create_from_dataset(dataset, asset_usdc.address) -@flaky.flaky +# @flaky.flaky def test_generic_routing_multihops_trade( web3: Web3, hot_wallet: HotWallet, @@ -119,6 +119,7 @@ def test_generic_routing_multihops_trade( generic_pricing_model: GenericPricing, asset_usdc: AssetIdentifier, asset_wbtc: AssetIdentifier, + asset_wmatic: AssetIdentifier, wbtc_weth_spot_pair: TradingPairIdentifier, weth_usdc_spot_pair: TradingPairIdentifier, wmatic_weth_spot_pair: TradingPairIdentifier, @@ -219,7 +220,6 @@ def test_generic_routing_multihops_trade( assert len(state.portfolio.open_positions) == 2 p = state.portfolio.open_positions[2] - print(p) assert p.get_opening_price() == pytest.approx(0.8526643063862225) # Check our wallet holds all tokens we expect. @@ -231,8 +231,10 @@ def test_generic_routing_multihops_trade( { asset_usdc.address, asset_wbtc.address, + asset_wmatic.address, }, decimalise=True, ) assert 0 < balances[asset_wbtc.address] < 1000, f"Got balance: {balances}" - assert balances[asset_usdc.address] == pytest.approx(9_500), f"Got balance: {balances}" + assert 0 < balances[asset_wmatic.address] < 1000, f"Got balance: {balances}" + assert balances[asset_usdc.address] == pytest.approx(9_000), f"Got balance: {balances}" diff --git a/tradeexecutor/ethereum/uniswap_v3/uniswap_v3_routing.py b/tradeexecutor/ethereum/uniswap_v3/uniswap_v3_routing.py index dbcf4bec4..edc155ba9 100644 --- a/tradeexecutor/ethereum/uniswap_v3/uniswap_v3_routing.py +++ b/tradeexecutor/ethereum/uniswap_v3/uniswap_v3_routing.py @@ -349,7 +349,7 @@ def settle_trade( if trade.is_buy(): assert path[0] == reserve.address, f"Was expecting the route path to start with reserve token {reserve}, got path {result.path}" - price = result.get_human_price(quote_token_details.address == result.token0.address) + price = result.get_human_price(base_token_details.address == result.token1.address) executed_reserve = result.amount_in / Decimal(10 ** reserve.decimals) executed_amount = result.amount_out / Decimal(10 ** base_token_details.decimals) @@ -359,7 +359,7 @@ def settle_trade( # Ordered other way around assert path[0] == base_token_details.address.lower(), f"Path is {path}, base token is {base_token_details}" assert path[-1] == reserve.address - price = result.get_human_price(quote_token_details.address == result.token0.address) + price = result.get_human_price(base_token_details.address == result.token0.address) executed_amount = -result.amount_in / Decimal(10 ** base_token_details.decimals) executed_reserve = result.amount_out / Decimal(10 ** reserve.decimals)