Handle NaN and negative loan calcs

This commit is contained in:
James Grugett 2022-08-19 12:36:24 -05:00
parent d79783ea25
commit e8ead84793

View File

@ -73,17 +73,15 @@ async function updateLoansCore() {
await writeAsync(firestore, betUpdates) await writeAsync(firestore, betUpdates)
const userPayouts = eligibleUsers const userPayouts = eligibleUsers.map((user) => {
.map((user) => { const updates = userLoanUpdates.filter(
const updates = userLoanUpdates.filter( (update) => update.userId === user.id
(update) => update.userId === user.id )
) return {
return { user,
user, payout: sumBy(updates, (update) => update.newLoan),
payout: sumBy(updates, (update) => update.newLoan), }
} })
})
.filter((update) => update.payout > 0)
log(`${userPayouts.length} user payouts`) log(`${userPayouts.length} user payouts`)
@ -156,7 +154,7 @@ const getBinaryContractLoanUpdate = (contract: CPMMContract, bets: Bet[]) => {
const oldestBet = minBy(bets, (bet) => bet.createdTime) const oldestBet = minBy(bets, (bet) => bet.createdTime)
const newLoan = calculateNewLoan(invested, loanAmount) 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 const loanTotal = (oldestBet.loanAmount ?? 0) + newLoan
@ -180,6 +178,8 @@ const getFreeResponseContractLoanUpdate = (
const newLoan = calculateNewLoan(bet.amount, loanAmount) const newLoan = calculateNewLoan(bet.amount, loanAmount)
const loanTotal = loanAmount + newLoan const loanTotal = loanAmount + newLoan
if (isNaN(newLoan) || newLoan <= 0) return undefined
return { return {
userId: bet.userId, userId: bet.userId,
contractId: contract.id, contractId: contract.id,