@@ -36,6 +36,7 @@ const App = () => {
3636 const [ receipts , setReceipts ] = useLocalStorage ( "receipts" , [ ] )
3737 const [ contracts , setContracts ] = useState ( [ ] as string [ ] )
3838 const [ themeType , setThemeType ] = useState ( "dark" as ThemeType )
39+ const timer = useRef ( null )
3940
4041 const clientInstanceRef = useRef ( clientInstance )
4142 clientInstanceRef . current = clientInstance
@@ -80,52 +81,56 @@ const App = () => {
8081 } , [ ] )
8182
8283 useEffect ( ( ) => {
83- if ( ! clientInstance ) {
84- return
85- }
86-
87- const receiptsNotVerified : Receipt [ ] = receipts . filter ( ( item : Receipt ) => {
88- return item . status === "Pending in queue"
84+ let receiptsNotVerified : Receipt [ ] = receipts . filter ( ( item : Receipt ) => {
85+ return item . status === "Pending in queue" || item . status === "Max rate limit reached"
8986 } )
9087
9188 if ( receiptsNotVerified . length > 0 ) {
92- const timer1 = setInterval ( ( ) => {
93- receiptsNotVerified . forEach ( async ( item ) => {
94- if ( ! clientInstanceRef . current ) {
95- return { }
96- }
97- const network = await getNetworkName ( clientInstanceRef . current )
98- if ( network === "vm" ) {
99- return { }
100- }
89+ if ( timer . current ) {
90+ clearInterval ( timer . current )
91+ timer . current = null
92+ }
93+ timer . current = setInterval ( async ( ) => {
94+ const network = await getNetworkName ( clientInstanceRef . current )
95+ if ( ! clientInstanceRef . current ) {
96+ return
97+ }
98+
99+ if ( network === "vm" ) {
100+ return
101+ }
102+ let newReceipts = receipts
103+ for ( const item of receiptsNotVerified ) {
104+ await new Promise ( r => setTimeout ( r , 500 ) ) // avoid api rate limit exceed.
105+ console . log ( 'checking receipt' , item . guid )
101106 const status = await getReceiptStatus (
102107 item . guid ,
103108 apiKey ,
104109 getEtherScanApi ( network )
105110 )
106111 if ( status . result === "Pass - Verified" || status . result === "Already Verified" ) {
107- const newReceipts = receipts . map ( ( currentReceipt : Receipt ) => {
112+ newReceipts = newReceipts . map ( ( currentReceipt : Receipt ) => {
108113 if ( currentReceipt . guid === item . guid ) {
109114 return {
110115 ...currentReceipt ,
111116 status : status . result ,
112117 }
113118 }
114119 return currentReceipt
115- } )
116-
117- clearInterval ( timer1 )
118- setReceipts ( newReceipts )
119-
120- return ( ) => {
121- clearInterval ( timer1 )
122- }
123- }
124- return { }
120+ } )
121+ }
122+ }
123+ receiptsNotVerified = newReceipts . filter ( ( item : Receipt ) => {
124+ return item . status === "Pending in queue" || item . status === "Max rate limit reached"
125125 } )
126- } , 5000 )
126+ if ( timer . current && receiptsNotVerified . length === 0 ) {
127+ clearInterval ( timer . current )
128+ timer . current = null
129+ }
130+ setReceipts ( newReceipts )
131+ } , 10000 )
127132 }
128- } , [ receipts , clientInstance , apiKey , setReceipts ] )
133+ } , [ receipts ] )
129134
130135 return (
131136 < AppContext . Provider
@@ -146,4 +151,4 @@ const App = () => {
146151 )
147152}
148153
149- export default App
154+ export default App
0 commit comments