Skip to content

Commit

Permalink
feat: reinstate fetching markets from the API
Browse files Browse the repository at this point in the history
This reverts commit 8af944c.
  • Loading branch information
gleiser-oliveira committed Sep 23, 2024
1 parent 344e4ce commit 3bdcf93
Show file tree
Hide file tree
Showing 48 changed files with 8,138 additions and 3,355 deletions.
1,283 changes: 1,283 additions & 0 deletions apps/evm/src/__mocks__/api/markets.json

Large diffs are not rendered by default.

4,030 changes: 4,030 additions & 0 deletions apps/evm/src/__mocks__/api/pools.json

Large diffs are not rendered by default.

122 changes: 5 additions & 117 deletions apps/evm/src/__mocks__/models/markets.ts
Original file line number Diff line number Diff line change
@@ -1,120 +1,8 @@
import BigNumber from 'bignumber.js';
import { formatToMarket } from 'utilities';
import apiMarketsResponse from '../api/markets.json';

import type { Market } from 'types';

export const markets: Market[] = [
{
address: '0x74469281310195a04840daf6edf576f559a3de80',
borrowerCount: 306,
supplierCount: 898,
totalXvsDistributedTokens: new BigNumber('318096'),
},
{
address: '0xd5c4c2e2facbeb59d0216d0595d63fcdc6f9a1a7',
borrowerCount: 232,
supplierCount: 638,
totalXvsDistributedTokens: new BigNumber('261028'),
},
{
address: '0xb7526572ffe56ab9d7489838bf2e18e3323b441a',
borrowerCount: 315,
supplierCount: 736,
totalXvsDistributedTokens: new BigNumber('211354'),
},
{
address: '0x08e0a5575de71037ae36abfafb516595fe68e5e4',
borrowerCount: 533,
supplierCount: 2235,
totalXvsDistributedTokens: new BigNumber('928041'),
},
{
address: '0x2e7222e51c0f6e98610a1543aa3836e092cde62c',
borrowerCount: 261,
supplierCount: 1700,
totalXvsDistributedTokens: new BigNumber('924581'),
},
{
address: '0x6d6f697e34145bb95c54e77482d97cc261dc237e',
borrowerCount: 77,
supplierCount: 326,
totalXvsDistributedTokens: new BigNumber('14741'),
},
{
address: '0xb6e9322c49fd75a367fcb17b0fcd62c5070ebcbe',
borrowerCount: 51,
supplierCount: 415,
totalXvsDistributedTokens: new BigNumber('269883'),
},
{
address: '0x162d005f0fff510e54958cfc5cf32a3180a84aab',
borrowerCount: 51,
supplierCount: 211,
totalXvsDistributedTokens: new BigNumber('39187'),
},
{
address: '0xafc13bc065abee838540823431055d2ea52eba52',
borrowerCount: 15,
supplierCount: 150,
totalXvsDistributedTokens: new BigNumber('56938'),
},
{
address: '0x488ab2826a154da01cc4cc16a8c83d4720d3ca2c',
borrowerCount: 31,
supplierCount: 188,
totalXvsDistributedTokens: new BigNumber('54890'),
},
{
address: '0x37c28de42ba3d22217995d146fc684b2326ede64',
borrowerCount: 19,
supplierCount: 157,
totalXvsDistributedTokens: new BigNumber('15830'),
},
{
address: '0xf912d3001caf6dc4add366a62cc9115b4303c9a9',
borrowerCount: 20,
supplierCount: 9,
totalXvsDistributedTokens: new BigNumber('2487'),
},
{
address: '0x3619bddc61189f33365cc572df3a68fb3b316516',
borrowerCount: 7,
supplierCount: 152,
totalXvsDistributedTokens: new BigNumber('11'),
},
{
address: '0xedac03d29ff74b5fdc0cc936f6288312e1459bc6',
borrowerCount: 9,
supplierCount: 195,
totalXvsDistributedTokens: new BigNumber('4473'),
},
{
address: '0x714db6c38a17883964b68a07d56ce331501d9eb6',
borrowerCount: 6,
supplierCount: 144,
totalXvsDistributedTokens: new BigNumber('805'),
},
{
address: '0x3a00d9b02781f47d033bad62edc55fbf8d083fb0',
borrowerCount: 5,
supplierCount: 138,
totalXvsDistributedTokens: new BigNumber('3480'),
},
{
address: '0x369fea97f6fb7510755dca389088d9e2e2819278',
borrowerCount: 3,
supplierCount: 133,
totalXvsDistributedTokens: new BigNumber('4020'),
},
{
address: '0xf206af85bc2761c4f876d27bd474681cfb335efa',
borrowerCount: 2,
supplierCount: 5,
totalXvsDistributedTokens: new BigNumber('601'),
},
{
address: '0x9c3015191d39cf1930f92eb7e7bcbd020bca286a',
borrowerCount: 0,
supplierCount: 3,
totalXvsDistributedTokens: new BigNumber('473'),
},
];
export const markets: Market[] = apiMarketsResponse.result.map(apiMarket =>
formatToMarket({ apiMarket }),
);
15 changes: 14 additions & 1 deletion apps/evm/src/__mocks__/models/pools.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import BigNumber from 'bignumber.js';

import apiPoolsResponse from '__mocks__/api/pools.json';
import { assetData } from '__mocks__/models/asset';

import type { Pool } from 'types';
import formatToPool from 'clients/api/queries/getApiPools/formatToPool';
import { CHAIN_METADATA } from 'constants/chainMetadata';
import { ChainId, type Pool } from 'types';

const { corePoolComptrollerContractAddress } = CHAIN_METADATA[ChainId.BSC_TESTNET];

export const apiPoolsData = apiPoolsResponse.result.map(apiPoolData =>
formatToPool({
apiPoolData,
corePoolComptrollerContractAddress,
chainId: ChainId.BSC_TESTNET,
}),
);

export const poolData: Pool[] = [
{
Expand Down
2 changes: 1 addition & 1 deletion apps/evm/src/clients/api/__mocks__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const getHypotheticalAccountLiquidity = vi.fn();
export const getLegacyPoolMarkets = vi.fn(async () => ({ markets }));
export const useGetLegacyPoolMarkets = vi.fn(() =>
useQuery({
queryKey: [FunctionKey.GET_MAIN_MARKETS],
queryKey: [FunctionKey.GET_LEGACY_CORE_POOL_MARKETS],
queryFn: getLegacyPoolMarkets,
}),
);
Expand Down
12 changes: 9 additions & 3 deletions apps/evm/src/clients/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,13 @@ export { default as useGetVaiTreasuryPercentage } from './queries/getVaiTreasury
export { default as getHypotheticalAccountLiquidity } from './queries/getHypotheticalAccountLiquidity';
export * from './queries/getHypotheticalAccountLiquidity';

export { default as getLegacyPoolMarkets } from './queries/getLegacyPoolMarkets';
export * from './queries/getLegacyPoolMarkets';
export { default as useGetLegacyPoolMarkets } from './queries/getLegacyPoolMarkets/useGetLegacyPoolMarkets';
export { default as getApiMarkets } from './queries/getApiMarkets';
export * from './queries/getApiMarkets';
export { default as useGetApiMarkets } from './queries/getApiMarkets/useGetApiMarkets';

export { default as getPools } from './queries/getApiPools';
export * from './queries/getApiPools';
export { default as useGetApiPools } from './queries/getApiPools/useGetApiPools';

export { default as getVTokenBalancesAll } from './queries/getVTokenBalancesAll';
export * from './queries/getVTokenBalancesAll';
Expand Down Expand Up @@ -200,6 +204,8 @@ export { default as useGetLegacyPool } from './queries/getLegacyPool/useGetLegac

export { default as useGetPools } from './queries/useGetPools';

export { default as useGetLegacyPoolMarkets } from './queries/useGetLegacyPoolMarkets';

export { default as useGetPool } from './queries/useGetPool';

export { default as getIsolatedPools } from './queries/getIsolatedPools';
Expand Down
2 changes: 1 addition & 1 deletion apps/evm/src/clients/api/mutations/borrow/useBorrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const useBorrow = (
}

queryClient.invalidateQueries({
queryKey: [FunctionKey.GET_MAIN_MARKETS],
queryKey: [FunctionKey.GET_LEGACY_CORE_POOL_MARKETS],
});
queryClient.invalidateQueries({
queryKey: [FunctionKey.GET_LEGACY_POOL],
Expand Down
2 changes: 1 addition & 1 deletion apps/evm/src/clients/api/mutations/repay/useRepay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const useRepay = (
});

queryClient.invalidateQueries({
queryKey: [FunctionKey.GET_MAIN_MARKETS],
queryKey: [FunctionKey.GET_LEGACY_CORE_POOL_MARKETS],
});
queryClient.invalidateQueries({
queryKey: [FunctionKey.GET_LEGACY_POOL],
Expand Down
2 changes: 1 addition & 1 deletion apps/evm/src/clients/api/mutations/supply/useSupply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const useSupply = (
queryKey: [FunctionKey.GET_V_TOKEN_BALANCES_ALL],
});
queryClient.invalidateQueries({
queryKey: [FunctionKey.GET_MAIN_MARKETS],
queryKey: [FunctionKey.GET_LEGACY_CORE_POOL_MARKETS],
});
queryClient.invalidateQueries({
queryKey: [FunctionKey.GET_LEGACY_POOL],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const useSwapTokens = (
});

queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_V_TOKEN_BALANCES_ALL] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_MAIN_MARKETS] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_LEGACY_CORE_POOL_MARKETS] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_LEGACY_POOL] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_ISOLATED_POOLS] });
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const useSwapTokensAndRepayAndRepay = (
});

queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_V_TOKEN_BALANCES_ALL] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_MAIN_MARKETS] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_LEGACY_CORE_POOL_MARKETS] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_LEGACY_POOL] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_ISOLATED_POOLS] });
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const useSwapTokensAndSupply = (
});

queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_V_TOKEN_BALANCES_ALL] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_MAIN_MARKETS] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_LEGACY_CORE_POOL_MARKETS] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_LEGACY_POOL] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_ISOLATED_POOLS] });
},
Expand Down
2 changes: 1 addition & 1 deletion apps/evm/src/clients/api/mutations/withdraw/useWithdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const useWithdraw = (
const accountAddress = await tokenContract?.signer.getAddress();

queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_V_TOKEN_BALANCES_ALL] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_MAIN_MARKETS] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_LEGACY_CORE_POOL_MARKETS] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_LEGACY_POOL] });
queryClient.invalidateQueries({ queryKey: [FunctionKey.GET_ISOLATED_POOLS] });

Expand Down
95 changes: 95 additions & 0 deletions apps/evm/src/clients/api/queries/getApiMarkets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import type { ChainId, Market } from 'types';
import { formatToMarket, restService } from 'utilities';

export interface ApiMarketData {
address: string;
symbol: string;
name: string;
underlyingAddress: string | null;
underlyingName: string;
underlyingSymbol: string;
underlyingDecimal: number;
borrowerDailyXvsMantissa: string | null;
supplierDailyXvsMantissa: string | null;
xvsBorrowIndex: string | null;
xvsSupplyIndex: string | null;
borrowRatePerBlock: string;
supplyRatePerBlock: string;
exchangeRateMantissa: string;
underlyingPriceMantissa: string;
totalBorrowsMantissa: string;
totalSupplyMantissa: string;
cashMantissa: string;
totalReservesMantissa: string;
reserveFactorMantissa: string;
collateralFactorMantissa: string;
borrowApy: string;
supplyApy: string;
borrowXvsApr: string | null;
supplyXvsApr: string | null;
liquidityCents: string;
tokenPriceCents: string;
totalDistributedMantissa: string | null;
lastCalculatedXvsAccruedBlockNumber: string | null;
supplyCapsMantissa: string;
borrowCapsMantissa: string;
borrowerCount: number;
supplierCount: number;
estimatedPrimeBorrowApyBoost: string | null;
estimatedPrimeSupplyApyBoost: string | null;
pausedActionsBitmap: number;
isListed: boolean;
poolComptrollerAddress: string;
rewardsDistributors: {
marketAddress: string;
rewardTokenAddress: string;
lastRewardingSupplyBlockOrTimestamp: string;
lastRewardingBorrowBlockOrTimestamp: string;
supplySpeed: string;
borrowSpeed: string;
priceMantissa: string;
rewardsDistributorContractAddress: string | null;
}[];
}

export interface GetApiMarketsResponse {
result: ApiMarketData[];
request: { addresses: string[] };
}

export interface GetApiMarketsInput {
chainId: ChainId;
poolComptrollerAddress?: string;
}

export interface GetApiMarketsOutput {
markets: Market[];
}

const getApiMarkets = async ({
chainId,
poolComptrollerAddress,
}: GetApiMarketsInput): Promise<GetApiMarketsOutput> => {
const response = await restService<GetApiMarketsResponse>({
endpoint: '/markets',
method: 'GET',
params: {
limit: 50,
isListed: 1,
chainId,
poolComptrollerAddress,
},
});

const payload = response.data;

if (payload && 'error' in payload) {
throw new Error(payload.error);
}

const markets: Market[] = (payload?.result || []).map(apiMarket => formatToMarket({ apiMarket }));

return { markets };
};

export default getApiMarkets;
37 changes: 37 additions & 0 deletions apps/evm/src/clients/api/queries/getApiMarkets/useGetApiMarkets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { type QueryObserverOptions, useQuery } from '@tanstack/react-query';

import getApiMarkets, {
type GetApiMarketsInput,
type GetApiMarketsOutput,
} from 'clients/api/queries/getApiMarkets';
import FunctionKey from 'constants/functionKey';
import { useGetToken } from 'libs/tokens';
import { useChainId } from 'libs/wallet';
import { callOrThrow } from 'utilities';

type TrimmedGetMarketsInput = Omit<GetApiMarketsInput, 'xvs'>;

export type UseGetMarketsQueryKey = [FunctionKey.GET_MARKETS, TrimmedGetMarketsInput];

type Options = QueryObserverOptions<
GetApiMarketsOutput,
Error,
GetApiMarketsOutput,
GetApiMarketsOutput,
UseGetMarketsQueryKey
>;

const useGetApiMarkets = (options?: Options) => {
const { chainId } = useChainId();
const xvs = useGetToken({
symbol: 'XVS',
});

return useQuery({
queryKey: [FunctionKey.GET_MARKETS, { chainId }],
queryFn: () => callOrThrow({ xvs }, params => getApiMarkets({ ...params, chainId })),
...options,
});
};

export default useGetApiMarkets;
Loading

0 comments on commit 3bdcf93

Please sign in to comment.