From 84d66d846274e8279e099553ece128bff7287250 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Mon, 15 Aug 2022 15:36:22 -0500 Subject: [PATCH] Merge fixes --- functions/src/create-answer.ts | 4 +--- functions/src/update-loans.ts | 40 ++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/functions/src/create-answer.ts b/functions/src/create-answer.ts index 2abaf44d..0b8b4e7a 100644 --- a/functions/src/create-answer.ts +++ b/functions/src/create-answer.ts @@ -75,10 +75,8 @@ export const createanswer = newEndpoint(opts, async (req, auth) => { } transaction.create(newAnswerDoc, answer) - const loanAmount = 0 - const { newBet, newPool, newTotalShares, newTotalBets } = - getNewMultiBetInfo(answerId, amount, contract, loanAmount) + getNewMultiBetInfo(answerId, amount, contract) const newBalance = user.balance - amount const betDoc = firestore.collection(`contracts/${contractId}/bets`).doc() diff --git a/functions/src/update-loans.ts b/functions/src/update-loans.ts index 7e442a8f..d574f304 100644 --- a/functions/src/update-loans.ts +++ b/functions/src/update-loans.ts @@ -1,7 +1,7 @@ import * as functions from 'firebase-functions' import * as admin from 'firebase-admin' import { getValues, log, writeAsync } from './utils' -import { Bet } from 'common/bet' +import { Bet, LimitBet } from 'common/bet' import { Contract, CPMMContract, FreeResponseContract } from 'common/contract' import { User } from 'common/user' import { Dictionary, groupBy, keyBy, minBy, sumBy } from 'lodash' @@ -34,21 +34,23 @@ async function updateLoansCore() { const contractsById = keyBy(contracts, (contract) => contract.id) const betsByUser = groupBy(bets, (bet) => bet.userId) - const userLoanUpdates = users.map((user) => - getUserLoanUpdates(betsByUser[user.id] ?? [], contractsById).betUpdates - ).flat() + const userLoanUpdates = users + .map( + (user) => + getUserLoanUpdates(betsByUser[user.id] ?? [], contractsById).betUpdates + ) + .flat() - const betUpdates = userLoanUpdates - .map((update) => ({ - doc: firestore - .collection('contracts') - .doc(update.contractId) - .collection('bets') - .doc(update.betId), - fields: { - loanAmount: update.loanTotal, - }, - })) + const betUpdates = userLoanUpdates.map((update) => ({ + doc: firestore + .collection('contracts') + .doc(update.contractId) + .collection('bets') + .doc(update.betId), + fields: { + loanAmount: update.loanTotal, + }, + })) await writeAsync(firestore, betUpdates) } @@ -91,7 +93,13 @@ const getBinaryContractLoanUpdate = (contract: CPMMContract, bets: Bet[]) => { const shares = YES || NO const outcome = YES ? 'YES' : 'NO' - const { saleValue } = calculateCpmmSale(contract, shares, outcome) + const unfilledBets: LimitBet[] = [] + const { saleValue } = calculateCpmmSale( + contract, + shares, + outcome, + unfilledBets + ) const loanAmount = sumBy(bets, (bet) => bet.loanAmount ?? 0) const oldestBet = minBy(bets, (bet) => bet.createdTime)