Skip to content

Commit

Permalink
feat(suite-native): bnb to bsc migration
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive authored and tomasklim committed Dec 21, 2024
1 parent cc18737 commit b2e6662
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/connect-common/files/coins-eth.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"is_testnet": false,
"name": "Arbitrum One",
"shortcut": "ARB",
"label": "ETH",
"slip44": 9001,
"support": {
"T1B1": "1.9.4",
Expand Down
2 changes: 1 addition & 1 deletion suite-native/discovery/src/discoveryConfigSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type DiscoveryInfo = {
networkSymbols: NetworkSymbol[];
};

type DiscoveryConfigState = {
export type DiscoveryConfigState = {
areTestnetsEnabled: boolean;
discoveryInfo: DiscoveryInfo | null;
isCoinEnablingInitFinished: boolean;
Expand Down
49 changes: 46 additions & 3 deletions suite-native/state/src/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import {
devicePersistTransform,
walletStopPersistTransform,
migrateDeviceState,
migrateEnabledDiscoveryNetworkSymbols,
migrateAccountBnbToBsc,
migrateTransactionsBnbToBsc,
} from '@suite-native/storage';
import { prepareAnalyticsReducer } from '@suite-common/analytics';
import {
Expand All @@ -31,7 +34,11 @@ import {
} from '@suite-common/message-system';
import { notificationsReducer } from '@suite-common/toast-notifications';
import { graphReducer, graphPersistTransform } from '@suite-native/graph';
import { discoveryConfigPersistWhitelist, discoveryConfigReducer } from '@suite-native/discovery';
import {
discoveryConfigPersistWhitelist,
discoveryConfigReducer,
DiscoveryConfigState,
} from '@suite-native/discovery';
import { featureFlagsPersistedKeys, featureFlagsReducer } from '@suite-native/feature-flags';
import { prepareTokenDefinitionsReducer } from '@suite-common/token-definitions';
import { nativeFirmwareReducer } from '@suite-native/firmware';
Expand Down Expand Up @@ -139,7 +146,23 @@ export const prepareRootReducers = async () => {
reducer: discoveryConfigReducer,
persistedKeys: discoveryConfigPersistWhitelist,
key: 'discoveryConfig',
version: 1,
version: 2,
migrations: {
2: (oldState: DiscoveryConfigState) => {
if (!oldState.enabledDiscoveryNetworkSymbols) return oldState;

const { enabledDiscoveryNetworkSymbols } = oldState;
const migrateNetworkSymbols = migrateEnabledDiscoveryNetworkSymbols(
enabledDiscoveryNetworkSymbols,
);
const migratedState = {
...oldState,
enabledDiscoveryNetworkSymbols: migrateNetworkSymbols,
};

return migratedState;
},
},
});

const featureFlagsPersistedReducer = await preparePersistReducer({
Expand Down Expand Up @@ -180,7 +203,27 @@ export const prepareRootReducers = async () => {
transforms: [walletPersistTransform, graphPersistTransform],
mergeLevel: 2,
key: 'root',
version: 1,
version: 2,
migrations: {
2: (oldState: { wallet: { accounts: any; transactions: { transactions: any } } }) => {
const oldStateWallet = oldState.wallet;
const migratedAccounts = migrateAccountBnbToBsc(oldStateWallet.accounts);

const migratedTransactions = migrateTransactionsBnbToBsc(
oldStateWallet.transactions?.transactions,
);
const migratedState = {
...oldState,
wallet: {
...oldStateWallet,
accounts: migratedAccounts,
transactions: { transactions: migratedTransactions },
},
};

return migratedState;
},
},
});

return rootReducer;
Expand Down
3 changes: 3 additions & 0 deletions suite-native/storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export * from './atomWithUnecryptedStorage';
export * from './migrations/account/v2';
export * from './migrations/account/v3';
export * from './migrations/device/v2';
export * from './migrations/discovery/v2';
export * from './migrations/wallet/transactions/v2';
export * from './migrations/wallet/accounts/v2';

export * from './transforms/deviceTransforms';
export * from './transforms/walletTransforms';
Expand Down
10 changes: 10 additions & 0 deletions suite-native/storage/src/migrations/discovery/v2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NetworkSymbol } from '@suite-common/wallet-config';

type NetworkSymbolOld = Exclude<NetworkSymbol, 'bsc'> | 'bnb';

export const migrateEnabledDiscoveryNetworkSymbols = (
oldEnabledDiscoveryNetworkSymbols: NetworkSymbol[],
): NetworkSymbol[] =>
(oldEnabledDiscoveryNetworkSymbols as NetworkSymbolOld[]).map(networkSymbol =>
networkSymbol === 'bnb' ? 'bsc' : networkSymbol,
);
17 changes: 17 additions & 0 deletions suite-native/storage/src/migrations/wallet/accounts/v2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type Account = {
symbol: string;
key: string;
};

export const migrateAccountBnbToBsc = (oldAccounts: Account[] | undefined): Account[] | undefined =>
oldAccounts?.map(oldAccount => {
const { key, symbol } = oldAccount;

if (symbol !== 'bnb') return oldAccount;

return {
...oldAccount,
key: key.replace('-bnb-', '-bsc-'),
symbol: 'bsc',
};
});
25 changes: 25 additions & 0 deletions suite-native/storage/src/migrations/wallet/transactions/v2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type TransactionStub = {
symbol: string;
};

type AccountTransactionsType = {
[x: string]: TransactionStub[];
};

export const migrateTransactionsBnbToBsc = (
oldTransactions: AccountTransactionsType | undefined,
): AccountTransactionsType | undefined => {
const newTransactions: AccountTransactionsType = {};

for (const oldKey in oldTransactions) {
const oldTxns = oldTransactions[oldKey];

const newKey = oldKey.replace('-bnb-', '-bsc-');
newTransactions[newKey] = oldTxns.map(oldTxn => ({
...oldTxn,
symbol: oldTxn.symbol.replace('bnb', 'bsc'),
}));
}

return newTransactions;
};
15 changes: 15 additions & 0 deletions suite-native/storage/src/tests/migrations/discoveryV2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NetworkSymbol } from '@suite-common/wallet-config';

import { migrateEnabledDiscoveryNetworkSymbols } from '../../migrations/discovery/v2';

describe('migrateEnabledDiscoveryNetworkSymbols', () => {
it('should migrate old enabled discovery network symbols - change bnb to bsc', () => {
const oldEnabledDiscoveryNetworkSymbols = ['btc', 'eth', 'bnb', 'test'] as NetworkSymbol[];

const migratedAccounts = migrateEnabledDiscoveryNetworkSymbols(
oldEnabledDiscoveryNetworkSymbols,
);

expect(migratedAccounts).toEqual(['btc', 'eth', 'bsc', 'test']);
});
});

0 comments on commit b2e6662

Please sign in to comment.