2022-03-02 03:31:48 +00:00
|
|
|
import * as _ from 'lodash'
|
2022-03-03 16:56:21 +00:00
|
|
|
|
2022-03-02 03:31:48 +00:00
|
|
|
import { Bet, MAX_LOAN_PER_CONTRACT } from './bet'
|
2022-02-17 23:00:19 +00:00
|
|
|
import {
|
2022-03-03 03:30:07 +00:00
|
|
|
calculateDpmShares,
|
|
|
|
getDpmProbability,
|
|
|
|
getDpmOutcomeProbability,
|
2022-03-03 03:20:35 +00:00
|
|
|
} from './calculate-dpm'
|
2022-03-03 16:56:21 +00:00
|
|
|
import { calculateCpmmPurchase, getCpmmProbability } from './calculate-cpmm'
|
2022-03-02 21:12:27 +00:00
|
|
|
import {
|
|
|
|
Binary,
|
|
|
|
CPMM,
|
|
|
|
DPM,
|
|
|
|
FreeResponse,
|
|
|
|
FullContract,
|
|
|
|
Multi,
|
|
|
|
} from './contract'
|
2022-01-10 23:52:03 +00:00
|
|
|
import { User } from './user'
|
2022-03-10 20:55:23 +00:00
|
|
|
import { noFees } from './fees'
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-03-02 21:12:27 +00:00
|
|
|
export const getNewBinaryCpmmBetInfo = (
|
|
|
|
user: User,
|
|
|
|
outcome: 'YES' | 'NO',
|
|
|
|
amount: number,
|
|
|
|
contract: FullContract<CPMM, Binary>,
|
2022-03-02 21:22:16 +00:00
|
|
|
loanAmount: number,
|
2022-03-02 21:12:27 +00:00
|
|
|
newBetId: string
|
|
|
|
) => {
|
2022-03-10 20:55:23 +00:00
|
|
|
const { shares, newPool, fees } = calculateCpmmPurchase(
|
|
|
|
contract,
|
|
|
|
amount,
|
|
|
|
outcome
|
|
|
|
)
|
2022-03-02 21:12:27 +00:00
|
|
|
|
2022-03-02 21:22:16 +00:00
|
|
|
const newBalance = user.balance - (amount - loanAmount)
|
2022-03-02 21:12:27 +00:00
|
|
|
|
2022-03-03 16:56:21 +00:00
|
|
|
const probBefore = getCpmmProbability(contract.pool)
|
2022-03-02 21:12:27 +00:00
|
|
|
const probAfter = getCpmmProbability(newPool)
|
|
|
|
|
|
|
|
const newBet: Bet = {
|
|
|
|
id: newBetId,
|
|
|
|
userId: user.id,
|
|
|
|
contractId: contract.id,
|
|
|
|
amount,
|
|
|
|
shares,
|
|
|
|
outcome,
|
2022-03-10 20:55:23 +00:00
|
|
|
fees,
|
2022-03-02 21:22:16 +00:00
|
|
|
loanAmount,
|
2022-03-02 21:12:27 +00:00
|
|
|
probBefore,
|
|
|
|
probAfter,
|
|
|
|
createdTime: Date.now(),
|
|
|
|
}
|
|
|
|
|
2022-03-10 20:55:23 +00:00
|
|
|
return { newBet, newPool, newBalance, fees }
|
2022-03-02 21:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getNewBinaryDpmBetInfo = (
|
2022-01-10 22:49:04 +00:00
|
|
|
user: User,
|
2022-01-10 23:52:03 +00:00
|
|
|
outcome: 'YES' | 'NO',
|
2022-01-10 22:49:04 +00:00
|
|
|
amount: number,
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, Binary>,
|
2022-03-02 03:31:48 +00:00
|
|
|
loanAmount: number,
|
2022-01-10 22:49:04 +00:00
|
|
|
newBetId: string
|
|
|
|
) => {
|
2022-01-10 23:52:03 +00:00
|
|
|
const { YES: yesPool, NO: noPool } = contract.pool
|
2022-01-10 22:49:04 +00:00
|
|
|
|
|
|
|
const newPool =
|
2022-01-10 23:52:03 +00:00
|
|
|
outcome === 'YES'
|
2022-01-10 22:49:04 +00:00
|
|
|
? { YES: yesPool + amount, NO: noPool }
|
2022-01-10 23:52:03 +00:00
|
|
|
: { YES: yesPool, NO: noPool + amount }
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
const shares = calculateDpmShares(contract.totalShares, amount, outcome)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
const { YES: yesShares, NO: noShares } = contract.totalShares
|
2022-01-10 22:49:04 +00:00
|
|
|
|
|
|
|
const newTotalShares =
|
2022-01-10 23:52:03 +00:00
|
|
|
outcome === 'YES'
|
2022-01-10 22:49:04 +00:00
|
|
|
? { YES: yesShares + shares, NO: noShares }
|
2022-01-10 23:52:03 +00:00
|
|
|
: { YES: yesShares, NO: noShares + shares }
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
const { YES: yesBets, NO: noBets } = contract.totalBets
|
2022-01-10 22:49:04 +00:00
|
|
|
|
|
|
|
const newTotalBets =
|
2022-01-10 23:52:03 +00:00
|
|
|
outcome === 'YES'
|
2022-01-10 22:49:04 +00:00
|
|
|
? { YES: yesBets + amount, NO: noBets }
|
2022-01-10 23:52:03 +00:00
|
|
|
: { YES: yesBets, NO: noBets + amount }
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
const probBefore = getDpmProbability(contract.totalShares)
|
|
|
|
const probAfter = getDpmProbability(newTotalShares)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
|
|
|
const newBet: Bet = {
|
|
|
|
id: newBetId,
|
|
|
|
userId: user.id,
|
|
|
|
contractId: contract.id,
|
|
|
|
amount,
|
2022-03-02 03:31:48 +00:00
|
|
|
loanAmount,
|
2022-01-10 22:49:04 +00:00
|
|
|
shares,
|
|
|
|
outcome,
|
|
|
|
probBefore,
|
|
|
|
probAfter,
|
|
|
|
createdTime: Date.now(),
|
2022-03-10 20:55:23 +00:00
|
|
|
fees: noFees,
|
2022-01-10 23:52:03 +00:00
|
|
|
}
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-03-02 03:31:48 +00:00
|
|
|
const newBalance = user.balance - (amount - loanAmount)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
return { newBet, newPool, newTotalShares, newTotalBets, newBalance }
|
|
|
|
}
|
2022-02-17 23:00:19 +00:00
|
|
|
|
|
|
|
export const getNewMultiBetInfo = (
|
|
|
|
user: User,
|
|
|
|
outcome: string,
|
|
|
|
amount: number,
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, Multi | FreeResponse>,
|
2022-03-02 03:31:48 +00:00
|
|
|
loanAmount: number,
|
2022-02-17 23:00:19 +00:00
|
|
|
newBetId: string
|
|
|
|
) => {
|
|
|
|
const { pool, totalShares, totalBets } = contract
|
|
|
|
|
|
|
|
const prevOutcomePool = pool[outcome] ?? 0
|
|
|
|
const newPool = { ...pool, [outcome]: prevOutcomePool + amount }
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
const shares = calculateDpmShares(contract.totalShares, amount, outcome)
|
2022-02-17 23:00:19 +00:00
|
|
|
|
|
|
|
const prevShares = totalShares[outcome] ?? 0
|
|
|
|
const newTotalShares = { ...totalShares, [outcome]: prevShares + shares }
|
|
|
|
|
|
|
|
const prevTotalBets = totalBets[outcome] ?? 0
|
|
|
|
const newTotalBets = { ...totalBets, [outcome]: prevTotalBets + amount }
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
const probBefore = getDpmOutcomeProbability(totalShares, outcome)
|
|
|
|
const probAfter = getDpmOutcomeProbability(newTotalShares, outcome)
|
2022-02-17 23:00:19 +00:00
|
|
|
|
|
|
|
const newBet: Bet = {
|
|
|
|
id: newBetId,
|
|
|
|
userId: user.id,
|
|
|
|
contractId: contract.id,
|
|
|
|
amount,
|
2022-03-02 03:31:48 +00:00
|
|
|
loanAmount,
|
2022-02-17 23:00:19 +00:00
|
|
|
shares,
|
|
|
|
outcome,
|
|
|
|
probBefore,
|
|
|
|
probAfter,
|
|
|
|
createdTime: Date.now(),
|
2022-03-10 20:55:23 +00:00
|
|
|
fees: noFees,
|
2022-02-17 23:00:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 03:31:48 +00:00
|
|
|
const newBalance = user.balance - (amount - loanAmount)
|
2022-02-17 23:00:19 +00:00
|
|
|
|
|
|
|
return { newBet, newPool, newTotalShares, newTotalBets, newBalance }
|
|
|
|
}
|
2022-03-02 03:31:48 +00:00
|
|
|
|
|
|
|
export const getLoanAmount = (yourBets: Bet[], newBetAmount: number) => {
|
|
|
|
const openBets = yourBets.filter((bet) => !bet.isSold && !bet.sale)
|
|
|
|
const prevLoanAmount = _.sumBy(openBets, (bet) => bet.loanAmount ?? 0)
|
|
|
|
const loanAmount = Math.min(
|
|
|
|
newBetAmount,
|
|
|
|
MAX_LOAN_PER_CONTRACT - prevLoanAmount
|
|
|
|
)
|
|
|
|
return loanAmount
|
|
|
|
}
|