Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
at main 39 lines 1.1 kB view raw
1import { useTransactionStatusLazyQuery } from "@hey/indexer"; 2import { useCallback } from "react"; 3 4const INITIAL_DELAY = 1000; 5const MAX_DELAY = 10000; 6const MAX_TIMEOUT = 60000; 7 8const useWaitForTransactionToComplete = () => { 9 const [getTransactionStatus] = useTransactionStatusLazyQuery({ 10 fetchPolicy: "no-cache" 11 }); 12 13 const waitForTransactionToComplete = useCallback( 14 async (hash: string, timeout = MAX_TIMEOUT) => { 15 let delay = INITIAL_DELAY; 16 const startTime = Date.now(); 17 18 while (Date.now() - startTime < timeout) { 19 const { data } = await getTransactionStatus({ 20 variables: { request: { txHash: hash } } 21 }); 22 23 if ( 24 data?.transactionStatus?.__typename === "FinishedTransactionStatus" 25 ) { 26 return; 27 } 28 29 await new Promise((resolve) => setTimeout(resolve, delay)); 30 delay = Math.min(delay * 2, MAX_DELAY); 31 } 32 }, 33 [getTransactionStatus] 34 ); 35 36 return waitForTransactionToComplete; 37}; 38 39export default useWaitForTransactionToComplete;