Skip to content

Commit

Permalink
chore(apps/staking): Deprecate axios and use fetch instead
Browse files Browse the repository at this point in the history
  • Loading branch information
nevendyulgerov committed Feb 17, 2025
1 parent 98369cd commit a4afd4b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 48 deletions.
84 changes: 48 additions & 36 deletions apps/staking/__tests__/services/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof axios.get>;
global.fetch = jest.fn();
const mockFetchGet = global.fetch as jest.MockedFunction<typeof global.fetch>;

const allChains = [
{
name: 'ethereum',
Expand All @@ -42,24 +42,28 @@ 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();

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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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';
Expand All @@ -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';
Expand All @@ -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);
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion apps/staking/__tests__/services/market.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof global.fetch>;

Expand Down
10 changes: 5 additions & 5 deletions apps/staking/src/pages/api/[chain]/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
10 changes: 4 additions & 6 deletions apps/staking/src/services/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,8 +29,8 @@ export interface IChainData {
export const allChainsUrl = 'https://chainid.network/chains.json';

export async function getAllChains(): Promise<IChainData[]> {
const response = await axios.get(allChainsUrl);
return response.data;
const response = await fetch(allChainsUrl);
return await response.json();
}

export const getChainUrl = (chainId: number) =>
Expand All @@ -51,8 +49,8 @@ export const chainErrorData = {

export async function getChain(chainId: number): Promise<IChainData> {
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,
Expand Down

0 comments on commit a4afd4b

Please sign in to comment.