Skip to content

Commit

Permalink
CP-9009: Integrate with bitcoin_sendTransaction from bitcoin module (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
atn4z7 authored Aug 21, 2024
1 parent 5b50e4d commit 7fcb5c5
Show file tree
Hide file tree
Showing 41 changed files with 686 additions and 1,165 deletions.
5 changes: 5 additions & 0 deletions packages/core-mobile/app/contexts/SendNFTContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { useNetworks } from 'hooks/networks/useNetworks'
import { useNetworkFee } from 'hooks/useNetworkFee'
import { useInAppRequest } from 'hooks/useInAppRequest'
import { audioFeedback, Audios } from 'utils/AudioFeedback'
import { showTransactionErrorToast } from 'utils/toast'
import { getJsonRpcErrorMessage } from 'utils/getJsonRpcErrorMessage'

export interface SendNFTContextState {
sendToken: NFTItem
Expand Down Expand Up @@ -132,6 +134,9 @@ export const SendNFTContextProvider = ({
})
.catch(reason => {
setSendStatus('Fail')
showTransactionErrorToast({
message: getJsonRpcErrorMessage(reason)
})
AnalyticsService.capture('NftSendFailed', {
errorMessage: reason?.error?.message,
chainId: activeNetwork.chainId
Expand Down
6 changes: 5 additions & 1 deletion packages/core-mobile/app/contexts/SendTokenContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { useInAppRequest } from 'hooks/useInAppRequest'
import { RootState } from 'store'
import { audioFeedback, Audios } from 'utils/AudioFeedback'
import { TokenWithBalance } from '@avalabs/vm-module-types'
import { showTransactionErrorToast } from 'utils/toast'
import { getJsonRpcErrorMessage } from 'utils/getJsonRpcErrorMessage'

export type SendStatus = 'Idle' | 'Sending' | 'Success' | 'Fail'

Expand Down Expand Up @@ -162,7 +164,9 @@ export const SendTokenContextProvider = ({
})
.catch(reason => {
setSendStatus('Fail')
setError(reason.message)
showTransactionErrorToast({
message: getJsonRpcErrorMessage(reason)
})
AnalyticsService.capture('SendTransactionFailed', {
errorMessage: reason.message,
chainId: activeNetwork.chainId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BlockchainNamespace } from 'store/rpc/types'
import { BlockchainNamespace } from '@avalabs/core-chains-sdk'

export type InjectedJavascripts = {
injectCoreAsRecent: string
Expand Down
1 change: 0 additions & 1 deletion packages/core-mobile/app/navigation/AppNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ enum ModalScreens {
EditSpendLimit = 'ModalScreens.EditSpendLimit',
AvalancheSendTransactionV2 = 'ModalScreens.AvalancheSendTransactionV2',
AvalancheSignTransactionV2 = 'ModalScreens.AvalancheSignTransactionV2',
BitcoinSendTransaction = 'ModalScreens.BitcoinSendTransaction',
AvalancheSetDeveloperMode = 'ModalScreens.AvalancheSetDeveloperMode',
StakeDisclaimer = 'ModalScreens.StakeDisclaimer',
CoreIntro = 'ModalScreens.CoreIntro',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ import {
ApprovalPopupParams,
AvalancheSendTransactionV2Params,
AvalancheSetDeveloperModeParams,
BitcoinSendTransactionParams,
BridgeAssetV2Params,
BridgeTransactionStatusParams,
BuyCarefullyParams,
Expand Down Expand Up @@ -180,7 +179,6 @@ export type WalletScreenStackParams = {
[AppNavigation.Modal.BrowserTabCloseAll]: { onConfirm: () => void }
[AppNavigation.Modal.AnalyticsConsentSheet]: undefined
[AppNavigation.Modal.UseWalletConnect]: { onContinue: () => void }
[AppNavigation.Modal.BitcoinSendTransaction]: BitcoinSendTransactionParams
[AppNavigation.Modal.AlertScreen]: AlertScreenParams
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { AreYouSureModal } from 'screens/browser/AreYouSureModal'
import AnalyticsConsentSheet from 'screens/mainView/AnalyticsConsentSheet'
import { AvalancheSetDeveloperMode } from 'screens/rpc/components/v2/AvalancheSetDeveloperMode'
import { UseWalletConnectModal } from 'screens/browser/UseWalletConnectModal'
import BitcoinSendTransaction from 'screens/rpc/components/v2/BitcoinSendTransaction'
import AlertScreen from 'screens/rpc/components/v2/AlertScreen'
import EditSpendLimit from 'components/EditSpendLimit'
import { SignOutModalScreen, WalletScreenSType } from './WalletScreenStack'
Expand Down Expand Up @@ -80,10 +79,6 @@ export const createModals = (WalletScreenS: WalletScreenSType): JSX.Element => {
name={AppNavigation.Modal.AvalancheSendTransactionV2}
component={AvalancheSendTransactionV2}
/>
<WalletScreenS.Screen
name={AppNavigation.Modal.BitcoinSendTransaction}
component={BitcoinSendTransaction}
/>
<WalletScreenS.Screen
name={AppNavigation.Modal.AvalancheSetDeveloperMode}
component={AvalancheSetDeveloperMode}
Expand Down
9 changes: 0 additions & 9 deletions packages/core-mobile/app/navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ import {
AvalancheSetDeveloperModeRpcRequest
} from 'store/rpc/handlers/avalanche_setDeveloperMode/types'
import { WCSessionProposal } from 'store/walletConnectV2/types'
import {
BitcoinSendTransactionApproveData,
BitcoinSendTransactionRpcRequest
} from 'store/rpc/handlers/bitcoin_sendTransaction/bitcoin_sendTransaction'
import { SiteScanResponse } from 'services/blockaid/types'
import { SpendLimit } from 'hooks/useSpendLimits'
import { ProposalTypes } from '@walletconnect/types'
Expand Down Expand Up @@ -153,11 +149,6 @@ export type AvalancheSetDeveloperModeParams = {
data: AvalancheSetDeveloperModeApproveData
}

export type BitcoinSendTransactionParams = {
request: BitcoinSendTransactionRpcRequest
data: BitcoinSendTransactionApproveData
}

export type BridgeAssetV2Params = {
request: AvalancheBridgeAssetRequestV2
amountStr: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Blockchain, useBridgeSDK } from '@avalabs/core-bridge-sdk'
import { selectBridgeAppConfig } from 'store/bridge'
import { useInAppRequest } from 'hooks/useInAppRequest'
import BridgeService from 'services/bridge/BridgeService'
import { selectIsDeveloperMode } from 'store/settings/advanced/slice'

type TransferParams = {
amount: string
Expand All @@ -18,6 +19,7 @@ export function useTransferAssetBTC(): {
} {
const config = useSelector(selectBridgeAppConfig)
const { currentBlockchain } = useBridgeSDK()
const isDeveloperMode = useSelector(selectIsDeveloperMode)

const { request } = useInAppRequest()

Expand All @@ -35,10 +37,11 @@ export function useTransferAssetBTC(): {
amount,
feeRate,
config,
isMainnet: !isDeveloperMode,
request
})
},
[currentBlockchain, config, request]
[currentBlockchain, config, isDeveloperMode, request]
)

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable sonarjs/cognitive-complexity */
import React, { useCallback, useEffect, useState } from 'react'
import { ActivityIndicator, StyleSheet, ScrollView } from 'react-native'
import { RpcMethod, TokenType } from '@avalabs/vm-module-types'
import { isAddress as isEvmAddress } from 'ethers'
import { TokenType } from '@avalabs/vm-module-types'
import { Space } from 'components/Space'
import AvaButton from 'components/AvaButton'
import { Row } from 'components/Row'
Expand Down Expand Up @@ -36,6 +36,7 @@ import GlobeSVG from 'components/svg/GlobeSVG'
import { useSpendLimits } from 'hooks/useSpendLimits'
import { isHex } from 'viem'
import { getChainIdFromCaip2 } from 'temp/caip2ChainIds'
import { isBtcAddress } from 'utils/isBtcAddress'
import RpcRequestBottomSheet from '../shared/RpcRequestBottomSheet'
import BalanceChange from './BalanceChange'
import { SpendLimits } from './SpendLimits'
Expand All @@ -55,6 +56,7 @@ const ApprovalPopup = (): JSX.Element => {
const caip2ChainId = request.chainId
const chainId = getChainIdFromCaip2(caip2ChainId)
const network = getNetwork(chainId)

const accountSelector =
'account' in signingData
? selectAccountByAddress(signingData.account)
Expand All @@ -77,13 +79,11 @@ const ApprovalPopup = (): JSX.Element => {
const approveDisabled =
!network ||
!account ||
(displayData.networkFeeSelector && !maxFeePerGas) ||
(displayData.networkFeeSelector && !maxPriorityFeePerGas) ||
(displayData.networkFeeSelector && maxFeePerGas === undefined) ||
(displayData.networkFeeSelector && maxPriorityFeePerGas === undefined) ||
submitting

const showNetworkFeeSelector =
displayData.networkFeeSelector &&
signingData.type === RpcMethod.ETH_SEND_TRANSACTION
const showNetworkFeeSelector = displayData.networkFeeSelector

const rejectAndClose = useCallback(
(message?: string) => {
Expand Down Expand Up @@ -323,8 +323,8 @@ const ApprovalPopup = (): JSX.Element => {
continue

if (typeof value === 'string') {
const isAddress = value.substring(0, 2) === '0x'

const isAddress =
isEvmAddress(value) || isBtcAddress(value, !network?.isTestnet)
detailsToDisplay.push(
<Row style={{ justifyContent: 'space-between' }} key={key}>
<Text variant="caption">{humanize(key)}</Text>
Expand Down Expand Up @@ -420,6 +420,29 @@ const ApprovalPopup = (): JSX.Element => {
return <BalanceChange balanceChange={balanceChange} />
}

const renderNetworkFeeSelector = (): JSX.Element | null => {
if (!showNetworkFeeSelector || !chainId) return null

let gasLimit: number | undefined

if (
typeof signingData.data === 'object' &&
'gasLimit' in signingData.data
) {
gasLimit = Number(signingData.data.gasLimit || 0)
}

if (!gasLimit) return null

return (
<NetworkFeeSelector
chainId={chainId}
gasLimit={gasLimit}
onFeesChange={handleFeesChange}
/>
)
}

return (
<>
<RpcRequestBottomSheet
Expand All @@ -440,17 +463,7 @@ const ApprovalPopup = (): JSX.Element => {
{renderSpendLimits()}
{renderBalanceChange()}
</View>
{showNetworkFeeSelector && chainId && (
<NetworkFeeSelector
chainId={chainId}
gasLimit={
signingData.data.gasLimit
? Number(signingData.data.gasLimit)
: 0
}
onFeesChange={handleFeesChange}
/>
)}
{renderNetworkFeeSelector()}
{renderDisclaimer()}
</ScrollView>
{renderApproveRejectButtons()}
Expand Down
Loading

0 comments on commit 7fcb5c5

Please sign in to comment.