Skip to content

Commit

Permalink
feat: add retry to axios in oneinch
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Oct 30, 2023
1 parent 693fcf3 commit 3059e0c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 14 additions & 6 deletions src/services/swap/oneInch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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}`);
}

Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 3059e0c

Please sign in to comment.