From f1db3f23485842f6a6d83d1db1ddd240caf26e52 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Mon, 27 Dec 2021 21:16:33 -0300 Subject: [PATCH] Add prefetch to nonce +-2 --- src/transaction/NavButton.tsx | 4 +- src/transaction/NavNonce.tsx | 23 +++++++- src/useErigonHooks.ts | 103 +++++++++++++++++++--------------- 3 files changed, 81 insertions(+), 49 deletions(-) diff --git a/src/transaction/NavButton.tsx b/src/transaction/NavButton.tsx index 34f847d..8eb9e16 100644 --- a/src/transaction/NavButton.tsx +++ b/src/transaction/NavButton.tsx @@ -14,7 +14,7 @@ const NavButton: React.FC = ({ }) => { if (disabled || txHash === undefined) { return ( - + {children} ); @@ -22,7 +22,7 @@ const NavButton: React.FC = ({ return ( {children} diff --git a/src/transaction/NavNonce.tsx b/src/transaction/NavNonce.tsx index 236b30f..787deac 100644 --- a/src/transaction/NavNonce.tsx +++ b/src/transaction/NavNonce.tsx @@ -6,9 +6,11 @@ import NavButton from "./NavButton"; import { ChecksummedAddress } from "../types"; import { RuntimeContext } from "../useRuntime"; import { + prefetchTransactionBySenderAndNonce, useTransactionBySenderAndNonce, useTransactionCount, } from "../useErigonHooks"; +import { useSWRConfig } from "swr"; type NavNonceProps = { sender: ChecksummedAddress; @@ -34,8 +36,27 @@ const NavNonce: React.FC = ({ sender, nonce }) => { count !== undefined ? count - 1 : undefined ); + // Prefetch + const swrConfig = useSWRConfig(); + const prefetch = () => { + if (provider && sender && nonce !== undefined) { + prefetchTransactionBySenderAndNonce( + swrConfig, + provider, + sender, + nonce - 2 + ); + prefetchTransactionBySenderAndNonce( + swrConfig, + provider, + sender, + nonce + 2 + ); + } + }; + return ( -
+
diff --git a/src/useErigonHooks.ts b/src/useErigonHooks.ts index 71b95cc..737c6d7 100644 --- a/src/useErigonHooks.ts +++ b/src/useErigonHooks.ts @@ -6,6 +6,7 @@ import { Contract } from "@ethersproject/contracts"; import { defaultAbiCoder } from "@ethersproject/abi"; import { BigNumber } from "@ethersproject/bignumber"; import { arrayify, hexDataSlice, isHexString } from "@ethersproject/bytes"; +import useSWR, { useSWRConfig } from "swr"; import { getInternalOperations } from "./nodeFunctions"; import { TokenMetas, @@ -507,24 +508,52 @@ export const useTransactionCount = ( provider: JsonRpcProvider | undefined, sender: ChecksummedAddress | undefined ): number | undefined => { - const [count, setCount] = useState(); + const { data, error } = useSWR( + provider && sender ? { provider, sender } : null, + async ({ provider, sender }): Promise => + provider.getTransactionCount(sender) + ); - useEffect(() => { - // Reset - setCount(undefined); + if (error) { + return undefined; + } + return data; +}; - if (provider === undefined || sender === undefined) { - return; - } +type TransactionBySenderAndNonceKey = { + provider: JsonRpcProvider; + sender: ChecksummedAddress; + nonce: number; +}; - const readCount = async () => { - const _count = await provider.getTransactionCount(sender); - setCount(_count); - }; - readCount(); - }, [provider, sender]); +const getTransactionBySenderAndNonceFetcher = async ({ + provider, + sender, + nonce, +}: TransactionBySenderAndNonceKey): Promise => { + const result = (await provider.send("ots_getTransactionBySenderAndNonce", [ + sender, + nonce, + ])) as string; - return count; + // Empty or success + if (result === "0x") { + return undefined; + } + + return result; +}; + +export const prefetchTransactionBySenderAndNonce = ( + { cache, mutate }: ReturnType, + provider: JsonRpcProvider, + sender: ChecksummedAddress, + nonce: number +) => { + const key: TransactionBySenderAndNonceKey = { provider, sender, nonce }; + if (cache.get(key)) { + mutate(key, getTransactionBySenderAndNonceFetcher(key)); + } }; export const useTransactionBySenderAndNonce = ( @@ -532,37 +561,19 @@ export const useTransactionBySenderAndNonce = ( sender: ChecksummedAddress | undefined, nonce: number | undefined ): string | undefined => { - const [txHash, setTxHash] = useState(); + const { data, error } = useSWR< + string | undefined, + any, + TransactionBySenderAndNonceKey | null + >( + provider && sender && nonce !== undefined + ? { provider, sender, nonce } + : null, + getTransactionBySenderAndNonceFetcher + ); - useEffect(() => { - // Reset - setTxHash(undefined); - - if ( - provider === undefined || - sender === undefined || - nonce === undefined || - nonce < 0 - ) { - return; - } - - const readTxHash = async () => { - const result = (await provider.send( - "ots_getTransactionBySenderAndNonce", - [sender, nonce] - )) as string; - - // Empty or success - if (result === "0x") { - setTxHash(undefined); - return; - } - - setTxHash(result); - }; - readTxHash(); - }, [provider, sender, nonce]); - - return txHash; + if (error) { + return undefined; + } + return data; };