Skip to content

Commit

Permalink
CP-9541: Fix 'No asset selected' error when bridging BTC.b to BTC (#2111
Browse files Browse the repository at this point in the history
)
  • Loading branch information
onghwan authored Nov 22, 2024
1 parent 61515e8 commit d729626
Show file tree
Hide file tree
Showing 24 changed files with 45 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { selectActiveAccount } from 'store/account'
import AnalyticsService from 'services/analytics/AnalyticsService'
import { RpcMethod } from 'store/rpc/types'
import { useInAppRequest } from 'hooks/useInAppRequest'
import { getEvmCaip2ChainId } from 'temp/caip2ChainIds'
import { getEvmCaip2ChainId } from 'utils/caip2ChainIds'
import { useNetworks } from 'hooks/networks/useNetworks'
import { showTransactionErrorToast } from 'utils/toast'
import { audioFeedback, Audios } from 'utils/AudioFeedback'
Expand Down
4 changes: 2 additions & 2 deletions packages/core-mobile/app/hooks/networks/useNetworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useCallback, useMemo } from 'react'
import { selectIsDeveloperMode } from 'store/settings/advanced'
import { type Network } from '@avalabs/core-chains-sdk'
import { isAvalancheChainId } from 'services/network/utils/isAvalancheNetwork'
import { addNamespaceToChain } from 'services/walletconnectv2/utils'
import { getCaip2ChainId } from 'utils/caip2ChainIds'
import { useGetNetworks } from './useGetNetworks'

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
Expand Down Expand Up @@ -175,6 +175,6 @@ export const useNetworks = () => {
const decorateWithCaip2ChainId = (networks: Networks): Networks =>
Object.entries(networks).reduce((acc, [key, network]) => {
const chainId = parseInt(key)
acc[chainId] = { ...network, caip2ChainId: addNamespaceToChain(chainId) }
acc[chainId] = { ...network, caip2ChainId: getCaip2ChainId(chainId) }
return acc
}, {} as Networks)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BridgeAsset, ChainAssetMap } from '@avalabs/bridge-unified'
import UnifiedBridgeService from 'services/bridge/UnifiedBridgeService'
import Logger from 'utils/Logger'
import { useNetworks } from 'hooks/networks/useNetworks'
import { addNamespaceToChain } from 'services/walletconnectv2/utils'
import { getCaip2ChainId } from 'utils/caip2ChainIds'

export const useBridgeAssets = (): {
chainAssetMap: ChainAssetMap
Expand All @@ -17,7 +17,7 @@ export const useBridgeAssets = (): {
UnifiedBridgeService.getAssets()
.then(allAssets => {
setChainAssetMap(allAssets)
const caipChainId = addNamespaceToChain(activeNetwork.chainId)
const caipChainId = getCaip2ChainId(activeNetwork.chainId)

setBridgeAssets(allAssets[caipChainId] ?? [])
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Network } from '@avalabs/core-chains-sdk'
import { BridgeAsset } from '@avalabs/bridge-unified'
import { useNetworks } from 'hooks/networks/useNetworks'
import { getChainIdFromCaip2 } from 'temp/caip2ChainIds'
import { getChainIdFromCaip2 } from 'utils/caip2ChainIds'
import { useBridgeAssets } from './useBridgeAssets'

export const useBridgeSourceNetworks = (): Network[] => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BridgeAsset, BridgeType } from '@avalabs/bridge-unified'
import { useMemo } from 'react'
import { getEvmCaip2ChainId } from 'temp/caip2ChainIds'
import { getCaip2ChainId } from 'utils/caip2ChainIds'

export const useBridgeType = (
bridgeAsset: BridgeAsset | undefined,
Expand All @@ -11,6 +11,6 @@ export const useBridgeType = (
return undefined
}

return bridgeAsset.destinations[getEvmCaip2ChainId(targetChainId)]?.[0]
return bridgeAsset.destinations[getCaip2ChainId(targetChainId)]?.[0]
}, [bridgeAsset, targetChainId])
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useNetworkContractTokens } from 'hooks/networks/useNetworkContractToken
import { useNetworks } from 'hooks/networks/useNetworks'
import { useMemo } from 'react'
import { isBitcoinChainId } from 'utils/network/isBitcoinNetwork'
import { getChainIdFromCaip2 } from 'temp/caip2ChainIds'
import { getChainIdFromCaip2 } from 'utils/caip2ChainIds'
import { isEthereumChainId } from 'services/network/utils/isEthereumNetwork'
import { Blockchain, BridgeTransaction } from '@avalabs/core-bridge-sdk'
import { isUnifiedBridgeTransfer } from '../utils/bridgeUtils'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Avatar from 'components/Avatar'
import GlobeSVG from 'components/svg/GlobeSVG'
import { useSpendLimits } from 'hooks/useSpendLimits'
import { isHex } from 'viem'
import { getChainIdFromCaip2 } from 'temp/caip2ChainIds'
import { getChainIdFromCaip2 } from 'utils/caip2ChainIds'
import RpcRequestBottomSheet from '../shared/RpcRequestBottomSheet'
import { DetailSectionView } from '../shared/DetailSectionView'
import BalanceChange from './BalanceChange'
Expand Down
2 changes: 1 addition & 1 deletion packages/core-mobile/app/screens/send/utils/avm/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SentryWrapper from 'services/sentry/SentryWrapper'
import { resolve } from '@avalabs/core-utils-sdk'
import { Request } from 'store/rpc/utils/createInAppRequest'
import { RpcMethod } from '@avalabs/vm-module-types'
import { getAvalancheCaip2ChainId } from 'temp/caip2ChainIds'
import { getAvalancheCaip2ChainId } from 'utils/caip2ChainIds'
import { AvalancheSendTransactionParams } from '@avalabs/avalanche-module'
import { stripChainAddress } from 'store/account/utils'
import WalletService from 'services/wallet/WalletService'
Expand Down
2 changes: 1 addition & 1 deletion packages/core-mobile/app/screens/send/utils/btc/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { resolve } from '@avalabs/core-utils-sdk'
import SentryWrapper from 'services/sentry/SentryWrapper'
import { Request } from 'store/rpc/utils/createInAppRequest'
import { BitcoinSendTransactionParams } from '@avalabs/bitcoin-module'
import { getBitcoinCaip2ChainId } from 'temp/caip2ChainIds'
import { getBitcoinCaip2ChainId } from 'utils/caip2ChainIds'

export const send = async ({
request,
Expand Down
2 changes: 1 addition & 1 deletion packages/core-mobile/app/screens/send/utils/evm/send.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SentryWrapper from 'services/sentry/SentryWrapper'
import { Request } from 'store/rpc/utils/createInAppRequest'
import { getEvmCaip2ChainId } from 'temp/caip2ChainIds'
import { getEvmCaip2ChainId } from 'utils/caip2ChainIds'
import { Transaction } from '@sentry/react'
import { transactionRequestToTransactionParams } from 'store/rpc/utils/transactionRequestToTransactionParams'
import { RpcMethod, TokenWithBalanceEVM } from '@avalabs/vm-module-types'
Expand Down
2 changes: 1 addition & 1 deletion packages/core-mobile/app/screens/send/utils/pvm/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { resolve } from '@avalabs/core-utils-sdk'
import { RpcMethod } from '@avalabs/vm-module-types'
import SentryWrapper from 'services/sentry/SentryWrapper'
import { Request } from 'store/rpc/utils/createInAppRequest'
import { getAvalancheCaip2ChainId } from 'temp/caip2ChainIds'
import { getAvalancheCaip2ChainId } from 'utils/caip2ChainIds'
import { AvalancheSendTransactionParams } from '@avalabs/avalanche-module'
import { stripChainAddress } from 'store/account/utils'
import WalletService from 'services/wallet/WalletService'
Expand Down
2 changes: 1 addition & 1 deletion packages/core-mobile/app/services/bridge/BridgeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Request } from 'store/rpc/utils/createInAppRequest'
import { RpcMethod } from 'store/rpc/types'
import { bnToBig, noop, stringToBN } from '@avalabs/core-utils-sdk'
import { transactionRequestToTransactionParams } from 'store/rpc/utils/transactionRequestToTransactionParams'
import { getBitcoinCaip2ChainId, getEvmCaip2ChainId } from 'temp/caip2ChainIds'
import { getBitcoinCaip2ChainId, getEvmCaip2ChainId } from 'utils/caip2ChainIds'

type TransferBTCParams = {
fromAccount: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import {
import { Network } from '@avalabs/core-chains-sdk'
import { assertNotUndefined } from 'utils/assertions'
import Logger from 'utils/Logger'
import { addNamespaceToChain } from 'services/walletconnectv2/utils'
import { BitcoinProvider } from '@avalabs/core-wallets-sdk'
import { lowerCaseKeys } from 'utils/lowerCaseKeys'
import { getCaip2ChainId } from 'utils/caip2ChainIds'

type BridgeService = ReturnType<typeof createUnifiedBridgeService>

Expand Down Expand Up @@ -205,7 +205,7 @@ export class UnifiedBridgeService {

private async buildChain(network: Network): Promise<Chain> {
return {
chainId: addNamespaceToChain(network.chainId),
chainId: getCaip2ChainId(network.chainId),
chainName: network.chainName,
rpcUrl: network.rpcUrl,
networkToken: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Config from 'react-native-config'
import Logger from 'utils/Logger'
import DeviceInfoService from 'services/deviceInfo/DeviceInfoService'
import { JsonMap } from 'store/posthog'
import { applyTempChainIdConversion } from 'temp/caip2ChainIds'
import { applyTempChainIdConversion } from 'utils/caip2ChainIds'
import { PostHogServiceNoop } from 'services/posthog/PostHogServiceNoop'
import { sanitizeFeatureFlags } from './sanitizeFeatureFlags'
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import Logger from 'utils/Logger'
import promiseWithTimeout from 'utils/js/promiseWithTimeout'
import { WalletConnectServiceNoop } from 'services/walletconnectv2/WalletConnectServiceNoop'
import { CorePrimaryAccount } from '@avalabs/types'
import { getCaip2ChainId } from 'utils/caip2ChainIds'
import {
CLIENT_METADATA,
WalletConnectCallbacks,
WalletConnectServiceInterface
} from './types'
import {
addNamespaceToChain,
getAddressWithCaip2ChainId,
updateAccountListInNamespace,
updateChainListInNamespace
Expand Down Expand Up @@ -205,7 +205,7 @@ class WalletConnectService implements WalletConnectServiceInterface {
account: CorePrimaryAccount
}): Promise<void> => {
const topic = session.topic
const caip2ChainId = addNamespaceToChain(chainId)
const caip2ChainId = getCaip2ChainId(chainId)

const blockchainNamespace = caip2ChainId.split(':')[0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
AvalancheCaip2ChainId,
BitcoinCaip2ChainId
} from '@avalabs/core-chains-sdk'
import { addNamespaceToChain, getAddressWithCaip2ChainId } from './utils'
import { getCaip2ChainId } from 'utils/caip2ChainIds'
import { getAddressWithCaip2ChainId } from './utils'

// Mock data
const mockAccount: CorePrimaryAccount = {
Expand All @@ -23,10 +24,10 @@ const mockAccount: CorePrimaryAccount = {
addressCoreEth: 'CoreEthAddress'
}

describe('addNamespaceToChain', () => {
describe('getCaip2ChainId', () => {
it('should add eip155 namespace to a chainId', () => {
const chainId = 1
const result = addNamespaceToChain(chainId)
const result = getCaip2ChainId(chainId)
expect(result).toBe('eip155:1')
})
})
Expand Down
21 changes: 1 addition & 20 deletions packages/core-mobile/app/services/walletconnectv2/utils.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
import { BlockchainNamespace } from '@avalabs/core-chains-sdk'
import {
getAvalancheCaip2ChainId,
getBitcoinCaip2ChainIdByChainId,
isXChainId,
isPChainId
} from 'temp/caip2ChainIds'
import { isXChainId, isPChainId } from 'utils/caip2ChainIds'
import { CorePrimaryAccount } from '@avalabs/types'

// prefix correct namespace to a chainId
// '1' -> 'eip155:1'
export const addNamespaceToChain = (chainId: number): string => {
const caip2ChainId =
getAvalancheCaip2ChainId(chainId) ||
getBitcoinCaip2ChainIdByChainId(chainId)

if (caip2ChainId) {
return caip2ChainId
}

return `${BlockchainNamespace.EIP155}:${chainId}`
}

// generate full address with caip2 chain ID based on the blockchain namespace
// an example result 'eip155:1:0x241b0073b66bfc19FCB54308861f604F5Eb8f51b'
export const getAddressWithCaip2ChainId = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
isCChainId,
isPChainId,
isBtcChainId
} from 'temp/caip2ChainIds'
} from 'utils/caip2ChainIds'

const CORE_WEB_HOSTNAMES = [
'localhost',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'store/network/slice'
import { selectIsBlockaidDappScanBlocked } from 'store/posthog/slice'
import { createInAppRequest } from 'store/rpc/utils/createInAppRequest'
import { getChainIdFromCaip2 } from 'temp/caip2ChainIds'
import { getChainIdFromCaip2 } from 'utils/caip2ChainIds'
import mergeWith from 'lodash/mergeWith'
import isArray from 'lodash/isArray'
import union from 'lodash/union'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Logger from 'utils/Logger'
import { selectNetwork } from 'store/network/slice'
import { isRpcRequest } from 'store/rpc/utils/isRpcRequest'
import { mapToVmNetwork } from 'vmModule/utils/mapToVmNetwork'
import { getChainIdFromCaip2 } from 'temp/caip2ChainIds'
import { getChainIdFromCaip2 } from 'utils/caip2ChainIds'
import { CorePrimaryAccount } from '@avalabs/types'
import { Avalanche } from '@avalabs/core-wallets-sdk'
import { getAddressByVM } from 'store/account/utils'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { selectActiveNetwork } from 'store/network'
import { UPDATE_SESSION_DELAY } from 'consts/walletConnect'
import AnalyticsService from 'services/analytics/AnalyticsService'
import { showDappConnectionSuccessToast } from 'utils/toast'
import { getChainIdFromCaip2 } from 'temp/caip2ChainIds'
import { getChainIdFromCaip2 } from 'utils/caip2ChainIds'
import { getJsonRpcErrorMessage } from 'utils/getJsonRpcErrorMessage'
import { AgnosticRpcProvider, RpcMethod, RpcProvider } from '../../types'
import { isSessionProposal, isUserRejectedError } from './utils'
Expand Down
2 changes: 1 addition & 1 deletion packages/core-mobile/app/store/unifiedBridge/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { createInAppRequest } from 'store/rpc/utils/createInAppRequest'
import { FeatureGates } from 'services/posthog/types'
import { getBitcoinProvider } from 'services/network/utils/providerUtils'
import { RpcMethod } from '@avalabs/vm-module-types'
import { getBitcoinCaip2ChainId, getEvmCaip2ChainId } from 'temp/caip2ChainIds'
import { getBitcoinCaip2ChainId, getEvmCaip2ChainId } from 'utils/caip2ChainIds'
import { TransactionParams } from '@avalabs/evm-module'
import {
removePendingTransfer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,17 @@ export const getChainIdFromCaip2 = (
? getBitcoinChainId(caip2ChainId)
: Number(caip2ChainId.split(':')[1])
}

// get caip2 chain id from chain id
// '1' -> 'eip155:1'
export const getCaip2ChainId = (chainId: number): string => {
const caip2ChainId =
getAvalancheCaip2ChainId(chainId) ||
getBitcoinCaip2ChainIdByChainId(chainId)

if (caip2ChainId) {
return caip2ChainId
}

return `${BlockchainNamespace.EIP155}:${chainId}`
}
2 changes: 1 addition & 1 deletion packages/core-mobile/app/vmModule/ModuleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { assertNotUndefined } from 'utils/assertions'
import { AvalancheModule } from '@avalabs/avalanche-module'
import { BlockchainId } from '@avalabs/glacier-sdk'
import { BitcoinModule } from '@avalabs/bitcoin-module'
import { getBitcoinCaip2ChainId, getEvmCaip2ChainId } from 'temp/caip2ChainIds'
import { getBitcoinCaip2ChainId, getEvmCaip2ChainId } from 'utils/caip2ChainIds'
import { APPLICATION_NAME, APPLICATION_VERSION } from 'utils/network/constants'
import { ModuleErrors, VmModuleErrors } from './errors'
import { approvalController } from './ApprovalController/ApprovalController'
Expand Down

0 comments on commit d729626

Please sign in to comment.