2022-01-10 23:52:03 +00:00
|
|
|
import { Bet } from './bet'
|
2022-02-14 02:24:03 +00:00
|
|
|
import { calculateShareValue, deductFees, getProbability } from './calculate'
|
2022-01-10 23:52:03 +00:00
|
|
|
import { Contract } from './contract'
|
2022-02-17 23:00:19 +00:00
|
|
|
import { CREATOR_FEE } 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-02-17 23:00:19 +00:00
|
|
|
const { pool, totalShares, totalBets } = contract
|
2022-03-02 03:31:48 +00:00
|
|
|
const { id: betId, amount, shares, outcome, loanAmount } = bet
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
const adjShareValue = calculateShareValue(contract, bet)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const newPool = { ...pool, [outcome]: pool[outcome] - adjShareValue }
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const newTotalShares = {
|
|
|
|
...totalShares,
|
|
|
|
[outcome]: totalShares[outcome] - shares,
|
|
|
|
}
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const newTotalBets = { ...totalBets, [outcome]: totalBets[outcome] - amount }
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const probBefore = getProbability(totalShares)
|
2022-01-12 19:01:04 +00:00
|
|
|
const probAfter = getProbability(newTotalShares)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-14 02:24:03 +00:00
|
|
|
const profit = adjShareValue - amount
|
|
|
|
const creatorFee = CREATOR_FEE * Math.max(0, profit)
|
|
|
|
const saleAmount = deductFees(amount, 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-03-02 03:31:48 +00:00
|
|
|
const newBalance = user.balance + saleAmount - (loanAmount ?? 0)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
newBet,
|
|
|
|
newPool,
|
|
|
|
newTotalShares,
|
|
|
|
newTotalBets,
|
|
|
|
newBalance,
|
|
|
|
creatorFee,
|
2022-01-10 23:52:03 +00:00
|
|
|
}
|
|
|
|
}
|