feat: Improved error handling

This was previously showing that the test passed even when it failed.

TODO:
- This still fails with "Insufficient balance". Need to debug why.
This commit is contained in:
TAMARA LIPOWSKI
2025-09-07 00:31:47 -04:00
committed by Tamara
parent f5bcd31d66
commit 02ed0fe216

View File

@@ -109,7 +109,7 @@ impl TestRunner {
} }
Err(e) => { Err(e) => {
failed_tests.push(test.name.clone()); failed_tests.push(test.name.clone());
error!("❗️{} failed: {}\n", test.name, e); error!("❗️{} failed: {:?}\n", test.name, e);
} }
} }
@@ -176,7 +176,7 @@ impl TestRunner {
) )
.wrap_err("Failed to run Tycho")?; .wrap_err("Failed to run Tycho")?;
let _ = tycho_runner.run_with_rpc_server( tycho_runner.run_with_rpc_server(
|expected_components, start_block, stop_block, skip_balance_check| { |expected_components, start_block, stop_block, skip_balance_check| {
validate_state( validate_state(
expected_components, expected_components,
@@ -191,9 +191,7 @@ impl TestRunner {
test.start_block, test.start_block,
test.stop_block, test.stop_block,
skip_balance_check, skip_balance_check,
)?; )?
Ok(())
} }
fn empty_database(&self) -> Result<(), Error> { fn empty_database(&self) -> Result<(), Error> {
@@ -406,29 +404,32 @@ fn validate_state(
state state
.spot_price(&tokens[0], &tokens[1]) .spot_price(&tokens[0], &tokens[1])
.map(|price| info!("Spot price {:?}: {:?}", formatted_token_str, price)) .map(|price| info!("Spot price {:?}: {:?}", formatted_token_str, price))
.map_err(|e| info!("Error calculating spot price for Pool {:?}: {:?}", id, e)) .into_diagnostic()
.ok(); .wrap_err(format!("Error calculating spot price for Pool {id:?}."))?;
// Test get_amount_out with different percentages of limits. The reserves or limits are // Test get_amount_out with different percentages of limits. The reserves or limits are
// relevant because we need to know how much to test with. We dont know if a pool is // relevant because we need to know how much to test with. We dont know if a pool is
// going to revert with 10 or 10 million USDC, for example, so by using the limits we // going to revert with 10 or 10 million USDC, for example, so by using the limits we
// can use “safe values” where the sim shouldnt break. // can use “safe values” where the sim shouldnt break.
// We then retrieve the amount out for 0.1%, 1% and 10%.
let percentages = [0.001, 0.01, 0.1]; let percentages = [0.001, 0.01, 0.1];
for percentage in &percentages {
// Get limits for this token pair // Get limits for this token pair
let limits = state let (max_input, max_output) = state
.get_limits(tokens[0].address.clone(), tokens[1].address.clone()) .get_limits(tokens[0].address.clone(), tokens[1].address.clone())
.map_err(|e| info!("Error getting limits for Pool {:?}: {:?}", id, e)) .into_diagnostic()
.ok(); .wrap_err(format!("Error getting limits for Pool {id:?}."))?;
if let Some((max_input, _max_output)) = limits { info!("Retrieved limits for pool {id}. | Max input: {max_input} {} | Max output: {max_output} {}", tokens[0].symbol, tokens[1].symbol);
// Calculate test amount as percentage of max input
for percentage in &percentages {
// For precision, multiply by 1000 then divide by 1000
let percentage_biguint = BigUint::from((percentage * 1000.0) as u32); let percentage_biguint = BigUint::from((percentage * 1000.0) as u32);
let thousand = BigUint::from(1000u32); let thousand = BigUint::from(1000u32);
let amount_in = (&max_input * &percentage_biguint) / &thousand; let amount_in = (&max_input * &percentage_biguint) / &thousand;
// Skip if amount is zero // Skip if amount is zero
if amount_in.is_zero() { if amount_in.is_zero() {
info!("Amount in multiplied by percentage {percentage} is zero. Skipping pool {id}.");
continue; continue;
} }
@@ -436,25 +437,21 @@ fn validate_state(
.get_amount_out(amount_in.clone(), &tokens[0], &tokens[1]) .get_amount_out(amount_in.clone(), &tokens[0], &tokens[1])
.map(|result| { .map(|result| {
info!( info!(
"Amount out for trading {:.1}% of max ({} -> {}): {} {} (gas: {})", "Amount out for trading {:.1}% of max: ({} {} -> {} {}) (gas: {})",
percentage * 100.0, percentage * 100.0,
amount_in,
&tokens[0].symbol, &tokens[0].symbol,
&tokens[1].symbol,
result.amount, result.amount,
&tokens[1].symbol, &tokens[1].symbol,
result.gas result.gas
) )
}) })
.map_err(|e| { .into_diagnostic()
info!( .wrap_err(format!(
"Error calculating amount out for Pool {:?} at {:.1}%: {:?}", "Error calculating amount out for Pool {:?} at {:.1}%.",
id, id,
percentage * 100.0, percentage * 100.0,
e ))?;
)
})
.ok();
}
} }
} }
} }