placeBet for numeric markets
This commit is contained in:
parent
d99928d16e
commit
d38453b29a
|
@ -9,6 +9,7 @@ import {
|
||||||
} from './contract'
|
} from './contract'
|
||||||
import { DPM_FEES } from './fees'
|
import { DPM_FEES } from './fees'
|
||||||
import { normpdf } from './normal'
|
import { normpdf } from './normal'
|
||||||
|
import { addObjects } from './util/object'
|
||||||
|
|
||||||
export function getDpmProbability(totalShares: { [outcome: string]: number }) {
|
export function getDpmProbability(totalShares: { [outcome: string]: number }) {
|
||||||
// For binary contracts only.
|
// For binary contracts only.
|
||||||
|
@ -106,6 +107,33 @@ export function calculateDpmShares(
|
||||||
return Math.sqrt(bet ** 2 + shares ** 2 + c) - shares
|
return Math.sqrt(bet ** 2 + shares ** 2 + c) - shares
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const zigZagOrder = (length: number) => {
|
||||||
|
const mid = Math.floor(length / 2)
|
||||||
|
|
||||||
|
return _.range(0, mid)
|
||||||
|
.flatMap((i) => [i, length - i - 1])
|
||||||
|
.concat(length % 2 === 0 ? [] : [mid])
|
||||||
|
}
|
||||||
|
|
||||||
|
export function calculateNumericDpmShares(
|
||||||
|
totalShares: {
|
||||||
|
[outcome: string]: number
|
||||||
|
},
|
||||||
|
bets: [string, number][]
|
||||||
|
) {
|
||||||
|
const shares: number[] = []
|
||||||
|
|
||||||
|
totalShares = _.cloneDeep(totalShares)
|
||||||
|
|
||||||
|
for (let i of zigZagOrder(bets.length)) {
|
||||||
|
const [bucket, bet] = bets[i]
|
||||||
|
shares[i] = calculateDpmShares(totalShares, bet, bucket)
|
||||||
|
totalShares = addObjects(totalShares, { [bucket]: shares[i] })
|
||||||
|
}
|
||||||
|
|
||||||
|
return { shares, totalShares }
|
||||||
|
}
|
||||||
|
|
||||||
export function calculateDpmRawShareValue(
|
export function calculateDpmRawShareValue(
|
||||||
totalShares: {
|
totalShares: {
|
||||||
[outcome: string]: number
|
[outcome: string]: number
|
||||||
|
|
|
@ -5,6 +5,8 @@ import {
|
||||||
calculateDpmShares,
|
calculateDpmShares,
|
||||||
getDpmProbability,
|
getDpmProbability,
|
||||||
getDpmOutcomeProbability,
|
getDpmOutcomeProbability,
|
||||||
|
getNumericBets,
|
||||||
|
calculateNumericDpmShares,
|
||||||
} from './calculate-dpm'
|
} from './calculate-dpm'
|
||||||
import { calculateCpmmPurchase, getCpmmProbability } from './calculate-cpmm'
|
import { calculateCpmmPurchase, getCpmmProbability } from './calculate-cpmm'
|
||||||
import {
|
import {
|
||||||
|
@ -14,9 +16,11 @@ import {
|
||||||
FreeResponse,
|
FreeResponse,
|
||||||
FullContract,
|
FullContract,
|
||||||
Multi,
|
Multi,
|
||||||
|
NumericContract,
|
||||||
} from './contract'
|
} from './contract'
|
||||||
import { User } from './user'
|
import { User } from './user'
|
||||||
import { noFees } from './fees'
|
import { noFees } from './fees'
|
||||||
|
import { addObjects } from './util/object'
|
||||||
|
|
||||||
export const getNewBinaryCpmmBetInfo = (
|
export const getNewBinaryCpmmBetInfo = (
|
||||||
user: User,
|
user: User,
|
||||||
|
@ -154,6 +158,48 @@ export const getNewMultiBetInfo = (
|
||||||
return { newBet, newPool, newTotalShares, newTotalBets, newBalance }
|
return { newBet, newPool, newTotalShares, newTotalBets, newBalance }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const getNumericBetsInfo = (
|
||||||
|
user: User,
|
||||||
|
outcome: string,
|
||||||
|
amount: number,
|
||||||
|
contract: NumericContract
|
||||||
|
) => {
|
||||||
|
const { pool, totalShares, totalBets } = contract
|
||||||
|
|
||||||
|
const bets = getNumericBets(contract, outcome, amount)
|
||||||
|
const newPool = addObjects(pool, Object.fromEntries(bets))
|
||||||
|
|
||||||
|
const { shares, totalShares: newTotalShares } = calculateNumericDpmShares(
|
||||||
|
contract.totalShares,
|
||||||
|
bets
|
||||||
|
)
|
||||||
|
|
||||||
|
const newTotalBets = addObjects(totalBets, Object.fromEntries(bets))
|
||||||
|
|
||||||
|
const newBets = bets.map(([outcome, amount], i) => {
|
||||||
|
const probBefore = getDpmOutcomeProbability(totalShares, outcome)
|
||||||
|
const probAfter = getDpmOutcomeProbability(newTotalShares, outcome)
|
||||||
|
|
||||||
|
const newBet: Omit<Bet, 'id'> = {
|
||||||
|
userId: user.id,
|
||||||
|
contractId: contract.id,
|
||||||
|
amount,
|
||||||
|
shares: shares[i],
|
||||||
|
outcome,
|
||||||
|
probBefore,
|
||||||
|
probAfter,
|
||||||
|
createdTime: Date.now(),
|
||||||
|
fees: noFees,
|
||||||
|
}
|
||||||
|
|
||||||
|
return newBet
|
||||||
|
})
|
||||||
|
|
||||||
|
const newBalance = user.balance - amount
|
||||||
|
|
||||||
|
return { newBets, newPool, newTotalShares, newTotalBets, newBalance }
|
||||||
|
}
|
||||||
|
|
||||||
export const getLoanAmount = (yourBets: Bet[], newBetAmount: number) => {
|
export const getLoanAmount = (yourBets: Bet[], newBetAmount: number) => {
|
||||||
const openBets = yourBets.filter((bet) => !bet.isSold && !bet.sale)
|
const openBets = yourBets.filter((bet) => !bet.isSold && !bet.sale)
|
||||||
const prevLoanAmount = _.sumBy(openBets, (bet) => bet.loanAmount ?? 0)
|
const prevLoanAmount = _.sumBy(openBets, (bet) => bet.loanAmount ?? 0)
|
||||||
|
|
|
@ -8,6 +8,7 @@ import {
|
||||||
getNewBinaryDpmBetInfo,
|
getNewBinaryDpmBetInfo,
|
||||||
getNewMultiBetInfo,
|
getNewMultiBetInfo,
|
||||||
getLoanAmount,
|
getLoanAmount,
|
||||||
|
getNumericBetsInfo,
|
||||||
} from '../../common/new-bet'
|
} from '../../common/new-bet'
|
||||||
import { addObjects, removeUndefinedProps } from '../../common/util/object'
|
import { addObjects, removeUndefinedProps } from '../../common/util/object'
|
||||||
import { Bet } from '../../common/bet'
|
import { Bet } from '../../common/bet'
|
||||||
|
@ -77,6 +78,7 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
||||||
|
|
||||||
const {
|
const {
|
||||||
newBet,
|
newBet,
|
||||||
|
newBets,
|
||||||
newPool,
|
newPool,
|
||||||
newTotalShares,
|
newTotalShares,
|
||||||
newTotalBets,
|
newTotalBets,
|
||||||
|
@ -103,6 +105,8 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
||||||
loanAmount,
|
loanAmount,
|
||||||
newBetDoc.id
|
newBetDoc.id
|
||||||
) as any)
|
) as any)
|
||||||
|
: outcomeType === 'NUMERIC' && mechanism === 'dpm-2'
|
||||||
|
? getNumericBetsInfo(user, outcome, amount, contract)
|
||||||
: getNewMultiBetInfo(
|
: getNewMultiBetInfo(
|
||||||
user,
|
user,
|
||||||
outcome,
|
outcome,
|
||||||
|
@ -119,7 +123,17 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
transaction.create(newBetDoc, newBet)
|
if (newBet) transaction.create(newBetDoc, newBet)
|
||||||
|
|
||||||
|
if (newBets) {
|
||||||
|
for (let newBet of newBets) {
|
||||||
|
const newBetDoc = firestore
|
||||||
|
.collection(`contracts/${contractId}/bets`)
|
||||||
|
.doc()
|
||||||
|
|
||||||
|
transaction.create(newBetDoc, { id: newBetDoc.id, ...newBet })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
transaction.update(
|
transaction.update(
|
||||||
contractDoc,
|
contractDoc,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user