-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathuseDollarValue.ts
More file actions
100 lines (87 loc) · 2.88 KB
/
Copy pathuseDollarValue.ts
File metadata and controls
100 lines (87 loc) · 2.88 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
import { useMemo } from 'react';
import { ChainId, ChainIds } from '@sovryn/ethers-provider';
import { toDisplayPrice } from '@sovryn/sdex';
import { Decimal } from '@sovryn/utils';
import { useTokenPrices } from '../contexts/TokenPricesContext';
import { COMMON_SYMBOLS } from '../utils/asset';
import { isRskChain } from '../utils/chain';
import { fromWei } from '../utils/math';
import { useCurrentChain } from './useChainStore';
import { useTokenDetailsByAsset } from './useTokenDetailsByAsset';
const STABLECOINS: Partial<Record<ChainIds, string[]>> = {
[ChainIds.MAINNET]: ['USDT', 'USDC', 'DAI'],
[ChainIds.BOB_MAINNET]: ['DLLR', 'USDT', 'USDC', 'DAI'],
[ChainIds.BOB_TESTNET]: ['DLLR', 'USDT', 'USDC', 'DAI'],
[ChainIds.RSK_MAINNET]: ['DLLR', 'XUSD', 'ZUSD', 'DOC', 'RUSDT'],
[ChainIds.RSK_TESTNET]: ['DLLR', 'XUSD', 'ZUSD', 'DOC', 'RUSDT'],
};
export function useDollarValue(
asset: string,
weiAmount: string,
chainId?: ChainId,
) {
const currentChainId = useCurrentChain();
const chain = chainId || currentChainId;
const { prices, loading: pricesLoading } = useTokenPrices();
const destination = useMemo(
() => (STABLECOINS[chain]?.[0] ?? COMMON_SYMBOLS.DLLR).toUpperCase(),
[chain],
);
const entry = useMemo(() => {
if (asset.toUpperCase() === COMMON_SYMBOLS.OSSOV) {
return COMMON_SYMBOLS.SOV;
}
if (isRskChain(chain)) {
if (asset.toUpperCase() === COMMON_SYMBOLS.ZUSD) {
return COMMON_SYMBOLS.XUSD;
} else if (asset.toLocaleLowerCase() === 'weth') {
return COMMON_SYMBOLS.ETH;
} else if (['wbtc', 'tbtc'].includes(asset.toLocaleLowerCase())) {
return COMMON_SYMBOLS.BTC;
} else if (asset.toLocaleLowerCase() === 'esov') {
return COMMON_SYMBOLS.SOV;
}
}
return asset.toUpperCase();
}, [asset, chain]);
const assetDetails = useTokenDetailsByAsset(entry, chain);
const destinationDetails = useTokenDetailsByAsset(destination, chain);
const usdPrice = useMemo(() => {
if (assetDetails && assetDetails.address) {
return prices[assetDetails.address.toLowerCase()] || '0';
}
return '0';
}, [prices, assetDetails]);
const usdValue = useMemo(() => {
if (
entry === destination ||
(STABLECOINS[chain]?.includes(entry) &&
STABLECOINS[chain]?.includes(destination))
) {
const amount = Number(fromWei(weiAmount, assetDetails?.decimals || 18));
return toDisplayPrice(
amount,
destinationDetails?.decimals || 18,
assetDetails?.decimals || 18,
).toString();
} else {
const amount = Decimal.fromBigNumberString(weiAmount)
.mul(usdPrice)
.toString();
return amount;
}
}, [
entry,
destination,
chain,
weiAmount,
assetDetails?.decimals,
destinationDetails?.decimals,
usdPrice,
]);
return {
loading: pricesLoading,
usdValue,
usdPrice,
};
}