2022-01-10 23:52:03 +00:00
|
|
|
import { Bet } from './bet'
|
2022-03-03 03:20:35 +00:00
|
|
|
import {
|
2022-03-03 03:30:07 +00:00
|
|
|
getDpmProbability,
|
|
|
|
calculateDpmShareValue,
|
|
|
|
deductDpmFees,
|
2022-03-03 03:20:35 +00:00
|
|
|
} from './calculate-dpm'
|
2022-03-03 17:19:18 +00:00
|
|
|
import { calculateCpmmSale, getCpmmProbability } from './calculate-cpmm'
|
2022-03-02 21:12:27 +00:00
|
|
|
import { Binary, DPM, CPMM, FullContract } 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,
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, any>,
|
2022-01-10 22:49:04 +00:00
|
|
|
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-03-03 03:30:07 +00:00
|
|
|
const adjShareValue = calculateDpmShareValue(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-03-03 03:30:07 +00:00
|
|
|
const probBefore = getDpmProbability(totalShares)
|
|
|
|
const probAfter = getDpmProbability(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)
|
2022-03-03 03:30:07 +00:00
|
|
|
const saleAmount = deductDpmFees(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
|
|
|
}
|
|
|
|
}
|
2022-03-02 21:12:27 +00:00
|
|
|
|
|
|
|
export const getCpmmSellBetInfo = (
|
|
|
|
user: User,
|
|
|
|
bet: Bet,
|
|
|
|
contract: FullContract<CPMM, Binary>,
|
|
|
|
newBetId: string
|
|
|
|
) => {
|
|
|
|
const { pool } = contract
|
|
|
|
const { id: betId, amount, shares, outcome } = bet
|
|
|
|
|
2022-03-03 17:19:18 +00:00
|
|
|
const { saleValue, newPool, creatorFee, saleAmount } = calculateCpmmSale(
|
|
|
|
contract,
|
|
|
|
bet
|
|
|
|
)
|
2022-03-02 21:12:27 +00:00
|
|
|
|
|
|
|
const probBefore = getCpmmProbability(pool)
|
|
|
|
const probAfter = getCpmmProbability(newPool)
|
|
|
|
|
|
|
|
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: -saleValue,
|
|
|
|
shares: -shares,
|
|
|
|
outcome,
|
|
|
|
probBefore,
|
|
|
|
probAfter,
|
|
|
|
createdTime: Date.now(),
|
|
|
|
sale: {
|
|
|
|
amount: saleAmount,
|
|
|
|
betId,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const newBalance = user.balance + saleAmount
|
|
|
|
|
|
|
|
return {
|
|
|
|
newBet,
|
|
|
|
newPool,
|
|
|
|
newBalance,
|
|
|
|
creatorFee,
|
|
|
|
}
|
|
|
|
}
|