manifold/common/sell-bet.ts
mantikoros c183e00d47
Cfmm (#64)
* cpmm initial commit: common logic, cloud functions

* remove unnecessary property

* contract type

* rename 'calculate.ts' => 'calculate-dpm.ts'

* rename dpm calculations

* use focus hook

* mechanism-agnostic calculations

* bet panel: use new calculations

* use new calculations

* delete markets cloud function

* use correct contract type in scripts / functions

* calculate fixed payouts; bets list calculations

* new bet: use calculateCpmmPurchase

* getOutcomeProbabilityAfterBet

* use deductFixedFees

* fix auto-refactor

* fix antes

* separate logic to payouts-dpm, payouts-fixed

* liquidity provision tracking

* remove comment

* liquidity label

* create liquidity provision even if no ante bet

* liquidity fee

* use all bets for getFixedCancelPayouts

* updateUserBalance: allow negative balances

* store initialProbability in contracts

* turn on liquidity fee; turn off creator fee

* Include time param in tweet url, so image preview is re-fetched

* share redemption

* cpmm ContractBetsTable display

* formatMoney: handle minus zero

* filter out redemption bets

* track fees on contract and bets; change fee schedule for cpmm markets; only pay out creator fees at resolution

* small fixes

* small fixes

* Redeem shares pays back loans first

* Fix initial point on graph

* calculateCpmmPurchase: deduct creator fee

* Filter out redemption bets from feed

* set env to dev for user-testing purposes

* creator fees messaging

* new cfmm: k = y^(1-p) * n^p

* addCpmmLiquidity

* correct price function

* enable fees

* handle overflow

* liquidity provision tracking

* raise fees

* Fix merge error

* fix dpm free response payout for single outcome

* Fix DPM payout calculation

* Remove hardcoding as dev

Co-authored-by: James Grugett <jahooma@gmail.com>
2022-03-15 17:27:51 -05:00

136 lines
2.8 KiB
TypeScript

import { Bet } from './bet'
import {
getDpmProbability,
calculateDpmShareValue,
deductDpmFees,
} from './calculate-dpm'
import { calculateCpmmSale, getCpmmProbability } from './calculate-cpmm'
import { Binary, DPM, CPMM, FullContract } from './contract'
import { DPM_CREATOR_FEE, DPM_PLATFORM_FEE, Fees } from './fees'
import { User } from './user'
export const getSellBetInfo = (
user: User,
bet: Bet,
contract: FullContract<DPM, any>,
newBetId: string
) => {
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 = getDpmProbability(totalShares)
const probAfter = getDpmProbability(newTotalShares)
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: Bet = {
id: newBetId,
userId: user.id,
contractId: contract.id,
amount: -adjShareValue,
shares: -shares,
outcome,
probBefore,
probAfter,
createdTime: Date.now(),
sale: {
amount: saleAmount,
betId,
},
fees,
}
const newBalance = user.balance + saleAmount - (loanAmount ?? 0)
return {
newBet,
newPool,
newTotalShares,
newTotalBets,
newBalance,
fees,
}
}
export const getCpmmSellBetInfo = (
user: User,
bet: Bet,
contract: FullContract<CPMM, Binary>,
newBetId: string
) => {
const { pool, p } = contract
const { id: betId, amount, shares, outcome } = bet
const { saleValue, newPool, fees } = calculateCpmmSale(contract, bet)
const probBefore = getCpmmProbability(pool, p)
const probAfter = getCpmmProbability(newPool, p)
console.log(
'SELL M$',
amount,
outcome,
'for M$',
saleValue,
'creator fee: M$',
fees.creatorFee
)
const newBet: Bet = {
id: newBetId,
userId: user.id,
contractId: contract.id,
amount: -saleValue,
shares: -shares,
outcome,
probBefore,
probAfter,
createdTime: Date.now(),
sale: {
amount: saleValue,
betId,
},
fees,
}
const newBalance = user.balance + saleValue
return {
newBet,
newPool,
newBalance,
fees,
}
}