From e8ead847937c3deaed24c7bd21cbc93fc5c692ee Mon Sep 17 00:00:00 2001 From: James Grugett Date: Fri, 19 Aug 2022 12:36:24 -0500 Subject: [PATCH] Handle NaN and negative loan calcs --- functions/src/update-loans.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/functions/src/update-loans.ts b/functions/src/update-loans.ts index 2e01fd7b..5536ece8 100644 --- a/functions/src/update-loans.ts +++ b/functions/src/update-loans.ts @@ -73,17 +73,15 @@ async function updateLoansCore() { await writeAsync(firestore, betUpdates) - const userPayouts = eligibleUsers - .map((user) => { - const updates = userLoanUpdates.filter( - (update) => update.userId === user.id - ) - return { - user, - payout: sumBy(updates, (update) => update.newLoan), - } - }) - .filter((update) => update.payout > 0) + const userPayouts = eligibleUsers.map((user) => { + const updates = userLoanUpdates.filter( + (update) => update.userId === user.id + ) + return { + user, + payout: sumBy(updates, (update) => update.newLoan), + } + }) log(`${userPayouts.length} user payouts`) @@ -156,7 +154,7 @@ const getBinaryContractLoanUpdate = (contract: CPMMContract, bets: Bet[]) => { const oldestBet = minBy(bets, (bet) => bet.createdTime) const newLoan = calculateNewLoan(invested, loanAmount) - if (newLoan <= 0 || !oldestBet) return undefined + if (isNaN(newLoan) || newLoan <= 0 || !oldestBet) return undefined const loanTotal = (oldestBet.loanAmount ?? 0) + newLoan @@ -180,6 +178,8 @@ const getFreeResponseContractLoanUpdate = ( const newLoan = calculateNewLoan(bet.amount, loanAmount) const loanTotal = loanAmount + newLoan + if (isNaN(newLoan) || newLoan <= 0) return undefined + return { userId: bet.userId, contractId: contract.id,