Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix crash browser owallet #973

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/helper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<sub>${m.length}</sub>` : 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<span style={{ fontSize: '1.6em', verticalAlign: 'sub' }}>{getSubscript(leadingZeros)}</span>
{significantDigits}
</>
);
}
return `0.${significantDigits}`;
}

const SUB_ELEMENT_LENGTH = 5; // </sub>
const DECIMALS_AFTER_SUB_LENGTH = 5;
const decimalAfterSub = replaceItem.indexOf('</sub>') + 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) => {
Expand Down
10 changes: 6 additions & 4 deletions src/pages/Pool-V3/components/CreatePosition/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -725,7 +726,6 @@ const CreatePosition = () => {
}
};


const handleGetTicks = () => {
try {
const fetchTickData = async () => {
Expand All @@ -736,7 +736,7 @@ const CreatePosition = () => {
poolKey: notInitPoolKey,
isXtoY: isXtoY,
xDecimal: tokenX.decimals,
yDecimal: tokenY.decimals,
yDecimal: tokenY.decimals
});

setLiquidityData(ticksData);
Expand Down Expand Up @@ -890,7 +890,8 @@ const CreatePosition = () => {
</div>
<div className={styles.minMaxPriceValue}>
<p>
<p>{numberWithCommas(Number(leftInputRounded), undefined, { maximumFractionDigits: 6 })}</p>
{/* <p>{numberWithCommas(Number(leftInputRounded), undefined, { maximumFractionDigits: 6 })}</p> */}
<p>{minimize(leftInputRounded)}</p>
haunv3 marked this conversation as resolved.
Show resolved Hide resolved
<p className={styles.pair}>
{tokenTo.name.toUpperCase()} / {tokenFrom.name.toUpperCase()}
</p>
Expand All @@ -915,7 +916,8 @@ const CreatePosition = () => {
</div>
<div className={styles.minMaxPriceValue}>
<p>
<p>{numberWithCommas(Number(rightInputRounded), undefined, { maximumFractionDigits: 6 })}</p>
<p>{minimize(rightInputRounded)}</p>
{/* <p>{numberWithCommas(Number(rightInputRounded), undefined, { maximumFractionDigits: 6 })}</p> */}
<p className={styles.pair}>
{tokenTo.name.toUpperCase()} / {tokenFrom.name.toUpperCase()}
</p>
Expand Down
14 changes: 7 additions & 7 deletions src/pages/Pool-V3/components/CreatePositionForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1126,11 +1126,9 @@ const CreatePositionForm: FC<CreatePoolFormProps> = ({
<div className={styles.currentPrice}>
<p>Current Price:</p>
<p>
<div
dangerouslySetInnerHTML={{
__html: `${`1 ${tokenFrom.name} = `}${minimize(midPrice.x)} ${tokenTo.name}`
}}
/>
<div>
1 {tokenFrom.name} = {minimize(midPrice.x.toString())} {tokenTo.name}
</div>
</p>
</div>

Expand All @@ -1142,7 +1140,8 @@ const CreatePositionForm: FC<CreatePoolFormProps> = ({
</div>
<div className={styles.minMaxPriceValue}>
<p>
<p>{numberWithCommas(Number(leftInputRounded), undefined, { maximumFractionDigits: 6 })}</p>
<p>{minimize(leftInputRounded)}</p>
{/* <p>{numberWithCommas(Number(leftInputRounded), undefined, { maximumFractionDigits: 6 })}</p> */}
<p className={styles.pair}>
{tokenTo.name.toUpperCase()} / {tokenFrom.name.toUpperCase()}
</p>
Expand All @@ -1167,7 +1166,8 @@ const CreatePositionForm: FC<CreatePoolFormProps> = ({
</div>
<div className={styles.minMaxPriceValue}>
<p>
<p>{numberWithCommas(Number(rightInputRounded), undefined, { maximumFractionDigits: 6 })}</p>
{/* <p>{numberWithCommas(Number(rightInputRounded), undefined, { maximumFractionDigits: 6 })}</p> */}
<p>{minimize(rightInputRounded)}</p>
<p className={styles.pair}>
{tokenTo.name.toUpperCase()} / {tokenFrom.name.toUpperCase()}
</p>
Expand Down
3 changes: 0 additions & 3 deletions src/pages/Pool-V3/components/NewPositionNoPool/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -260,7 +259,6 @@ const NewPositionNoPool = ({
Number(nearestTickIndex(+leftInput, tickSpacing, isXtoY, tokenXDecimals, tokenYDecimals))
);


changeRangeHandler(newLeft, rightRange);
}}
/>
Expand Down Expand Up @@ -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;

Expand Down
10 changes: 2 additions & 8 deletions src/pages/Pool-V3/components/PositionItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,9 @@ const PositionItem = ({ position }) => {
<div className={styles.info}>
<div className={styles.item}>
<p>Price Range</p>
<span
className={styles.value}
dangerouslySetInnerHTML={{ __html: `\$${minimize(xToY ? min : 1 / max)}` }}
/>
<span className={styles.value}>{minimize((xToY ? min : 1 / max).toString())}</span>
{' - '}
<span
className={styles.value}
dangerouslySetInnerHTML={{ __html: `\$${minimize(xToY ? max : 1 / min)}` }}
/>
<span className={styles.value}>{minimize((xToY ? max : 1 / min).toString())}</span>

<span className={styles.value}>
{/* {numberWithCommas(Number(formatNumbers(undefined)(xToY ? min : 1 / max)), undefined, {
Expand Down
3 changes: 1 addition & 2 deletions src/pages/UniversalSwap/Component/HeaderTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ export const UsdPrice = ({
const headerTabSimple = () => {
return (
<div>
<span dangerouslySetInnerHTML={{ __html: `\$${!priceUsd ? '--' : minimize(priceUsd)}` }} />

<div>{minimize(priceUsd.toString())}</div>
<span
className={cx('percent', isIncrementUsd ? 'increment' : 'decrement', {
hidePercent: chartTokenType === ChartTokenType.Volume
Expand Down
Loading