8b7cd20b6f
* Remove some old loan code * Almost complete implementation of updateLoans cloud function * Merge fixes * Use invested instead of sale value, check if eligible, perform payouts * Run monday 12am * Implement loan income notification * Fix imports * Loan update fixes / debug * Handle NaN and negative loan calcs * Working loan notification * Loan modal! * Move loans calculation to /common * Better layout * Pay back loan on sell shares * Pay back fraction of loan on redeem * Sell bet loan: negate buy bet's loan * Modal tweaks * Compute and store nextLoanCached for all users * lint * Update loans with newest portfolio * Filter loans to only unresolved contracts * Tweak spacing * Increase memory
136 lines
2.9 KiB
TypeScript
136 lines
2.9 KiB
TypeScript
import { Bet, LimitBet } from './bet'
|
|
import {
|
|
calculateDpmShareValue,
|
|
deductDpmFees,
|
|
getDpmOutcomeProbability,
|
|
} from './calculate-dpm'
|
|
import { calculateCpmmSale, getCpmmProbability } from './calculate-cpmm'
|
|
import { CPMMContract, DPMContract } from './contract'
|
|
import { DPM_CREATOR_FEE, DPM_PLATFORM_FEE, Fees } from './fees'
|
|
import { sumBy } from 'lodash'
|
|
|
|
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, loanAmount } = bet
|
|
|
|
const adjShareValue = calculateDpmShareValue(contract, bet)
|
|
|
|
const newPool = { ...pool, [outcome]: pool[outcome] - adjShareValue }
|
|
|
|
const newTotalShares = {
|
|
...totalShares,
|
|
[outcome]: totalShares[outcome] - shares,
|
|
}
|
|
|
|
const newTotalBets = { ...totalBets, [outcome]: totalBets[outcome] - amount }
|
|
|
|
const probBefore = getDpmOutcomeProbability(totalShares, outcome)
|
|
const probAfter = getDpmOutcomeProbability(newTotalShares, outcome)
|
|
|
|
const profit = adjShareValue - amount
|
|
|
|
const creatorFee = DPM_CREATOR_FEE * Math.max(0, profit)
|
|
const platformFee = DPM_PLATFORM_FEE * Math.max(0, profit)
|
|
const fees: Fees = {
|
|
creatorFee,
|
|
platformFee,
|
|
liquidityFee: 0,
|
|
}
|
|
|
|
const saleAmount = deductDpmFees(amount, adjShareValue)
|
|
|
|
console.log(
|
|
'SELL M$',
|
|
amount,
|
|
outcome,
|
|
'for M$',
|
|
saleAmount,
|
|
'creator fee: M$',
|
|
creatorFee
|
|
)
|
|
|
|
const newBet: CandidateBet<Bet> = {
|
|
contractId: contract.id,
|
|
amount: -adjShareValue,
|
|
shares: -shares,
|
|
outcome,
|
|
probBefore,
|
|
probAfter,
|
|
createdTime: Date.now(),
|
|
sale: {
|
|
amount: saleAmount,
|
|
betId,
|
|
},
|
|
fees,
|
|
loanAmount: -(loanAmount ?? 0),
|
|
}
|
|
|
|
return {
|
|
newBet,
|
|
newPool,
|
|
newTotalShares,
|
|
newTotalBets,
|
|
fees,
|
|
}
|
|
}
|
|
|
|
export const getCpmmSellBetInfo = (
|
|
shares: number,
|
|
outcome: 'YES' | 'NO',
|
|
contract: CPMMContract,
|
|
unfilledBets: LimitBet[],
|
|
loanPaid: number
|
|
) => {
|
|
const { pool, p } = contract
|
|
|
|
const { saleValue, cpmmState, fees, makers, takers } = calculateCpmmSale(
|
|
contract,
|
|
shares,
|
|
outcome,
|
|
unfilledBets
|
|
)
|
|
|
|
const probBefore = getCpmmProbability(pool, p)
|
|
const probAfter = getCpmmProbability(cpmmState.pool, cpmmState.p)
|
|
|
|
const takerAmount = sumBy(takers, 'amount')
|
|
const takerShares = sumBy(takers, 'shares')
|
|
|
|
console.log(
|
|
'SELL M$',
|
|
shares,
|
|
outcome,
|
|
'for M$',
|
|
saleValue,
|
|
'creator fee: M$',
|
|
fees.creatorFee
|
|
)
|
|
|
|
const newBet: CandidateBet<Bet> = {
|
|
contractId: contract.id,
|
|
amount: takerAmount,
|
|
shares: takerShares,
|
|
outcome,
|
|
probBefore,
|
|
probAfter,
|
|
createdTime: Date.now(),
|
|
loanAmount: -loanPaid,
|
|
fees,
|
|
fills: takers,
|
|
isFilled: true,
|
|
isCancelled: false,
|
|
orderAmount: takerAmount,
|
|
}
|
|
|
|
return {
|
|
newBet,
|
|
newPool: cpmmState.pool,
|
|
newP: cpmmState.p,
|
|
fees,
|
|
makers,
|
|
takers,
|
|
}
|
|
}
|