diff --git a/src/special/london/Blocks.tsx b/src/special/london/Blocks.tsx index 1cbead2..e510d5d 100644 --- a/src/special/london/Blocks.tsx +++ b/src/special/london/Blocks.tsx @@ -1,4 +1,10 @@ -import React, { useState, useEffect, useContext, useMemo } from "react"; +import React, { + useState, + useEffect, + useContext, + useMemo, + useCallback, +} from "react"; import { ethers } from "ethers"; import { Line } from "react-chartjs-2"; import { ChartData, ChartOptions } from "chart.js"; @@ -47,23 +53,24 @@ const options: ChartOptions = { type BlocksProps = { latestBlock: ethers.providers.Block; + targetBlockNumber: number; }; -const Blocks: React.FC = ({ latestBlock }) => { +const Blocks: React.FC = ({ latestBlock, targetBlockNumber }) => { const { provider } = useContext(RuntimeContext); const [blocks, setBlock] = useState([]); const [now, setNow] = useState(Date.now()); - useEffect(() => { - if (!provider) { - return; - } + const addBlock = useCallback( + async (blockNumber: number) => { + if (!provider) { + return; + } - const _readBlock = async () => { - const extBlock = await readBlock(provider, latestBlock.number.toString()); + const extBlock = await readBlock(provider, blockNumber.toString()); setNow(Date.now()); setBlock((_blocks) => { - if (_blocks.length > 0 && latestBlock.number === _blocks[0].number) { + if (_blocks.length > 0 && blockNumber === _blocks[0].number) { return _blocks; } @@ -74,12 +81,16 @@ const Blocks: React.FC = ({ latestBlock }) => { ); // Little hack to fix out of order block notifications - newBlocks.sort((a, b) => a.number - b.number); + newBlocks.sort((a, b) => b.number - a.number); return newBlocks; }); - }; - _readBlock(); - }, [provider, latestBlock]); + }, + [provider] + ); + + useEffect(() => { + addBlock(latestBlock.number); + }, [addBlock, latestBlock]); const data: ChartData = useMemo(() => { return { diff --git a/src/special/london/London.tsx b/src/special/london/London.tsx index 965d05c..0663796 100644 --- a/src/special/london/London.tsx +++ b/src/special/london/London.tsx @@ -3,13 +3,7 @@ import { useLatestBlock } from "../../useLatestBlock"; import { RuntimeContext } from "../../useRuntime"; import Countdown from "./Countdown"; import Blocks from "./Blocks"; - -const londonBlockNumber: { [chainId: string]: number } = { - "1": 12965000, - "3": 10499401, - "4": 8897988, - "5": 5062605, -}; +import { londonBlockNumber } from "./params"; const London: React.FC = () => { const { provider } = useContext(RuntimeContext); @@ -31,7 +25,7 @@ const London: React.FC = () => { ); } - return ; + return ; }; export default React.memo(London); diff --git a/src/special/london/params.ts b/src/special/london/params.ts new file mode 100644 index 0000000..4f04caf --- /dev/null +++ b/src/special/london/params.ts @@ -0,0 +1,6 @@ +export const londonBlockNumber: { [chainId: string]: number } = { + "1": 12965000, + "3": 10499401, + "4": 8897988, + "5": 5062605, +};