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-03-02 21:12:27 +00:00
|
|
|
import {
|
|
|
|
calculateCpmmSale,
|
|
|
|
calculateCpmmShareValue,
|
|
|
|
getCpmmProbability,
|
|
|
|
} from './calculate-cpmm'
|
|
|
|
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-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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
const { saleValue, newPool } = calculateCpmmSale(contract, bet)
|
|
|
|
|
|
|
|
const probBefore = getCpmmProbability(pool)
|
|
|
|
const probAfter = getCpmmProbability(newPool)
|
|
|
|
|
|
|
|
const profit = saleValue - amount
|
|
|
|
const creatorFee = CREATOR_FEE * Math.max(0, profit)
|
|
|
|
const saleAmount = deductFees(amount, profit)
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|