-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuseAssets.ts
More file actions
133 lines (111 loc) · 4.01 KB
/
useAssets.ts
File metadata and controls
133 lines (111 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { useMemo } from 'react';
import { useChain } from '@interchain-kit/react';
import { defaultContext } from '@tanstack/react-query';
import BigNumber from 'bignumber.js';
import { useGetAllBalances } from '@interchainjs/react/cosmos/bank/v1beta1/query.rpc.react';
import { Coin } from '@interchainjs/react/cosmos/base/v1beta1/coin';
import { PrettyAsset } from '@/components';
import { useChainUtils } from './useChainUtils';
import { useChainAssetsPrices } from './useChainAssetsPrices';
import { getPagination } from './useTotalAssets';
import { useRpcEndpoint } from '../common';
(BigInt.prototype as any).toJSON = function () {
return this.toString();
};
export const useAssets = (chainName: string) => {
const { address } = useChain(chainName);
const { data: rpcEndpoint, isFetching } = useRpcEndpoint(chainName);
const isReady = !!address && !!rpcEndpoint;
const allBalancesQuery = useGetAllBalances({
request: {
address: address || '',
pagination: getPagination(100n),
resolveDenom: false,
},
options: {
enabled: isReady,
select: ({ balances }) => balances || [],
context: defaultContext,
},
clientResolver: rpcEndpoint,
customizedQueryKey: ['allBalances', address],
});
const pricesQuery = useChainAssetsPrices(chainName);
const dataQueries = {
allBalances: allBalancesQuery,
prices: pricesQuery,
};
const queriesToRefetch = [dataQueries.allBalances];
const queries = Object.values(dataQueries);
const isInitialFetching = queries.some(({ isLoading }) => isLoading);
const isRefetching = queries.some(({ isRefetching }) => isRefetching);
const isLoading = isFetching || isInitialFetching || isRefetching;
type AllQueries = typeof dataQueries;
type QueriesData = {
[Key in keyof AllQueries]: NonNullable<AllQueries[Key]['data']>;
};
const {
ibcAssets,
getAssetByDenom,
convRawToDispAmount,
calcCoinDollarValue,
denomToSymbol,
getPrettyChainName,
} = useChainUtils(chainName);
const data = useMemo(() => {
if (isLoading) return;
const queriesData = Object.fromEntries(
Object.entries(dataQueries).map(([key, query]) => [key, query.data])
) as QueriesData;
const { allBalances, prices: rawPrices } = queriesData;
// Filter out undefined values to ensure proper indexing
const prices = Object.fromEntries(
Object.entries(rawPrices ?? {}).filter(([, value]) => value != null)
);
const nativeAndIbcBalances: Coin[] = allBalances?.filter(
({ denom }) => !denom.startsWith('gamm') && prices[denom]
);
const emptyBalances: Coin[] = ibcAssets
.filter(({ base }) => {
const notInBalances = !nativeAndIbcBalances?.find(
({ denom }) => denom === base
);
return notInBalances && prices[base];
})
.map((asset) => ({ denom: asset.base, amount: '0' }))
.reduce((acc: { denom: string; amount: string }[], current) => {
if (!acc.some((balance) => balance.denom === current.denom)) {
acc.push(current);
}
return acc;
}, []);
const finalAssets = [...(nativeAndIbcBalances ?? []), ...emptyBalances]
.map(({ amount, denom }) => {
const asset = getAssetByDenom(denom);
const symbol = denomToSymbol(denom);
const dollarValue = calcCoinDollarValue(prices, { amount, denom });
return {
symbol,
logoUrl: asset?.logoURIs?.png || asset?.logoURIs?.svg,
prettyChainName: getPrettyChainName(denom),
displayAmount: convRawToDispAmount(denom, amount),
dollarValue,
amount,
denom,
};
})
.sort((a, b) =>
new BigNumber(a.dollarValue).lt(b.dollarValue) ? 1 : -1
);
return {
prices,
allBalances,
assets: finalAssets as PrettyAsset[],
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isLoading]);
const refetch = () => {
queriesToRefetch.forEach((query) => query.refetch());
};
return { data, isLoading, refetch };
};