f533d9bfcb
* Fetch balance of users with open limit orders & cancel orders with insufficient balance
* Fix imports
* Fix bugs
* Fix a bug
* Remove redundant cast
* buttons overlaying content fix (#1005)
* buttons overlaying content fix
* stats: round DAU number
* made set width for portfolio/profit fields (#1006)
* tournaments: included resolved markets
* made delete red, moved button for regular posts (#1008)
* Fix localstorage saved user being overwritten on every page load
* Market page: Show no right panel while user loading
* Don't flash sign in button if user is loading
* election map coloring
* market group modal scroll fix (#1009)
* midterms: posititoning, make mobile friendly
* Un-daisy share buttons (#1010)
* Make embed and challenge buttons non-daisyui
* Allow link Buttons. Change tweet, dupe buttons.
* lint
* don't insert extra lines when upload photos
* Map fixes (#1011)
* usa map: fix sizing
* useSetIframeBackbroundColor
* preload contracts
* seo
* remove hook
* turn off sprig on dev
* Render timestamp only on client to prevent error of server not matching client
* Make sized container have default height so graph doesn't jump
* midterms: use null in static props
* Create common card component (#1012)
* Create common card component
* lint
* add key prop to pills
* redirect to /home after login
* create market: use transaction
* card: reduce border size
* Update groupContracts in db trigger
* Default sort to best
* Save comment sort per user rather than per contract
* Refactor Pinned Items into a reusable component
* Revert "create market: use transaction"
This reverts commit e1f24f24a9
.
* Mark @v with a (Bot) label
* fix padding on daily movers
* fix type errors
* Wrap sprig init in check for window
* unindex date-docs from search engines
* Auto-prettification
* compute elasticity
* change dpm elasticity
* Fix google lighthouse issues (#1013)
* don't hide free response panel on open resolve
* liquidity sort
* Limit order trade log: '/' to 'of'. Remove 'of' in 'of YES'.
* Date doc: Toggle to disable creating a prediction market
* Listen for date doc changes
* Fix merge error
* Don't cancel all a users limit orders if they go negative
Co-authored-by: ingawei <46611122+ingawei@users.noreply.github.com>
Co-authored-by: mantikoros <sgrugett@gmail.com>
Co-authored-by: Sinclair Chen <abc.sinclair@gmail.com>
Co-authored-by: mantikoros <95266179+mantikoros@users.noreply.github.com>
Co-authored-by: Ian Philips <iansphilips@gmail.com>
Co-authored-by: Pico2x <pico2x@gmail.com>
Co-authored-by: Austin Chen <akrolsmir@gmail.com>
Co-authored-by: sipec <sipec@users.noreply.github.com>
245 lines
6.3 KiB
TypeScript
245 lines
6.3 KiB
TypeScript
import { last, sortBy, sum, sumBy, uniq } from 'lodash'
|
|
import { calculatePayout } from './calculate'
|
|
import { Bet, LimitBet } from './bet'
|
|
import { Contract, CPMMContract, DPMContract } from './contract'
|
|
import { PortfolioMetrics, User } from './user'
|
|
import { DAY_MS } from './util/time'
|
|
import { getBinaryCpmmBetInfo, getNewMultiBetInfo } from './new-bet'
|
|
import { getCpmmProbability } from './calculate-cpmm'
|
|
|
|
const computeInvestmentValue = (
|
|
bets: Bet[],
|
|
contractsDict: { [k: string]: Contract }
|
|
) => {
|
|
return sumBy(bets, (bet) => {
|
|
const contract = contractsDict[bet.contractId]
|
|
if (!contract || contract.isResolved) return 0
|
|
if (bet.sale || bet.isSold) return 0
|
|
|
|
const payout = calculatePayout(contract, bet, 'MKT')
|
|
const value = payout - (bet.loanAmount ?? 0)
|
|
if (isNaN(value)) return 0
|
|
return value
|
|
})
|
|
}
|
|
|
|
export const computeInvestmentValueCustomProb = (
|
|
bets: Bet[],
|
|
contract: Contract,
|
|
p: number
|
|
) => {
|
|
return sumBy(bets, (bet) => {
|
|
if (!contract || contract.isResolved) return 0
|
|
if (bet.sale || bet.isSold) return 0
|
|
const { outcome, shares } = bet
|
|
|
|
const betP = outcome === 'YES' ? p : 1 - p
|
|
|
|
const payout = betP * shares
|
|
const value = payout - (bet.loanAmount ?? 0)
|
|
if (isNaN(value)) return 0
|
|
return value
|
|
})
|
|
}
|
|
|
|
export const computeElasticity = (
|
|
bets: Bet[],
|
|
contract: Contract,
|
|
betAmount = 50
|
|
) => {
|
|
const { mechanism, outcomeType } = contract
|
|
return mechanism === 'cpmm-1' &&
|
|
(outcomeType === 'BINARY' || outcomeType === 'PSEUDO_NUMERIC')
|
|
? computeBinaryCpmmElasticity(bets, contract, betAmount)
|
|
: computeDpmElasticity(contract, betAmount)
|
|
}
|
|
|
|
export const computeBinaryCpmmElasticity = (
|
|
bets: Bet[],
|
|
contract: CPMMContract,
|
|
betAmount: number
|
|
) => {
|
|
const limitBets = bets
|
|
.filter(
|
|
(b) =>
|
|
!b.isFilled &&
|
|
!b.isSold &&
|
|
!b.isRedemption &&
|
|
!b.sale &&
|
|
!b.isCancelled &&
|
|
b.limitProb !== undefined
|
|
)
|
|
.sort((a, b) => a.createdTime - b.createdTime) as LimitBet[]
|
|
|
|
const userIds = uniq(limitBets.map((b) => b.userId))
|
|
// Assume all limit orders are good.
|
|
const userBalances = Object.fromEntries(
|
|
userIds.map((id) => [id, Number.MAX_SAFE_INTEGER])
|
|
)
|
|
|
|
const { newPool: poolY, newP: pY } = getBinaryCpmmBetInfo(
|
|
'YES',
|
|
betAmount,
|
|
contract,
|
|
undefined,
|
|
limitBets,
|
|
userBalances
|
|
)
|
|
const resultYes = getCpmmProbability(poolY, pY)
|
|
|
|
const { newPool: poolN, newP: pN } = getBinaryCpmmBetInfo(
|
|
'NO',
|
|
betAmount,
|
|
contract,
|
|
undefined,
|
|
limitBets,
|
|
userBalances
|
|
)
|
|
const resultNo = getCpmmProbability(poolN, pN)
|
|
|
|
return resultYes - resultNo
|
|
}
|
|
|
|
export const computeDpmElasticity = (
|
|
contract: DPMContract,
|
|
betAmount: number
|
|
) => {
|
|
return getNewMultiBetInfo('', 2 * betAmount, contract).newBet.probAfter
|
|
}
|
|
|
|
const computeTotalPool = (userContracts: Contract[], startTime = 0) => {
|
|
const periodFilteredContracts = userContracts.filter(
|
|
(contract) => contract.createdTime >= startTime
|
|
)
|
|
return sum(
|
|
periodFilteredContracts.map((contract) => sum(Object.values(contract.pool)))
|
|
)
|
|
}
|
|
|
|
export const computeVolume = (contractBets: Bet[], since: number) => {
|
|
return sumBy(contractBets, (b) =>
|
|
b.createdTime > since && !b.isRedemption ? Math.abs(b.amount) : 0
|
|
)
|
|
}
|
|
|
|
const calculateProbChangeSince = (descendingBets: Bet[], since: number) => {
|
|
const newestBet = descendingBets[0]
|
|
if (!newestBet) return 0
|
|
|
|
const betBeforeSince = descendingBets.find((b) => b.createdTime < since)
|
|
|
|
if (!betBeforeSince) {
|
|
const oldestBet = last(descendingBets) ?? newestBet
|
|
return newestBet.probAfter - oldestBet.probBefore
|
|
}
|
|
|
|
return newestBet.probAfter - betBeforeSince.probAfter
|
|
}
|
|
|
|
export const calculateProbChanges = (descendingBets: Bet[]) => {
|
|
const now = Date.now()
|
|
const yesterday = now - DAY_MS
|
|
const weekAgo = now - 7 * DAY_MS
|
|
const monthAgo = now - 30 * DAY_MS
|
|
|
|
return {
|
|
day: calculateProbChangeSince(descendingBets, yesterday),
|
|
week: calculateProbChangeSince(descendingBets, weekAgo),
|
|
month: calculateProbChangeSince(descendingBets, monthAgo),
|
|
}
|
|
}
|
|
|
|
export const calculateCreatorVolume = (userContracts: Contract[]) => {
|
|
const allTimeCreatorVolume = computeTotalPool(userContracts, 0)
|
|
const monthlyCreatorVolume = computeTotalPool(
|
|
userContracts,
|
|
Date.now() - 30 * DAY_MS
|
|
)
|
|
const weeklyCreatorVolume = computeTotalPool(
|
|
userContracts,
|
|
Date.now() - 7 * DAY_MS
|
|
)
|
|
|
|
const dailyCreatorVolume = computeTotalPool(
|
|
userContracts,
|
|
Date.now() - 1 * DAY_MS
|
|
)
|
|
|
|
return {
|
|
daily: dailyCreatorVolume,
|
|
weekly: weeklyCreatorVolume,
|
|
monthly: monthlyCreatorVolume,
|
|
allTime: allTimeCreatorVolume,
|
|
}
|
|
}
|
|
|
|
export const calculateNewPortfolioMetrics = (
|
|
user: User,
|
|
contractsById: { [k: string]: Contract },
|
|
currentBets: Bet[]
|
|
) => {
|
|
const investmentValue = computeInvestmentValue(currentBets, contractsById)
|
|
const newPortfolio = {
|
|
investmentValue: investmentValue,
|
|
balance: user.balance,
|
|
totalDeposits: user.totalDeposits,
|
|
timestamp: Date.now(),
|
|
userId: user.id,
|
|
}
|
|
return newPortfolio
|
|
}
|
|
|
|
const calculateProfitForPeriod = (
|
|
startTime: number,
|
|
descendingPortfolio: PortfolioMetrics[],
|
|
currentProfit: number
|
|
) => {
|
|
const startingPortfolio = descendingPortfolio.find(
|
|
(p) => p.timestamp < startTime
|
|
)
|
|
|
|
if (startingPortfolio === undefined) {
|
|
return currentProfit
|
|
}
|
|
|
|
const startingProfit = calculatePortfolioProfit(startingPortfolio)
|
|
|
|
return currentProfit - startingProfit
|
|
}
|
|
|
|
export const calculatePortfolioProfit = (portfolio: PortfolioMetrics) => {
|
|
return portfolio.investmentValue + portfolio.balance - portfolio.totalDeposits
|
|
}
|
|
|
|
export const calculateNewProfit = (
|
|
portfolioHistory: PortfolioMetrics[],
|
|
newPortfolio: PortfolioMetrics
|
|
) => {
|
|
const allTimeProfit = calculatePortfolioProfit(newPortfolio)
|
|
const descendingPortfolio = sortBy(
|
|
portfolioHistory,
|
|
(p) => p.timestamp
|
|
).reverse()
|
|
|
|
const newProfit = {
|
|
daily: calculateProfitForPeriod(
|
|
Date.now() - 1 * DAY_MS,
|
|
descendingPortfolio,
|
|
allTimeProfit
|
|
),
|
|
weekly: calculateProfitForPeriod(
|
|
Date.now() - 7 * DAY_MS,
|
|
descendingPortfolio,
|
|
allTimeProfit
|
|
),
|
|
monthly: calculateProfitForPeriod(
|
|
Date.now() - 30 * DAY_MS,
|
|
descendingPortfolio,
|
|
allTimeProfit
|
|
),
|
|
allTime: allTimeProfit,
|
|
}
|
|
|
|
return newProfit
|
|
}
|