diff --git a/src/infra/dex/balancer/mod.rs b/src/infra/dex/balancer/mod.rs index 00150ed..26fa805 100644 --- a/src/infra/dex/balancer/mod.rs +++ b/src/infra/dex/balancer/mod.rs @@ -108,10 +108,6 @@ impl Sor { slippage: &dex::Slippage, tokens: &auction::Tokens, ) -> Result { - // Receiving this error indicates that V2 is now supported on the current chain. - let Some(v2_vault) = &self.v2_vault else { - return Err(Error::DisabledApiVersion(ApiVersion::V2)); - }; let query = dto::Query::from_domain(order, tokens, self.chain_id)?; let quote = { // Set up a tracing span to make debugging of API requests easier. @@ -164,16 +160,22 @@ impl Sor { let gas = U256::from(quote.swaps.len()) * U256::from(Self::GAS_PER_SWAP); let (spender, calls) = match quote.protocol_version { - dto::ProtocolVersion::V2 => ( - v2_vault.address(), - self.encode_v2_swap( - order, - "e, - max_input.into_alloy(), - min_output.into_alloy(), - v2_vault, - )?, - ), + dto::ProtocolVersion::V2 => { + // Check if V2 is available for this chain + let Some(v2_vault) = &self.v2_vault else { + return Err(Error::DisabledApiVersion(ApiVersion::V2)); + }; + ( + v2_vault.address(), + self.encode_v2_swap( + order, + "e, + max_input.into_alloy(), + min_output.into_alloy(), + v2_vault, + )?, + ) + } dto::ProtocolVersion::V3 => { // In Balancer v3, the spender must be the Permit2 contract, as it's the one // doing the transfer of funds from the settlement diff --git a/src/infra/dex/balancer/query_swap_provider.rs b/src/infra/dex/balancer/query_swap_provider.rs index 748d21b..80e8cbf 100644 --- a/src/infra/dex/balancer/query_swap_provider.rs +++ b/src/infra/dex/balancer/query_swap_provider.rs @@ -238,25 +238,16 @@ impl OnChainQuerySwapProvider { } }; - // For V3, the result is a single amount - // We need to determine which is the input and which is the output based on - // order side - let (swap_amount, return_amount) = match order.side { - order::Side::Sell => { - // For sell orders: swap_amount is the input (known), return_amount is the - // output (queried) - (quote.swap_amount_raw.into_alloy(), result) - } - order::Side::Buy => { - // For buy orders: swap_amount is the input (queried), return_amount is the - // output (known) - (result, quote.return_amount_raw.into_alloy()) - } - }; - + // swap_amount: the given/exact amount from SOR + // + // The on-chain query result is always the "calculated" amount: + // - For sell orders: result = output amount (what user receives) + // - For buy orders: result = input amount (what user needs to pay) + // + // return_amount: the calculated amount from on-chain query Ok(OnChainAmounts { - swap_amount, - return_amount, + swap_amount: quote.swap_amount_raw.into_alloy(), + return_amount: result, }) }