Skip to content

Commit

Permalink
feat: add a store migration to use xverse btc api url as default (#830)
Browse files Browse the repository at this point in the history
* feat: add a store migration to use xverse btc api url as default

* style: comments

* chore: fix for knip

* chore: add a knip pre push hook

* chore: prepush

* fix: be more defensive
  • Loading branch information
teebszet authored Dec 17, 2024
1 parent c694e05 commit 9c5cb73
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run knip
40 changes: 36 additions & 4 deletions src/app/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-underscore-dangle */
import { defaultMainnet, type SettingsNetwork } from '@secretkeylabs/xverse-core';
import { markAlertsForShow } from '@utils/alertTracker';
import chromeStorage from '@utils/chromeStorage';
import { applyMiddleware, combineReducers, createStore } from 'redux';
Expand All @@ -15,6 +16,7 @@ import type {
WalletStateV4,
WalletStateV5,
WalletStateV6,
WalletStateV7,
} from './wallet/actions/migrationTypes';
import { type AvatarInfo, type WalletState } from './wallet/actions/types';
import walletReducer, { initialWalletState, rehydrateError } from './wallet/reducer';
Expand Down Expand Up @@ -154,11 +156,41 @@ const migrations = {
const { allowNestedSegWitAddress, ...migratedState } = state;
return migratedState as WalletState;
},
7: (state: WalletStateV6): WalletState => ({
7: (
// NOTE: because we forgot to bump the store version on L222, need to be defensive here
// and only initialise if not defined
state: WalletStateV6 & { showBalanceInBtc: boolean; hasBackedUpWallet: boolean },
): WalletStateV7 => ({
...state,
showBalanceInBtc: false,
hasBackedUpWallet: true,
showBalanceInBtc: state.showBalanceInBtc ?? false,
hasBackedUpWallet: state.hasBackedUpWallet ?? true,
}),
8: (state: WalletStateV7): WalletState => {
const migrateMainnetNetwork = (currentNetwork: SettingsNetwork) => ({
...currentNetwork,
btcApiUrl:
currentNetwork.btcApiUrl === 'https://mempool.space/api'
? defaultMainnet.btcApiUrl
: currentNetwork.btcApiUrl,
fallbackBtcApiUrl:
!currentNetwork.fallbackBtcApiUrl ||
currentNetwork.fallbackBtcApiUrl === 'https://btc-1.xverse.app'
? defaultMainnet.fallbackBtcApiUrl
: currentNetwork.fallbackBtcApiUrl,
});
return {
...state,
savedNetworks:
state.savedNetworks?.map((network: SettingsNetwork) =>
network.type === 'Mainnet' ? migrateMainnetNetwork(network) : network,
) ?? initialWalletState.savedNetworks,
network:
(state.network?.type === 'Mainnet'
? migrateMainnetNetwork(state.network)
: state.network) ?? initialWalletState.network,
};
},

/* *
* When adding a new migration, add the new wallet state type to the migrationTypes file
* and add the migration here.
Expand Down Expand Up @@ -188,7 +220,7 @@ const migrations = {
};

const WalletPersistConfig: PersistConfig<WalletState> = {
version: 6,
version: 8,
key: 'walletState',
storage: chromeStorage.local,
migrate: createMigrate(migrations as any, { debug: false }),
Expand Down
6 changes: 4 additions & 2 deletions src/app/stores/wallet/actions/migrationTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ export type WalletStateV5 = Omit<

export type WalletStateV6 = Omit<WalletStateV5, 'allowNestedSegWitAddress'>;

type WalletStateV7 = WalletStateV6 & {
// should be exported and used when we add next migration
export type WalletStateV7 = WalletStateV6 & {
showBalanceInBtc: boolean;
hasBackedUpWallet: boolean;
};

// should be exported and used when we add next migration
type WalletStateV8 = WalletStateV7; // no changes. just a data migration

0 comments on commit 9c5cb73

Please sign in to comment.