diff --git a/packages/frontend/src/blockchain/hooks/useAuctionWinners.ts b/packages/frontend/src/blockchain/hooks/useAuctionWinners.ts index dd4ee6cd..be545147 100644 --- a/packages/frontend/src/blockchain/hooks/useAuctionWinners.ts +++ b/packages/frontend/src/blockchain/hooks/useAuctionWinners.ts @@ -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: [ { @@ -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]) } diff --git a/packages/frontend/src/components/bids/allBids/SettledBidsList.tsx b/packages/frontend/src/components/bids/allBids/SettledBidsList.tsx index 395f3532..4e71dc30 100644 --- a/packages/frontend/src/components/bids/allBids/SettledBidsList.tsx +++ b/packages/frontend/src/components/bids/allBids/SettledBidsList.tsx @@ -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]) @@ -52,6 +52,7 @@ function divideBids( bids: Bid[], auctionWinners: readonly bigint[] | undefined, raffleWinners: readonly bigint[] | undefined, + goldenWinner: bigint | undefined, ): Bids { const settledBids: Bids = { auction: [], @@ -69,7 +70,7 @@ function divideBids( settledBids.auction.push(bid) return } - if (bidderID === raffleWinners[0]) { + if (bidderID === goldenWinner) { settledBids.goldenTicket = bid return }