Skip to content

Commit

Permalink
Added error handling on underflowed liquidity
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOsiris committed May 1, 2023
1 parent 986abb8 commit b910988
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/amm/uniswap_v3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,11 @@ impl AutomatedMarketMaker for UniswapV3Pool {
}

current_state.liquidity = if liquidity_net < 0 {
current_state.liquidity - (-liquidity_net as u128)
if current_state.liquidity < (-liquidity_net as u128) {
return Err(SwapSimulationError::LiquidityUnderflow);
} else {
current_state.liquidity - (-liquidity_net as u128)
}
} else {
current_state.liquidity + (liquidity_net as u128)
};
Expand Down Expand Up @@ -398,7 +402,11 @@ impl AutomatedMarketMaker for UniswapV3Pool {
}

current_state.liquidity = if liquidity_net < 0 {
current_state.liquidity - (-liquidity_net as u128)
if current_state.liquidity < (-liquidity_net as u128) {
return Err(SwapSimulationError::LiquidityUnderflow);
} else {
current_state.liquidity - (-liquidity_net as u128)
}
} else {
current_state.liquidity + (liquidity_net as u128)
};
Expand Down
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,6 @@ pub enum SwapSimulationError {
InvalidTick,
#[error("Uniswap v3 math error")]
UniswapV3MathError(#[from] UniswapV3MathError),
#[error("Liquidity underflow")]
LiquidityUnderflow,
}

0 comments on commit b910988

Please sign in to comment.