Skip to content

Commit ee02425

Browse files
committed
feat(suite-native): only download token definitions for relevant networks
1 parent 47555aa commit ee02425

File tree

10 files changed

+52
-9
lines changed

10 files changed

+52
-9
lines changed

suite-native/discovery/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@suite-native/device": "workspace:*",
2727
"@suite-native/device-mutex": "workspace:*",
2828
"@suite-native/feature-flags": "workspace:*",
29+
"@suite-native/tokens": "workspace:*",
2930
"@trezor/connect": "workspace:*",
3031
"@trezor/utils": "workspace:*",
3132
"proxy-memoize": "2.0.2",

suite-native/discovery/src/discoveryConfigSlice.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
2-
import { pipe } from '@mobily/ts-belt';
2+
import { A, F, pipe } from '@mobily/ts-belt';
33
import { memoize, memoizeWithArgs } from 'proxy-memoize';
44

55
import {
@@ -21,6 +21,10 @@ import {
2121
FeatureFlagsRootState,
2222
selectIsFeatureFlagEnabled,
2323
} from '@suite-native/feature-flags';
24+
import {
25+
selectNetworkSymbolsOfAccountsWithTokensAllowed,
26+
TokensRootState,
27+
} from '@suite-native/tokens';
2428

2529
type DiscoveryInfo = {
2630
startTimestamp: number;
@@ -193,6 +197,15 @@ export const selectDeviceEnabledDiscoveryNetworkSymbols = memoizeWithArgs(
193197
{ size: 2 },
194198
);
195199

200+
export const selectTokenDefinitionsEnabledNetworks = memoize(
201+
(state: TokensRootState & DiscoveryConfigSliceRootState) => {
202+
const enabledNetworkSymbols = selectEnabledDiscoveryNetworkSymbols(state);
203+
const accountNetworkSymbols = selectNetworkSymbolsOfAccountsWithTokensAllowed(state);
204+
205+
return F.toMutable(A.uniq([...enabledNetworkSymbols, ...accountNetworkSymbols]));
206+
},
207+
);
208+
196209
export const {
197210
toggleAreTestnetsEnabled,
198211
setDiscoveryInfo,

suite-native/discovery/src/discoveryMiddleware.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createMiddlewareWithExtraDeps } from '@suite-common/redux-utils';
2+
import { periodicCheckTokenDefinitionsThunk } from '@suite-common/token-definitions';
13
import {
24
deviceActions,
35
discoveryActions,
@@ -6,14 +8,15 @@ import {
68
authorizeDeviceThunk,
79
accountsActions,
810
} from '@suite-common/wallet-core';
9-
import { createMiddlewareWithExtraDeps } from '@suite-common/redux-utils';
1011
import { isFirmwareVersionSupported } from '@suite-native/device';
1112

1213
import { startDescriptorPreloadedDiscoveryThunk, discoveryCheckThunk } from './discoveryThunks';
1314
import {
1415
selectAreTestnetsEnabled,
1516
selectIsCoinEnablingInitFinished,
1617
toggleAreTestnetsEnabled,
18+
setEnabledDiscoveryNetworkSymbols,
19+
toggleEnabledDiscoveryNetworkSymbol,
1720
} from './discoveryConfigSlice';
1821

1922
export const prepareDiscoveryMiddleware = createMiddlewareWithExtraDeps(
@@ -75,6 +78,15 @@ export const prepareDiscoveryMiddleware = createMiddlewareWithExtraDeps(
7578
dispatch(discoveryCheckThunk());
7679
}
7780

81+
// if we changed enabled networks, check for token definitions right away
82+
if (
83+
(toggleEnabledDiscoveryNetworkSymbol.match(action) ||
84+
setEnabledDiscoveryNetworkSymbols.match(action)) &&
85+
isCoinEnablingInitFinished
86+
) {
87+
dispatch(periodicCheckTokenDefinitionsThunk());
88+
}
89+
7890
return action;
7991
},
8092
);

suite-native/discovery/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
{ "path": "../device" },
3030
{ "path": "../device-mutex" },
3131
{ "path": "../feature-flags" },
32+
{ "path": "../tokens" },
3233
{ "path": "../../packages/connect" },
3334
{ "path": "../../packages/utils" }
3435
]

suite-native/module-accounts-import/src/accountsImportThunks.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import { getXpubOrDescriptorInfo } from '@trezor/utxo-lib';
1111
import { getAccountIdentity, shouldUseIdentities } from '@suite-common/wallet-utils';
1212
import { Timestamp, TokenAddress } from '@suite-common/wallet-types';
1313
import { FiatCurrencyCode } from '@suite-common/suite-config';
14-
import { selectFilterKnownTokens } from '@suite-common/token-definitions';
14+
import {
15+
periodicCheckTokenDefinitionsThunk,
16+
selectFilterKnownTokens,
17+
} from '@suite-common/token-definitions';
1518

1619
import { paymentTypeToAccountType } from './constants';
1720

@@ -71,6 +74,7 @@ export const importAccountThunk = createThunk(
7174
}),
7275
);
7376
}
77+
dispatch(periodicCheckTokenDefinitionsThunk());
7478
},
7579
);
7680

suite-native/state/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"@suite-native/module-send": "workspace:*",
3232
"@suite-native/settings": "workspace:*",
3333
"@suite-native/storage": "workspace:*",
34-
"@suite-native/tokens": "workspace:*",
3534
"@trezor/transport-native": "workspace:*",
3635
"@trezor/utils": "workspace:*",
3736
"expo-device": "6.0.2",

suite-native/state/src/extraDependencies.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import {
1313
} from '@suite-native/settings';
1414
import { mergeDeepObject } from '@trezor/utils';
1515
import { NativeUsbTransport } from '@trezor/transport-native';
16-
import { selectEnabledDiscoveryNetworkSymbols } from '@suite-native/discovery';
17-
import { NETWORK_SYMBOLS_WITH_TOKENS } from '@suite-native/tokens';
16+
import {
17+
selectEnabledDiscoveryNetworkSymbols,
18+
selectTokenDefinitionsEnabledNetworks,
19+
} from '@suite-native/discovery';
1820

1921
const deviceType = Device.isDevice ? 'device' : 'emulator';
2022

@@ -34,7 +36,7 @@ export const extraDependencies: ExtraDependencies = mergeDeepObject(extraDepende
3436
// otherwise disableAccountsThunk might erase accounts not supported by current device
3537
selectEnabledNetworks: selectEnabledDiscoveryNetworkSymbols,
3638
// todo: this is temporary solution to make token definitions work on native in portfolio tracker
37-
selectTokenDefinitionsEnabledNetworks: () => NETWORK_SYMBOLS_WITH_TOKENS,
39+
selectTokenDefinitionsEnabledNetworks,
3840
selectBitcoinAmountUnit: selectBitcoinUnits,
3941
selectAreSatsAmountUnit,
4042
selectLocalCurrency: selectFiatCurrencyCode,

suite-native/state/tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
{ "path": "../module-send" },
3535
{ "path": "../settings" },
3636
{ "path": "../storage" },
37-
{ "path": "../tokens" },
3837
{
3938
"path": "../../packages/transport-native"
4039
},

suite-native/tokens/src/tokensSelectors.ts

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
AccountsRootState,
1111
DeviceRootState,
1212
selectAccountByKey,
13+
selectAccounts,
1314
selectAccountTransactions,
1415
selectVisibleDeviceAccountsByNetworkSymbol,
1516
TransactionsRootState,
@@ -275,3 +276,14 @@ export const selectAccountHasAnyKnownToken = (state: TokensRootState, accountKey
275276

276277
return anyOfTokensIsKnown;
277278
};
279+
280+
export const selectNetworkSymbolsOfAccountsWithTokensAllowed = (state: TokensRootState) =>
281+
selectAccounts(state)
282+
.filter(a => isCoinWithTokens(a.symbol))
283+
.reduce((acc, account) => {
284+
if (!acc.includes(account.symbol)) {
285+
acc.push(account.symbol);
286+
}
287+
288+
return acc;
289+
}, new Array<NetworkSymbol>());

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -9734,6 +9734,7 @@ __metadata:
97349734
"@suite-native/device": "workspace:*"
97359735
"@suite-native/device-mutex": "workspace:*"
97369736
"@suite-native/feature-flags": "workspace:*"
9737+
"@suite-native/tokens": "workspace:*"
97379738
"@trezor/connect": "workspace:*"
97389739
"@trezor/utils": "workspace:*"
97399740
proxy-memoize: "npm:2.0.2"
@@ -10507,7 +10508,6 @@ __metadata:
1050710508
"@suite-native/module-send": "workspace:*"
1050810509
"@suite-native/settings": "workspace:*"
1050910510
"@suite-native/storage": "workspace:*"
10510-
"@suite-native/tokens": "workspace:*"
1051110511
"@trezor/transport-native": "workspace:*"
1051210512
"@trezor/utils": "workspace:*"
1051310513
expo-device: "npm:6.0.2"

0 commit comments

Comments
 (0)