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.
- Loading branch information
1 parent
5800c37
commit 1223af1
Showing
10 changed files
with
183 additions
and
22 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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createConfig, webSocket } from 'wagmi' | ||
import { arbitrum, arbitrumSepolia, hardhat } from 'wagmi/chains' | ||
|
||
export const wagmiConfig = createConfig({ | ||
chains: [arbitrum, arbitrumSepolia, hardhat], | ||
ssr: true, | ||
transports: { | ||
[arbitrum.id]: webSocket(`wss://arbitrum-mainnet.infura.io/ws/v3/${process.env.NEXT_PUBLIC_INFURA_KEY}`), | ||
[arbitrumSepolia.id]: webSocket(`wss://arbitrum-sepolia.infura.io/ws/v3/${process.env.NEXT_PUBLIC_INFURA_KEY}`), | ||
[hardhat.id]: webSocket('http://127.0.0.1:8545'), | ||
}, | ||
}) | ||
|
||
declare module 'wagmi' { | ||
interface Register { | ||
config: typeof wagmiConfig | ||
} | ||
} |
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,12 +1,15 @@ | ||
import { GlobalStyles } from '@/styles/GobalStyles' | ||
import type { AppProps } from 'next/app' | ||
import { BlockchainProviders } from '@/providers/wagmi' | ||
import { BidsProvider } from '@/providers/BidsProvider/provider' | ||
|
||
export default function App({ Component, pageProps }: AppProps) { | ||
return ( | ||
<BlockchainProviders> | ||
<GlobalStyles /> | ||
<Component {...pageProps} /> | ||
<BidsProvider> | ||
<GlobalStyles /> | ||
<Component {...pageProps} /> | ||
</BidsProvider> | ||
</BlockchainProviders> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { useBids, BidsProvider } from './provider' | ||
export type { Bid } from './types' |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { createContext, ReactNode, useContext, useReducer } from 'react' | ||
import { Hex } from 'viem' | ||
import { defaultBidsState, reduceBids } from '@/providers/BidsProvider/reduceBids' | ||
import { useWatchEvents } from '@/providers/BidsProvider/useWatchEvents' | ||
import { Bid } from '@/providers/BidsProvider/types' | ||
|
||
const BidsContext = createContext({ | ||
bids: new Map<Hex, Bid>(), | ||
bidList: new Array<Bid>(), | ||
isLoading: true, | ||
}) | ||
|
||
export const useBids = () => useContext(BidsContext) | ||
|
||
export const BidsProvider = ({ children }: { children: ReactNode }) => { | ||
const [bidsState, updateBids] = useReducer(reduceBids, defaultBidsState) | ||
const { isLoading } = useWatchEvents(updateBids) | ||
|
||
return <BidsContext.Provider value={{ ...bidsState, isLoading }}>{children}</BidsContext.Provider> | ||
} |
75 changes: 75 additions & 0 deletions
75
packages/frontend/src/providers/BidsProvider/reduceBids.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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Bid } from '@/providers/BidsProvider/types' | ||
import { Hex } from 'viem' | ||
import { SupportedChainId } from '@/blockchain/chain' | ||
|
||
interface BidEvent { | ||
args: { | ||
bidder?: Hex | ||
bidderID?: bigint | ||
bidAmount?: bigint | ||
} | ||
} | ||
|
||
interface BidsState { | ||
bids: Map<Hex, Bid> | ||
bidList: Bid[] | ||
startBlock: bigint | undefined | ||
chainId: SupportedChainId | undefined | ||
} | ||
|
||
export const defaultBidsState: BidsState = { | ||
bids: new Map<Hex, Bid>(), | ||
bidList: [], | ||
startBlock: undefined, | ||
chainId: undefined, | ||
} | ||
|
||
export interface BidEventsState { | ||
events: BidEvent[] | ||
startBlock: bigint | undefined | ||
chainId: SupportedChainId | ||
} | ||
|
||
export const reduceBids = (previousState: BidsState, state: BidEventsState): BidsState => { | ||
const { events, startBlock, chainId } = state | ||
const bids = getInitialBids(previousState, state) | ||
events.forEach((event) => handleBid(bids, event.args)) | ||
|
||
return { | ||
bids, | ||
bidList: Array.from(bids.values()).sort(biggerFirst), | ||
startBlock: startBlock, | ||
chainId: chainId, | ||
} | ||
} | ||
|
||
const getInitialBids = (previousState: BidsState, { startBlock, chainId }: BidEventsState) => { | ||
if (startBlock !== previousState.startBlock || chainId !== previousState.chainId) { | ||
return new Map<Hex, Bid>() | ||
} | ||
return previousState.bids | ||
} | ||
|
||
const handleBid = (bids: Map<Hex, Bid>, eventArgs: BidEvent['args']) => { | ||
if (!eventArgs.bidder || !eventArgs.bidAmount || !eventArgs.bidderID) { | ||
return | ||
} | ||
const existingBid = bids.get(eventArgs.bidder) | ||
if (existingBid) { | ||
existingBid.amount += eventArgs.bidAmount | ||
bids.set(eventArgs.bidder, existingBid) | ||
return | ||
} | ||
bids.set(eventArgs.bidder, { | ||
address: eventArgs.bidder, | ||
bidderId: eventArgs.bidderID, | ||
amount: eventArgs.bidAmount, | ||
}) | ||
} | ||
|
||
const biggerFirst = (a: Bid, b: Bid) => { | ||
if (a.amount === b.amount) { | ||
return 0 | ||
} | ||
return a.amount > b.amount ? -1 : 1 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Hex } from 'viem' | ||
|
||
export interface Bid { | ||
address: Hex | ||
amount: bigint | ||
bidderId: bigint | ||
} |
46 changes: 46 additions & 0 deletions
46
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 { AUCTION_ABI } from '@/blockchain/abi/auction' | ||
|
||
const newBidEvent = parseAbiItem('event NewBid(address bidder, uint256 bidderID, uint256 bidAmount)') | ||
|
||
export const useWatchEvents = (onEvents: (eventsState: BidEventsState) => 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, | ||
}) | ||
|
||
useWatchContractEvent({ | ||
chainId, | ||
abi: AUCTION_ABI, | ||
address: AUCTION_ADDRESSES[chainId], | ||
fromBlock: blockNumber, | ||
eventName: 'NewBid', | ||
onLogs: (logs) => onEvents({ events: logs, chainId, startBlock: blockNumber }), | ||
enabled: !isBlockLoading && !areInitialBidsLoading, | ||
}) | ||
|
||
return { isLoading: isBlockLoading || areInitialBidsLoading } | ||
} |
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
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