Fix sell to pay back loans
This commit is contained in:
parent
75e48204ef
commit
ce76a9754b
|
@ -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,
|
||||||
|
|
|
@ -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
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user