import {
ExplorerDataType,
formatCurrencyAmount,
getExplorerLink,
truncateDecimalValue,
} from "../../utils/";
import useMappedChainData from "../../hooks/useMappedChainData";
import { ArrowRight, CheckCircle, ExternalLink } from "react-feather";
import { ReactNode, useContext } from "react";
import { CustomizeContext } from "../../providers/CustomizeProvider";
import { Spinner } from "../common/Spinner";
// Component that shows the steps involved in the route in detail.
// Each Step is shown individually.
export const TxStepDetails = ({
activeRoute,
currentTxIndex,
completed,
inProgress,
forReview,
txData,
refuel,
}: {
activeRoute: any;
currentTxIndex?: number;
completed?: boolean;
inProgress?: boolean;
forReview?: boolean;
txData?: any;
refuel?: any;
}) => {
const mappedChainData = useMappedChainData();
return (
{activeRoute?.userTxs?.map((tx, txIndex) => {
const txComplete =
tx?.userTxStatus === "completed" ||
txIndex < currentTxIndex ||
completed;
const currentTx = currentTxIndex === tx?.userTxIndex;
const _txHash = txData?.[txIndex]?.txHash ?? txData?.[txIndex]?.hash;
const url = _txHash
? getExplorerLink(
mappedChainData?.[tx?.chainId]?.explorers[0],
_txHash,
ExplorerDataType.TRANSACTION
)
: null;
// function to return token details -
// @returns - amount, chain id, symbol and protocol name.
const getTxDetail = (stepId: number, toAsset: boolean) => {
const step = stepId === null ? tx : tx?.steps?.[stepId];
return {
amount: formatCurrencyAmount(
step?.[toAsset ? "toAmount" : "fromAmount"],
step?.[toAsset ? "toAsset" : "fromAsset"]?.decimals
),
chainId: step?.[toAsset ? "toAsset" : "fromAsset"]?.chainId,
symbol: step?.[toAsset ? "toAsset" : "fromAsset"]?.symbol,
protocolName: step?.protocol?.displayName,
};
};
if (tx?.userTxType === "fund-movr") {
const isSwap = tx?.steps?.length > 1;
const swapSrc = getTxDetail(0, false);
const swapDest = getTxDetail(0, true);
const bridgeSrc = getTxDetail(isSwap ? 1 : 0, false);
const bridgeDest = getTxDetail(isSwap ? 1 : 0, true);
// Destination Transaction Urls
const destinationTxUrl = tx?.destinationTxHash
? getExplorerLink(
mappedChainData?.[bridgeDest?.chainId]?.explorers[0],
tx.destinationTxHash,
ExplorerDataType.TRANSACTION
)
: null;
const refuelDestinationTxUrl = tx?.refuelDestinationHash
? getExplorerLink(
mappedChainData?.[bridgeDest?.chainId]?.explorers[0],
tx.refuelDestinationHash,
ExplorerDataType.TRANSACTION
)
: null;
const refuelSrc = {
amount: formatCurrencyAmount(
refuel?.fromAmount,
refuel?.fromAsset?.decimals
),
chainId: refuel?.fromChainId,
symbol: refuel?.fromAsset?.symbol,
};
const refuelDest = {
amount: formatCurrencyAmount(
refuel?.toAmount,
refuel?.toAsset?.decimals
),
chainId: refuel?.toChainId,
symbol: refuel?.toAsset?.symbol,
};
return (
{isSwap ? (
{Number(swapSrc?.amount).toFixed(3)} {swapSrc?.symbol} for{" "}
{Number(swapDest?.amount).toFixed(3)} {swapDest?.symbol}{" "}
via {swapDest?.protocolName} on{" "}
{mappedChainData?.[swapSrc.chainId]?.name}
{Number(bridgeSrc?.amount).toFixed(3)} {bridgeSrc?.symbol}{" "}
on {mappedChainData?.[bridgeSrc?.chainId]?.name} to{" "}
{Number(bridgeDest?.amount).toFixed(3)}{" "}
{bridgeDest?.symbol} on{" "}
{mappedChainData?.[bridgeDest?.chainId]?.name} via{" "}
{bridgeSrc?.protocolName} bridge.{" "}
{destinationTxUrl && (
Dest tx{" "}
)}
{refuel && (
For Refuel :{" "}
{Number(refuelSrc?.amount).toFixed(3)}{" "}
{refuelSrc?.symbol} on{" "}
{mappedChainData?.[refuelSrc?.chainId]?.name} to{" "}
{truncateDecimalValue(Number(refuelDest?.amount), 3)}{" "}
{refuelDest?.symbol} on{" "}
{mappedChainData?.[refuelDest?.chainId]?.name} via{" "}
Refuel.{" "}
{refuelDestinationTxUrl && (
Dest tx{" "}
)}
)}
) : (
{Number(bridgeSrc?.amount).toFixed(3)} {bridgeSrc?.symbol}{" "}
on {mappedChainData?.[bridgeSrc?.chainId]?.name} to{" "}
{Number(bridgeDest?.amount).toFixed(3)}{" "}
{bridgeDest?.symbol} on{" "}
{mappedChainData?.[bridgeDest?.chainId]?.name} via{" "}
{bridgeSrc?.protocolName} bridge.{" "}
{destinationTxUrl && (
Dest tx{" "}
)}
{/* Refuel statement */}
{refuel && (
For Refuel :{" "}
{Number(refuelSrc?.amount).toFixed(3)}{" "}
{refuelSrc?.symbol} on{" "}
{mappedChainData?.[refuelSrc?.chainId]?.name} to{" "}
{truncateDecimalValue(Number(refuelDest?.amount), 3)}{" "}
{refuelDest?.symbol} on{" "}
{mappedChainData?.[refuelDest?.chainId]?.name} via{" "}
Refuel.{" "}
{refuelDestinationTxUrl && (
Dest tx{" "}
)}
)}
)}
);
} else if (tx?.userTxType === "dex-swap") {
const swapSrc = getTxDetail(null, false);
const swapDest = getTxDetail(null, true);
return (
{Number(swapSrc?.amount).toFixed(3)} {swapSrc?.symbol} for{" "}
{Number(swapDest?.amount).toFixed(3)} {swapDest?.symbol} via{" "}
{swapDest?.protocolName} on{" "}
{mappedChainData?.[swapDest?.chainId]?.name}
);
} else if (tx?.userTxType === "claim") {
return (
Claim {tx?.toAsset?.symbol} on{" "}
{mappedChainData?.[tx?.chainId].name}
);
}
})}
);
};
// Component to show one tx in detail based on its type.
const TxStep = ({
label,
children,
complete = false,
currentTx = false,
url = null,
inProgress = false,
forReview = false,
bridgeTx = false,
txHash,
}: {
label: string;
children: ReactNode;
complete?: boolean;
currentTx?: boolean;
url?: string | null;
inProgress?: boolean;
forReview?: boolean;
bridgeTx?: boolean;
txHash?: string;
}) => {
const active = complete || currentTx;
const customSettings = useContext(CustomizeContext);
const { borderRadius } = customSettings.customization;
return (