2022-01-10 23:52:03 +00:00
|
|
|
import { Bet } from './bet'
|
|
|
|
import { Contract } from './contract'
|
|
|
|
import { CREATOR_FEE, PLATFORM_FEE } from './fees'
|
|
|
|
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: yesStart, NO: noStart } = contract.startPool
|
|
|
|
const { YES: yesShares, NO: noShares } = contract.totalShares
|
|
|
|
const { YES: yesBets, NO: noBets } = contract.totalBets
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
const [y, n, s] = [yesPool, noPool, shares]
|
2022-01-10 22:49:04 +00:00
|
|
|
|
|
|
|
const shareValue =
|
2022-01-10 23:52:03 +00:00
|
|
|
outcome === 'YES'
|
2022-01-10 22:49:04 +00:00
|
|
|
? // https://www.wolframalpha.com/input/?i=b+%2B+%28b+n%5E2%29%2F%28y+%28-b+%2B+y%29%29+%3D+c+solve+b
|
|
|
|
(n ** 2 +
|
|
|
|
s * y +
|
|
|
|
y ** 2 -
|
|
|
|
Math.sqrt(
|
|
|
|
n ** 4 + (s - y) ** 2 * y ** 2 + 2 * n ** 2 * y * (s + y)
|
|
|
|
)) /
|
|
|
|
(2 * y)
|
|
|
|
: (y ** 2 +
|
|
|
|
s * n +
|
|
|
|
n ** 2 -
|
|
|
|
Math.sqrt(
|
|
|
|
y ** 4 + (s - n) ** 2 * n ** 2 + 2 * y ** 2 * n * (s + n)
|
|
|
|
)) /
|
2022-01-10 23:52:03 +00:00
|
|
|
(2 * n)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
const startPool = yesStart + noStart
|
|
|
|
const pool = yesPool + noPool - startPool
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
const probBefore = yesPool ** 2 / (yesPool ** 2 + noPool ** 2)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
const f = pool / (probBefore * yesShares + (1 - probBefore) * noShares)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
const myPool = outcome === 'YES' ? yesPool - yesStart : noPool - noStart
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
const adjShareValue = Math.min(Math.min(1, f) * shareValue, myPool)
|
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
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
const probAfter = newPool.YES ** 2 / (newPool.YES ** 2 + newPool.NO ** 2)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
const creatorFee = CREATOR_FEE * adjShareValue
|
|
|
|
const saleAmount = (1 - CREATOR_FEE - PLATFORM_FEE) * 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
|
|
|
'M$/share:',
|
2022-01-10 22:49:04 +00:00
|
|
|
f,
|
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
|
|
|
}
|
|
|
|
}
|