diff --git a/package.json b/package.json index ff313ff..119d40b 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "amqplib": "^0.10.3", "async-mutex": "^0.4.0", "axios": "^1.6.0", + "axios-retry": "^3.8.1", "class-transformer": "^0.5.1", "class-validator": "^0.14.0", "cors": "^2.8.5", diff --git a/src/services/swap/oneInch.ts b/src/services/swap/oneInch.ts index 930a68d..11f6728 100644 --- a/src/services/swap/oneInch.ts +++ b/src/services/swap/oneInch.ts @@ -8,6 +8,7 @@ import { } from "@gearbox-protocol/sdk"; import type { AxiosInstance } from "axios"; import axios from "axios"; +import axiosRetry from "axios-retry"; import type { BigNumberish, ethers, Wallet } from "ethers"; import { Service } from "typedi"; @@ -33,13 +34,20 @@ export default class OneInch extends BaseSwapper implements ISwapper { throw new Error("1inch API key not provided"); } const baseURL = `https://api.1inch.dev/swap/v5.2/${CHAINS[network]}`; - this.apiClient = axios.create({ - baseURL, - headers: { - Authorization: `Bearer ${config.oneInchApiKey}`, - accept: "application/json", + this.apiClient = axiosRetry( + axios.create({ + baseURL, + headers: { + Authorization: `Bearer ${config.oneInchApiKey}`, + accept: "application/json", + }, + }), + { + retries: 5, + retryCondition: e => e.response?.status === 429, + retryDelay: axiosRetry.exponentialDelay, }, - }); + ) as any as AxiosInstance; this.log.debug(`API URL: ${baseURL}`); } diff --git a/yarn.lock b/yarn.lock index f195017..bdfc52e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -664,6 +664,13 @@ chalk "^2.4.2" js-tokens "^4.0.0" +"@babel/runtime@^7.15.4": + version "7.23.2" + resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.23.2.tgz#062b0ac103261d68a966c4c7baf2ae3e62ec3885" + integrity sha512-mM8eg4yl5D6i3lu2QKPuPH4FArvJ8KhTofbE7jwMUv9KX5mBvwPAqnV3MlyBNqdp9RyRKP6Yck8TrfYrPvX3bg== + dependencies: + regenerator-runtime "^0.14.0" + "@babel/runtime@^7.21.0": version "7.22.15" resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.15.tgz#38f46494ccf6cf020bd4eed7124b425e83e523b8" @@ -2926,6 +2933,14 @@ available-typed-arrays@^1.0.5: resolved "https://registry.yarnpkg.com/available-typed-arrays/-/available-typed-arrays-1.0.5.tgz#92f95616501069d07d10edb2fc37d3e1c65123b7" integrity sha512-DMD0KiN46eipeziST1LPP/STfDU0sufISXmjSgvVsoU2tqxctQeASejWcfNtxYKqETM1UxQ8sp2OrSBWpHY6sw== +axios-retry@^3.8.1: + version "3.8.1" + resolved "https://registry.yarnpkg.com/axios-retry/-/axios-retry-3.8.1.tgz#4bb53f87ea537bce904c477e5c2808571066acbb" + integrity sha512-4XseuArB4CEbfLRtMpUods2q8MLBvD4r8ifKgK4SP2FRgzQIPUDpzZ+cjQ/19eu3w2UpKgkJA+myEh2BYDSjqQ== + dependencies: + "@babel/runtime" "^7.15.4" + is-retry-allowed "^2.2.0" + axios@^0.27.2: version "0.27.2" resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972" @@ -5324,6 +5339,11 @@ is-regex@^1.1.4: call-bind "^1.0.2" has-tostringtag "^1.0.0" +is-retry-allowed@^2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-2.2.0.tgz#88f34cbd236e043e71b6932d09b0c65fb7b4d71d" + integrity sha512-XVm7LOeLpTW4jV19QSH38vkswxoLud8sQ57YwJVTPWdiaI9I8keEhGFpBlslyVsgdQy4Opg8QOLb8YRgsyZiQg== + is-set@^2.0.1, is-set@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/is-set/-/is-set-2.0.2.tgz#90755fa4c2562dc1c5d4024760d6119b94ca18ec"