Some cleanup
This commit is contained in:
parent
3d4f993998
commit
71401c4482
|
@ -1,4 +1,4 @@
|
||||||
import { sumBy } from 'lodash'
|
import { sortBy, sumBy } from 'lodash'
|
||||||
|
|
||||||
import { Bet, LimitBet, MAX_LOAN_PER_CONTRACT, NumericBet } from './bet'
|
import { Bet, LimitBet, MAX_LOAN_PER_CONTRACT, NumericBet } from './bet'
|
||||||
import {
|
import {
|
||||||
|
@ -152,11 +152,18 @@ const computeFill = (
|
||||||
export const getBinaryCpmmBetInfo = (
|
export const getBinaryCpmmBetInfo = (
|
||||||
outcome: 'YES' | 'NO',
|
outcome: 'YES' | 'NO',
|
||||||
betAmount: number,
|
betAmount: number,
|
||||||
contract: CPMMBinaryContract,
|
contract: CPMMBinaryContract | PseudoNumericContract,
|
||||||
limitProb: number | undefined,
|
limitProb: number | undefined,
|
||||||
unfilledBets: LimitBet[] // Sorted by limitProb, createdTime
|
unfilledBets: LimitBet[]
|
||||||
) => {
|
) => {
|
||||||
console.log({ outcome, betAmount, limitProb, unfilledBets })
|
const sortedBets = sortBy(
|
||||||
|
unfilledBets,
|
||||||
|
(bet) => (outcome === 'YES' ? bet.limitProb : -bet.limitProb),
|
||||||
|
(bet) => bet.createdTime
|
||||||
|
)
|
||||||
|
|
||||||
|
console.log({ outcome, betAmount, limitProb, sortedBets })
|
||||||
|
|
||||||
const takers: {
|
const takers: {
|
||||||
matchedBetId: string | null
|
matchedBetId: string | null
|
||||||
amount: number
|
amount: number
|
||||||
|
@ -170,19 +177,21 @@ export const getBinaryCpmmBetInfo = (
|
||||||
|
|
||||||
let i = 0
|
let i = 0
|
||||||
while (true) {
|
while (true) {
|
||||||
const matchedBet: LimitBet | undefined = unfilledBets[i]
|
const matchedBet: LimitBet | undefined = sortedBets[i]
|
||||||
const fill = computeFill(amount, outcome, limitProb, cpmmState, matchedBet)
|
const fill = computeFill(amount, outcome, limitProb, cpmmState, matchedBet)
|
||||||
if (!fill) break
|
if (!fill) break
|
||||||
|
|
||||||
const { maker, taker } = fill
|
const { taker, maker } = fill
|
||||||
|
|
||||||
amount -= taker.amount
|
amount -= taker.amount
|
||||||
|
|
||||||
if (maker.matchedBetId === null) {
|
if (maker.matchedBetId === null) {
|
||||||
|
// Matched against pool.
|
||||||
cpmmState = maker.state
|
cpmmState = maker.state
|
||||||
totalFees = addObjects(totalFees, maker.fees)
|
totalFees = addObjects(totalFees, maker.fees)
|
||||||
takers.push(taker)
|
takers.push(taker)
|
||||||
} else {
|
} else {
|
||||||
|
// Matched against bet.
|
||||||
takers.push(taker)
|
takers.push(taker)
|
||||||
makers.push(maker)
|
makers.push(maker)
|
||||||
i++
|
i++
|
||||||
|
@ -228,40 +237,6 @@ export const getBinaryCpmmBetInfo = (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getNewBinaryCpmmBetInfo = (
|
|
||||||
outcome: 'YES' | 'NO',
|
|
||||||
amount: number,
|
|
||||||
contract: CPMMBinaryContract | PseudoNumericContract,
|
|
||||||
loanAmount: number
|
|
||||||
) => {
|
|
||||||
const { shares, newPool, newP, fees } = calculateCpmmPurchase(
|
|
||||||
contract,
|
|
||||||
amount,
|
|
||||||
outcome
|
|
||||||
)
|
|
||||||
|
|
||||||
const { pool, p, totalLiquidity } = contract
|
|
||||||
const probBefore = getCpmmProbability(pool, p)
|
|
||||||
const probAfter = getCpmmProbability(newPool, newP)
|
|
||||||
|
|
||||||
const newBet: CandidateBet<Bet> = {
|
|
||||||
contractId: contract.id,
|
|
||||||
amount,
|
|
||||||
shares,
|
|
||||||
outcome,
|
|
||||||
fees,
|
|
||||||
loanAmount,
|
|
||||||
probBefore,
|
|
||||||
probAfter,
|
|
||||||
createdTime: Date.now(),
|
|
||||||
}
|
|
||||||
|
|
||||||
const { liquidityFee } = fees
|
|
||||||
const newTotalLiquidity = (totalLiquidity ?? 0) + liquidityFee
|
|
||||||
|
|
||||||
return { newBet, newPool, newP, newTotalLiquidity }
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getNewBinaryDpmBetInfo = (
|
export const getNewBinaryDpmBetInfo = (
|
||||||
outcome: 'YES' | 'NO',
|
outcome: 'YES' | 'NO',
|
||||||
amount: number,
|
amount: number,
|
||||||
|
|
|
@ -7,7 +7,6 @@ import { User } from '../../common/user'
|
||||||
import {
|
import {
|
||||||
BetInfo,
|
BetInfo,
|
||||||
getBinaryCpmmBetInfo,
|
getBinaryCpmmBetInfo,
|
||||||
getNewBinaryDpmBetInfo,
|
|
||||||
getNewMultiBetInfo,
|
getNewMultiBetInfo,
|
||||||
getNumericBetsInfo,
|
getNumericBetsInfo,
|
||||||
} from '../../common/new-bet'
|
} from '../../common/new-bet'
|
||||||
|
@ -16,7 +15,8 @@ import { redeemShares } from './redeem-shares'
|
||||||
import { log } from './utils'
|
import { log } from './utils'
|
||||||
import { LimitBet } from 'common/bet'
|
import { LimitBet } from 'common/bet'
|
||||||
import { Query } from 'firebase-admin/firestore'
|
import { Query } from 'firebase-admin/firestore'
|
||||||
import { sortBy, sumBy } from 'lodash'
|
import { sumBy } from 'lodash'
|
||||||
|
import { floatingEqual } from 'common/util/math'
|
||||||
|
|
||||||
const bodySchema = z.object({
|
const bodySchema = z.object({
|
||||||
contractId: z.string(),
|
contractId: z.string(),
|
||||||
|
@ -79,11 +79,8 @@ export const placebet = newEndpoint({}, async (req, auth) => {
|
||||||
> => {
|
> => {
|
||||||
if (
|
if (
|
||||||
(outcomeType == 'BINARY' || outcomeType === 'PSEUDO_NUMERIC') &&
|
(outcomeType == 'BINARY' || outcomeType === 'PSEUDO_NUMERIC') &&
|
||||||
mechanism == 'dpm-2'
|
mechanism == 'cpmm-1'
|
||||||
) {
|
) {
|
||||||
const { outcome } = validate(binarySchema, req.body)
|
|
||||||
return getNewBinaryDpmBetInfo(outcome, amount, contract, loanAmount)
|
|
||||||
} else if (outcomeType == 'BINARY' && mechanism == 'cpmm-1') {
|
|
||||||
const { outcome, limitProb } = validate(binarySchema, req.body)
|
const { outcome, limitProb } = validate(binarySchema, req.body)
|
||||||
const boundedLimitProb = limitProb ?? (outcome === 'YES' ? 1 : 0)
|
const boundedLimitProb = limitProb ?? (outcome === 'YES' ? 1 : 0)
|
||||||
const unfilledBetsQuery = contractDoc
|
const unfilledBetsQuery = contractDoc
|
||||||
|
@ -98,11 +95,7 @@ export const placebet = newEndpoint({}, async (req, auth) => {
|
||||||
) as Query<LimitBet>
|
) as Query<LimitBet>
|
||||||
|
|
||||||
const unfilledBetsSnap = await trans.get(unfilledBetsQuery)
|
const unfilledBetsSnap = await trans.get(unfilledBetsQuery)
|
||||||
const unfilledBets = sortBy(
|
const unfilledBets = unfilledBetsSnap.docs.map((doc) => doc.data())
|
||||||
unfilledBetsSnap.docs.map((doc) => doc.data()),
|
|
||||||
(bet) => (outcome === 'YES' ? bet.limitProb : -bet.limitProb),
|
|
||||||
(bet) => bet.createdTime
|
|
||||||
)
|
|
||||||
|
|
||||||
return getBinaryCpmmBetInfo(
|
return getBinaryCpmmBetInfo(
|
||||||
outcome,
|
outcome,
|
||||||
|
@ -146,8 +139,7 @@ export const placebet = newEndpoint({}, async (req, auth) => {
|
||||||
const newFill = { amount, shares, matchedBetId: betDoc.id }
|
const newFill = { amount, shares, matchedBetId: betDoc.id }
|
||||||
const fills = [...bet.fills, newFill]
|
const fills = [...bet.fills, newFill]
|
||||||
const totalShares = sumBy(fills, 'shares')
|
const totalShares = sumBy(fills, 'shares')
|
||||||
const isFilled =
|
const isFilled = floatingEqual(sumBy(fills, 'amount'), bet.amount)
|
||||||
Math.abs(sumBy(fills, 'amount') - bet.amount) < 0.0000001
|
|
||||||
|
|
||||||
log('Updated a matched limit bet.')
|
log('Updated a matched limit bet.')
|
||||||
trans.update(contractDoc.collection('bets').doc(bet.id), {
|
trans.update(contractDoc.collection('bets').doc(bet.id), {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user