Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#197 Remove axios package #200

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 48 additions & 36 deletions __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
19 changes: 10 additions & 9 deletions __tests__/services/market.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { act, renderHook, waitFor } from '@testing-library/react';
import axios from 'axios';
import { useMarketInformation, endpoint } from '../../src/services/market';
import { endpoint, useMarketInformation } from '../../src/services/market';

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>;

describe('market service', () => {
it('should invoke GET http request 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 act(async () => {
Expand All @@ -45,10 +46,10 @@ describe('market service', () => {
circulating_supply: 10,
},
};
mockAxiosGet.mockImplementation(() => {
mockFetchGet.mockImplementation(() => {
return Promise.resolve({
data,
});
json: () => new Promise((resolve) => resolve(data)),
} as Response);
});

const { result } = renderHook(() => useMarketInformation());
Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,20 @@
"@emotion/react": "11.11.1",
"@emotion/styled": "11.11.0",
"@ethersproject/bignumber": "5.7.0",
"@fontsource/rubik": "4.5.0",
"@fortawesome/fontawesome-free": "5.15.3",
"@libsql/client": "^0.14.0",
"@metamask/onboarding": "1.0.1",
"@safe-global/safe-apps-provider": "^0.18.5",
"@safe-global/safe-apps-sdk": "^9.1.0",
"@types/humanize-duration": "3.25.1",
"@unleash/proxy-client-react": "^4.5.1",
"@web3-onboard/coinbase": "^2.4.1",
"@web3-onboard/core": "^2.23.1",
"@web3-onboard/gnosis": "^2.3.2",
"@web3-onboard/injected-wallets": "^2.11.3",
"@web3-onboard/react": "^2.10.1",
"@web3-onboard/walletconnect": "^2.6.2",
"@fontsource/rubik": "4.5.0",
"@fortawesome/fontawesome-free": "5.15.3",
"@libsql/client": "^0.14.0",
"@metamask/onboarding": "1.0.1",
"@types/humanize-duration": "3.25.1",
"axios": "0.21.4",
"big-number-input": "1.0.3",
"chart.js": "3.9.1",
"cors": "2.8.5",
Expand All @@ -70,8 +69,8 @@
"next": "14.2.21",
"popper.js": "1.16.1",
"react": "18.3.1",
"react-dom": "18.3.1",
"react-chartjs-2": "4.3.1",
"react-dom": "18.3.1",
"react-flow-renderer": "9.6.6",
"react-ga4": "1.4.1",
"react-gtm-module": "2.0.11",
Expand All @@ -88,7 +87,6 @@
"@babel/core": "7.22.8",
"@babel/plugin-proposal-class-properties": "7.18.6",
"@babel/plugin-proposal-private-methods": "7.18.6",
"dotenv-cli": "7.2.1",
"@storybook/addon-actions": "6.5.16",
"@storybook/addon-essentials": "6.5.16",
"@storybook/addon-links": "6.5.16",
Expand All @@ -111,6 +109,7 @@
"babel-jest": "29.5.0",
"babel-loader": "9.1.2",
"chromatic": "5.10.2",
"dotenv-cli": "7.2.1",
"drizzle-kit": "^0.30.0",
"eslint": "7.32.0",
"eslint-config-custom": "*",
Expand Down
10 changes: 5 additions & 5 deletions 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 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
8 changes: 4 additions & 4 deletions src/services/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
// PARTICULAR PURPOSE. See the GNU General Public License for more details.

import { useEffect, useState } from 'react';
import axios from 'axios';

export type MarketInformation = {
price?: number;
Expand All @@ -29,9 +28,10 @@ export const useMarketInformation = () => {
useEffect(() => {
setLoading(true);
setError('');
axios
.get(endpoint)
.then(({ data }) => {

fetch(endpoint)
.then((res) => res.json())
.then((data) => {
setLoading(false);
setError('');
setMarketInformation({
Expand Down