Skip to content

Commit

Permalink
Refactored Draw Status Hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ncookiez committed Oct 5, 2023
1 parent 1817317 commit 889ca44
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
50 changes: 50 additions & 0 deletions apps/analytics/src/hooks/useAllDrawsStatus.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { PrizePool } from '@generationsoftware/hyperstructure-client-js'
import { useFirstDrawStartTimestamp } from '@generationsoftware/hyperstructure-react-hooks'
import { getSecondsSinceEpoch } from '@shared/utilities'
import { useMemo } from 'react'
import { DrawStatus } from './useDrawStatus'
import { useRngTxs } from './useRngTxs'

export const useAllDrawsStatus = (prizePool: PrizePool, drawIds: number[]) => {
const { data: firstDrawOpenedAt, isFetched: isFetchedFirstDrawOpenedAt } =
useFirstDrawStartTimestamp(prizePool)

const { data: allRngTxs, isFetched: isFetchedAllRngTxs } = useRngTxs(prizePool)

const data = useMemo(() => {
if (!!firstDrawOpenedAt && !!allRngTxs) {
const allDrawsStatus: {
id: number
status: DrawStatus
openedAt: number
endedAt?: number
closedAt?: number
periodsSkipped: number
}[] = []

drawIds.forEach((drawId) => {
const rngTxs = allRngTxs.find((txs) => txs.rng.drawId === drawId)
const prevRngTxs = allRngTxs.find((txs) => txs.rng.drawId === drawId - 1)

const openedAt = prevRngTxs?.relay.l2?.endedAt ?? firstDrawOpenedAt
const endedAt = rngTxs?.relay.l2?.endedAt
const closedAt = rngTxs?.relay.l2?.timestamp

const timeSinceEnded = !!endedAt ? getSecondsSinceEpoch() - endedAt : -1
const drawPeriod = prizePool.drawPeriodInSeconds as number
const isFinalized = timeSinceEnded > drawPeriod

const status: DrawStatus = isFinalized ? 'finalized' : !!closedAt ? 'closed' : 'open'
const periodsSkipped = !!endedAt ? Math.floor((endedAt - openedAt - 1) / drawPeriod) : 0

allDrawsStatus.push({ id: drawId, status, openedAt, endedAt, closedAt, periodsSkipped })
})

return allDrawsStatus
}
}, [drawIds, firstDrawOpenedAt, allRngTxs])

const isFetched = isFetchedFirstDrawOpenedAt && isFetchedAllRngTxs

return { data, isFetched }
}
29 changes: 5 additions & 24 deletions apps/analytics/src/hooks/useDrawStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,24 @@ import { PrizePool } from '@generationsoftware/hyperstructure-client-js'
import { useFirstDrawStartTimestamp } from '@generationsoftware/hyperstructure-react-hooks'
import { getSecondsSinceEpoch } from '@shared/utilities'
import { useMemo } from 'react'
import { useBlock } from './useBlock'
import { useDrawClosedEvents } from './useDrawClosedEvents'
import { useRngTxs } from './useRngTxs'

type DrawStatus = 'open' | 'closed' | 'finalized'
export type DrawStatus = 'open' | 'closed' | 'finalized'

export const useDrawStatus = (prizePool: PrizePool, drawId: number) => {
const { data: firstDrawOpenedAt, isFetched: isFetchedFirstDrawOpenedAt } =
useFirstDrawStartTimestamp(prizePool)

const { data: drawClosedEvents, isFetched: isFetchedDrawClosedEvents } =
useDrawClosedEvents(prizePool)
const closeEvent = drawClosedEvents?.find((e) => e.args.drawId === drawId)

const { data: closeBlock, isFetched: isFetchedCloseBlock } = useBlock(
prizePool?.chainId,
closeEvent?.blockNumber as bigint
)

const { data: allRngTxs, isFetched: isFetchedAllRngTxs } = useRngTxs(prizePool)

const data = useMemo(() => {
if (
!!firstDrawOpenedAt &&
(!!closeBlock || (!!drawClosedEvents && !closeEvent)) &&
!!allRngTxs
) {
if (!!firstDrawOpenedAt && !!allRngTxs) {
const rngTxs = allRngTxs.find((txs) => txs.rng.drawId === drawId)
const prevRngTxs = allRngTxs.find((txs) => txs.rng.drawId === drawId - 1)

const openedAt = prevRngTxs?.relay.l2?.endedAt ?? firstDrawOpenedAt
const endedAt = rngTxs?.relay.l2?.endedAt
const closedAt = !!closeBlock ? Number(closeBlock.timestamp) : undefined
const closedAt = rngTxs?.relay.l2?.timestamp

const timeSinceEnded = !!endedAt ? getSecondsSinceEpoch() - endedAt : -1
const drawPeriod = prizePool.drawPeriodInSeconds as number
Expand All @@ -45,13 +30,9 @@ export const useDrawStatus = (prizePool: PrizePool, drawId: number) => {

return { status, openedAt, endedAt, closedAt, periodsSkipped }
}
}, [drawId, firstDrawOpenedAt, closeBlock, allRngTxs])
}, [drawId, firstDrawOpenedAt, allRngTxs])

const isFetched =
isFetchedFirstDrawOpenedAt &&
isFetchedDrawClosedEvents &&
(!closeEvent || isFetchedCloseBlock) &&
isFetchedAllRngTxs
const isFetched = isFetchedFirstDrawOpenedAt && isFetchedAllRngTxs

return { ...data, isFetched }
}
4 changes: 3 additions & 1 deletion apps/analytics/src/hooks/useRngTxs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface RelayTx {
hash: `0x${string}`
endedAt: number
blockNumber: bigint
timestamp: number
}

interface RelayMsgTx {
Expand Down Expand Up @@ -134,7 +135,8 @@ export const useRngTxs = (prizePool: PrizePool) => {
feeRecipient: secondRelayEvent.args.recipient,
hash: secondRelayEvent.transactionHash,
endedAt: periodStart,
blockNumber: secondRelayEvent.blockNumber
blockNumber: secondRelayEvent.blockNumber,
timestamp: Number(drawClosedBlock?.timestamp as bigint)
}
: undefined
}
Expand Down

0 comments on commit 889ca44

Please sign in to comment.