|
|
@ -2,16 +2,17 @@ |
|
|
|
//
|
|
|
|
//
|
|
|
|
// Mostly taken from the removed version that was in ethers.js
|
|
|
|
// Mostly taken from the removed version that was in ethers.js
|
|
|
|
// See: https://github.com/ethers-io/ethers.js/discussions/3006
|
|
|
|
// See: https://github.com/ethers-io/ethers.js/discussions/3006
|
|
|
|
|
|
|
|
import { assert } from 'console'; |
|
|
|
import { ethers } from 'ethers'; |
|
|
|
import { ethers } from 'ethers'; |
|
|
|
|
|
|
|
|
|
|
|
import { utils } from '@abacus-network/utils'; |
|
|
|
import { utils } from '@abacus-network/utils'; |
|
|
|
|
|
|
|
|
|
|
|
export type RetryOptions = { |
|
|
|
export type RetryOptions = { |
|
|
|
// The wait interval in between
|
|
|
|
// Maximum number of times to make the RPC
|
|
|
|
interval: number; |
|
|
|
maxRequests: number; |
|
|
|
|
|
|
|
|
|
|
|
// Maximum number of times to retry
|
|
|
|
// Exponential backoff base value
|
|
|
|
retryLimit: number; |
|
|
|
baseRetryMs: number; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
export class RetryProvider extends ethers.providers.BaseProvider { |
|
|
|
export class RetryProvider extends ethers.providers.BaseProvider { |
|
|
@ -20,6 +21,10 @@ export class RetryProvider extends ethers.providers.BaseProvider { |
|
|
|
readonly retryOptions: RetryOptions, |
|
|
|
readonly retryOptions: RetryOptions, |
|
|
|
) { |
|
|
|
) { |
|
|
|
super(provider.getNetwork()); |
|
|
|
super(provider.getNetwork()); |
|
|
|
|
|
|
|
assert( |
|
|
|
|
|
|
|
retryOptions.maxRequests >= 1, |
|
|
|
|
|
|
|
'RetryOptions.maxRequests must be >= 1', |
|
|
|
|
|
|
|
); |
|
|
|
ethers.utils.defineReadOnly(this, 'provider', provider); |
|
|
|
ethers.utils.defineReadOnly(this, 'provider', provider); |
|
|
|
ethers.utils.defineReadOnly(this, 'retryOptions', retryOptions); |
|
|
|
ethers.utils.defineReadOnly(this, 'retryOptions', retryOptions); |
|
|
|
} |
|
|
|
} |
|
|
@ -28,8 +33,8 @@ export class RetryProvider extends ethers.providers.BaseProvider { |
|
|
|
perform(method: string, params: any): Promise<any> { |
|
|
|
perform(method: string, params: any): Promise<any> { |
|
|
|
return utils.retryAsync( |
|
|
|
return utils.retryAsync( |
|
|
|
() => this.provider.perform(method, params), |
|
|
|
() => this.provider.perform(method, params), |
|
|
|
this.retryOptions.retryLimit, |
|
|
|
this.retryOptions.maxRequests, |
|
|
|
this.retryOptions.interval, |
|
|
|
this.retryOptions.baseRetryMs, |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -41,13 +46,17 @@ export class RetryJsonRpcProvider extends ethers.providers.JsonRpcProvider { |
|
|
|
readonly retryOptions: RetryOptions, |
|
|
|
readonly retryOptions: RetryOptions, |
|
|
|
) { |
|
|
|
) { |
|
|
|
super(provider.connection, provider.network); |
|
|
|
super(provider.connection, provider.network); |
|
|
|
|
|
|
|
assert( |
|
|
|
|
|
|
|
retryOptions.maxRequests >= 1, |
|
|
|
|
|
|
|
'RetryOptions.maxRequests must be >= 1', |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async send(method: string, params: Array<any>): Promise<any> { |
|
|
|
async send(method: string, params: Array<any>): Promise<any> { |
|
|
|
return utils.retryAsync( |
|
|
|
return utils.retryAsync( |
|
|
|
async () => this.provider.send(method, params), |
|
|
|
async () => this.provider.send(method, params), |
|
|
|
this.retryOptions.retryLimit, |
|
|
|
this.retryOptions.maxRequests, |
|
|
|
this.retryOptions.interval, |
|
|
|
this.retryOptions.baseRetryMs, |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|