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

View File

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