Skip to content

Commit

Permalink
0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
KABBOUCHI committed Oct 6, 2023
1 parent fbd6b8f commit 90d8314
Show file tree
Hide file tree
Showing 9 changed files with 937 additions and 29 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ const providerWithCustomOptions = new JsonRpcRetryProvider(
);
```

```ts
import { JsonRpcRetryProvider } from "@instadapp/utils";

const provider = new JsonRpcRetryProvider([
'https://rpc.ankr.com/invalid',
'https://rpc.ankr.com/invalid-2',
'https://rpc.ankr.com/eth',
'https://eth.llamarpc.com',
])
```

### JsonRpcRetryBatchProvider

```js
Expand Down
18 changes: 17 additions & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,23 @@ export default defineBuildConfig({
emitCJS: true,
inlineDependencies: true
},
externals: ['@ethersproject/abi'],
externals: [
'@ethersproject/abi',
'@ethersproject/providers',
'@ethersproject/properties',
'@ethersproject/networks',
'@ethersproject/bytes',
'@ethersproject/web',
'@ethersproject/transactions',
'@ethersproject/hash',
'@ethersproject/abstract-provider',
'@ethersproject/abstract-signer',
'@ethersproject/bignumber',
'@ethersproject/strings',
'@ethersproject/logger',
'@ethersproject/contracts',
'@ethersproject/address'
],
clean: true,
declaration: true
})
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@instadapp/utils",
"version": "0.4.11",
"version": "0.5.0",
"description": "",
"repository": "instadapp/utils",
"license": "MIT",
Expand All @@ -27,13 +27,13 @@
"test": "yarn lint && vitest"
},
"dependencies": {
"@ethersproject/address": "^5.6.1",
"@ethersproject/contracts": "^5.6.2",
"@ethersproject/providers": "^5.6.8",
"async-retry": "^1.3.3",
"axios": "^0.27.2"
},
"devDependencies": {
"@ethersproject/address": "^5.6.1",
"@ethersproject/contracts": "^5.6.2",
"@ethersproject/providers": "^5.6.8",
"@nuxtjs/eslint-config-typescript": "latest",
"@types/async-retry": "^1.4.4",
"@vitest/ui": "^0.17.1",
Expand Down
3 changes: 2 additions & 1 deletion src/abi/fetcher/AbiFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const DEFAULTS: IAbiFetcherOptions = {
bsc: 'https://bsc-dataseed.binance.org',
gnosis: 'https://rpc.gnosischain.com',
'polygon-zkevm': 'https://zkevm-rpc.com',
base: 'https://mainnet.base.org'
base: 'https://mainnet.base.org',
fuse: 'https://rpc.fuse.io'
},
networkToEtherscanAPI: {
polygon: 'https://api.polygonscan.com/api',
Expand Down
9 changes: 7 additions & 2 deletions src/promises/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface RetryOptions {
delay?: number;
timeouts: number[];
onRetry?: (error: Error, attempt: number) => void|Promise<void>;
}

export async function promiseTimeout<T> (promise: Promise<T>, ms: number) {
Expand All @@ -26,7 +27,7 @@ export function retry<T = any> (
retriesLeft?: number
) {
return new Promise<T>((resolve, reject) => {
const { timeouts } = options
const { timeouts, onRetry } = options

if (typeof retriesLeft === 'undefined' || retriesLeft === null) {
retriesLeft = timeouts.length
Expand All @@ -39,10 +40,14 @@ export function retry<T = any> (
const execution = promiseTimeout(operation(), timeout)

// If the promise is successful, resolve it and bubble the result up
return execution.then(resolve).catch((reason: any) => {
return execution.then(resolve).catch(async (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) {
// Call onRetry if provided
if (onRetry) {
await onRetry(reason, retriesLeft)
}
// Delay the new attempt slightly
return wait(options.delay || 300)
.then(retry.bind(null, operation, options, retriesLeft - 1))
Expand Down

0 comments on commit 90d8314

Please sign in to comment.