Skip to content

Commit

Permalink
🧹 Update retry logic & exclude typescript from the bundle (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Mar 1, 2024
1 parent d0d1cb3 commit 0dace79
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-spiders-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@layerzerolabs/ua-devtools-evm-hardhat": patch
---

Use retry functionality provided by devtools
5 changes: 2 additions & 3 deletions packages/ua-devtools-evm-hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"lint:fix": "eslint --fix '**/*.{js,ts,json}'"
},
"dependencies": {
"exponential-backoff": "~3.1.1",
"p-memoize": "~4.0.4"
"p-memoize": "~4.0.4",
"typescript": "^5.3.3"
},
"devDependencies": {
"@ethersproject/abi": "^5.7.0",
Expand Down Expand Up @@ -66,7 +66,6 @@
"jest": "^29.7.0",
"ts-node": "^10.9.2",
"tsup": "~8.0.1",
"typescript": "^5.3.3",
"zod": "^3.22.4"
},
"peerDependencies": {
Expand Down
42 changes: 4 additions & 38 deletions packages/ua-devtools-evm-hardhat/src/oapp/typescript/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Statement,
VariableStatement,
} from 'typescript'
import { formatEid, OmniAddress } from '@layerzerolabs/devtools'
import { createRetryFactory, createSimpleRetryStrategy, formatEid, OmniAddress } from '@layerzerolabs/devtools'
import { getEidForNetworkName } from '@layerzerolabs/devtools-evm-hardhat'
import { getReceiveConfig, getSendConfig } from '@/utils/taskHelpers'
import { Timeout, Uln302ExecutorConfig, Uln302UlnConfig } from '@layerzerolabs/protocol-devtools'
Expand Down Expand Up @@ -41,10 +41,8 @@ import {
ULN_CONFIG,
ZERO,
} from '@/oapp/typescript/constants'
import { backOff } from 'exponential-backoff'
import { createLogger } from '@layerzerolabs/io-devtools'

const logger = createLogger()
const retryThreeTimes = createRetryFactory(createSimpleRetryStrategy<[string, string]>(3))

/**
* Normalizes the identifier name by replacing hyphens with underscores.
Expand Down Expand Up @@ -383,8 +381,8 @@ export const createDefaultConfig = async (fromNetwork: string, toNetwork: string
let receiveDefaultConfig: [OmniAddress, Uln302UlnConfig, Timeout] | undefined
try {
;[sendDefaultConfig, receiveDefaultConfig] = await Promise.all([
basicRetryPolicy(() => getSendConfig(fromNetwork, toNetwork)),
basicRetryPolicy(() => getReceiveConfig(fromNetwork, toNetwork)),
retryThreeTimes(getSendConfig)(fromNetwork, toNetwork),
retryThreeTimes(getReceiveConfig)(fromNetwork, toNetwork),
])
} catch (error) {
console.error('Failed to get send and receive default configs:', error)
Expand Down Expand Up @@ -449,35 +447,3 @@ export const generateLzConfig = async (
)
),
])

/**
* Retry policy function that retries a given asynchronous operation with exponential backoff strategy.
* @template T - The type of the result returned by the asynchronous operation.
* @param {() => Promise<T>} fn - The asynchronous operation to be retried.
* @param {number} [maxAttempts=3] - The maximum number of attempts.
* @param {number} [baseDelay=1000] - The base delay in milliseconds before the first retry.
* @param {number} [maxDelay=15000] - The maximum delay in milliseconds between retries.
* @returns {Promise<T>} - A promise resolving to the result of the operation if successful.
* @throws {Error} - Throws an error if all attempts fail.
*/
async function basicRetryPolicy<T>(fn: () => Promise<T>, maxAttempts: number = 3): Promise<T> {
const operation = async () => {
return await fn()
}

const backoffOptions = {
numOfAttempts: maxAttempts,
retry: (e: any, attemptNumber: number) => {
logger.info(`Attempt ${attemptNumber}/${maxAttempts} failed with error: ${e}`)
if (attemptNumber < maxAttempts) {
logger.info('Retrying...')
return true
} else {
logger.info(`All ${maxAttempts} attempts failed.`)
return false
}
},
}

return await backOff(operation, backoffOptions)
}
9 changes: 3 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0dace79

Please sign in to comment.