From 76b12d70b653a99d05e01d0e3396e8441fa07cc0 Mon Sep 17 00:00:00 2001 From: Yudho <149754501+yudhomax@users.noreply.github.com> Date: Fri, 11 Oct 2024 20:47:14 +0530 Subject: [PATCH] feat(app): show deposit value attached with the receipt in txns page (#545) --- .../src/components/Transactions/Receipt.tsx | 9 ++- .../Transactions/Receipts/ReceiptRow.tsx | 59 ++++++++++++++++++- .../Receipts/ReceiptSummaryRow.tsx | 22 +++---- apps/app/src/pages/txns/[hash].tsx | 15 +++-- 4 files changed, 85 insertions(+), 20 deletions(-) diff --git a/apps/app/src/components/Transactions/Receipt.tsx b/apps/app/src/components/Transactions/Receipt.tsx index e263d2bb..d8f00d9e 100644 --- a/apps/app/src/components/Transactions/Receipt.tsx +++ b/apps/app/src/components/Transactions/Receipt.tsx @@ -9,10 +9,15 @@ interface Props { txn: TransactionInfo; rpcTxn: RPCTransactionInfo; loading: boolean; + statsData: { + stats: Array<{ + near_price: string; + }>; + }; } const Receipt = (props: Props) => { - const { rpcTxn, txn, loading } = props; + const { rpcTxn, txn, loading, statsData } = props; const [receipt, setReceipt] = useState(null); @@ -99,7 +104,7 @@ const Receipt = (props: Props) => { ) : ( - + )} ); diff --git a/apps/app/src/components/Transactions/Receipts/ReceiptRow.tsx b/apps/app/src/components/Transactions/Receipts/ReceiptRow.tsx index 8604ae4e..2050876a 100644 --- a/apps/app/src/components/Transactions/Receipts/ReceiptRow.tsx +++ b/apps/app/src/components/Transactions/Receipts/ReceiptRow.tsx @@ -1,5 +1,10 @@ import Question from '@/components/Icons/Question'; -import { convertToMetricPrefix, localFormat, yoctoToNear } from '@/utils/libs'; +import { + convertToMetricPrefix, + fiatValue, + localFormat, + yoctoToNear, +} from '@/utils/libs'; import { ReceiptsPropsInfo } from '@/utils/types'; import { Tooltip } from '@reach/tooltip'; import useTranslation from 'next-translate/useTranslation'; @@ -10,20 +15,28 @@ import { useEffect, useRef, useState } from 'react'; import useRpc from '@/hooks/useRpc'; import TxnsReceiptStatus from '@/components/common/TxnsReceiptStatus'; import useHash from '@/hooks/useHash'; +import { networkId } from '@/utils/config'; interface Props { receipt: ReceiptsPropsInfo | any; borderFlag?: boolean; loading: boolean; + statsData: { + stats: Array<{ + near_price: string; + }>; + }; } const ReceiptRow = (props: Props) => { - const { receipt, borderFlag, loading } = props; + const { receipt, borderFlag, loading, statsData } = props; const { t } = useTranslation(); const [block, setBlock] = useState<{ height: string } | null>(null); const { getBlockDetails } = useRpc(); const [pageHash] = useHash(); + const currentPrice = statsData?.stats?.[0]?.near_price || 0; + const lastBlockHash = useRef(null); const rowRef = useRef(null); @@ -301,6 +314,41 @@ const ReceiptRow = (props: Props) => { '' )} + +
+
+ +
+ +
+
+ Value +
+ {!receipt || loading ? ( +
+ + + +
+ ) : ( +
+ {receipt && receipt?.actions[0]?.args?.deposit + ? yoctoToNear(receipt?.actions[0]?.args?.deposit, true) + : receipt?.actions[0]?.args?.deposit ?? '0'}{' '} + Ⓝ + {currentPrice && networkId === 'mainnet' + ? ` ($${fiatValue( + yoctoToNear(receipt?.actions[0]?.args?.deposit ?? 0, false), + currentPrice, + )})` + : ''} +
+ )} +
+
{ {receipt?.outcome?.outgoing_receipts?.map((rcpt: any) => (
- +
))} diff --git a/apps/app/src/components/Transactions/Receipts/ReceiptSummaryRow.tsx b/apps/app/src/components/Transactions/Receipts/ReceiptSummaryRow.tsx index 06417bea..f4e24bfe 100644 --- a/apps/app/src/components/Transactions/Receipts/ReceiptSummaryRow.tsx +++ b/apps/app/src/components/Transactions/Receipts/ReceiptSummaryRow.tsx @@ -35,16 +35,16 @@ const ReceiptSummaryRow = (props: Props) => { const getGasAttached = (actions: Action[]): string => { const gasAttached = actions - .map((action) => action.args) + .map((action) => action?.args) .filter( (args): args is FunctionCallActionView['FunctionCall'] => 'gas' in args, ); - if (gasAttached.length === 0) { + if (gasAttached?.length === 0) { return '0'; } - return gasAttached.reduce( + return gasAttached?.reduce( (acc, args) => Big(acc || '0') .plus(args.gas) @@ -59,8 +59,8 @@ const ReceiptSummaryRow = (props: Props) => { const isSuccess = status && (('SuccessValue' in status && - status.SuccessValue !== null && - status.SuccessValue !== undefined) || + status?.SuccessValue !== null && + status?.SuccessValue !== undefined) || 'SuccessReceiptId' in status); return ( @@ -78,7 +78,7 @@ const ReceiptSummaryRow = (props: Props) => { > {' '} {receipt.id} @@ -86,7 +86,7 @@ const ReceiptSummaryRow = (props: Props) => {
- {formatActionKind(action.action_kind)} + {formatActionKind(action?.action_kind)} {action.args?.method_name} @@ -128,13 +128,13 @@ const ReceiptSummaryRow = (props: Props) => { - {action.args?.deposit - ? yoctoToNear(action.args?.deposit, true) - : action.args?.deposit ?? '0'}{' '} + {action?.args?.deposit + ? yoctoToNear(action?.args?.deposit, true) + : action?.args?.deposit ?? '0'}{' '} Ⓝ {currentPrice && networkId === 'mainnet' ? ` ($${fiatValue( - yoctoToNear(action.args?.deposit ?? 0, false), + yoctoToNear(action?.args?.deposit ?? 0, false), currentPrice, )})` : ''} diff --git a/apps/app/src/pages/txns/[hash].tsx b/apps/app/src/pages/txns/[hash].tsx index ac2b5413..755bd612 100644 --- a/apps/app/src/pages/txns/[hash].tsx +++ b/apps/app/src/pages/txns/[hash].tsx @@ -154,6 +154,7 @@ const Txn = ({ const { transactionStatus, getBlockDetails } = useRpc(); const rpcUrl: string = useRpcStore((state) => state.rpc); const switchRpc: () => void = useRpcStore((state) => state.switchRpc); + const [allRpcProviderError, setAllRpcProviderError] = useState(false); const requestSignInWithWallet = useAuthStore( (store) => store.requestSignInWithWallet, @@ -193,7 +194,13 @@ const Txn = ({ useEffect(() => { if (rpcError) { - switchRpc(); + try { + switchRpc(); + } catch (error) { + setRpcError(true); + setAllRpcProviderError(true); + console.error('Failed to switch RPC:', error); + } } }, [rpcError, switchRpc]); @@ -349,13 +356,12 @@ const Txn = ({
{/* */} - {rpcError && error ? ( + {rpcError && (error || allRpcProviderError) ? (
} - message="Sorry, we are unable to locate this transaction hash. Please try using a - different RPC." + message="Sorry, we are unable to locate this transaction hash. Please try again later." mutedText={hash || ''} />
@@ -427,6 +433,7 @@ const Txn = ({ txn={txn ? txn : rpcData} rpcTxn={rpcTxn} loading={rpcError || !rpcTxn} + statsData={statsData} />