Skip to content

Commit

Permalink
refactor: single source chainId type
Browse files Browse the repository at this point in the history
  • Loading branch information
TravellerOnTheRun committed Feb 5, 2024
1 parent 3ba297e commit 313711b
Show file tree
Hide file tree
Showing 22 changed files with 76 additions and 100 deletions.
7 changes: 3 additions & 4 deletions src/components/accounts/AccountBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { StyleSheet, TextInput, View } from 'react-native'
import { TouchableOpacity } from 'react-native-gesture-handler'
import Icon from 'react-native-vector-icons/FontAwesome'

import { ChainID } from 'lib/eoaWallet'

import {
AppButton,
AppButtonBackgroundVarietyEnum,
AppTouchable,
Input,
Typography,
Expand All @@ -23,14 +23,13 @@ import { WalletIsDeployed } from 'store/slices/settingsSlice/types'
import { selectAccounts } from 'store/slices/accountsSlice/selector'
import { AccountPayload } from 'store/slices/accountsSlice/types'
import { useAppDispatch, useAppSelector } from 'store/storeUtils'
import { ChainTypesByIdType } from 'shared/constants/chainConstants'

import { CheckIcon } from '../icons/CheckIcon'

interface AccountBoxProps {
address: string
smartWalletAddress: string | null
chainId: ChainTypesByIdType
chainId: ChainID
walletIsDeployed: WalletIsDeployed
publicKeys: PublicKeyItemType[]
id?: number
Expand Down
12 changes: 4 additions & 8 deletions src/components/address/AddressInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import { useTranslation } from 'react-i18next'
import { isBitcoinAddressValid } from '@rsksmart/rif-wallet-bitcoin'
import { Network } from 'bitcoin-address-validation'

import { ChainID } from 'lib/eoaWallet'

import { getRnsResolver } from 'core/setup'
import { sharedColors } from 'shared/constants'
import { Contact, ContactWithAddressRequired } from 'shared/types'
import {
ChainTypeEnum,
ChainTypesByIdType,
chainTypesById,
} from 'shared/constants/chainConstants'
import { castStyle } from 'shared/utils'
import { ContactCard } from 'screens/contacts/components'
import { ProposedContact } from 'screens/send/TransactionForm'
Expand All @@ -37,7 +34,7 @@ export interface AddressInputProps extends Omit<InputProps, 'value'> {
newDisplayValue: string,
isValid: boolean,
) => void
chainId: ChainTypesByIdType
chainId: ChainID
contactList?: Contact[]
onSetLoadingRNS?: (isLoading: boolean) => void
searchContacts?: (textString: string) => void
Expand Down Expand Up @@ -241,8 +238,7 @@ export const AddressInput = ({
let validationMessage: AddressValidationMessage
if (isBitcoin) {
if (isBitcoinValid) {
const isMainnet = chainTypesById[chainId] === ChainTypeEnum.MAINNET
const network = isMainnet ? Network.mainnet : Network.testnet
const network = chainId === 30 ? Network.mainnet : Network.testnet
const isNetworkValid = isBitcoinAddressValid(userInput, network)
validationMessage = isNetworkValid
? AddressValidationMessage.VALID
Expand Down
5 changes: 2 additions & 3 deletions src/components/address/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
} from '@rsksmart/rsk-utils'

import { shortAddress } from 'lib/utils'

import { ChainTypesByIdType } from 'shared/constants/chainConstants'
import { ChainID } from 'lib/eoaWallet'

export enum AddressValidationMessage {
INVALID_ADDRESS = 'Invalid address',
Expand Down Expand Up @@ -64,7 +63,7 @@ export const isMyAddress = (

export const getAddressDisplayText = (
inputAddress: string,
chainId: ChainTypesByIdType,
chainId: ChainID,
) => {
const checksumAddress = toChecksumAddress(inputAddress, chainId)
const displayAddress = shortAddress(checksumAddress)
Expand Down
10 changes: 2 additions & 8 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import config from 'config.json'
import ReactNativeConfig from 'react-native-config'
import { constants } from 'ethers'

import {
ChainTypesByIdType,
chainTypesById,
} from 'shared/constants/chainConstants'
import { chainTypesById } from 'shared/constants/chainConstants'
import { SETTINGS } from 'core/types'
import { TokenSymbol } from 'screens/home/TokenImage'
import { ChainID } from 'src/lib/eoaWallet'
Expand Down Expand Up @@ -39,10 +36,7 @@ export const getWalletSetting = (
*/
export const getEnvSetting = (setting: SETTINGS) => ReactNativeConfig[setting]

export const getTokenAddress = (
symbol: TokenSymbol,
chainId: ChainTypesByIdType,
) => {
export const getTokenAddress = (symbol: TokenSymbol, chainId: ChainID) => {
const contracts = chainId === 31 ? testnetContracts : mainnetContracts

const result = Object.keys(contracts).find(
Expand Down
18 changes: 6 additions & 12 deletions src/core/hooks/bitcoin/initializeBitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
} from '@rsksmart/rif-wallet-bitcoin'
import { RifWalletServicesFetcher } from '@rsksmart/rif-wallet-services'

import { ChainID } from 'lib/eoaWallet'

import {
BitcoinNetworkStore,
StoredBitcoinNetworks,
Expand All @@ -16,29 +18,21 @@ import { bitcoinMainnet, bitcoinTestnet } from 'shared/constants'
import { onRequest } from 'store/slices/settingsSlice'
import { AppDispatch } from 'store/index'
import { Bitcoin } from 'store/slices/settingsSlice/types'
import {
ChainTypeEnum,
chainTypesById,
ChainTypesByIdType,
} from 'shared/constants/chainConstants'

const NETWORKS_INITIAL_STATE: Bitcoin = {
networksArr: [],
networksMap: {},
}

const onNoNetworksPresent = (chainId: ChainTypesByIdType) => {
const bitcoinNetwork =
chainTypesById[chainId] === ChainTypeEnum.MAINNET
? bitcoinMainnet
: bitcoinTestnet
const onNoNetworksPresent = (chainId: ChainID) => {
const bitcoinNetwork = chainId === 30 ? bitcoinMainnet : bitcoinTestnet

BitcoinNetworkStore.addNewNetwork(bitcoinNetwork.name, bitcoinNetwork.bips)

return BitcoinNetworkStore.getStoredNetworks()
}

const BITCOIN_CHAINID_MAP: Record<ChainTypesByIdType, string> = {
const BITCOIN_CHAINID_MAP: Record<ChainID, string> = {
30: bitcoinMainnet.name,
31: bitcoinTestnet.name,
}
Expand All @@ -56,7 +50,7 @@ export const initializeBitcoin = (
mnemonic: string,
dispatch: AppDispatch,
fetcher: RifWalletServicesFetcher,
chainId: ChainTypesByIdType,
chainId: ChainID,
) => {
// Return Object which contains both array and map
const networksObj = NETWORKS_INITIAL_STATE
Expand Down
24 changes: 9 additions & 15 deletions src/core/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,26 @@ import mainnetContracts from '@rsksmart/rsk-contract-metadata'
import testnetContracts from '@rsksmart/rsk-testnet-contract-metadata'
import axios from 'axios'

import { ChainID } from 'lib/eoaWallet'

import { SETTINGS } from 'core/types'
import { MAINNET, TESTNET } from 'screens/rnsManager/addresses.json'
import {
ChainTypeEnum,
ChainTypesByIdType,
chainTypesById,
} from 'shared/constants/chainConstants'
import { ITokenWithoutLogo } from 'store/slices/balancesSlice/types'
import { Wallet } from 'shared/wallet'

import { getWalletSetting } from './config'

export const createPublicAxios = (chainId: ChainTypesByIdType) =>
export const createPublicAxios = (chainId: ChainID) =>
axios.create({
baseURL: getWalletSetting(SETTINGS.RIF_WALLET_SERVICE_URL, chainId),
})

export const abiEnhancer = new AbiEnhancer()

export const getRnsResolver = (chainId: ChainTypesByIdType, wallet: Wallet) => {
const isMainnet = chainTypesById[chainId] === ChainTypeEnum.MAINNET
const rnsRegistryAddress = isMainnet
? MAINNET.rnsRegistryAddress
: TESTNET.rnsRegistryAddress
export const getRnsResolver = (chainId: ChainID, wallet: Wallet) => {
const rnsRegistryAddress =
chainId === 30 ? MAINNET.rnsRegistryAddress : TESTNET.rnsRegistryAddress

return new AddrResolver(rnsRegistryAddress, wallet)
}

Expand Down Expand Up @@ -61,8 +57,6 @@ const defaultTestnetTokens: ITokenWithoutLogo[] = Object.keys(testnetContracts)
usdBalance: 0,
}
})
export const getDefaultTokens = (chainId: ChainTypesByIdType) => {
return chainTypesById[chainId] === ChainTypeEnum.MAINNET
? defaultMainnetTokens
: defaultTestnetTokens
export const getDefaultTokens = (chainId: ChainID) => {
return chainId === 30 ? defaultMainnetTokens : defaultTestnetTokens
}
4 changes: 3 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import moment from 'moment'
import { abiEnhancer } from 'core/setup'
import { ApiTransactionWithExtras } from 'src/redux/slices/transactionsSlice'

import { ChainID } from './eoaWallet'

export function shortAddress(address: string, amount = 4): string {
if (!address) {
return ''
Expand Down Expand Up @@ -187,7 +189,7 @@ export const removeLeadingZeros = (value: string) => {
*/
export const createPendingTxFromTxResponse = async (
txResponse: TransactionResponse,
{ chainId, from, to }: { chainId: number; from: string; to: string },
{ chainId, from, to }: { chainId: ChainID; from: string; to: string },
) => {
try {
const enhancedTx = await abiEnhancer.enhance(chainId, {
Expand Down
5 changes: 2 additions & 3 deletions src/redux/slices/settingsSlice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
SocketsEvents,
socketsEvents,
} from 'src/subscriptions/rifSockets'
import { ChainTypesByIdType } from 'shared/constants/chainConstants'
import { getCurrentChainId } from 'storage/ChainStorage'
import { resetReduxStorage } from 'storage/ReduxStorage'
import {
Expand Down Expand Up @@ -77,7 +76,7 @@ export const getRifRelayConfig = (chainId: ChainID): RifRelayConfig => {
}
}

const sslPinning = async (chainId: ChainTypesByIdType) => {
const sslPinning = async (chainId: ChainID) => {
const rifWalletServiceDomain = getWalletSetting(
SETTINGS.RIF_WALLET_SERVICE_URL,
chainId,
Expand Down Expand Up @@ -396,7 +395,7 @@ const settingsSlice = createSlice({
closeRequest: state => {
state.requests.pop()
},
setChainId: (state, { payload }: PayloadAction<ChainTypesByIdType>) => {
setChainId: (state, { payload }: PayloadAction<ChainID>) => {
state.chainId = payload
},
setAppIsActive: (state, { payload }: PayloadAction<boolean>) => {
Expand Down
11 changes: 9 additions & 2 deletions src/redux/slices/settingsSlice/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { BitcoinNetworkWithBIPRequest } from '@rsksmart/rif-wallet-bitcoin'
import { ColorValue } from 'react-native'

import { ChainID } from 'lib/eoaWallet'

import { RequestWithBitcoin } from 'shared/types'
import { ChainTypesByIdType } from 'shared/constants/chainConstants'
import { InitializeWallet, Wallet } from 'shared/wallet'

export type ResetAppPayload =
| undefined
| {
wallet: Wallet
}

export interface WalletIsDeployed {
loading: boolean
txHash: string | null
Expand Down Expand Up @@ -71,7 +78,7 @@ export interface SettingsSlice {
topColor: ColorValue
selectedWallet: string
loading: boolean
chainId: ChainTypesByIdType
chainId: ChainID
appIsActive: boolean
unlocked: boolean
previouslyUnlocked: boolean
Expand Down
13 changes: 3 additions & 10 deletions src/redux/slices/transactionsSlice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
convertTokenToUSD,
convertUnixTimeToFromNowFormat,
} from 'lib/utils'
import { ChainID } from 'lib/eoaWallet'

import {
ActivityMixedType,
Expand All @@ -27,18 +28,13 @@ import { resetSocketState } from 'store/shared/actions/resetSocketState'
import { UsdPricesState } from 'store/slices/usdPricesSlice'
import { getTokenAddress } from 'core/config'
import { AsyncThunkWithTypes } from 'store/store'
import {
ChainTypeEnum,
chainTypesById,
ChainTypesByIdType,
} from 'shared/constants/chainConstants'
import { TokenSymbol } from 'screens/home/TokenImage'
import { rbtcMap } from 'shared/utils'

export const activityDeserializer: (
activityTransaction: ActivityMixedType,
prices: UsdPricesState,
chainId: ChainTypesByIdType,
chainId: ChainID,
) => ActivityRowPresentationObject = (activityTransaction, prices, chainId) => {
if ('isBitcoin' in activityTransaction) {
const fee = activityTransaction.fees
Expand Down Expand Up @@ -82,10 +78,7 @@ export const activityDeserializer: (
const etx = activityTransaction.enhancedTransaction

// RBTC
const rbtcSymbol =
chainTypesById[chainId] === ChainTypeEnum.MAINNET
? TokenSymbol.RBTC
: TokenSymbol.TRBTC
const rbtcSymbol = chainId === 30 ? TokenSymbol.RBTC : TokenSymbol.TRBTC
const rbtcAddress = constants.AddressZero
const feeRbtc = BigNumber.from(tx.receipt?.gasUsed || 0)

Expand Down
5 changes: 3 additions & 2 deletions src/screens/activity/ActivityScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { useTranslation } from 'react-i18next'
import { FlatList, Image, RefreshControl, StyleSheet, View } from 'react-native'
import { useIsFocused } from '@react-navigation/native'

import { ChainID } from 'lib/eoaWallet'

import { Typography } from 'components/typography'
import { abiEnhancer } from 'core/setup'
import { rootTabsRouteNames } from 'navigation/rootNavigator'
Expand All @@ -20,7 +22,6 @@ import {
} from 'store/slices/transactionsSlice/selectors'
import { useAppDispatch, useAppSelector } from 'store/storeUtils'
import { useWallet } from 'shared/wallet'
import { ChainTypesByIdType } from 'src/shared/constants/chainConstants'

import { ActivityBasicRow } from './ActivityRow'

Expand Down Expand Up @@ -116,7 +117,7 @@ const styles = StyleSheet.create({

export const enhanceTransactionInput = async (
transaction: IApiTransaction,
chainId: ChainTypesByIdType,
chainId: ChainID,
): Promise<EnhancedResult | null> => {
try {
const enhancedTx = await abiEnhancer.enhance(chainId, {
Expand Down
7 changes: 2 additions & 5 deletions src/screens/rnsManager/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainTypesByIdType } from 'shared/constants/chainConstants'
import { ChainID } from 'lib/eoaWallet'

import { TESTNET, MAINNET } from './addresses.json'

Expand All @@ -9,10 +9,7 @@ export interface RNS_ADDRESSES_TYPE {
rnsRegistryAddress: string
}

export const RNS_ADDRESSES_BY_CHAIN_ID: Record<
ChainTypesByIdType,
RNS_ADDRESSES_TYPE
> = {
export const RNS_ADDRESSES_BY_CHAIN_ID: Record<ChainID, RNS_ADDRESSES_TYPE> = {
30: MAINNET,
31: TESTNET,
}
4 changes: 2 additions & 2 deletions src/screens/send/TransactionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
sanitizeMaxDecimalText,
shortAddress,
} from 'lib/utils'
import { ChainID } from 'lib/eoaWallet'

import {
AddressInput,
Expand All @@ -32,7 +33,6 @@ import { castStyle } from 'shared/utils'
import { IPrice } from 'src/subscriptions/types'
import { TokenBalanceObject } from 'store/slices/balancesSlice/types'
import { Contact, ContactWithAddressRequired } from 'src/shared/types'
import { ChainTypesByIdType } from 'shared/constants/chainConstants'
import { navigationContainerRef } from 'src/core/Core'
import { rootTabsRouteNames } from 'src/navigation/rootNavigator'
import { contactsStackRouteNames } from 'src/navigation/contactsNavigator'
Expand All @@ -51,7 +51,7 @@ interface Props {
isWalletDeployed: boolean
tokenList: TokenBalanceObject[]
tokenPrices: Record<string, IPrice>
chainId: ChainTypesByIdType
chainId: ChainID
totalUsdBalance: string
initialValues: {
asset?: TokenBalanceObject
Expand Down
1 change: 0 additions & 1 deletion src/screens/walletConnect/ScanQRScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from 'navigation/rootNavigator'
import { selectChainId } from 'store/slices/settingsSlice'
import { useAppSelector } from 'store/storeUtils'
import { chainTypesById } from 'shared/constants/chainConstants'
import { AndroidQRScanner } from 'screens/walletConnect/AndroidQRScanner'

export const ScanQRScreen = ({
Expand Down
Loading

0 comments on commit 313711b

Please sign in to comment.