diff --git a/common/loans.ts b/common/loans.ts index c07a7ba6..049b3476 100644 --- a/common/loans.ts +++ b/common/loans.ts @@ -1,9 +1,21 @@ import { Dictionary, groupBy, sumBy, minBy } from 'lodash' import { Bet } from './bet' import { getContractBetMetrics } from './calculate' -import { Contract, CPMMContract, FreeResponseContract, MultipleChoiceContract } from './contract' +import { + Contract, + CPMMContract, + FreeResponseContract, + MultipleChoiceContract, +} from './contract' import { filterDefined } from './util/array' +const LOAN_WEEKLY_RATE = 0.05 + +const calculateNewLoan = (investedValue: number, loanTotal: number) => { + const netValue = investedValue - loanTotal + return netValue * LOAN_WEEKLY_RATE +} + export const getUserLoanUpdates = ( bets: Bet[], contractsById: Dictionary @@ -80,10 +92,3 @@ const getFreeResponseContractLoanUpdate = ( } }) } - -const LOAN_WEEKLY_RATE = 0.05 - -const calculateNewLoan = (investedValue: number, loanTotal: number) => { - const netValue = investedValue - loanTotal - return netValue * LOAN_WEEKLY_RATE -} \ No newline at end of file diff --git a/common/sell-bet.ts b/common/sell-bet.ts index 79470d70..bc8fe596 100644 --- a/common/sell-bet.ts +++ b/common/sell-bet.ts @@ -13,7 +13,7 @@ export type CandidateBet = Omit export const getSellBetInfo = (bet: Bet, contract: DPMContract) => { const { pool, totalShares, totalBets } = contract - const { id: betId, amount, shares, outcome } = bet + const { id: betId, amount, shares, outcome, loanAmount } = bet const adjShareValue = calculateDpmShareValue(contract, bet) @@ -64,6 +64,7 @@ export const getSellBetInfo = (bet: Bet, contract: DPMContract) => { betId, }, fees, + loanAmount: -(loanAmount ?? 0), } return { diff --git a/functions/src/sell-bet.ts b/functions/src/sell-bet.ts index 18df4536..22dc3f12 100644 --- a/functions/src/sell-bet.ts +++ b/functions/src/sell-bet.ts @@ -50,11 +50,12 @@ export const sellbet = newEndpoint({}, async (req, auth) => { /* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */ const saleAmount = newBet.sale!.amount - const newBalance = user.balance + saleAmount - (bet.loanAmount ?? 0) + const newBalance = user.balance + saleAmount + (newBet.loanAmount ?? 0) const newBetDoc = firestore.collection(`contracts/${contractId}/bets`).doc() transaction.update(userDoc, { balance: newBalance }) transaction.update(betDoc, { isSold: true }) + // Note: id should have been newBetDoc.id! But leaving it for now so it's consistent. transaction.create(newBetDoc, { id: betDoc.id, userId: user.id, ...newBet }) transaction.update( contractDoc,