Skip to content

Commit

Permalink
Add proper loan numbers to multi-sell panel
Browse files Browse the repository at this point in the history
  • Loading branch information
IanPhilips committed Dec 6, 2024
1 parent 80d52dd commit 4220f89
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion backend/api/src/multi-sell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { APIError, type APIHandler } from './helpers/endpoint'
import { onCreateBets } from 'api/on-create-bet'
import { executeNewBetResult } from 'api/place-bet'
import { getContract, getUser, log } from 'shared/utils'
import { groupBy, keyBy, keyBy, mapValues, sumBy } from 'lodash'
import { groupBy, keyBy, mapValues, sumBy } from 'lodash'
import { getCpmmMultiSellSharesInfo } from 'common/sell-bet'
import { runTransactionWithRetries } from 'shared/transact-with-retries'
import { convertBet } from 'common/supabase/bets'
Expand Down
14 changes: 12 additions & 2 deletions web/components/answers/numeric-sell-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ import { useUnfilledBetsAndBalanceByUserId } from 'web/hooks/use-bets'
import { api } from 'web/lib/api/api'
import { MoneyDisplay } from '../bet/money-display'
import { useUserContractBets } from 'web/hooks/use-user-bets'
import { useAllSavedContractMetrics } from 'web/hooks/use-saved-contract-metrics'
import { ContractMetric } from 'common/contract-metric'

export const NumericSellPanel = (props: {
contract: CPMMNumericContract
userBets: Bet[]
contractMetrics: ContractMetric[]
cancel: () => void
}) => {
const { contract, userBets, cancel } = props
const { contract, userBets, contractMetrics, cancel } = props
const { answers, min: minimum, max: maximum } = contract
const isCashContract = contract.token === 'CASH'
const expectedValue = getExpectedValue(contract)
Expand Down Expand Up @@ -150,13 +153,16 @@ export const NumericSellPanel = (props: {
const betsOnAnswersToSell = userBets.filter(
(bet) => bet.answerId && answerIdsToSell.includes(bet.answerId)
)
const metricsOnAnswersToSell = contractMetrics.filter(
(m) => m.answerId && answerIdsToSell.includes(m.answerId)
)
const invested = getInvested(contract, betsOnAnswersToSell)

const userBetsToSellByAnswerId = groupBy(
betsOnAnswersToSell.filter((bet) => bet.shares !== 0),
(bet) => bet.answerId
)
const loanPaid = sumBy(betsOnAnswersToSell, (bet) => bet.loanAmount ?? 0)
const loanPaid = sumBy(metricsOnAnswersToSell, (m) => m.loan ?? 0)
const { newBetResults, updatedAnswers, totalFee } =
calculateCpmmMultiArbitrageSellYesEqually(
contract.answers,
Expand Down Expand Up @@ -338,6 +344,9 @@ export const MultiNumericSellPanel = (props: {
userId: string
}) => {
const { contract, userId } = props
const contractMetrics = useAllSavedContractMetrics(contract)?.filter(
(m) => m.answerId != null
)
const userBets = useUserContractBets(userId, contract.id)

const [showSellPanel, setShowSellPanel] = useState(false)
Expand All @@ -363,6 +372,7 @@ export const MultiNumericSellPanel = (props: {
cancel={() => setShowSellPanel(false)}
contract={contract}
userBets={userBets}
contractMetrics={contractMetrics ?? []}
/>
)}
</Col>
Expand Down
14 changes: 11 additions & 3 deletions web/hooks/use-saved-contract-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ import { useBatchedGetter } from './use-batched-getter'
export const useSavedContractMetrics = (
contract: Contract,
answerId?: string
) => {
const allMetrics = useAllSavedContractMetrics(contract, answerId)
return allMetrics?.find((m) =>
answerId ? m.answerId === answerId : m.answerId == null
)
}

export const useAllSavedContractMetrics = (
contract: Contract,
answerId?: string
) => {
const user = useUser()
const [savedMetrics, setSavedMetrics] = usePersistentLocalState<
Expand Down Expand Up @@ -62,9 +72,7 @@ export const useSavedContractMetrics = (
enabled: !!user?.id,
})

return savedMetrics?.find((m) =>
answerId ? m.answerId === answerId : m.answerId == null
)
return savedMetrics
}

export const useReadLocalContractMetrics = (contractId: string) => {
Expand Down

0 comments on commit 4220f89

Please sign in to comment.