Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
interface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
interface
Commits
081ae15a
Unverified
Commit
081ae15a
authored
Jul 11, 2020
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
retry failed requests up to 3 times
parent
f5a5c5e7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
1 deletion
+26
-1
updater.tsx
src/state/multicall/updater.tsx
+2
-1
retry.ts
src/utils/retry.ts
+24
-0
No files found.
src/state/multicall/updater.tsx
View file @
081ae15a
...
...
@@ -5,6 +5,7 @@ import { useActiveWeb3React } from '../../hooks'
import
{
useMulticallContract
}
from
'
../../hooks/useContract
'
import
useDebounce
from
'
../../hooks/useDebounce
'
import
chunkArray
from
'
../../utils/chunkArray
'
import
{
retry
}
from
'
../../utils/retry
'
import
{
useBlockNumber
}
from
'
../application/hooks
'
import
{
AppDispatch
,
AppState
}
from
'
../index
'
import
{
...
...
@@ -142,7 +143,7 @@ export default function Updater() {
)
chunkedCalls
.
forEach
((
chunk
,
index
)
=>
fetchChunk
(
multicallContract
,
chunk
,
latestBlockNumber
)
retry
(()
=>
fetchChunk
(
multicallContract
,
chunk
,
latestBlockNumber
)
)
.
then
(({
results
:
returnData
,
blockNumber
:
fetchBlockNumber
})
=>
{
// accumulates the length of all previous indices
const
firstCallKeyIndex
=
chunkedCalls
.
slice
(
0
,
index
).
reduce
<
number
>
((
memo
,
curr
)
=>
memo
+
curr
.
length
,
0
)
...
...
src/utils/retry.ts
0 → 100644
View file @
081ae15a
function
wait
(
ms
:
number
):
Promise
<
void
>
{
return
new
Promise
(
resolve
=>
setTimeout
(
resolve
,
ms
))
}
function
waitRandom
(
min
:
number
,
max
:
number
):
Promise
<
void
>
{
return
wait
(
min
+
Math
.
round
(
Math
.
random
()
*
(
max
-
min
)))
}
/**
* Retries the function that returns the promise until the promise successfully resolves up to n retries
* @param fn function to retry
* @param n how many times to retry
* @param minWait min wait between retries in ms
* @param maxWait max wait between retries in ms
*/
export
function
retry
<
T
>
(
fn
:
()
=>
Promise
<
T
>
,
{
n
=
3
,
minWait
=
500
,
maxWait
=
1000
}:
{
n
?:
number
;
minWait
?:
number
;
maxWait
?:
number
}
=
{}
):
Promise
<
T
>
{
return
fn
().
catch
(
error
=>
{
if
(
n
===
0
)
throw
error
return
waitRandom
(
minWait
,
maxWait
).
then
(()
=>
retry
(
fn
,
{
n
:
n
-
1
,
minWait
,
maxWait
}))
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment