From 2bd57d3b434062e12775130264c9e54d231ddd0f Mon Sep 17 00:00:00 2001 From: yann300 Date: Tue, 23 May 2023 12:27:25 +0200 Subject: [PATCH] avoid api rate limit exceed --- apps/etherscan/src/app/app.tsx | 63 ++++++++++--------- apps/etherscan/src/app/types/Receipt.ts | 2 +- .../components/workspace-hamburger-item.tsx | 2 +- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/apps/etherscan/src/app/app.tsx b/apps/etherscan/src/app/app.tsx index e7978f403a..be6564d422 100644 --- a/apps/etherscan/src/app/app.tsx +++ b/apps/etherscan/src/app/app.tsx @@ -36,6 +36,7 @@ const App = () => { const [receipts, setReceipts] = useLocalStorage("receipts", []) const [contracts, setContracts] = useState([] as string[]) const [themeType, setThemeType] = useState("dark" as ThemeType) + const timer = useRef(null) const clientInstanceRef = useRef(clientInstance) clientInstanceRef.current = clientInstance @@ -80,31 +81,35 @@ const App = () => { }, []) useEffect(() => { - if (!clientInstance) { - return - } - - const receiptsNotVerified: Receipt[] = receipts.filter((item: Receipt) => { - return item.status === "Pending in queue" + let receiptsNotVerified: Receipt[] = receipts.filter((item: Receipt) => { + return item.status === "Pending in queue" || item.status === "Max rate limit reached" }) if (receiptsNotVerified.length > 0) { - const timer1 = setInterval(() => { - receiptsNotVerified.forEach(async (item) => { - if (!clientInstanceRef.current) { - return {} - } - const { network, networkId } = await getNetworkName(clientInstanceRef.current) - if (network === "vm") { - return {} - } + if (timer.current) { + clearInterval(timer.current) + timer.current = null + } + timer.current = setInterval(async () => { + const { network, networkId } = await getNetworkName(clientInstanceRef.current) + if (!clientInstanceRef.current) { + return + } + + if (network === "vm") { + return + } + let newReceipts = receipts + for (const item of receiptsNotVerified) { + await new Promise(r => setTimeout(r, 500)) // avoid api rate limit exceed. + console.log('checking receipt', item.guid) const status = await getReceiptStatus( item.guid, apiKey, getEtherScanApi(network, networkId) ) if (status.result === "Pass - Verified" || status.result === "Already Verified") { - const newReceipts = receipts.map((currentReceipt: Receipt) => { + newReceipts = newReceipts.map((currentReceipt: Receipt) => { if (currentReceipt.guid === item.guid) { return { ...currentReceipt, @@ -112,20 +117,20 @@ const App = () => { } } return currentReceipt - }) - - clearInterval(timer1) - setReceipts(newReceipts) - - return () => { - clearInterval(timer1) - } - } - return {} + }) + } + } + receiptsNotVerified = newReceipts.filter((item: Receipt) => { + return item.status === "Pending in queue" || item.status === "Max rate limit reached" }) - }, 5000) + if (timer.current && receiptsNotVerified.length === 0) { + clearInterval(timer.current) + timer.current = null + } + setReceipts(newReceipts) + }, 10000) } - }, [receipts, clientInstance, apiKey, setReceipts]) + }, [receipts]) return ( { ) } -export default App +export default App \ No newline at end of file diff --git a/apps/etherscan/src/app/types/Receipt.ts b/apps/etherscan/src/app/types/Receipt.ts index 0ba32da969..2469aa02a8 100644 --- a/apps/etherscan/src/app/types/Receipt.ts +++ b/apps/etherscan/src/app/types/Receipt.ts @@ -1,4 +1,4 @@ -export type ReceiptStatus = "Pending in queue" | "Pass - Verified" | "Already Verified" +export type ReceiptStatus = "Pending in queue" | "Pass - Verified" | "Already Verified" | "Max rate limit reached" export interface Receipt { guid: string diff --git a/libs/remix-ui/workspace/src/lib/components/workspace-hamburger-item.tsx b/libs/remix-ui/workspace/src/lib/components/workspace-hamburger-item.tsx index dc333d301a..082746eec5 100644 --- a/libs/remix-ui/workspace/src/lib/components/workspace-hamburger-item.tsx +++ b/libs/remix-ui/workspace/src/lib/components/workspace-hamburger-item.tsx @@ -48,4 +48,4 @@ export function HamburgerMenuItem (props: HamburgerMenuItemProps) { ) - } + } \ No newline at end of file