Wrong simulation function used in reverse operation path
The reverse simulation function for multi-hop swaps incorrectly used forward simulation instead of reverse simulation, leading to incorrect price quotes for users trying to determine required input tokens for a desired output amount.
reverse_simulate_swap_operations function incorrectly uses query_simulation instead of query_reverse_simulation when calculating multi-hop trades in reverse. As a result, users will get incorrect price calculations when they attempt to determine how many input tokens they need for a desired output amount.
In summary:
Users receive incorrect price quotes for trades.
The error compounds in multi-hop trades, causing issues.
The doc also clearly states that offer_amount in the response is “The amount of the offer asset needed to get the ask amount.”
Now, let’s compare again with the implementation:
let res = query_simulation( <--- This is WRONG! Its using forward simulation
deps,
coin(amount.u128(), token_out_denom),
token_in_denom,
pool_identifier,
)?;
The bug is clear because it’s using query_simulation instead of query_reverse_simulation.
Also consider a multi-hop trade A -> B -> C where a user wants 100 C tokens:
The fn calculates how many B tokens you get for X C tokens (forward).
Then calculates how many A tokens you get for Y B tokens (forward).
This is incorrect for reverse price discovery.
It shoould actually be:
Calculate how many B tokens you need to get 100 C tokens (reverse).
Then calculate how many A tokens you need to get the required B tokens (reverse).
The contract already has a correct query_reverse_simulation function that handles proper price calculation for reverse operations, but it’s not being used.
Recommended mitigation steps
pub fn reverse_simulate_swap_operations(
deps: Deps,
ask_amount: Uint128,
operations: Vec<SwapOperation>,
) -> Result<SimulateSwapOperationsResponse, ContractError> {
let operations_len = operations.len();
if operations_len == 0 {
return Err(ContractError::NoSwapOperationsProvided);
}
let mut amount = ask_amount;
for operation in operations.into_iter().rev() {
match operation {
SwapOperation::MantraSwap {
token_in_denom,
token_out_denom,
pool_identifier,
} => {
- let res = query_simulation(
+ let res = query_reverse_simulation(
deps,
coin(amount.u128(), token_out_denom),
token_in_denom,
pool_identifier,
)?;
- amount = res.return_amount;
+ amount = res.offer_amount;
}
}
}
Ok(SimulateSwapOperationsResponse { amount })
}
For judging, we base on the commit frozen for the audit scope; so while it’s good to see the team found and fixed the issue independently, it wouldn’t be fair to wardens to not reward a valid finding.
The original report as published on Code4rena, by Rhaydden. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
The function called query_simulation (forward simulation) instead of query_reverse_simulation when computing the required input amount for a desired output.
What the attacker needed
None
The fix
The function should call query_reverse_simulation and use the offer_amount from its response instead of query_simulation and return_amount.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
When you want to swap tokens, you might ask the system how many tokens you need to put in to get a certain amount out. This function is supposed to answer that, but it used the wrong calculation method, so the answer was wrong. That means users could get bad price estimates.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.