From 997d68a5741a36910a7d56717856e6f4c103197c Mon Sep 17 00:00:00 2001 From: James Grugett Date: Mon, 15 Aug 2022 19:04:35 -0500 Subject: [PATCH] Compute invested & display in your bets --- common/calculate.ts | 27 +++++- web/components/bets-list.tsx | 164 +++++++++++++++++++---------------- 2 files changed, 112 insertions(+), 79 deletions(-) diff --git a/common/calculate.ts b/common/calculate.ts index d25fd313..dd5b590c 100644 --- a/common/calculate.ts +++ b/common/calculate.ts @@ -1,4 +1,4 @@ -import { maxBy } from 'lodash' +import { maxBy, sortBy, sum } from 'lodash' import { Bet, LimitBet } from './bet' import { calculateCpmmSale, @@ -133,8 +133,29 @@ export function resolvedPayout(contract: Contract, bet: Bet) { : calculateDpmPayout(contract, bet, outcome) } +function getCpmmInvested(yourBets: Bet[]) { + const totalShares: { [outcome: string]: number } = {} + const totalSpent: { [outcome: string]: number } = {} + + const sortedBets = sortBy(yourBets, 'createdTime') + for (const bet of sortedBets) { + const { outcome, shares, amount } = bet + if (amount > 0) { + totalShares[outcome] = (totalShares[outcome] ?? 0) + shares + totalSpent[outcome] = (totalSpent[outcome] ?? 0) + amount + } else if (amount < 0) { + const averagePrice = totalSpent[outcome] / totalShares[outcome] + totalShares[outcome] = totalShares[outcome] + shares + totalSpent[outcome] = totalSpent[outcome] + averagePrice * shares + } + } + + return sum(Object.values(totalSpent)) +} + export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) { const { resolution } = contract + const isCpmm = contract.mechanism === 'cpmm-1' let currentInvested = 0 let totalInvested = 0 @@ -178,8 +199,10 @@ export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) { (shares) => !floatingEqual(shares, 0) ) + const invested = isCpmm ? getCpmmInvested(yourBets) : currentInvested + return { - invested: Math.max(0, currentInvested), + invested, payout, netPayout, profit, diff --git a/web/components/bets-list.tsx b/web/components/bets-list.tsx index b919cccd..e8d85dba 100644 --- a/web/components/bets-list.tsx +++ b/web/components/bets-list.tsx @@ -428,95 +428,105 @@ export function BetsSummary(props: { : 'NO' : 'YES' + const canSell = + isYourBets && + isCpmm && + (isBinary || isPseudoNumeric) && + !isClosed && + !resolution && + hasShares && + sharesOutcome && + user + return ( - - {!isCpmm && ( + +
Invested
{formatMoney(invested)}
- )} - {resolution ? ( -
Payout
+
Profit
- {formatMoney(payout)} + {formatMoney(profit)}
- ) : isBinary ? ( - <> - -
- Payout if -
-
{formatMoney(yesWinnings)}
- - -
- Payout if -
-
{formatMoney(noWinnings)}
- - - ) : isPseudoNumeric ? ( - <> - -
- Payout if {'>='} {formatLargeNumber(contract.max)} -
-
{formatMoney(yesWinnings)}
- - -
- Payout if {'<='} {formatLargeNumber(contract.min)} -
-
{formatMoney(noWinnings)}
- - - ) : ( - -
- Current value -
-
{formatMoney(payout)}
- - )} - -
Profit
-
- {formatMoney(profit)} - {isYourBets && - isCpmm && - (isBinary || isPseudoNumeric) && - !isClosed && - !resolution && - hasShares && - sharesOutcome && - user && ( - <> - - {showSellModal && ( - - )} - + {canSell && ( + <> + + {showSellModal && ( + )} -
- -
+ + )} +
+ + {resolution ? ( + +
Payout
+
+ {formatMoney(payout)}{' '} + +
+ + ) : isBinary ? ( + <> + +
+ Payout if +
+
+ {formatMoney(yesWinnings)} +
+ + +
+ Payout if +
+
{formatMoney(noWinnings)}
+ + + ) : isPseudoNumeric ? ( + <> + +
+ Payout if {'>='} {formatLargeNumber(contract.max)} +
+
+ {formatMoney(yesWinnings)} +
+ + +
+ Payout if {'<='} {formatLargeNumber(contract.min)} +
+
{formatMoney(noWinnings)}
+ + + ) : ( + +
+ Current value +
+
{formatMoney(payout)}
+ + )} +
+ ) }