1+ import { useQuery } from '@tanstack/react-query'
12import { abs } from 'biggystring'
23import {
34 EdgeAccount ,
@@ -9,6 +10,7 @@ import {
910 EdgeTxSwap
1011} from 'edge-core-js'
1112import * as React from 'react'
13+ import { useMemo } from 'react'
1214import { View } from 'react-native'
1315import FastImage from 'react-native-fast-image'
1416import IonIcon from 'react-native-vector-icons/Ionicons'
@@ -34,8 +36,10 @@ import { getExchangeDenom } from '../../selectors/DenominationSelectors'
3436import { useSelector } from '../../types/reactRedux'
3537import { EdgeAppSceneProps } from '../../types/routerTypes'
3638import { getCurrencyCodeWithAccount } from '../../util/CurrencyInfoHelpers'
39+ import { getEdgeTxSwapFromReportTxInfo } from '../../util/getEdgeTxSwapFromReportTxInfo'
3740import { matchJson } from '../../util/matchJson'
3841import { getMemoTitle } from '../../util/memoUtils'
42+ import { fetchGetTxInfo } from '../../util/reportsServer'
3943import {
4044 convertCurrencyFromExchangeRates ,
4145 convertNativeToExchange ,
@@ -86,6 +90,37 @@ const TransactionDetailsComponent = (props: Props) => {
8690 const styles = getStyles ( theme )
8791 const iconColor = useIconColor ( { pluginId : currencyInfo . pluginId , tokenId } )
8892
93+ const transactionSwapData =
94+ convertActionToSwapData ( account , transaction ) ?? transaction . swapData
95+
96+ // Query for transaction info from reports server only if the transaction is
97+ // a receive (we need to get potential swap data) or the transaction has
98+ // swap data (we need to get the status)
99+ const shouldShowTradeDetails =
100+ ! transaction . isSend || transactionSwapData != null
101+ const { data : reportsTxInfo , isLoading : isReportsTxInfoLoading } = useQuery ( {
102+ queryKey : [ 'txInfo' , transaction . txid ] ,
103+ queryFn : async ( ) => {
104+ return await fetchGetTxInfo ( transaction )
105+ } ,
106+ staleTime : query =>
107+ // Only cache if the status has resolved, otherwise we'll always consider
108+ // the data to be stale:
109+ [ 'processing' , 'pending' , undefined ] . includes ( query . state . data ?. status )
110+ ? 0 // No cache
111+ : Infinity , // Cache forever
112+ enabled : shouldShowTradeDetails ,
113+ retry : false
114+ } )
115+
116+ const swapDataFromReports = useMemo (
117+ ( ) =>
118+ reportsTxInfo == null
119+ ? null
120+ : getEdgeTxSwapFromReportTxInfo ( wallet , transaction , reportsTxInfo ) ,
121+ [ reportsTxInfo , transaction , wallet ]
122+ )
123+
89124 // Choose a default category based on metadata or the txAction
90125 const {
91126 action,
@@ -96,8 +131,7 @@ const TransactionDetailsComponent = (props: Props) => {
96131 savedData
97132 } = getTxActionDisplayInfo ( transaction , account , wallet )
98133
99- const swapData =
100- convertActionToSwapData ( account , transaction ) ?? transaction . swapData
134+ const swapData = transactionSwapData ?? swapDataFromReports
101135
102136 const thumbnailPath =
103137 useContactThumbnail ( mergedData . name ) ?? pluginIdIcons [ iconPluginId ?? '' ]
@@ -546,7 +580,11 @@ const TransactionDetailsComponent = (props: Props) => {
546580 < SwapDetailsCard
547581 swapData = { swapData }
548582 transaction = { transaction }
549- sourceWallet = { wallet }
583+ sourceWallet = {
584+ swapData . payoutWalletId === wallet . id ? undefined : wallet
585+ }
586+ reportsTxInfo = { reportsTxInfo }
587+ isReportsTxInfoLoading = { isReportsTxInfoLoading }
550588 />
551589 ) }
552590 </ EdgeAnim >
@@ -591,6 +629,7 @@ const TransactionDetailsComponent = (props: Props) => {
591629 ) }
592630 />
593631 </ EdgeAnim >
632+
594633 < EdgeAnim enter = { { type : 'fadeInDown' , distance : 140 } } >
595634 < ButtonsView
596635 layout = "column"
0 commit comments