From c3b15ac5dcd3e3749739cb350bd3c370c8c7aaef Mon Sep 17 00:00:00 2001 From: mantikoros Date: Wed, 15 Jun 2022 11:33:58 -0500 Subject: [PATCH] prevent total liquidity stat from going negative --- functions/src/withdraw-liquidity.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/functions/src/withdraw-liquidity.ts b/functions/src/withdraw-liquidity.ts index cee3002e..4c48ce49 100644 --- a/functions/src/withdraw-liquidity.ts +++ b/functions/src/withdraw-liquidity.ts @@ -78,7 +78,13 @@ export const withdrawLiquidity = functions } as Partial) const newPool = subtractObjects(contract.pool, userShares) - const newTotalLiquidity = contract.totalLiquidity - payout + + const minPoolShares = Math.min(...Object.values(newPool)) + const adjustedTotal = contract.totalLiquidity - payout + + // total liquidity is a bogus number; use minPoolShares to prevent from going negative + const newTotalLiquidity = Math.max(adjustedTotal, minPoolShares) + trans.update(contractDoc, { pool: newPool, totalLiquidity: newTotalLiquidity,