-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuseChainUtils.ts
More file actions
186 lines (159 loc) · 5.4 KB
/
useChainUtils.ts
File metadata and controls
186 lines (159 loc) · 5.4 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { useMemo } from 'react';
import { useWalletManager } from '@interchain-kit/react';
import { Asset, AssetList } from '@chain-registry/types';
import { asset_lists as ibcAssetLists } from '@chain-registry/assets';
import { assetLists as chainAssets, ibcData as ibc } from 'chain-registry';
import { Coin } from '@interchainjs/react/types';
import BigNumber from 'bignumber.js';
import { PrettyAsset } from '@/components';
import { CoinDenom, CoinSymbol, Exponent, PriceHash } from '@/utils';
import { useStarshipChains } from '../common';
export const useChainUtils = (chainName: string) => {
const { chains, assetLists } = useWalletManager();
const { data: starshipData } = useStarshipChains();
const { chains: starshipChains = [], assets: starshipAssets = [] } =
starshipData?.v1 ?? {};
const isStarshipChain = starshipChains.some(
(chain) => chain.chainName === chainName
);
const filterAssets = (assetList: AssetList[]): Asset[] => {
return (
assetList
.find(({ chainName }) => chainName === chainName)
?.assets?.filter(({ typeAsset }) => typeAsset !== 'ics20') || []
);
};
const { nativeAssets, ibcAssets } = useMemo(() => {
// @ts-ignore
const nativeAssets = filterAssets([
...chainAssets,
...(isStarshipChain ? starshipAssets : []),
]);
// @ts-ignore
const ibcAssets = filterAssets(ibcAssetLists);
return { nativeAssets, ibcAssets };
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [chainName]);
const allAssets = [...nativeAssets, ...ibcAssets];
const getIbcAssetsLength = () => {
return ibcAssets.length;
};
const getAssetByDenom = (denom: CoinDenom): Asset => {
return allAssets.find((asset) => asset.base === denom) as Asset;
};
const denomToSymbol = (denom: CoinDenom): CoinSymbol => {
const asset = getAssetByDenom(denom);
const symbol = asset?.symbol;
if (!symbol) {
return denom;
}
return symbol;
};
const symbolToDenom = (symbol: CoinSymbol, chainName?: string): CoinDenom => {
const asset = allAssets.find(
(asset) =>
asset.symbol === symbol &&
(!chainName ||
asset.traces?.[0].counterparty.chainName.toLowerCase() ===
chainName.toLowerCase())
);
const base = asset?.base;
if (!base) {
return symbol;
}
return base;
};
const getExponentByDenom = (denom: CoinDenom): Exponent => {
const asset = getAssetByDenom(denom);
const unit = asset.denomUnits.find(({ denom }) => denom === asset.display);
return unit?.exponent || 0;
};
const convRawToDispAmount = (symbol: string, amount: string | number) => {
const denom = symbolToDenom(symbol);
return new BigNumber(amount)
.shiftedBy(-getExponentByDenom(denom))
.toString();
};
const calcCoinDollarValue = (prices: PriceHash, coin: Coin) => {
const { denom, amount } = coin;
return new BigNumber(amount)
.shiftedBy(-getExponentByDenom(denom))
.multipliedBy(prices[denom])
.toString();
};
const getChainName = (ibcDenom: CoinDenom) => {
if (nativeAssets.find((asset) => asset.base === ibcDenom)) {
return chainName;
}
const asset = ibcAssets.find((asset) => asset.base === ibcDenom);
const ibcChainName = asset?.traces?.[0].counterparty.chainName;
if (!ibcChainName)
throw Error('chainName not found for ibcDenom: ' + ibcDenom);
return ibcChainName;
};
const getPrettyChainName = (ibcDenom: CoinDenom) => {
const chainName = getChainName(ibcDenom);
const chain = chains.find((chain) => chain.chainName === chainName);
if (!chain) throw Error('chain not found');
return chain.prettyName;
};
const isNativeAsset = ({ denom }: PrettyAsset) => {
return !!nativeAssets.find((asset) => asset.base === denom);
};
const getNativeDenom = (chainName: string) => {
const assetList = assetLists.find((chain) => chain.chainName === chainName);
const denom = assetList?.assets?.[0]?.base;
if (!denom) throw Error('denom not found');
return denom;
};
const getDenomBySymbolAndChain = (chainName: string, symbol: string) => {
const assetList = assetLists.find((chain) => chain.chainName === chainName);
const denom = assetList?.assets.find(
(asset) => asset.symbol === symbol
)?.base;
if (!denom) throw Error('denom not found');
return denom;
};
const getIbcInfo = (fromChainName: string, toChainName: string) => {
let flipped = false;
let ibcInfo = ibc.find(
(i) =>
i.chain1.chainName === fromChainName &&
i.chain2.chainName === toChainName
);
if (!ibcInfo) {
ibcInfo = ibc.find(
(i) =>
i.chain1.chainName === toChainName &&
i.chain2.chainName === fromChainName
);
flipped = true;
}
if (!ibcInfo) {
throw new Error('cannot find IBC info');
}
const key = flipped ? 'chain2' : 'chain1';
const sourcePort = ibcInfo.channels[0][key].portId;
const sourceChannel = ibcInfo.channels[0][key].channelId;
return { sourcePort, sourceChannel };
};
return {
isStarshipChain,
allAssets,
nativeAssets,
ibcAssets,
getAssetByDenom,
denomToSymbol,
symbolToDenom,
convRawToDispAmount,
calcCoinDollarValue,
getIbcAssetsLength,
getChainName,
getPrettyChainName,
isNativeAsset,
getNativeDenom,
getIbcInfo,
getExponentByDenom,
getDenomBySymbolAndChain,
};
};