prevent total liquidity stat from going negative

This commit is contained in:
mantikoros 2022-06-15 11:33:58 -05:00
parent cc4b9abd9f
commit c3b15ac5dc

View File

@ -78,7 +78,13 @@ export const withdrawLiquidity = functions
} as Partial<User>)
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,