2022-01-10 23:52:03 +00:00
|
|
|
import { Bet } from './bet'
|
2022-03-15 22:27:51 +00:00
|
|
|
import {
|
|
|
|
getDpmProbability,
|
|
|
|
calculateDpmShareValue,
|
|
|
|
deductDpmFees,
|
|
|
|
} from './calculate-dpm'
|
|
|
|
import { calculateCpmmSale, getCpmmProbability } from './calculate-cpmm'
|
2022-06-01 02:42:35 +00:00
|
|
|
import { CPMMContract, DPMContract } from './contract'
|
2022-03-15 22:27:51 +00:00
|
|
|
import { DPM_CREATOR_FEE, DPM_PLATFORM_FEE, Fees } from './fees'
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-06-07 20:54:58 +00:00
|
|
|
export type CandidateBet<T extends Bet> = Omit<T, 'id' | 'userId'>
|
|
|
|
|
2022-06-07 21:08:56 +00:00
|
|
|
export const getSellBetInfo = (bet: Bet, contract: DPMContract) => {
|
2022-02-17 23:00:19 +00:00
|
|
|
const { pool, totalShares, totalBets } = contract
|
2022-06-07 21:08:56 +00:00
|
|
|
const { id: betId, amount, shares, outcome } = bet
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-03-15 22:27:51 +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-15 22:27:51 +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
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
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)
|
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
|
|
|
|
2022-06-07 21:08:56 +00:00
|
|
|
const newBet: CandidateBet<Bet> = {
|
2022-01-10 22:49:04 +00:00
|
|
|
contractId: contract.id,
|
|
|
|
amount: -adjShareValue,
|
|
|
|
shares: -shares,
|
|
|
|
outcome,
|
|
|
|
probBefore,
|
|
|
|
probAfter,
|
|
|
|
createdTime: Date.now(),
|
|
|
|
sale: {
|
|
|
|
amount: saleAmount,
|
|
|
|
betId,
|
|
|
|
},
|
2022-03-15 22:27:51 +00:00
|
|
|
fees,
|
2022-01-10 23:52:03 +00:00
|
|
|
}
|
2022-01-10 22:49:04 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
newBet,
|
|
|
|
newPool,
|
|
|
|
newTotalShares,
|
|
|
|
newTotalBets,
|
2022-03-15 22:27:51 +00:00
|
|
|
fees,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getCpmmSellBetInfo = (
|
2022-03-29 19:56:56 +00:00
|
|
|
shares: number,
|
|
|
|
outcome: 'YES' | 'NO',
|
2022-06-01 02:42:35 +00:00
|
|
|
contract: CPMMContract,
|
2022-06-07 20:54:58 +00:00
|
|
|
prevLoanAmount: number
|
2022-03-15 22:27:51 +00:00
|
|
|
) => {
|
|
|
|
const { pool, p } = contract
|
|
|
|
|
2022-03-30 02:30:04 +00:00
|
|
|
const { saleValue, newPool, newP, fees } = calculateCpmmSale(
|
|
|
|
contract,
|
2022-03-29 19:56:56 +00:00
|
|
|
shares,
|
2022-03-30 02:30:04 +00:00
|
|
|
outcome
|
|
|
|
)
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-03-31 05:54:37 +00:00
|
|
|
const loanPaid = Math.min(prevLoanAmount, saleValue)
|
2022-03-15 22:27:51 +00:00
|
|
|
const probBefore = getCpmmProbability(pool, p)
|
|
|
|
const probAfter = getCpmmProbability(newPool, p)
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
'SELL M$',
|
2022-03-29 19:56:56 +00:00
|
|
|
shares,
|
2022-03-15 22:27:51 +00:00
|
|
|
outcome,
|
|
|
|
'for M$',
|
|
|
|
saleValue,
|
|
|
|
'creator fee: M$',
|
|
|
|
fees.creatorFee
|
|
|
|
)
|
|
|
|
|
2022-06-07 20:54:58 +00:00
|
|
|
const newBet: CandidateBet<Bet> = {
|
2022-03-15 22:27:51 +00:00
|
|
|
contractId: contract.id,
|
|
|
|
amount: -saleValue,
|
|
|
|
shares: -shares,
|
|
|
|
outcome,
|
|
|
|
probBefore,
|
|
|
|
probAfter,
|
|
|
|
createdTime: Date.now(),
|
2022-03-31 05:54:37 +00:00
|
|
|
loanAmount: -loanPaid,
|
2022-03-15 22:27:51 +00:00
|
|
|
fees,
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
newBet,
|
|
|
|
newPool,
|
2022-03-29 19:56:56 +00:00
|
|
|
newP,
|
2022-03-15 22:27:51 +00:00
|
|
|
fees,
|
2022-01-10 23:52:03 +00:00
|
|
|
}
|
|
|
|
}
|