Sell bet loan: negate buy bet's loan

This commit is contained in:
James Grugett 2022-08-19 18:16:13 -05:00
parent 2ba3ff8982
commit 1129b09898
3 changed files with 17 additions and 10 deletions

View File

@ -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<Contract>
@ -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
}

View File

@ -13,7 +13,7 @@ export type CandidateBet<T extends Bet> = Omit<T, 'id' | 'userId'>
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 {

View File

@ -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,