-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
73 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './abi' | ||
export * from './cast' | ||
export * from './providers' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './retry-provider' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { JsonRpcProvider } from '@ethersproject/providers' | ||
import wait from 'waait' | ||
import Bluebird from 'bluebird' | ||
|
||
export interface RetryOptions { | ||
delay?: number; | ||
timeouts: number[]; | ||
} | ||
|
||
export function promiseTimeout<T> (ms: number, promise: Promise<T>): Promise<T> { | ||
return Bluebird.resolve(promise).timeout(ms) | ||
} | ||
|
||
export function retryOperation ( | ||
retriesLeft: number, | ||
operation: () => Promise<any>, | ||
options: RetryOptions | ||
) { | ||
return new Promise((resolve, reject) => { | ||
const { timeouts } = options | ||
// Find the timeout for this specific iteration | ||
const timeout = timeouts[timeouts.length - retriesLeft] | ||
|
||
// Wrap the original operation in a timeout | ||
const execution = promiseTimeout(timeout, operation()) | ||
|
||
// If the promise is successful, resolve it and bubble the result up | ||
return execution.then(resolve).catch((reason: any) => { | ||
// If there are any retries left, we call the same retryOperation function again, | ||
// but decrementing the number of retries left by 1 | ||
if (retriesLeft - 1 > 0) { | ||
// Delay the new attempt slightly | ||
return wait(options.delay || 50) | ||
.then(retryOperation.bind(null, retriesLeft - 1, operation, options)) | ||
.then(resolve) | ||
.catch(reject) | ||
} | ||
// Reject (and bubble the result up) if there are no more retries | ||
return reject(reason) | ||
}) | ||
}) | ||
} | ||
|
||
export class JsonRpcRetryProvider extends JsonRpcProvider { | ||
public perform (method: string, params: any): Promise<any> { | ||
const timeouts = [5_000, 10_000] | ||
const operation = () => super.perform(method, params) | ||
|
||
return retryOperation(2, operation, { timeouts, delay: 50 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -664,6 +664,11 @@ | |
dependencies: | ||
"@types/retry" "*" | ||
|
||
"@types/bluebird@^3.5.36": | ||
version "3.5.36" | ||
resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.36.tgz#00d9301d4dc35c2f6465a8aec634bb533674c652" | ||
integrity sha512-HBNx4lhkxN7bx6P0++W8E289foSu8kO8GCk2unhuVggO+cE7rh9DhZUyPhUxNRG9m+5B5BTKxZQ5ZP92x/mx9Q== | ||
|
||
"@types/bn.js@^5.1.0": | ||
version "5.1.0" | ||
resolved "https://registry.yarnpkg.com/@types/bn.js/-/bn.js-5.1.0.tgz#32c5d271503a12653c62cf4d2b45e6eab8cebc68" | ||
|
@@ -1053,7 +1058,7 @@ blakejs@^1.1.0: | |
resolved "https://registry.yarnpkg.com/blakejs/-/blakejs-1.2.1.tgz#5057e4206eadb4a97f7c0b6e197a505042fc3814" | ||
integrity sha512-QXUSXI3QVc/gJME0dBpXrag1kbzOqCjCX8/b54ntNyW6sjtoqxqRk3LTmXzaJoh71zMsDCjM+47jS7XiwN/+fQ== | ||
|
||
bluebird@^3.5.0: | ||
bluebird@^3.5.0, bluebird@^3.7.2: | ||
version "3.7.2" | ||
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f" | ||
integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg== | ||
|
@@ -5693,6 +5698,11 @@ vue-eslint-parser@^8.0.1: | |
lodash "^4.17.21" | ||
semver "^7.3.5" | ||
|
||
waait@^1.0.5: | ||
version "1.0.5" | ||
resolved "https://registry.yarnpkg.com/waait/-/waait-1.0.5.tgz#6a3c7aaa88bd0a1a545e9d47890b9595bebf9aa7" | ||
integrity sha512-wp+unA4CpqxvBUKHHv8D86fK4jWByHAWyhEXXVHfVUZfK+16ylpj7hjQ58Z8j9ntu8XNukRQT8Fi5qbyJ8rkyw== | ||
|
||
[email protected]: | ||
version "1.7.4" | ||
resolved "https://registry.yarnpkg.com/web3-bzz/-/web3-bzz-1.7.4.tgz#9419e606e38a9777443d4ce40506ebd796e06075" | ||
|