Skip to content

Commit 98e2dd3

Browse files
authored
Merge pull request #398 from propeller-heads/usv4/tnl/ENG-5139-fix-panic
fix: (USV4) Used checked sub and propagate error
2 parents e8e0f12 + 7633604 commit 98e2dd3

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/evm/protocol/uniswap_v4/state.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{any::Any, collections::HashMap};
22

33
use alloy::primitives::{Address, Sign, I256, U256};
44
use num_bigint::BigUint;
5-
use num_traits::{ToPrimitive, Zero};
5+
use num_traits::{CheckedSub, ToPrimitive, Zero};
66
use revm::primitives::I128;
77
use tracing::trace;
88
use tycho_common::{
@@ -383,8 +383,19 @@ impl ProtocolSim for UniswapV4State {
383383
let y2 = self.get_amount_out(x2.clone(), base, quote)?;
384384

385385
// Calculate the marginal price
386-
let num = &y2.amount - &y1.amount;
387-
let den = &x2 - &x1;
386+
let num = y2
387+
.amount
388+
.checked_sub(&y1.amount)
389+
.ok_or_else(|| {
390+
SimulationError::FatalError(
391+
"Cannot calculate spot price: y2 < y1".to_string(),
392+
)
393+
})?;
394+
let den = x2.checked_sub(&x1).ok_or_else(|| {
395+
SimulationError::FatalError(
396+
"Cannot calculate spot price: x2 < x1".to_string(),
397+
)
398+
})?;
388399

389400
if den == BigUint::from(0u64) {
390401
return Err(SimulationError::FatalError(

0 commit comments

Comments
 (0)