From f09c49dfed160cfed47a7ba2713ed918e032a154 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Tue, 1 Feb 2022 19:14:05 -0300 Subject: [PATCH] Fix prefetch --- src/transaction/NavNonce.tsx | 25 +++++++--------- src/useErigonHooks.ts | 58 ++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/transaction/NavNonce.tsx b/src/transaction/NavNonce.tsx index fb94b66..9efd47f 100644 --- a/src/transaction/NavNonce.tsx +++ b/src/transaction/NavNonce.tsx @@ -1,4 +1,4 @@ -import React, { useContext } from "react"; +import React, { useContext, useEffect } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faChevronLeft } from "@fortawesome/free-solid-svg-icons/faChevronLeft"; import { faChevronRight } from "@fortawesome/free-solid-svg-icons/faChevronRight"; @@ -22,25 +22,22 @@ const NavNonce: React.FC = ({ sender, nonce }) => { // Prefetch const swrConfig = useSWRConfig(); - const prefetch = () => { - if (provider && sender && nonce !== undefined) { + useEffect(() => { + if (!provider || !sender || nonce === undefined || count === undefined) { + return; + } + + prefetchTransactionBySenderAndNonce(swrConfig, provider, sender, nonce - 1); + prefetchTransactionBySenderAndNonce(swrConfig, provider, sender, nonce + 1); + if (count > 0) { prefetchTransactionBySenderAndNonce( swrConfig, provider, sender, - nonce - 2 - ); - prefetchTransactionBySenderAndNonce( - swrConfig, - provider, - sender, - nonce + 2 + count - 1 ); } - }; - - // Always prefetch - prefetch(); + }, [swrConfig, provider, sender, nonce, count]); return (
diff --git a/src/useErigonHooks.ts b/src/useErigonHooks.ts index 7eb847c..45378f5 100644 --- a/src/useErigonHooks.ts +++ b/src/useErigonHooks.ts @@ -530,39 +530,49 @@ export const useTransactionCount = ( }; type TransactionBySenderAndNonceKey = { - provider: JsonRpcProvider; + network: number; sender: ChecksummedAddress; nonce: number; }; -export const getTransactionBySenderAndNonceFetcher = async ({ - provider, - sender, - nonce, -}: TransactionBySenderAndNonceKey): Promise => { - if (nonce < 0) { - return undefined; - } - - const result = (await provider.send("ots_getTransactionBySenderAndNonce", [ +const getTransactionBySenderAndNonceFetcher = + (provider: JsonRpcProvider) => + async ({ + network, sender, nonce, - ])) as string; + }: TransactionBySenderAndNonceKey): Promise => { + if (nonce < 0) { + return undefined; + } - // Empty or success - return result; -}; + const result = (await provider.send("ots_getTransactionBySenderAndNonce", [ + sender, + nonce, + ])) as string; + + // Empty or success + return result; + }; export const prefetchTransactionBySenderAndNonce = ( - { cache, mutate }: ReturnType, + { mutate }: ReturnType, provider: JsonRpcProvider, sender: ChecksummedAddress, nonce: number ) => { - const key: TransactionBySenderAndNonceKey = { provider, sender, nonce }; - if (cache.get(key)) { - mutate(key, getTransactionBySenderAndNonceFetcher(key)); - } + const key: TransactionBySenderAndNonceKey = { + network: provider.network.chainId, + sender, + nonce, + }; + mutate(key, (curr: any) => { + if (curr) { + return curr; + } + return getTransactionBySenderAndNonceFetcher(provider)(key); + }); + // } }; export const useTransactionBySenderAndNonce = ( @@ -576,9 +586,13 @@ export const useTransactionBySenderAndNonce = ( TransactionBySenderAndNonceKey | null >( provider && sender && nonce !== undefined - ? { provider, sender, nonce } + ? { + network: provider.network.chainId, + sender, + nonce, + } : null, - getTransactionBySenderAndNonceFetcher + getTransactionBySenderAndNonceFetcher(provider!) ); if (error) {