Skip to content

Commit

Permalink
✨ Fetch winners in claiming closed state (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-tarczynski authored Aug 2, 2024
1 parent 0f8b1ff commit 679554b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
73 changes: 68 additions & 5 deletions packages/frontend/src/blockchain/hooks/useAuctionWinners.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { useChainId, useReadContracts } from 'wagmi'
import { AUCTION_ABI } from '@/blockchain/abi/auction'
import { AUCTION_ADDRESSES } from '@/blockchain/auctionAddresses'
import { useContractState } from '@/blockchain/hooks/useAuctionState'
import { ContractState } from '@/types/ContractState'
import { useBids } from '@/providers/BidsProvider'
import { WinType } from '@/types/winType'
import { useMemo } from 'react'

export const useAuctionWinners = () => {
const chainId = useChainId()
const { state, isLoading: isStateLoading } = useContractState()
const { data, isLoading } = useReadContracts({
contracts: [
{
Expand All @@ -20,11 +26,68 @@ export const useAuctionWinners = () => {
},
],
allowFailure: false,
query: {
enabled: !isStateLoading && state === ContractState.RAFFLE_SETTLED,
},
})

return {
auctionWinners: data?.[0],
raffleWinners: data?.[1],
isLoading,
}
const closedState = useAuctionWinnersInClosedState()
return state === ContractState.CLAIMING_CLOSED
? closedState
: {
auctionWinners: data?.[0],
raffleWinners: data?.[1],
goldenWinner: data?.[1]?.[0],
isLoading: isLoading,
}
}

const useAuctionWinnersInClosedState = () => {
const chainId = useChainId()
const { bidList } = useBids()
const { state, isLoading: isStateLoading } = useContractState()

const bidWinTypeContracts = bidList.map(
(bid) =>
({
chainId,
abi: AUCTION_ABI,
address: AUCTION_ADDRESSES[chainId],
functionName: 'getBidWinType',
args: [bid.bidderId],
} as const),
)

const { data, isLoading } = useReadContracts({
contracts: bidWinTypeContracts,
allowFailure: false,
query: {
enabled: !isStateLoading && state === ContractState.CLAIMING_CLOSED,
},
})

return useMemo(() => {
const auctionWinners: bigint[] = []
const raffleWinners: bigint[] = []
let goldenWinner: bigint | undefined = undefined
data?.forEach((winType, index) => {
switch (winType) {
case WinType.Auction:
auctionWinners.push(bidList[index].bidderId)
break
case WinType.Raffle:
raffleWinners.push(bidList[index].bidderId)
break
case WinType.GoldenTicket:
goldenWinner = bidList[index].bidderId
break
}
})
return {
auctionWinners,
raffleWinners,
goldenWinner,
isLoading: isStateLoading || isLoading,
}
}, [bidList, data, isLoading, isStateLoading])
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ interface SettledBidsListProps {
export const SettledBidsList = ({ search }: SettledBidsListProps) => {
const { bidList } = useBids()
const matchesSearch = bidMatchesSearch(search)
const { auctionWinners, raffleWinners } = useAuctionWinners()
const { auctionWinners, raffleWinners, goldenWinner } = useAuctionWinners()

const settledBids = useMemo(
() => divideBids(bidList, auctionWinners, raffleWinners),
[bidList, auctionWinners, raffleWinners],
() => divideBids(bidList, auctionWinners, raffleWinners, goldenWinner),
[bidList, auctionWinners, raffleWinners, goldenWinner],
)

const filteredBids = useMemo(() => filterBids(settledBids, matchesSearch), [settledBids, matchesSearch])
Expand All @@ -52,6 +52,7 @@ function divideBids(
bids: Bid[],
auctionWinners: readonly bigint[] | undefined,
raffleWinners: readonly bigint[] | undefined,
goldenWinner: bigint | undefined,
): Bids {
const settledBids: Bids = {
auction: [],
Expand All @@ -69,7 +70,7 @@ function divideBids(
settledBids.auction.push(bid)
return
}
if (bidderID === raffleWinners[0]) {
if (bidderID === goldenWinner) {
settledBids.goldenTicket = bid
return
}
Expand Down

0 comments on commit 679554b

Please sign in to comment.