forked from TrueFiEng/devcon-raffle
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🪄 Utilize getBidsWithAddresses function (#19)
- Loading branch information
1 parent
eaaac13
commit a0b2fc9
Showing
2 changed files
with
66 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 23 additions & 32 deletions
55
packages/frontend/src/providers/BidsProvider/useWatchEvents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,37 @@ | ||
import { parseAbiItem } from 'viem' | ||
import { BidEventsState } from '@/providers/BidsProvider/reduceBids' | ||
import { useBlockNumber, useChainId, useConfig, useWatchContractEvent } from 'wagmi' | ||
import { useQuery } from '@tanstack/react-query' | ||
import { getLogs } from 'viem/actions' | ||
import { AUCTION_ADDRESSES, DEPLOYMENT_BLOCK } from '@/blockchain/auctionAddresses' | ||
import { useChainId, useReadContract, useWatchContractEvent } from 'wagmi' | ||
import { AUCTION_ADDRESSES } from '@/blockchain/auctionAddresses' | ||
import { AUCTION_ABI } from '@/blockchain/abi/auction' | ||
import { useEffect } from 'react' | ||
import { ReduceBidsAction } from '@/providers/BidsProvider/reduceBids' | ||
|
||
const newBidEvent = parseAbiItem('event NewBid(address bidder, uint256 bidderID, uint256 bidAmount)') | ||
|
||
export const useWatchEvents = (onEvents: (eventsState: BidEventsState) => void) => { | ||
export const useWatchEvents = (dispatch: (eventsState: ReduceBidsAction) => void) => { | ||
const chainId = useChainId() | ||
const config = useConfig() | ||
const client = config.getClient({ chainId }) | ||
|
||
const { data: blockNumber, isLoading: isBlockLoading } = useBlockNumber() | ||
|
||
const { isLoading: areInitialBidsLoading } = useQuery({ | ||
queryKey: ['bids', chainId], | ||
queryFn: async () => { | ||
const logs = await getLogs(client, { | ||
address: AUCTION_ADDRESSES[chainId], | ||
event: newBidEvent, | ||
fromBlock: DEPLOYMENT_BLOCK[chainId], | ||
toBlock: blockNumber, | ||
}) | ||
onEvents({ events: logs, chainId, startBlock: blockNumber }) | ||
return logs | ||
}, | ||
enabled: !isBlockLoading, | ||
staleTime: Infinity, | ||
gcTime: Infinity, | ||
const { data, isLoading: areInitialBidsLoading } = useReadContract({ | ||
chainId, | ||
abi: AUCTION_ABI, | ||
address: AUCTION_ADDRESSES[chainId], | ||
functionName: 'getBidsWithAddresses', | ||
}) | ||
|
||
useEffect(() => { | ||
if (!data) { | ||
return | ||
} | ||
dispatch({ | ||
type: 'InitialBids', | ||
bids: data, | ||
}) | ||
}, [data, dispatch]) | ||
|
||
useWatchContractEvent({ | ||
chainId, | ||
abi: AUCTION_ABI, | ||
address: AUCTION_ADDRESSES[chainId], | ||
fromBlock: blockNumber, | ||
eventName: 'NewBid', | ||
onLogs: (logs) => onEvents({ events: logs, chainId, startBlock: blockNumber }), | ||
enabled: !isBlockLoading && !areInitialBidsLoading, | ||
onLogs: (logs) => dispatch({ type: 'NewBids', events: logs }), | ||
enabled: !areInitialBidsLoading, | ||
}) | ||
|
||
return { isLoading: isBlockLoading || areInitialBidsLoading } | ||
return { isLoading: areInitialBidsLoading } | ||
} |