-
Notifications
You must be signed in to change notification settings - Fork 13
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
Showing
26 changed files
with
437 additions
and
250 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
52 changes: 37 additions & 15 deletions
52
templates/react/next-ethers/src/components/BlockNumber.tsx
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,32 +1,54 @@ | ||
'use client' | ||
|
||
import { useState, useEffect } from 'react'; | ||
|
||
import { useState, useEffect, useCallback } from 'react'; | ||
import { useEthereum } from './Context'; | ||
|
||
export function BlockNumber() { | ||
const { getProvider } = useEthereum(); | ||
const [blockNumber, setBlockNumber] = useState<bigint | null>(null); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchAndSubscribeToBlockUpdates = useCallback(async () => { | ||
setError(null); | ||
const provider = getProvider(); | ||
if (!provider) { | ||
setError("Provider not available"); | ||
return () => {}; | ||
} | ||
|
||
try { | ||
const currentBlockNumber = await provider.getBlockNumber(); | ||
setBlockNumber(BigInt(currentBlockNumber)); | ||
|
||
const onBlockHandler = (blockNumber: number) => { | ||
setBlockNumber(BigInt(blockNumber)); | ||
}; | ||
|
||
provider.on("block", onBlockHandler); | ||
|
||
return () => { | ||
provider.off("block", onBlockHandler); | ||
}; | ||
} catch (err) { | ||
setError(`Error: ${err instanceof Error ? err.message : String(err)}`); | ||
return () => {}; | ||
} | ||
}, [getProvider]); | ||
|
||
if (!provider) return; | ||
|
||
const onBlockHandler = (block: bigint) => { | ||
setBlockNumber(block); | ||
}; | ||
|
||
provider.on("block", onBlockHandler); | ||
useEffect(() => { | ||
const unsubscribe = fetchAndSubscribeToBlockUpdates(); | ||
|
||
return () => { | ||
provider.off("block", onBlockHandler); | ||
}; | ||
}, [getProvider]); | ||
}, [fetchAndSubscribeToBlockUpdates]); | ||
|
||
return ( | ||
<div> | ||
{blockNumber?.toString()} | ||
{error ? ( | ||
<div>Error: {error}</div> | ||
) : blockNumber === null ? ( | ||
<div>Loading block number...</div> | ||
) : ( | ||
<div>{blockNumber.toString()}</div> | ||
)} | ||
</div> | ||
); | ||
} |
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
Oops, something went wrong.