From a4afd4bc6bcac4867f0c522764393a9476c29879 Mon Sep 17 00:00:00 2001 From: Neven Dyulgerov Date: Mon, 17 Feb 2025 13:21:29 +0200 Subject: [PATCH] chore(apps/staking): Deprecate axios and use fetch instead --- apps/staking/__tests__/services/chain.test.ts | 84 +++++++++++-------- .../staking/__tests__/services/market.test.ts | 1 - apps/staking/src/pages/api/[chain]/stats.ts | 10 +-- apps/staking/src/services/chain.ts | 10 +-- 4 files changed, 57 insertions(+), 48 deletions(-) diff --git a/apps/staking/__tests__/services/chain.test.ts b/apps/staking/__tests__/services/chain.test.ts index 137b1435..47bab898 100644 --- a/apps/staking/__tests__/services/chain.test.ts +++ b/apps/staking/__tests__/services/chain.test.ts @@ -9,23 +9,23 @@ // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A // PARTICULAR PURPOSE. See the GNU General Public License for more details. -import axios from 'axios'; import { - getAllChains, allChainsUrl, - getChainUrl, - getChain, chainErrorData, convertChainIdToNetworkId, convertNetworkIdToChainId, + getAllChains, + getChain, getChainByChainId, getChainByKeyValue, getChainByNetworkId, + getChainUrl, IChainData, } from '../../src/services/chain'; -jest.mock('axios'); -const mockAxiosGet = axios.get as jest.MockedFunction; +global.fetch = jest.fn(); +const mockFetchGet = global.fetch as jest.MockedFunction; + const allChains = [ { name: 'ethereum', @@ -42,11 +42,13 @@ const allChains = [ ] as unknown as IChainData[]; describe('chain service', () => { - it('should invoke GET http request with correct endpoint', async () => { + it('should invoke GET http request for all chains with correct endpoint', async () => { let url = null; - mockAxiosGet.mockImplementation((endpoint) => { + mockFetchGet.mockImplementation((endpoint) => { url = endpoint; - return Promise.resolve({ data: {} }); + return Promise.resolve({ + json: () => new Promise((resolve) => resolve({})), + } as Response); }); await getAllChains(); @@ -54,12 +56,14 @@ describe('chain service', () => { expect(url).toBe(allChainsUrl); }); - it('should invoke GET http request with correct chain url', async () => { + it('should invoke GET http request for specific chain with correct chain url', async () => { const chainId = 5; let url = null; - mockAxiosGet.mockImplementation((endpoint) => { + mockFetchGet.mockImplementation((endpoint) => { url = endpoint; - return Promise.resolve({ data: {} }); + return Promise.resolve({ + json: () => new Promise((resolve) => resolve({})), + } as Response); }); await getChain(chainId); @@ -69,8 +73,10 @@ describe('chain service', () => { it('should invoke GET http request with correct chain url', async () => { const chainId = 5; - mockAxiosGet.mockImplementation(() => { - return Promise.reject({ data: {} }); + mockFetchGet.mockImplementation(() => { + return Promise.reject({ + json: () => new Promise((resolve) => resolve({})), + } as Response); }); const result = await getChain(chainId); @@ -85,12 +91,15 @@ describe('chain service', () => { it('should return networkId when invoking convertChainIdToNetworkId function', async () => { const chainId = 5; const networkId = 2; - mockAxiosGet.mockImplementation(() => { + mockFetchGet.mockImplementation(() => { return Promise.resolve({ - data: { - networkId, - }, - }); + json: () => + new Promise((resolve) => + resolve({ + networkId, + }) + ), + } as Response); }); const result = await convertChainIdToNetworkId(chainId); @@ -101,12 +110,15 @@ describe('chain service', () => { it('should return networkId when invoking getChainByChainId function', async () => { const chainId = 5; const networkId = 2; - mockAxiosGet.mockImplementation(() => { + mockFetchGet.mockImplementation(() => { return Promise.resolve({ - data: { - networkId, - }, - }); + json: () => + new Promise((resolve) => + resolve({ + networkId, + }) + ), + } as Response); }); const result = await getChainByChainId(chainId); @@ -115,10 +127,10 @@ describe('chain service', () => { }); it('should return correct chainData when invoking getChainByKeyValue function', async () => { - mockAxiosGet.mockImplementation(() => { + mockFetchGet.mockImplementation(() => { return Promise.resolve({ - data: allChains, - }); + json: () => new Promise((resolve) => resolve(allChains)), + } as Response); }); const key = 'name'; @@ -129,10 +141,10 @@ describe('chain service', () => { }); it('should throw error when invoking getChainByKeyValue function with key/value that does not match any chain', async () => { - mockAxiosGet.mockImplementation(() => { + mockFetchGet.mockImplementation(() => { return Promise.resolve({ - data: allChains, - }); + json: () => new Promise((resolve) => resolve(allChains)), + } as Response); }); const key = 'name'; @@ -151,10 +163,10 @@ describe('chain service', () => { ...chain, networkId: index + 1, })); - mockAxiosGet.mockImplementation(() => { + mockFetchGet.mockImplementation(() => { return Promise.resolve({ - data: chains, - }); + json: () => new Promise((resolve) => resolve(chains)), + } as Response); }); const result = await getChainByNetworkId(networkId); @@ -171,10 +183,10 @@ describe('chain service', () => { networkId: index + 1, chainId: index + 1, })); - mockAxiosGet.mockImplementation(() => { + mockFetchGet.mockImplementation(() => { return Promise.resolve({ - data: chains, - }); + json: () => new Promise((resolve) => resolve(chains)), + } as Response); }); const result = await convertNetworkIdToChainId(networkId); diff --git a/apps/staking/__tests__/services/market.test.ts b/apps/staking/__tests__/services/market.test.ts index 8863d835..83c5d43d 100644 --- a/apps/staking/__tests__/services/market.test.ts +++ b/apps/staking/__tests__/services/market.test.ts @@ -12,7 +12,6 @@ import { act, renderHook, waitFor } from '@testing-library/react'; import { endpoint, useMarketInformation } from '../../src/services/market'; -jest.mock('axios'); global.fetch = jest.fn(); const mockFetchGet = global.fetch as jest.MockedFunction; diff --git a/apps/staking/src/pages/api/[chain]/stats.ts b/apps/staking/src/pages/api/[chain]/stats.ts index 7d19eb24..ee43fb01 100644 --- a/apps/staking/src/pages/api/[chain]/stats.ts +++ b/apps/staking/src/pages/api/[chain]/stats.ts @@ -9,9 +9,8 @@ // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A // PARTICULAR PURPOSE. See the GNU General Public License for more details. -import axios from 'axios'; import Cors from 'cors'; -import { FixedNumber, constants } from 'ethers'; +import { constants, FixedNumber } from 'ethers'; import { NextApiRequest, NextApiResponse } from 'next'; import { getEstimatedRewardRate, getRewardRate } from '../../../utils/reward'; @@ -62,9 +61,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { }); const endpoint = `https://api.coingecko.com/api/v3/coins/cartesi?localization=false&tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false`; - const marketData = await axios.get(endpoint); + const response = await fetch(endpoint); + const marketData = await response.json(); const circulatingSupply = Math.round( - marketData.data.market_data.circulating_supply + marketData.market_data.circulating_supply ); let projectedAnnualEarnings = 0, @@ -85,7 +85,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { const { activeStake } = getEstimatedRewardRate(blocks, constants.One, 0, 0); res.json({ - price: +marketData.data.market_data.current_price.usd.toFixed(4), + price: +marketData.market_data.current_price.usd.toFixed(4), circulatingSupply, totalStaked: toCTSI(summary.totalStaked).toUnsafeFloat(), totalUsers: summary.totalUsers, diff --git a/apps/staking/src/services/chain.ts b/apps/staking/src/services/chain.ts index 1debacf6..8385b915 100644 --- a/apps/staking/src/services/chain.ts +++ b/apps/staking/src/services/chain.ts @@ -9,8 +9,6 @@ // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A // PARTICULAR PURPOSE. See the GNU General Public License for more details. -import axios from 'axios'; - export interface IChainData { name: string; chain: string; @@ -31,8 +29,8 @@ export interface IChainData { export const allChainsUrl = 'https://chainid.network/chains.json'; export async function getAllChains(): Promise { - const response = await axios.get(allChainsUrl); - return response.data; + const response = await fetch(allChainsUrl); + return await response.json(); } export const getChainUrl = (chainId: number) => @@ -51,8 +49,8 @@ export const chainErrorData = { export async function getChain(chainId: number): Promise { try { - const response = await axios.get(getChainUrl(chainId)); - return response.data; + const response = await fetch(getChainUrl(chainId)); + return await response.json(); } catch (e) { return { ...chainErrorData,