manifold/common/sell-bet.ts

79 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-01-10 23:52:03 +00:00
import { Bet } from './bet'
import { calculateShareValue, getProbability } from './calculate'
2022-01-10 23:52:03 +00:00
import { Contract } from './contract'
import { CREATOR_FEE, FEES } from './fees'
2022-01-10 23:52:03 +00:00
import { User } from './user'
2022-01-10 22:49:04 +00:00
export const getSellBetInfo = (
user: User,
bet: Bet,
contract: Contract,
newBetId: string
) => {
2022-01-10 23:52:03 +00:00
const { id: betId, amount, shares, outcome } = bet
2022-01-10 22:49:04 +00:00
2022-01-10 23:52:03 +00:00
const { YES: yesPool, NO: noPool } = contract.pool
const { YES: yesShares, NO: noShares } = contract.totalShares
const { YES: yesBets, NO: noBets } = contract.totalBets
2022-01-10 22:49:04 +00:00
const adjShareValue = calculateShareValue(contract, bet)
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 - adjShareValue, NO: noPool }
2022-01-10 23:52:03 +00:00
: { YES: yesPool, NO: noPool - adjShareValue }
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
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
const probBefore = getProbability(contract.totalShares)
const probAfter = getProbability(newTotalShares)
2022-01-10 22:49:04 +00:00
2022-01-10 23:52:03 +00:00
const creatorFee = CREATOR_FEE * adjShareValue
const saleAmount = (1 - FEES) * adjShareValue
2022-01-10 22:49:04 +00:00
console.log(
2022-01-10 23:52:03 +00:00
'SELL M$',
2022-01-10 22:49:04 +00:00
amount,
outcome,
2022-01-10 23:52:03 +00:00
'for M$',
2022-01-10 22:49:04 +00:00
saleAmount,
2022-01-10 23:52:03 +00:00
'creator fee: M$',
2022-01-10 22:49:04 +00:00
creatorFee
2022-01-10 23:52:03 +00:00
)
2022-01-10 22:49:04 +00:00
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,
},
2022-01-10 23:52:03 +00:00
}
2022-01-10 22:49:04 +00:00
2022-01-10 23:52:03 +00:00
const newBalance = user.balance + saleAmount
2022-01-10 22:49:04 +00:00
return {
newBet,
newPool,
newTotalShares,
newTotalBets,
newBalance,
creatorFee,
2022-01-10 23:52:03 +00:00
}
}