Fix sell to pay back loans

This commit is contained in:
James Grugett 2022-03-31 00:54:37 -05:00
parent 75e48204ef
commit ce76a9754b
2 changed files with 9 additions and 1 deletions

View File

@ -88,6 +88,7 @@ export const getCpmmSellBetInfo = (
shares: number,
outcome: 'YES' | 'NO',
contract: FullContract<CPMM, Binary>,
prevLoanAmount: number,
newBetId: string
) => {
const { pool, p } = contract
@ -98,6 +99,9 @@ export const getCpmmSellBetInfo = (
outcome
)
const loanPaid = Math.min(prevLoanAmount, saleValue)
const netAmount = saleValue - loanPaid
const probBefore = getCpmmProbability(pool, p)
const probAfter = getCpmmProbability(newPool, p)
@ -121,10 +125,11 @@ export const getCpmmSellBetInfo = (
probBefore,
probAfter,
createdTime: Date.now(),
loanAmount: -loanPaid,
fees,
}
const newBalance = user.balance + saleValue
const newBalance = user.balance + netAmount
return {
newBet,

View File

@ -51,6 +51,8 @@ export const sellShares = functions.runWith({ minInstances: 1 }).https.onCall(
contractDoc.collection('bets').where('userId', '==', userId)
)
const prevLoanAmount = _.sumBy(userBets, (bet) => bet.loanAmount ?? 0)
const [yesBets, noBets] = _.partition(
userBets ?? [],
(bet) => bet.outcome === 'YES'
@ -77,6 +79,7 @@ export const sellShares = functions.runWith({ minInstances: 1 }).https.onCall(
shares,
outcome,
contract,
prevLoanAmount,
newBetDoc.id
)