diff --git a/screens/Trading/components/ConfirmMobile.tsx b/screens/Trading/components/ConfirmMobile.tsx index 5cd155a3..1886ecb5 100644 --- a/screens/Trading/components/ConfirmMobile.tsx +++ b/screens/Trading/components/ConfirmMobile.tsx @@ -1,7 +1,7 @@ import { useState, createContext, useMemo } from "react"; import { Modal as MUIModal, Box, useTheme } from "@mui/material"; import { BeatLoader } from "react-spinners"; -import { useAppDispatch } from "../../../redux/hooks"; +import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; import { Wrapper } from "../../../components/Modal/style"; import { DEFAULT_POSITION } from "../../../utils/config"; import { CloseIcon } from "../../../components/Modal/svg"; @@ -16,12 +16,18 @@ import { RedSolidSubmitButton as RedSolidButton, } from "../../../components/Modal/button"; import { shrinkToken } from "../../../store"; +import { beautifyPrice } from "../../../utils/beautyNumbet"; + +const getTokenSymbolOnly = (assetId) => { + return assetId === "wNEAR" ? "NEAR" : assetId || ""; +}; export const ModalContext = createContext(null) as any; const ConfirmMobile = ({ open, onClose, action, confirmInfo }) => { const dispatch = useAppDispatch(); const theme = useTheme(); const [selectedCollateralType, setSelectedCollateralType] = useState(DEFAULT_POSITION); + const { ReduxcategoryAssets1, ReduxcategoryAssets2 } = useAppSelector((state) => state.category); const actionShowRedColor = action === "Long"; const [isDisabled, setIsDisabled] = useState(false); const { marginConfigTokens } = useMarginConfigToken(); @@ -35,6 +41,7 @@ const ConfirmMobile = ({ open, onClose, action, confirmInfo }) => { ? getAssetById(confirmInfo.longOutputName?.token_id) : getAssetById(confirmInfo.longInputName?.token_id), ); + const cateSymbol = getTokenSymbolOnly(ReduxcategoryAssets1.metadata.symbol); const confirmOpenPosition = async () => { // console.log( // action == "Long" @@ -128,7 +135,7 @@ const ConfirmMobile = ({ open, onClose, action, confirmInfo }) => { actionShowRedColor ? "bg-primary text-primary" : "bg-red-50 text-red-50" }`} > - {action} NEAR {confirmInfo.rangeMount}X + {action} {cateSymbol} {confirmInfo.rangeMount}X
@@ -163,8 +170,10 @@ const ConfirmMobile = ({ open, onClose, action, confirmInfo }) => { {/*
${confirmInfo.entryPrice || "-"}
*/}
$ - {confirmInfo.tokenInAmount / - Number(shrinkToken(confirmInfo.estimateData?.min_amount_out, decimalsP))} + {beautifyPrice( + confirmInfo.tokenInAmount / + Number(shrinkToken(confirmInfo.estimateData?.min_amount_out, decimalsP)), + )}
@@ -189,7 +198,7 @@ const ConfirmMobile = ({ open, onClose, action, confirmInfo }) => {
Liq. Price
-
${confirmInfo.LiqPrice}
+
${beautifyPrice(confirmInfo.LiqPrice)}
@@ -250,7 +259,7 @@ const ConfirmMobile = ({ open, onClose, action, confirmInfo }) => { {isDisabled ? ( ) : ( - ` Confirm ${action} NEAR ${confirmInfo.rangeMount}X` + ` Confirm ${action} ${cateSymbol} ${confirmInfo.rangeMount}X` )} ) : ( @@ -262,7 +271,7 @@ const ConfirmMobile = ({ open, onClose, action, confirmInfo }) => { {isDisabled ? ( ) : ( - ` Confirm ${action} NEAR ${confirmInfo.rangeMount}X` + ` Confirm ${action} ${cateSymbol} ${confirmInfo.rangeMount}X` )} )} diff --git a/screens/Trading/components/TradingOperate.tsx b/screens/Trading/components/TradingOperate.tsx index 00cbf0cf..f6bad4f7 100644 --- a/screens/Trading/components/TradingOperate.tsx +++ b/screens/Trading/components/TradingOperate.tsx @@ -20,6 +20,7 @@ import { YellowSolidSubmitButton as YellowSolidButton, RedSolidSubmitButton as RedSolidButton, } from "../../../components/Modal/button"; +import { beautifyPrice } from "../../../utils/beautyNumbet"; // main components const TradingOperate = () => { @@ -63,6 +64,14 @@ const TradingOperate = () => { const balance = useAppSelector(getAccountBalance); const accountId = useAppSelector(getAccountId); + const getTokenSymbol = (assetId) => { + if (!assetId?.token_id) return ""; + const asset = assets.data[assetId.token_id]; + return asset?.metadata.symbol === "wNEAR" ? "NEAR" : asset?.metadata.symbol || ""; + }; + const getTokenSymbolOnly = (assetId) => { + return assetId === "wNEAR" ? "NEAR" : assetId || ""; + }; // pools const { simplePools, stablePools, stablePoolsDetail } = usePoolsData(); @@ -115,6 +124,7 @@ const TradingOperate = () => { // }, 200); // }; + const cateSymbol = getTokenSymbolOnly(ReduxcategoryAssets1.metadata.symbol); // slippageTolerance change ecent useEffect(() => { dispatch(setSlippageToleranceFromRedux(0.5)); @@ -394,7 +404,7 @@ const TradingOperate = () => {
handleTabClick("long")}> - Long NEAR + Long {cateSymbol}
{ } onClick={() => handleTabClick("short")} > - Short NEAR + Short {cateSymbol}
{/* slip start */} @@ -490,23 +500,26 @@ const TradingOperate = () => {
-
-
Minimum received
-
- {Number(toInternationalCurrencySystem_number(longOutput)) * - (1 - slippageTolerance / 100)}{" "} - NEAR -
-
Position Size
- {toInternationalCurrencySystem_number(longOutput)} NEAR + {toInternationalCurrencySystem_number(longOutput)} {cateSymbol} (${toInternationalCurrencySystem_number(longOutputUsd)})
+ +
+
Minimum received
+
+ {beautifyPrice( + Number(toInternationalCurrencySystem_number(longOutput)) * + (1 - slippageTolerance / 100), + )}{" "} + {cateSymbol} +
+
Liq. Price
${toInternationalCurrencySystem_number(LiqPrice)}
@@ -515,7 +528,7 @@ const TradingOperate = () => {
Fee

{formatDecimal(Fee.fee)}

- NEAR + {cateSymbol} (${formatDecimal(Fee.fee * (Fee.price || 0))}) @@ -570,7 +583,7 @@ const TradingOperate = () => { disabled={isDisabled} onClick={handleConfirmButtonClick} > - Long NEAR {rangeMount}x + Long {cateSymbol} {rangeMount}x {isConfirmModalOpen && ( {
-
-
Minimum received
-
- {Number(toInternationalCurrencySystem_number(shortOutput)) * - (1 - slippageTolerance / 100)}{" "} - NEAR -
-
Position Size
- {toInternationalCurrencySystem_number(shortOutput)} NEAR + {toInternationalCurrencySystem_number(shortOutput)} {cateSymbol} (${toInternationalCurrencySystem_number(shortOutputUsd)})
+
+
Minimum received
+
+ {beautifyPrice( + Number(toInternationalCurrencySystem_number(shortOutput)) * + (1 - slippageTolerance / 100), + )}{" "} + {cateSymbol} +
+
Liq. Price
${toInternationalCurrencySystem_number(LiqPrice)}
@@ -660,7 +675,7 @@ const TradingOperate = () => {
Fee

{formatDecimal(Fee.fee)}

- NEAR + {cateSymbol} (${formatDecimal(Fee.fee * (Fee.price || 0))}) @@ -715,7 +730,7 @@ const TradingOperate = () => { disabled={isDisabled} onClick={handleConfirmButtonClick} > - Short NEAR {rangeMount}x + Short {cateSymbol} {rangeMount}x {isConfirmModalOpen && ( = ({ tokenList, type, setOwnBanl
{selectedItem?.metadata?.symbol === "wNEAR" ? "NEAR" : selectedItem?.metadata?.symbol}
- + {type === "cate2" && }
{type === "cate2" && ( @@ -138,7 +138,7 @@ const TradingToken: React.FC = ({ tokenList, type, setOwnBanl
)} {/* */} - {showModal && ( + {showModal && type === "cate2" && (
{ const { marginAccountList, parseTokenValue, getAssetDetails, getAssetById } = useMarginAccount(); const { categoryAssets1, categoryAssets2 } = useMarginConfigToken(); const { ReduxcategoryAssets1, ReduxcategoryAssets2 } = useAppSelector((state) => state.category); - const router = useRouter(); const { id }: any = router.query; const dispatch = useAppDispatch(); @@ -178,9 +177,9 @@ const Trading = () => { ? "NEAR" : currentTokenCate1?.metadata?.symbol}

- + {/* */}
- {showPopupCate1 && ( + {/* {showPopupCate1 && (
handleMouseEnter("1")} onMouseLeave={() => handleMouseLeave("1")} @@ -209,7 +208,7 @@ const Trading = () => {
))}
- )} + )} */}
{/* cate2 */}
diff --git a/utils/beautyNumbet.tsx b/utils/beautyNumbet.tsx new file mode 100644 index 00000000..ffd0813e --- /dev/null +++ b/utils/beautyNumbet.tsx @@ -0,0 +1,82 @@ +export const beautifyPrice = (num: number) => { + // 处理科学计数法的数字 + let numStr = num.toString(); + if (numStr.includes("e")) { + const [base, exp] = numStr.split("e"); + const expNum = parseInt(exp, 10); + if (expNum < 0) { + const absExp = Math.abs(expNum); + numStr = `0.${"0".repeat(absExp - 1)}${base.replace(".", "")}`; + } else { + numStr = base.replace(".", "") + "0".repeat(expNum); + } + } + + const arr = numStr.split("."); + const integerPart = arr[0]; + const decimalPart = arr[1] || ""; + + if (!decimalPart) { + // 整数部分如果超过5位,只显示前5位 + const digits = integerPart.slice(0, 5); + // 如果最后一位是0,则只取前4位 + return ( + + {digits.endsWith("0") ? digits.slice(0, 4) : digits} + + ); + } + + if (+integerPart === 0) { + const nonZeroIndex = decimalPart.split("").findIndex((n) => +n !== 0); + if (nonZeroIndex <= 1) { + // 取5位有效数字,但如果最后一位是0则只取4位 + let significantDigits = decimalPart.replace(/0+$/, "").slice(0, 5); + if (significantDigits.endsWith("0")) { + significantDigits = significantDigits.slice(0, 4); + } + return ( + + {`0.${significantDigits}`} + + ); + } + const nonZeroPart = decimalPart.substring(nonZeroIndex); + let digits = nonZeroPart.slice(0, 4); + if (digits.endsWith("0")) { + digits = digits.slice(0, 3); + } + return ( + + 0.0 + {nonZeroIndex} + {digits} + + ); + } + + // 整数部分不为0时 + const floatPartLength = Math.max(5 - integerPart.length, 2); + let formattedDecimal = decimalPart.slice(0, floatPartLength).replace(/0+$/, ""); + + // 如果整数加小数的最后一位是0,则去掉 + const fullNumber = integerPart + (formattedDecimal || ""); + if (fullNumber.endsWith("0")) { + if (formattedDecimal) { + formattedDecimal = formattedDecimal.slice(0, -1); + } else { + return ( + + {integerPart.slice(0, -1)} + + ); + } + } + + return ( + + {integerPart} + {formattedDecimal ? `.${formattedDecimal}` : ""} + + ); +};