diff --git a/common/sell-bet.ts b/common/sell-bet.ts index 1bdc1fd6..5392ab67 100644 --- a/common/sell-bet.ts +++ b/common/sell-bet.ts @@ -88,6 +88,7 @@ export const getCpmmSellBetInfo = ( shares: number, outcome: 'YES' | 'NO', contract: FullContract, + 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, diff --git a/functions/src/sell-shares.ts b/functions/src/sell-shares.ts index a7753b0e..e4dbcbc9 100644 --- a/functions/src/sell-shares.ts +++ b/functions/src/sell-shares.ts @@ -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 )