Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/infra/dex/balancer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ impl Sor {
slippage: &dex::Slippage,
tokens: &auction::Tokens,
) -> Result<dex::Swap, Error> {
// 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.
Expand Down Expand Up @@ -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,
&quote,
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,
&quote,
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
Expand Down
27 changes: 9 additions & 18 deletions src/infra/dex/balancer/query_swap_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down
Loading