From 8740138ebe9c32f80a41817991d296bab84ff592 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Van Date: Mon, 30 Sep 2024 13:54:22 +0700 Subject: [PATCH 1/2] fix crash browser owallet using c --- src/helper/index.tsx | 31 ++++++++++++------- .../components/CreatePosition/index.tsx | 10 +++--- .../components/CreatePositionForm/index.tsx | 14 ++++----- .../components/NewPositionNoPool/index.tsx | 3 -- .../Pool-V3/components/PositionItem/index.tsx | 10 ++---- .../UniversalSwap/Component/HeaderTab.tsx | 3 +- 6 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/helper/index.tsx b/src/helper/index.tsx index 37f223695..38454092f 100644 --- a/src/helper/index.tsx +++ b/src/helper/index.tsx @@ -579,19 +579,26 @@ export interface GetIconInterface { height?: number; } -export const minimize = (priceUsd: number) => { - let isNeedSub = false; - const replaceItem = priceUsd.toString().replace(/(?<=\.)0+/, (m) => { - isNeedSub = m.length > 3; - return isNeedSub ? `0${m.length}` : m; - }); - - if (!isNeedSub) return formatMoney(priceUsd); +export const minimize = (priceUsd: string) => { + const regex = /^0\.0*(\d+)/; + const match = priceUsd.match(regex); + const getSubscript = (num) => String.fromCharCode(0x2080 + num); + + if (match) { + const leadingZeros = match[0].length - match[1].length - 2; + const significantDigits = match[1].slice(0, leadingZeros > 0 ? 4 : 6); + if (leadingZeros > 0) { + return ( + <> + 0.0{getSubscript(leadingZeros)} + {significantDigits} + + ); + } + return `0.${significantDigits}`; + } - const SUB_ELEMENT_LENGTH = 5; // - const DECIMALS_AFTER_SUB_LENGTH = 5; - const decimalAfterSub = replaceItem.indexOf('') + SUB_ELEMENT_LENGTH + DECIMALS_AFTER_SUB_LENGTH; - return replaceItem.slice(0, decimalAfterSub); + return numberWithCommas(Number(priceUsd), undefined, { maximumFractionDigits: 6 }); }; export const getIcon = ({ isLightTheme, type, chainId, coinGeckoId, width, height }: GetIconInterface) => { diff --git a/src/pages/Pool-V3/components/CreatePosition/index.tsx b/src/pages/Pool-V3/components/CreatePosition/index.tsx index af71bdb17..f497e137a 100644 --- a/src/pages/Pool-V3/components/CreatePosition/index.tsx +++ b/src/pages/Pool-V3/components/CreatePosition/index.tsx @@ -47,6 +47,7 @@ import { convertBalanceToBigint } from 'pages/Pool-V3/helpers/number'; import { calculateTokenAmountsWithSlippage, calcYPerXPriceBySqrtPrice } from 'pages/Pool-V3/helpers/helper'; import { numberWithCommas } from 'helper/format'; import { extractAddress } from 'pages/Pool-V3/helpers/format'; +import { minimize } from 'helper'; export type PriceInfo = { startPrice: number; @@ -725,7 +726,6 @@ const CreatePosition = () => { } }; - const handleGetTicks = () => { try { const fetchTickData = async () => { @@ -736,7 +736,7 @@ const CreatePosition = () => { poolKey: notInitPoolKey, isXtoY: isXtoY, xDecimal: tokenX.decimals, - yDecimal: tokenY.decimals, + yDecimal: tokenY.decimals }); setLiquidityData(ticksData); @@ -890,7 +890,8 @@ const CreatePosition = () => {

-

{numberWithCommas(Number(leftInputRounded), undefined, { maximumFractionDigits: 6 })}

+ {/*

{numberWithCommas(Number(leftInputRounded), undefined, { maximumFractionDigits: 6 })}

*/} +

{minimize(leftInputRounded)}

{tokenTo.name.toUpperCase()} / {tokenFrom.name.toUpperCase()}

@@ -915,7 +916,8 @@ const CreatePosition = () => {

-

{numberWithCommas(Number(rightInputRounded), undefined, { maximumFractionDigits: 6 })}

+

{minimize(rightInputRounded)}

+ {/*

{numberWithCommas(Number(rightInputRounded), undefined, { maximumFractionDigits: 6 })}

*/}

{tokenTo.name.toUpperCase()} / {tokenFrom.name.toUpperCase()}

diff --git a/src/pages/Pool-V3/components/CreatePositionForm/index.tsx b/src/pages/Pool-V3/components/CreatePositionForm/index.tsx index 249421fbc..a093899f1 100644 --- a/src/pages/Pool-V3/components/CreatePositionForm/index.tsx +++ b/src/pages/Pool-V3/components/CreatePositionForm/index.tsx @@ -1126,11 +1126,9 @@ const CreatePositionForm: FC = ({

Current Price:

-

+
+ 1 {tokenFrom.name} = {minimize(midPrice.x.toString())} {tokenTo.name} +

@@ -1142,7 +1140,8 @@ const CreatePositionForm: FC = ({

-

{numberWithCommas(Number(leftInputRounded), undefined, { maximumFractionDigits: 6 })}

+

{minimize(leftInputRounded)}

+ {/*

{numberWithCommas(Number(leftInputRounded), undefined, { maximumFractionDigits: 6 })}

*/}

{tokenTo.name.toUpperCase()} / {tokenFrom.name.toUpperCase()}

@@ -1167,7 +1166,8 @@ const CreatePositionForm: FC = ({

-

{numberWithCommas(Number(rightInputRounded), undefined, { maximumFractionDigits: 6 })}

+ {/*

{numberWithCommas(Number(rightInputRounded), undefined, { maximumFractionDigits: 6 })}

*/} +

{minimize(rightInputRounded)}

{tokenTo.name.toUpperCase()} / {tokenFrom.name.toUpperCase()}

diff --git a/src/pages/Pool-V3/components/NewPositionNoPool/index.tsx b/src/pages/Pool-V3/components/NewPositionNoPool/index.tsx index dbe3c9d53..3e8137253 100644 --- a/src/pages/Pool-V3/components/NewPositionNoPool/index.tsx +++ b/src/pages/Pool-V3/components/NewPositionNoPool/index.tsx @@ -246,7 +246,6 @@ const NewPositionNoPool = ({ onLeftInputChange((floatValue || 0).toString()); }} onBlur={() => { - const tokenXDecimals = isXtoY ? fromToken.decimals : toToken.decimals; const tokenYDecimals = isXtoY ? toToken.decimals : fromToken.decimals; @@ -260,7 +259,6 @@ const NewPositionNoPool = ({ Number(nearestTickIndex(+leftInput, tickSpacing, isXtoY, tokenXDecimals, tokenYDecimals)) ); - changeRangeHandler(newLeft, rightRange); }} /> @@ -322,7 +320,6 @@ const NewPositionNoPool = ({ onRightInputChange((floatValue || 0).toString()); }} onBlur={() => { - const tokenXDecimals = isXtoY ? fromToken.decimals : toToken.decimals; const tokenYDecimals = isXtoY ? toToken.decimals : fromToken.decimals; diff --git a/src/pages/Pool-V3/components/PositionItem/index.tsx b/src/pages/Pool-V3/components/PositionItem/index.tsx index 1982e5f5a..7bc6177e6 100644 --- a/src/pages/Pool-V3/components/PositionItem/index.tsx +++ b/src/pages/Pool-V3/components/PositionItem/index.tsx @@ -271,15 +271,9 @@ const PositionItem = ({ position }) => {

Price Range

- +
{minimize(xToY ? min : 1 / max)}
{' - '} - +
{minimize(xToY ? max : 1 / min)}
{/* {numberWithCommas(Number(formatNumbers(undefined)(xToY ? min : 1 / max)), undefined, { diff --git a/src/pages/UniversalSwap/Component/HeaderTab.tsx b/src/pages/UniversalSwap/Component/HeaderTab.tsx index c2998aa66..297add00a 100644 --- a/src/pages/UniversalSwap/Component/HeaderTab.tsx +++ b/src/pages/UniversalSwap/Component/HeaderTab.tsx @@ -112,8 +112,7 @@ export const UsdPrice = ({ const headerTabSimple = () => { return (
- - +
{minimize(priceUsd.toString())}
Date: Mon, 30 Sep 2024 15:25:27 +0700 Subject: [PATCH 2/2] fix crash browser owallet using c --- src/pages/Pool-V3/components/PositionItem/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/Pool-V3/components/PositionItem/index.tsx b/src/pages/Pool-V3/components/PositionItem/index.tsx index 7bc6177e6..f623db1fe 100644 --- a/src/pages/Pool-V3/components/PositionItem/index.tsx +++ b/src/pages/Pool-V3/components/PositionItem/index.tsx @@ -271,9 +271,9 @@ const PositionItem = ({ position }) => {

Price Range

-
{minimize(xToY ? min : 1 / max)}
+ {minimize((xToY ? min : 1 / max).toString())} {' - '} -
{minimize(xToY ? max : 1 / min)}
+ {minimize((xToY ? max : 1 / min).toString())} {/* {numberWithCommas(Number(formatNumbers(undefined)(xToY ? min : 1 / max)), undefined, {