-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathassetsSelectors.ts
167 lines (136 loc) · 5.62 KB
/
assetsSelectors.ts
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
import { A, D, F, G, pipe } from '@mobily/ts-belt';
import { memoize, memoizeWithArgs } from 'proxy-memoize';
import { calculateAssetsPercentage } from '@suite-common/assets';
import { TokenDefinitionsRootState } from '@suite-common/token-definitions';
import { networks, NetworkSymbol } from '@suite-common/wallet-config';
import {
AccountsRootState,
DeviceRootState,
FiatRatesRootState,
selectCurrentFiatRates,
selectDeviceAccounts,
selectVisibleDeviceAccounts,
selectVisibleDeviceAccountsByNetworkSymbol,
} from '@suite-common/wallet-core';
import { getAccountFiatBalance } from '@suite-common/wallet-utils';
import { selectAccountListSections } from '@suite-native/accounts';
import { sortAccountsByNetworksAndAccountTypes } from '@suite-native/accounts/src/utils';
import { orderedNetworkSymbols } from '@suite-native/config';
import { selectFiatCurrencyCode, SettingsSliceRootState } from '@suite-native/settings';
import {
NativeStakingRootState,
selectAccountCryptoBalanceWithStaking,
} from '@suite-native/staking';
export interface AssetType {
symbol: NetworkSymbol;
assetBalance: string;
fiatBalance: string | null;
}
export type AssetsRootState = AccountsRootState &
FiatRatesRootState &
SettingsSliceRootState &
TokenDefinitionsRootState &
NativeStakingRootState &
DeviceRootState;
/*
We do not memoize any of following selectors because they are using only with `useSelectorDeepComparison` hook which is faster than memoization in proxy-memoize.
*/
export const selectVisibleDeviceAccountsKeysByNetworkSymbol = (
state: AssetsRootState,
networkSymbol: NetworkSymbol | null,
) => {
if (G.isNull(networkSymbol)) return [];
const accounts = selectDeviceAccounts(state).filter(
account => account.symbol === networkSymbol && account.visible,
);
return accounts.map(account => account.key);
};
export const selectDeviceNetworksWithAssets = (state: AssetsRootState) => {
const accounts = selectVisibleDeviceAccounts(state);
return pipe(
accounts,
A.map(account => account.symbol),
A.uniq,
A.sort((a, b) => {
const aOrder = orderedNetworkSymbols.indexOf(a) ?? Number.MAX_SAFE_INTEGER;
const bOrder = orderedNetworkSymbols.indexOf(b) ?? Number.MAX_SAFE_INTEGER;
return aOrder - bOrder;
}),
);
};
export const selectBottomSheetDeviceNetworkItems = memoizeWithArgs(
(state: AssetsRootState, networkSymbol: NetworkSymbol) =>
pipe(
selectVisibleDeviceAccountsByNetworkSymbol(state, networkSymbol),
sortAccountsByNetworksAndAccountTypes,
A.map(account => selectAccountListSections(state, account.key)),
A.flat,
F.toMutable,
),
{ size: D.keys(networks).length },
);
const selectDeviceAssetsWithBalances = memoize((state: AssetsRootState) => {
const accounts = selectVisibleDeviceAccounts(state);
const deviceNetworksWithAssets = selectDeviceNetworksWithAssets(state);
const fiatCurrencyCode = selectFiatCurrencyCode(state);
const rates = selectCurrentFiatRates(state);
const accountsWithFiatBalance = accounts.map(account => {
const fiatValue = getAccountFiatBalance({
account,
localCurrency: fiatCurrencyCode,
rates,
});
return {
symbol: account.symbol,
fiatValue,
cryptoValue: selectAccountCryptoBalanceWithStaking(state, account.key),
};
});
let totalFiatBalance = 0;
const assets = deviceNetworksWithAssets.map((networkSymbol: NetworkSymbol) => {
const networkAccounts = accountsWithFiatBalance.filter(
account => account.symbol === networkSymbol,
);
const assetBalance = networkAccounts.reduce(
(sum, { cryptoValue }) => sum + Number(cryptoValue),
0,
);
const fiatBalance = networkAccounts.reduce(
(sum, { fiatValue }) => (fiatValue ? Number(fiatValue) + (sum ?? 0) : sum),
null as number | null,
);
totalFiatBalance += fiatBalance ?? 0;
const asset: AssetType = {
symbol: networkSymbol,
// For assets we should always only 8 decimals to save space
assetBalance: assetBalance.toFixed(8),
fiatBalance: fiatBalance !== null ? fiatBalance.toFixed(2) : null,
};
return asset;
});
return { assets, totalFiatBalance: totalFiatBalance.toFixed(2) };
});
export const selectAssetCryptoValue = (state: AssetsRootState, symbol: NetworkSymbol) => {
const assets = selectDeviceAssetsWithBalances(state);
const asset = assets.assets.find(a => a.symbol === symbol);
return asset?.assetBalance ?? '0';
};
export const selectAssetFiatValue = (state: AssetsRootState, symbol: NetworkSymbol) => {
const assets = selectDeviceAssetsWithBalances(state);
const asset = assets.assets.find(a => a.symbol === symbol);
return asset?.fiatBalance ?? null;
};
const selectAssetsFiatValuePercentage = (state: AssetsRootState) => {
const assets = selectDeviceAssetsWithBalances(state);
const percentages = calculateAssetsPercentage(assets.assets);
return percentages;
};
export const selectAssetFiatValuePercentage = (state: AssetsRootState, symbol: NetworkSymbol) => {
const assetsPercentages = selectAssetsFiatValuePercentage(state);
const asset = assetsPercentages.find(a => a.symbol === symbol);
const assetPercentage = {
fiatPercentage: Math.ceil(asset?.fiatPercentage ?? 0),
fiatPercentageOffset: Math.floor(asset?.fiatPercentageOffset ?? 0),
};
return assetPercentage;
};