-
Notifications
You must be signed in to change notification settings - Fork 354
/
Copy pathaccounts.ts
528 lines (467 loc) · 17.6 KB
/
accounts.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import { handleActions, ReducerMap } from "redux-actions";
import type { Action } from "redux-actions";
import { createSelector, createSelectorCreator, defaultMemoize, OutputSelector } from "reselect";
import uniq from "lodash/uniq";
import {
Account,
AccountLike,
AccountLikeArray,
AccountRaw,
SubAccount,
} from "@ledgerhq/types-live";
import type {
CryptoCurrency,
CryptoOrTokenCurrency,
TokenCurrency,
} from "@ledgerhq/types-cryptoassets";
import isEqual from "lodash/isEqual";
import {
isAccountEmpty,
flattenAccounts,
getAccountCurrency,
isUpToDateAccount,
clearAccount,
makeEmptyTokenAccount,
isAccountBalanceUnconfirmed,
} from "@ledgerhq/live-common/account/index";
import { decodeNftId } from "@ledgerhq/coin-framework/nft/nftId";
import { orderByLastReceived } from "@ledgerhq/live-nft";
import type { AccountsState, State } from "./types";
import type {
AccountsDeleteAccountPayload,
AccountsImportAccountsPayload,
AccountsPayload,
AccountsReorderPayload,
AccountsUpdateAccountWithUpdaterPayload,
SettingsBlacklistTokenPayload,
DangerouslyOverrideStatePayload,
AccountsReplacePayload,
} from "../actions/types";
import { AccountsActionTypes } from "../actions/types";
import accountModel from "../logic/accountModel";
import { blacklistedTokenIdsSelector, nftCollectionsStatusByNetworkSelector } from "./settings";
import { galleryChainFiltersSelector } from "./nft";
import {
accountNameWithDefaultSelector,
accountUserDataExportSelector,
HandlersPayloads,
WalletHandlerType,
} from "@ledgerhq/live-wallet/store";
import { importAccountsReduce } from "@ledgerhq/live-wallet/liveqr/importAccounts";
import { walletSelector } from "./wallet";
import { nestedSortAccounts } from "@ledgerhq/live-wallet/ordering";
import { AddAccountsAction } from "@ledgerhq/live-wallet/addAccounts";
import { NftStatus } from "@ledgerhq/live-nft/types";
import { nftCollectionParser } from "~/hooks/nfts/useNftCollectionsStatus";
export const INITIAL_STATE: AccountsState = {
active: [],
};
const handlers: ReducerMap<AccountsState, Payload> = {
[WalletHandlerType.INIT_ACCOUNTS]: (_, action) => ({
active: (action.payload as HandlersPayloads["INIT_ACCOUNTS"]).accounts,
}),
[AccountsActionTypes.ACCOUNTS_USER_IMPORT]: (s, action) => ({
active: importAccountsReduce(
s.active,
(action as Action<AccountsImportAccountsPayload>).payload,
),
}),
[AccountsActionTypes.ADD_ACCOUNT]: (state, action) => {
const account = (action as Action<Account>).payload;
if (state.active.some(a => a.id === account.id)) return state;
return {
active: [...state.active, account],
};
},
[AccountsActionTypes.REORDER_ACCOUNTS]: (state, action) => ({
active: nestedSortAccounts(state.active, (action as Action<AccountsReorderPayload>).payload),
}),
[WalletHandlerType.ADD_ACCOUNTS]: (s, action) => {
const { payload } = action as AddAccountsAction;
return {
active: payload.allAccounts,
};
},
[AccountsActionTypes.UPDATE_ACCOUNT]: (state, action) => {
const {
payload: { accountId, updater },
} = action as Action<AccountsUpdateAccountWithUpdaterPayload>;
function update(existingAccount: Account) {
if (accountId !== existingAccount.id) return existingAccount;
return { ...existingAccount, ...updater(existingAccount) };
}
return {
active: state.active.map(update),
};
},
[AccountsActionTypes.DELETE_ACCOUNT]: (state, action) => ({
active: state.active.filter(
acc => acc.id !== (action as Action<AccountsDeleteAccountPayload>).payload.id,
),
}),
[AccountsActionTypes.SET_ACCOUNTS]: (state, action) => ({
active: (action as Action<AccountsReplacePayload>).payload,
}),
[AccountsActionTypes.CLEAN_CACHE]: (state: AccountsState) => ({
active: state.active.map(clearAccount),
}),
[AccountsActionTypes.DANGEROUSLY_OVERRIDE_STATE]: (
state: AccountsState,
action,
): AccountsState => ({
...state,
...(action as Action<DangerouslyOverrideStatePayload>).payload.accounts,
}),
};
// Selectors
export function exportSelector(state: State): {
active: {
data: AccountRaw;
version: number;
}[];
} {
const active = [];
for (const account of state.accounts.active) {
const accountUserData = accountUserDataExportSelector(state.wallet, { account });
if (accountUserData) {
active.push(accountModel.encode([account, accountUserData]));
}
}
return { active };
}
/**
* Warning: use this selector directly in `useSelector` only if you really need
* the full data of all the accounts in your hook or component.
* For many needs, other more accurate and memoized selectors (see below) are
* available.
* Using these selectors will prevent unnecessary updates (causing a re-render)
* of your hook or component every time something somewhere in one of the
* accounts changes.
* This is important because the `accounts` array is modified at the end almost
* every account sync, so potentially you could avoid many unnessary and
* expensive re-renders.
*/
export const accountsSelector = (s: State): Account[] => s.accounts.active;
// NB some components don't need to refresh every time an account is updated, usually it's only
// when the balance/name/length/starred/swapHistory of accounts changes.
const accountHash = (a: AccountLike) =>
`${a.id}-${a.balance.toString()}-swapHistory(${a.swapHistory.length})`;
// TODO can we share with desktop in common?
const shallowAccountsSelectorCreator = createSelectorCreator(defaultMemoize, (a, b): boolean =>
isEqual(
flattenAccounts(a as AccountLikeArray).map(accountHash),
flattenAccounts(b as AccountLikeArray).map(accountHash),
),
);
export const shallowAccountsSelector = shallowAccountsSelectorCreator(accountsSelector, a => a);
export const flattenAccountsSelector = createSelector(accountsSelector, flattenAccounts);
export const flattenAccountsEnforceHideEmptyTokenSelector = createSelector(
accountsSelector,
accounts =>
flattenAccounts(accounts, {
enforceHideEmptySubAccounts: true,
}),
);
export const accountsCountSelector = createSelector(accountsSelector, acc => acc.length);
/** Returns a boolean that is true if and only if there is no account */
export const hasNoAccountsSelector = createSelector(accountsSelector, acc => acc.length <= 0);
/** Returns a boolean that is true if and only if all accounts are empty */
export const areAccountsEmptySelector = createSelector(accountsSelector, accounts =>
accounts.every(isAccountEmpty),
);
export const currenciesSelector = createSelector(accountsSelector, accounts =>
uniq(flattenAccounts(accounts).map(a => getAccountCurrency(a))).sort((a, b) =>
a.name.localeCompare(b.name),
),
);
export const cryptoCurrenciesSelector = createSelector(accountsSelector, accounts =>
uniq(accounts.map(a => a.currency)).sort((a, b) => a.name.localeCompare(b.name)),
);
export const accountsTuplesByCurrencySelector = createSelector(
walletSelector,
accountsSelector,
(_: State, currency: CryptoCurrency | TokenCurrency) => currency,
(_: State, currency: CryptoCurrency | TokenCurrency, accountIds?: Map<string, boolean>) =>
accountIds,
(
wallet,
accounts,
currency,
accountIds,
): { account: AccountLike; subAccount: SubAccount | null; name: string }[] => {
if (currency.type === "TokenCurrency") {
return accounts
.filter(account => {
// not checking subAccounts against accountIds for TokenCurrency
// because the wallet-api is not able to setup empty accounts
// for all parentAccounts and currencies we support
// and we would lose the empty token accounts in the select account
return account.currency.id === currency.parentCurrency.id;
})
.map(account => ({
name: accountNameWithDefaultSelector(wallet, account),
account,
subAccount:
(account.subAccounts &&
account.subAccounts.find(
(subAcc: SubAccount) =>
subAcc.type === "TokenAccount" && subAcc.token.id === currency.id,
)) ||
makeEmptyTokenAccount(account, currency),
}));
}
return accounts
.filter(
account =>
account.currency.id === currency.id && (accountIds ? accountIds.has(account.id) : true),
)
.map(account => ({
name: accountNameWithDefaultSelector(wallet, account),
account,
subAccount: null,
}));
},
);
export const flattenAccountsByCryptoCurrencySelector = createSelector(
flattenAccountsSelector,
(_: State, currency: string) => currency,
(accounts, currency): AccountLike[] => {
return currency
? accounts.filter(a => currency === (a.type === "TokenAccount" ? a.token.id : a.currency.id))
: accounts;
},
);
const emptyTuples: ReturnType<typeof accountsTuplesByCurrencySelector> = [];
export const accountsByCryptoCurrencyScreenSelector =
(currency: CryptoOrTokenCurrency, accountIds?: Map<string, boolean>) => (state: State) => {
// TODO look if we can remove this check as the types should already protect here
if (!currency) return emptyTuples;
return accountsTuplesByCurrencySelector(state, currency, accountIds);
};
const emptyArray: AccountLike[] = [];
export const flattenAccountsByCryptoCurrencyScreenSelector =
(currency?: CryptoCurrency | TokenCurrency) => (state: State) => {
if (!currency) return emptyArray;
return flattenAccountsByCryptoCurrencySelector(state, currency.id);
};
export const accountSelector = createSelector(
accountsSelector,
(
_: State,
{ accountId, parentAccount }: { accountId?: string | null; parentAccount?: Account },
) => [accountId, parentAccount] as const,
(accounts, [accountId, parentAccount]) => accounts.find(a => a.id === accountId) || parentAccount,
);
export const parentAccountSelector = createSelector(
accountsSelector,
(_: State, { account }: { account?: AccountLike }) =>
account && account.type !== "Account" ? account.parentId : null,
(accounts, accountId) => accounts.find(a => a.id === accountId),
);
export const accountScreenSelector =
(route?: {
params?: {
account?: AccountLike;
accountId?: string | null;
parentId?: string | null;
};
}) =>
(state: State) => {
const { accountId, parentId } = route?.params || {};
const parentAccount: Account | null | undefined = parentId
? accountSelector(state, {
accountId: parentId,
})
: null;
let account = route?.params?.account;
if (accountId === parentId && parentAccount) {
account = parentAccount;
}
if (!account) {
if (parentAccount) {
const { subAccounts } = parentAccount;
if (subAccounts) {
account = subAccounts.find(t => t.id === accountId);
}
} else {
account = accountSelector(state, {
accountId,
});
}
}
return {
parentAccount,
account,
};
};
export const isUpToDateSelector = createSelector(accountsSelector, accounts =>
accounts.every(isUpToDateAccount),
);
export const subAccountByCurrencyOrderedSelector = createSelector(
accountsSelector,
(_: State, currency: CryptoCurrency | TokenCurrency) => currency,
(accounts, currency) => {
const flatAccounts = flattenAccounts(accounts);
return flatAccounts
.filter(
(account: AccountLike) =>
(account.type === "TokenAccount" ? account.token.id : account.currency.id) ===
currency.id,
)
.map((account: AccountLike) => ({
account,
parentAccount:
account.type === "TokenAccount" && account.parentId
? accounts.find(fa => fa.type === "Account" && fa.id === account.parentId)
: {},
}))
.sort((a: { account: AccountLike }, b: { account: AccountLike }) =>
a.account.balance.gt(b.account.balance)
? -1
: a.account.balance.eq(b.account.balance)
? 0
: 1,
);
},
);
export const subAccountByCurrencyOrderedScreenSelector =
(route: { params?: { currency?: CryptoOrTokenCurrency } }) => (state: State) => {
const currency = route?.params?.currency;
if (!currency) return [];
return subAccountByCurrencyOrderedSelector(state, currency);
};
function accountHasPositiveBalance(account: AccountLike) {
return Boolean(account.balance?.gt(0));
}
export const accountsWithPositiveBalanceCountSelector = createSelector(
accountsSelector,
acc => acc.filter(accountHasPositiveBalance).length,
);
/**
* Selector creators
*/
type AccountsLikeSelector = OutputSelector<
State,
AccountLike[],
(res: Account[], res2: string[]) => AccountLike[]
>;
function makeAccountsCountSelectors(accountsSelector: AccountsLikeSelector) {
return createSelector(accountsSelector, accounts => accounts.length);
}
function makeHasAccountsSelectors(accountsSelector: AccountsLikeSelector) {
return createSelector(accountsSelector, accounts => accounts.length > 0);
}
function makeAccountsWithPositiveBalanceCountSelector(accountsSelector: AccountsLikeSelector) {
return createSelector(
accountsSelector,
accounts => accounts.filter(accountHasPositiveBalance).length,
);
}
function makeHasAccountsWithPositiveBalanceSelector(accountsSelector: AccountsLikeSelector) {
return createSelector(
accountsSelector,
accounts => accounts.filter(accountHasPositiveBalance).length > 0,
);
}
/**
* TOKEN-ACCOUNTS SELECTORS
*/
export const tokenAccountsSelector = createSelector(accountsSelector, accounts =>
flattenAccounts(accounts).filter(acc => acc.type === "TokenAccount"),
);
export const tokenAccountsNotBlacklistedSelector = createSelector(
tokenAccountsSelector,
blacklistedTokenIdsSelector,
(accounts, blacklistedIds) =>
accounts.filter(acc => !blacklistedIds.includes(getAccountCurrency(acc).id)),
);
export const tokenAccountsCountSelector = makeAccountsCountSelectors(tokenAccountsSelector);
export const tokenAccountsNotBlacklistedCountSelector = makeAccountsCountSelectors(
tokenAccountsNotBlacklistedSelector,
);
export const hasTokenAccountsSelector = makeHasAccountsSelectors(tokenAccountsSelector);
export const hasTokenAccountsNotBlacklistedSelector = makeHasAccountsSelectors(
tokenAccountsNotBlacklistedSelector,
);
export const tokenAccountsWithPositiveBalanceCountSelector =
makeAccountsWithPositiveBalanceCountSelector(tokenAccountsSelector);
export const tokenAccountsNotBlackListedWithPositiveBalanceCountSelector =
makeAccountsWithPositiveBalanceCountSelector(tokenAccountsNotBlacklistedSelector);
export const hasTokenAccountsWithPositiveBalanceSelector =
makeHasAccountsWithPositiveBalanceSelector(tokenAccountsSelector);
export const hasTokenAccountsNotBlackListedWithPositiveBalanceSelector =
makeHasAccountsWithPositiveBalanceSelector(tokenAccountsNotBlacklistedSelector);
/**
* NON-TOKEN-ACCOUNTS SELECTORS
*/
export const nonTokenAccountsSelector = createSelector(accountsSelector, accounts =>
flattenAccounts(accounts).filter(acc => acc.type !== "TokenAccount"),
);
export const nonTokenAccountsCountSelector = makeAccountsCountSelectors(nonTokenAccountsSelector);
export const hasNonTokenAccountsSelector = makeHasAccountsSelectors(nonTokenAccountsSelector);
export const nonTokenAccountsWithPositiveBalanceCountSelector =
makeAccountsWithPositiveBalanceCountSelector(nonTokenAccountsSelector);
export const hasNonTokenAccountsWithPositiveBalanceSelector =
makeHasAccountsWithPositiveBalanceSelector(nonTokenAccountsSelector);
export const nftsSelector = createSelector(accountsSelector, accounts =>
accounts.map(a => a.nfts ?? []).flat(),
);
export const orderedNftsSelector = createSelector(
accountsSelector,
nftsSelector,
(accounts, nfts) => orderByLastReceived(accounts, nfts),
);
/**
* Returns the list of all the NFTs from non hidden collections accross all
* accounts, ordered by last received.
*
* /!\ Use this with a deep equal comparison if possible as it will always
* return a new array if `accounts` or `hiddenNftCollections` changes.
*
* Example:
* ```
* import isEqual from "lodash/isEqual";
* // ...
* const orderedVisibleNfts = useSelector(orderedVisibleNftsSelector, isEqual)
* ```
* */
export const orderedVisibleNftsSelector = createSelector(
accountsSelector,
nftCollectionsStatusByNetworkSelector,
(_: State, hideSpam: boolean) => hideSpam,
(accounts, nftCollectionsStatusByNetwork, hideSpam) => {
const nfts = accounts.map(a => a.nfts ?? []).flat();
const hiddenNftCollections = nftCollectionParser(
nftCollectionsStatusByNetwork,
([_, status]) =>
hideSpam ? status !== NftStatus.whitelisted : status === NftStatus.blacklisted,
);
const visibleNfts = nfts.filter(
nft => !hiddenNftCollections.includes(`${decodeNftId(nft.id).accountId}|${nft.contract}`),
);
return orderByLastReceived(accounts, visibleNfts);
},
);
export const hasNftsSelector = createSelector(nftsSelector, nfts => {
return !!nfts.length;
});
export const filteredNftsSelector = createSelector(
galleryChainFiltersSelector,
orderedVisibleNftsSelector,
(galleryFilters, orderedNfts) => {
const activeFilters = Object.entries(galleryFilters)
.filter(([_, value]) => value)
.map(([key, _]) => key);
return orderedNfts.filter(nft => activeFilters.includes(nft.currencyId));
},
);
/**
* Returns a boolean that is true if and only if some of the accounts have an
* unconfirmed balance
*/
export const areSomeAccountsBalanceUnconfirmedSelector = createSelector(
accountsSelector,
accounts => accounts.some(isAccountBalanceUnconfirmed),
);
type Payload = AccountsPayload | SettingsBlacklistTokenPayload | AddAccountsAction["payload"];
export default handleActions<AccountsState, Payload>(handlers, INITIAL_STATE);