* Split BuyAmountInput out of AmountInput * Buy and sell tabs. Compute some sell info * In progress * BuyPanel & SellPanel with banner above that shows current shares and toggle button * Remove "Remaining balance" * Bring back 'Place a trade'. Tweaks * Sell shares cloud function. * Sell all shares by default. Switch back to buy if sell all your shares. * Cache your shares in local storage so sell banner doesn't flicker. * Compute sale value of shares with binary search to keep k constant. * Update bets table to show BUY or SELL * Fixes from Stephen's review * Don't allow selling more than max shares in cloud function * Use modal for sell shares on desktop. * Handle floating point precision in max shares you can sell.
136 lines
2.7 KiB
TypeScript
136 lines
2.7 KiB
TypeScript
import { Bet } from './bet'
|
|
import {
|
|
getDpmProbability,
|
|
calculateDpmShareValue,
|
|
deductDpmFees,
|
|
} from './calculate-dpm'
|
|
import { calculateCpmmSale, getCpmmProbability } from './calculate-cpmm'
|
|
import { Binary, DPM, CPMM, FullContract } from './contract'
|
|
import { DPM_CREATOR_FEE, DPM_PLATFORM_FEE, Fees } from './fees'
|
|
import { User } from './user'
|
|
|
|
export const getSellBetInfo = (
|
|
user: User,
|
|
bet: Bet,
|
|
contract: FullContract<DPM, any>,
|
|
newBetId: string
|
|
) => {
|
|
const { pool, totalShares, totalBets } = contract
|
|
const { id: betId, amount, shares, outcome, loanAmount } = bet
|
|
|
|
const adjShareValue = calculateDpmShareValue(contract, bet)
|
|
|
|
const newPool = { ...pool, [outcome]: pool[outcome] - adjShareValue }
|
|
|
|
const newTotalShares = {
|
|
...totalShares,
|
|
[outcome]: totalShares[outcome] - shares,
|
|
}
|
|
|
|
const newTotalBets = { ...totalBets, [outcome]: totalBets[outcome] - amount }
|
|
|
|
const probBefore = getDpmProbability(totalShares)
|
|
const probAfter = getDpmProbability(newTotalShares)
|
|
|
|
const profit = adjShareValue - amount
|
|
|
|
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)
|
|
|
|
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: -adjShareValue,
|
|
shares: -shares,
|
|
outcome,
|
|
probBefore,
|
|
probAfter,
|
|
createdTime: Date.now(),
|
|
sale: {
|
|
amount: saleAmount,
|
|
betId,
|
|
},
|
|
fees,
|
|
}
|
|
|
|
const newBalance = user.balance + saleAmount - (loanAmount ?? 0)
|
|
|
|
return {
|
|
newBet,
|
|
newPool,
|
|
newTotalShares,
|
|
newTotalBets,
|
|
newBalance,
|
|
fees,
|
|
}
|
|
}
|
|
|
|
export const getCpmmSellBetInfo = (
|
|
user: User,
|
|
shares: number,
|
|
outcome: 'YES' | 'NO',
|
|
contract: FullContract<CPMM, Binary>,
|
|
newBetId: string
|
|
) => {
|
|
const { pool, p } = contract
|
|
|
|
const { saleValue, newPool, newP, fees } = calculateCpmmSale(contract, {
|
|
shares,
|
|
outcome,
|
|
})
|
|
|
|
const probBefore = getCpmmProbability(pool, p)
|
|
const probAfter = getCpmmProbability(newPool, p)
|
|
|
|
console.log(
|
|
'SELL M$',
|
|
shares,
|
|
outcome,
|
|
'for M$',
|
|
saleValue,
|
|
'creator fee: M$',
|
|
fees.creatorFee
|
|
)
|
|
|
|
const newBet: Bet = {
|
|
id: newBetId,
|
|
userId: user.id,
|
|
contractId: contract.id,
|
|
amount: -saleValue,
|
|
shares: -shares,
|
|
outcome,
|
|
probBefore,
|
|
probAfter,
|
|
createdTime: Date.now(),
|
|
fees,
|
|
}
|
|
|
|
const newBalance = user.balance + saleValue
|
|
|
|
return {
|
|
newBet,
|
|
newPool,
|
|
newP,
|
|
newBalance,
|
|
fees,
|
|
}
|
|
}
|