Fix profit calculation for cpmm sales. Change totalValue to investment value excluding sales

This commit is contained in:
James Grugett 2022-04-13 17:25:32 -05:00
parent a9028b0a94
commit bfb708d477
2 changed files with 8 additions and 12 deletions

View File

@ -116,7 +116,6 @@ export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) {
const { resolution } = contract const { resolution } = contract
let invested = 0 let invested = 0
let salesInvested = 0
let payout = 0 let payout = 0
let loan = 0 let loan = 0
let sellProfit = 0 let sellProfit = 0
@ -126,7 +125,7 @@ export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) {
const { isSold, sale, amount, loanAmount, isRedemption } = bet const { isSold, sale, amount, loanAmount, isRedemption } = bet
if (isSold) { if (isSold) {
sellProfit -= amount sellProfit -= amount
salesInvested += amount invested += amount
} else if (sale) { } else if (sale) {
sellProfit += sale.amount sellProfit += sale.amount
} else { } else {
@ -134,6 +133,8 @@ export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) {
redeemed += -1 * amount redeemed += -1 * amount
} else if (amount > 0) { } else if (amount > 0) {
invested += amount invested += amount
} else {
sellProfit -= amount
} }
loan += loanAmount ?? 0 loan += loanAmount ?? 0
@ -143,17 +144,13 @@ export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) {
} }
} }
const investedIncludingSales = invested + salesInvested const profit = payout - invested + sellProfit + redeemed
const profitPercent = (profit / invested) * 100
const totalValue = payout + sellProfit + redeemed
const profit = totalValue - invested
const profitPercent = (profit / investedIncludingSales) * 100
const netInvestment = payout - loan const netInvestment = payout - loan
return { return {
invested, invested,
payout, payout,
totalValue,
profit, profit,
profitPercent, profitPercent,
netInvestment, netInvestment,
@ -164,7 +161,6 @@ export function getContractBetNullMetrics() {
return { return {
invested: 0, invested: 0,
payout: 0, payout: 0,
totalValue: 0,
profit: 0, profit: 0,
profitPercent: 0, profitPercent: 0,
netInvestment: 0, netInvestment: 0,

View File

@ -94,7 +94,7 @@ export function BetsList(props: { user: User }) {
} }
const SORTS: Record<BetSort, (c: Contract) => number> = { const SORTS: Record<BetSort, (c: Contract) => number> = {
profit: (c) => contractsMetrics[c.id].profit, profit: (c) => contractsMetrics[c.id].profit,
value: (c) => contractsMetrics[c.id].totalValue, value: (c) => contractsMetrics[c.id].payout,
newest: (c) => newest: (c) =>
Math.max(...contractBets[c.id].map((bet) => bet.createdTime)), Math.max(...contractBets[c.id].map((bet) => bet.createdTime)),
resolutionTime: (c) => -(c.resolutionTime ?? c.closeTime ?? Infinity), resolutionTime: (c) => -(c.resolutionTime ?? c.closeTime ?? Infinity),
@ -215,7 +215,7 @@ function MyContractBets(props: {
const isBinary = outcomeType === 'BINARY' const isBinary = outcomeType === 'BINARY'
const probPercent = getBinaryProbPercent(contract) const probPercent = getBinaryProbPercent(contract)
const { totalValue, profit, profitPercent } = getContractBetMetrics( const { payout, profit, profitPercent } = getContractBetMetrics(
contract, contract,
bets bets
) )
@ -271,7 +271,7 @@ function MyContractBets(props: {
<Row className="mr-5 justify-end sm:mr-8"> <Row className="mr-5 justify-end sm:mr-8">
<Col> <Col>
<div className="whitespace-nowrap text-right text-lg"> <div className="whitespace-nowrap text-right text-lg">
{formatMoney(metric === 'profit' ? profit : totalValue)} {formatMoney(metric === 'profit' ? profit : payout)}
</div> </div>
<div className="text-right"> <div className="text-right">
<ProfitBadge profitPercent={profitPercent} /> <ProfitBadge profitPercent={profitPercent} />