Skip to content

Commit

Permalink
US-1574 Mainnet!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Freshenext committed Sep 13, 2023
1 parent ae203be commit 4a99f45
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface GlobalErrorHandlerProviderType {
GlobalErrorHandlerViewComp?: FC
}

const GlobalErrorHandlerContext = createContext<GlobalErrorHandlerType>({
export const GlobalErrorHandlerContext = createContext<GlobalErrorHandlerType>({
setGlobalError: () => {},
globalError: null,
handleReload: () => {},
Expand Down
20 changes: 12 additions & 8 deletions src/core/CoreWithStore.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { Provider } from 'react-redux'
import { PersistGate } from 'redux-persist/integration/react'

import { store, persistor } from 'store/store'
import { createNewStore } from 'store/store'

import { Core } from './Core'

export const CoreWithStore = () => (
<Provider store={store}>
<PersistGate persistor={persistor}>
<Core />
</PersistGate>
</Provider>
)
export const CoreWithStore = () => {
const { store, persistor } = createNewStore()

return (
<Provider store={store}>
<PersistGate persistor={persistor}>
<Core />
</PersistGate>
</Provider>
)
}
61 changes: 32 additions & 29 deletions src/redux/rootReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { persistReducer, createMigrate, PersistConfig } from 'redux-persist'
import { reduxStorage } from 'storage/ReduxStorage'
import { contactsReducer } from 'store/slices/contactsSlice'
import { ProfileStatus } from 'navigation/profileNavigator/types'
import { getCurrentChainId } from 'storage/ChainStorage'

import { accountsReducer } from './slices/accountsSlice'
import { balancesReducer } from './slices/balancesSlice'
Expand All @@ -28,35 +29,37 @@ const migrations = {
}),
}

const settingsPersistConfig: PersistConfig<SettingsSlice> = {
key: 'settings',
whitelist: ['pin', 'chainId', 'isFirstLaunch', 'usedBitcoinAddresses'],
storage: reduxStorage,
}
export const createRootReducer = () => {
const settingsPersistConfig: PersistConfig<SettingsSlice> = {
key: 'settings',
whitelist: ['pin', 'chainId', 'isFirstLaunch', 'usedBitcoinAddresses'],
storage: reduxStorage(getCurrentChainId()),
}

const rootPersistConfig = {
key: 'root',
storage: reduxStorage,
version: 0,
migrate: createMigrate(migrations),
whitelist: [
'profile',
'accounts',
'contacts',
'balances',
'usdPrices',
'transactions',
],
}
const rootPersistConfig = {
key: 'root',
version: 0,
migrate: createMigrate(migrations),
whitelist: [
'profile',
'accounts',
'contacts',
'balances',
'usdPrices',
'transactions',
],
storage: reduxStorage(getCurrentChainId()),
}

const reducers = combineReducers({
usdPrices: usdPriceReducer,
balances: balancesReducer,
transactions: transactionsReducer,
settings: persistReducer(settingsPersistConfig, settingsSliceReducer),
profile: profileReducer,
accounts: accountsReducer,
contacts: contactsReducer,
})
const reducers = combineReducers({
usdPrices: usdPriceReducer,
balances: balancesReducer,
transactions: transactionsReducer,
settings: persistReducer(settingsPersistConfig, settingsSliceReducer),
profile: profileReducer,
accounts: accountsReducer,
contacts: contactsReducer,
})

export const rootReducer = persistReducer(rootPersistConfig, reducers)
return persistReducer(rootPersistConfig, reducers)
}
12 changes: 10 additions & 2 deletions src/redux/slices/settingsSlice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import {
SocketsEvents,
socketsEvents,
} from 'src/subscriptions/rifSockets'
import { ChainTypesByIdType } from 'shared/constants/chainConstants'
import {
chainTypesById,
ChainTypesByIdType,
} from 'shared/constants/chainConstants'
import { getCurrentChainId } from 'storage/ChainStorage'

import {
Bitcoin,
Expand Down Expand Up @@ -322,7 +326,11 @@ const initialState: SettingsSlice = {

const settingsSlice = createSlice({
name: 'settings',
initialState,
initialState: () => ({
...initialState,
chainId: getCurrentChainId(),
chainType: chainTypesById[getCurrentChainId()],
}),
reducers: {
setIsFirstLaunch: (state, { payload }: PayloadAction<boolean>) => {
state.isFirstLaunch = payload
Expand Down
14 changes: 10 additions & 4 deletions src/redux/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import {
// REGISTER,
} from 'redux-persist'

import { rootReducer } from './rootReducer'
import { createRootReducer } from './rootReducer'

// Must use redux-debugger plugin in flipper for the redux debugger to work

export const createStore = (preloadedState = {}) =>
configureStore({
reducer: rootReducer,
reducer: createRootReducer(),
preloadedState,
middleware: getDefaultMiddlewares => {
const middlewares = getDefaultMiddlewares({
Expand All @@ -31,8 +31,14 @@ export const createStore = (preloadedState = {}) =>

export const store = createStore()

export const persistor = persistStore(store)

export const createNewStore = () => {
const newStore = createStore()
const newPersistor = persistStore(newStore)
return {
store: newStore,
persistor: newPersistor,
}
}
// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
Expand Down
26 changes: 24 additions & 2 deletions src/screens/settings/SettingsScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { version } from 'package.json'
import { useCallback, useEffect, useMemo } from 'react'
import { useCallback, useContext, useEffect, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import { Platform, ScrollView, StyleSheet, View } from 'react-native'

Expand All @@ -15,17 +15,21 @@ import { sharedColors, sharedStyles } from 'shared/constants'
import { castStyle } from 'shared/utils'
import {
selectChainId,
selectChainType,
selectPin,
selectWalletIsDeployed,
} from 'store/slices/settingsSlice'
import { useAppSelector } from 'store/storeUtils'
import { chainTypesById } from 'shared/constants/chainConstants'
import { ChainTypeEnum, chainTypesById } from 'shared/constants/chainConstants'
import { GlobalErrorHandlerContext } from 'components/GlobalErrorHandler/GlobalErrorHandlerContext'
import { getCurrentChainId, setCurrentChainId } from 'storage/ChainStorage'

export const SettingsScreen = ({
navigation,
}: SettingsScreenProps<settingsStackRouteNames.SettingsScreen>) => {
const statePIN = useAppSelector(selectPin)
const chainId = useAppSelector(selectChainId)
const chainType = useAppSelector(selectChainType)
const walletIsDeployed = useAppSelector(selectWalletIsDeployed)

const smartWalletFactoryAddress = useMemo(
Expand Down Expand Up @@ -78,6 +82,12 @@ export const SettingsScreen = ({
}, [navigation])
const { t } = useTranslation()

const { handleReload } = useContext(GlobalErrorHandlerContext)
const onSwitchChains = () => {
const currentChainId = getCurrentChainId()
setCurrentChainId(currentChainId === 31 ? 30 : 31)
handleReload()
}
return (
<ScrollView style={styles.container}>
<View style={styles.mainView}>
Expand Down Expand Up @@ -129,6 +139,18 @@ export const SettingsScreen = ({
</AppTouchable>
)}
</View>
<AppTouchable
width={'100%'}
accessibilityLabel="Wallet Backup"
style={styles.settingsItem}
onPress={onSwitchChains}>
<Typography type={'h3'}>
Switch to{' '}
{chainType === ChainTypeEnum.MAINNET
? ChainTypeEnum.TESTNET
: ChainTypeEnum.MAINNET}
</Typography>
</AppTouchable>
<View style={styles.bottomView}>
<AppTouchable
width={'100%'}
Expand Down
9 changes: 9 additions & 0 deletions src/storage/ChainStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ChainTypesByIdType } from 'shared/constants/chainConstants'

import { MainStorage } from './MainStorage'

export const getCurrentChainId: () => ChainTypesByIdType = () =>
MainStorage.get('chainId') || 31

export const setCurrentChainId = (chainId: ChainTypesByIdType) =>
MainStorage.set('chainId', chainId)
43 changes: 38 additions & 5 deletions src/storage/ReduxStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,50 @@ import { MMKVStorage } from './MMKVStorage'

const storage = new MMKVStorage()

export const reduxStorage: Storage = {
export const reduxStorage: (chainId: number) => Storage = (
chainId: number,
) => ({
setItem: (key, value) => {
storage.set(key, value)
const keyToUse = `${key}_${chainId}`
if (chainId === 31) {
storage.set(key, value)
}
storage.set(keyToUse, value)
return Promise.resolve(true)
},
getItem: key => {
const value = storage.get(key)
let keyToUse = `${key}_${chainId}`
if (chainId === 31) {
keyToUse = key
}
const value = storage.get(keyToUse)
return Promise.resolve(value)
},
removeItem: key => {
storage.delete(key)
const keyToUse = `${key}_${chainId}`
if (chainId === 31) {
storage.delete(key)
}
storage.delete(keyToUse)
return Promise.resolve()
},
}
})

// Incremental rollout - uncomment this and remove code above after 1 month (due date: 12 Oct 2023)
/*
export const reduxStorage: (chainId: number) => Storage = (
chainId: number,
) => ({
setItem: (key, value) => {
storage.set(`${key}_${chainId}`, value)
return Promise.resolve(true)
},
getItem: key => {
const value = storage.get(`${key}_${chainId}`)
return Promise.resolve(value)
},
removeItem: key => {
storage.delete(`${key}_${chainId}`)
return Promise.resolve()
},
})*/
4 changes: 4 additions & 0 deletions src/subscriptions/rifSockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export const rifSockets = ({
currentInstance.cache = new MMKVStorage('txs', encryptionKey)
},
},
{
cacheBlockNumberText: `blockNumber_${chainId}`,
cacheTxsText: `cachedTxs_${chainId}`,
},
)

const connectSocket = () => {
Expand Down

0 comments on commit 4a99f45

Please sign in to comment.