From baae24792269e78201102ecb3d4492c116ae7525 Mon Sep 17 00:00:00 2001 From: Inokentii Mazhara Date: Mon, 27 Nov 2023 14:23:23 +0200 Subject: [PATCH] TW-1194 Remove old 'fetch' method --- package.json | 1 - src/utils/fetch.ts | 23 ----------------------- src/utils/tezos.ts | 24 +++++++++++++++++++++--- yarn.lock | 9 +-------- 4 files changed, 22 insertions(+), 35 deletions(-) delete mode 100644 src/utils/fetch.ts diff --git a/package.json b/package.json index e8c3441..3d580b2 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,6 @@ "bignumber.js": "^9.1.0", "body-parser": "^1.20.2", "cors": "^2.8.5", - "cross-fetch": "^3.1.5", "dotenv": "^9.0.2", "express": "^4.18.2", "firebase-admin": "^10.0.2", diff --git a/src/utils/fetch.ts b/src/utils/fetch.ts deleted file mode 100644 index 235f38c..0000000 --- a/src/utils/fetch.ts +++ /dev/null @@ -1,23 +0,0 @@ -import crossFetch from 'cross-fetch'; - -export class InvalidStatusError extends Error { - constructor(message: string, public status: number) { - super(message); - } -} - -export class NotOkFetchError extends Error {} - -export default async function fetch(url: string, init?: RequestInit): Promise { - const res = await crossFetch(url, init); - - const body = await res.json(); - if (res.status >= 400) { - throw new InvalidStatusError(body.message, res.status); - } - if (!res.ok) { - throw new NotOkFetchError('An error occurred while fetching'); - } - - return body; -} diff --git a/src/utils/tezos.ts b/src/utils/tezos.ts index f8b87a7..49754f6 100644 --- a/src/utils/tezos.ts +++ b/src/utils/tezos.ts @@ -1,10 +1,12 @@ import { compose, MichelCodecPacker, Signer, TezosToolkit } from '@taquito/taquito'; import { tzip12 } from '@taquito/tzip12'; import { tzip16 } from '@taquito/tzip16'; +import axios, { AxiosError } from 'axios'; import memoizee from 'memoizee'; import { ITicker } from '../interfaces/ticker.interface'; -import fetch from './fetch'; +import { isDefined } from './helpers'; +import logger from './logger'; import SingleQueryDataProvider from './SingleQueryDataProvider'; import { BcdTokenData } from './tzkt'; @@ -53,9 +55,25 @@ export const getStorage = memoizee( ); const getTezExchangeRate = async () => { - const { price: rawPrice } = await fetch('https://api.binance.com/api/v3/ticker/price?symbol=XTZUSDT'); + try { + const { data } = await axios.get('https://api.binance.com/api/v3/ticker/price?symbol=XTZUSDT'); + + return Number(data.price); + } catch (e) { + if (!(e instanceof AxiosError)) { + logger.error('Request for TEZ exchange rate failed with unknown error'); + } else if (isDefined(e.response) && isDefined(e.response.data)) { + logger.error( + `Request for TEZ exchange rate failed with status ${e.response.status} and message ${e.response.data}` + ); + } else if (isDefined(e.response) && isDefined(e.response.status)) { + logger.error(`Request for TEZ exchange rate failed with status ${e.response.status}`); + } else { + logger.error('Request for TEZ exchange rate failed without response'); + } - return Number(rawPrice); + throw e; + } }; export const tezExchangeRateProvider = new SingleQueryDataProvider(60000, getTezExchangeRate); diff --git a/yarn.lock b/yarn.lock index aef39d5..47b0342 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1311,13 +1311,6 @@ cross-env@^7.0.3: dependencies: cross-spawn "^7.0.1" -cross-fetch@^3.1.5: - version "3.1.5" - resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.5.tgz#e1389f44d9e7ba767907f7af8454787952ab534f" - integrity sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw== - dependencies: - node-fetch "2.6.7" - cross-spawn@^6.0.5: version "6.0.5" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4" @@ -3168,7 +3161,7 @@ nice-try@^1.0.4: resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== -node-fetch@2.6.7, node-fetch@^2.6.1: +node-fetch@^2.6.1: version "2.6.7" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad" integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==