From 6a9a40f95e06f44e9461874d7a8ea7d6662a4a94 Mon Sep 17 00:00:00 2001 From: saml33 Date: Wed, 25 Jan 2023 14:11:42 +1100 Subject: [PATCH] remove trimdecimals --- components/shared/BalancesTable.tsx | 20 +++++++++++--------- components/swap/SwapHistoryTable.tsx | 6 +++--- components/trade/PerpButtonGroup.tsx | 19 +++++++++---------- components/trade/PerpPositions.tsx | 10 +++++----- components/trade/PerpSlider.tsx | 10 +++++----- components/trade/SpotButtonGroup.tsx | 18 +++++++----------- components/trade/SpotSlider.tsx | 10 +++++----- utils/numbers.ts | 8 -------- 8 files changed, 45 insertions(+), 56 deletions(-) diff --git a/components/shared/BalancesTable.tsx b/components/shared/BalancesTable.tsx index 0a5babe2..a7a775f7 100644 --- a/components/shared/BalancesTable.tsx +++ b/components/shared/BalancesTable.tsx @@ -12,7 +12,6 @@ import { floorToDecimal, formatNumericValue, getDecimalCount, - trimDecimals, } from 'utils/numbers' import { breakpoints } from 'utils/theme' import { calculateLimitPriceForMarketOrder } from 'utils/tradeForm' @@ -250,29 +249,32 @@ const Balance = ({ bank }: { bank: Bank }) => { } let minOrderDecimals: number - let tickSize: number + let tickDecimals: number if (selectedMarket instanceof Serum3Market) { const market = group.getSerum3ExternalMarket( selectedMarket.serumMarketExternal ) minOrderDecimals = getDecimalCount(market.minOrderSize) - tickSize = getDecimalCount(market.tickSize) + tickDecimals = getDecimalCount(market.tickSize) } else { minOrderDecimals = getDecimalCount(selectedMarket.minOrderSize) - tickSize = getDecimalCount(selectedMarket.tickSize) + tickDecimals = getDecimalCount(selectedMarket.tickSize) } if (type === 'quote') { - const trimmedBalance = trimDecimals(balance, tickSize) - const baseSize = trimDecimals(trimmedBalance / price, minOrderDecimals) - const quoteSize = trimDecimals(baseSize * price, tickSize) + const floorBalance = floorToDecimal(balance, tickDecimals).toNumber() + const baseSize = floorToDecimal( + floorBalance / price, + minOrderDecimals + ).toNumber() + const quoteSize = floorToDecimal(baseSize * price, tickDecimals) set((s) => { s.tradeForm.baseSize = baseSize.toString() s.tradeForm.quoteSize = quoteSize.toString() }) } else { - const baseSize = trimDecimals(balance, minOrderDecimals) - const quoteSize = trimDecimals(baseSize * price, tickSize) + const baseSize = floorToDecimal(balance, minOrderDecimals).toNumber() + const quoteSize = floorToDecimal(baseSize * price, tickDecimals) set((s) => { s.tradeForm.baseSize = baseSize.toString() s.tradeForm.quoteSize = quoteSize.toString() diff --git a/components/swap/SwapHistoryTable.tsx b/components/swap/SwapHistoryTable.tsx index 6aaf37e9..e600df46 100644 --- a/components/swap/SwapHistoryTable.tsx +++ b/components/swap/SwapHistoryTable.tsx @@ -13,7 +13,7 @@ import { IconButton, LinkButton } from '../shared/Button' import { Transition } from '@headlessui/react' import SheenLoader from '../shared/SheenLoader' import mangoStore from '@store/mangoStore' -import { countLeadingZeros, trimDecimals } from '../../utils/numbers' +import { countLeadingZeros, floorToDecimal } from '../../utils/numbers' import useLocalStorageState from 'hooks/useLocalStorageState' import { PAGINATION_PAGE_LENGTH, PREFERRED_EXPLORER_KEY } from 'utils/constants' import Tooltip from '@components/shared/Tooltip' @@ -100,7 +100,7 @@ const SwapHistoryTable = () => { } = h const borrowAmount = loan > 0 - ? `${trimDecimals(loan, countLeadingZeros(loan) + 2)}` + ? `${floorToDecimal(loan, countLeadingZeros(loan) + 2)}` : 0 const borrowFee = swap_in_loan_origination_fee > 0 @@ -273,7 +273,7 @@ const SwapHistoryTable = () => { const borrowAmount = loan > 0 - ? `${trimDecimals(loan, countLeadingZeros(loan) + 2)}` + ? `${floorToDecimal(loan, countLeadingZeros(loan) + 2)}` : 0 const borrowFee = swap_in_loan_origination_fee > 0 diff --git a/components/trade/PerpButtonGroup.tsx b/components/trade/PerpButtonGroup.tsx index edbce738..322cf88d 100644 --- a/components/trade/PerpButtonGroup.tsx +++ b/components/trade/PerpButtonGroup.tsx @@ -4,7 +4,7 @@ import mangoStore from '@store/mangoStore' import useMangoAccount from 'hooks/useMangoAccount' import useSelectedMarket from 'hooks/useSelectedMarket' import { useCallback, useMemo, useState } from 'react' -import { trimDecimals } from 'utils/numbers' +import { floorToDecimal } from 'utils/numbers' const PerpButtonGroup = ({ minOrderDecimals, @@ -50,28 +50,27 @@ const PerpButtonGroup = ({ set((s) => { if (s.tradeForm.side === 'buy') { - s.tradeForm.quoteSize = trimDecimals(size, tickDecimals).toFixed( - tickDecimals - ) + s.tradeForm.quoteSize = floorToDecimal(size, tickDecimals).toString() if (Number(s.tradeForm.price)) { - s.tradeForm.baseSize = trimDecimals( + s.tradeForm.baseSize = floorToDecimal( size / Number(s.tradeForm.price), minOrderDecimals - ).toFixed(minOrderDecimals) + ).toString() } else { s.tradeForm.baseSize = '' } } else if (s.tradeForm.side === 'sell') { - s.tradeForm.baseSize = trimDecimals(size, minOrderDecimals).toFixed( + s.tradeForm.baseSize = floorToDecimal( + size, minOrderDecimals - ) + ).toString() if (Number(s.tradeForm.price)) { - s.tradeForm.quoteSize = trimDecimals( + s.tradeForm.quoteSize = floorToDecimal( size * Number(s.tradeForm.price), tickDecimals - ).toFixed(tickDecimals) + ).toString() } } }) diff --git a/components/trade/PerpPositions.tsx b/components/trade/PerpPositions.tsx index 1d8fb7a4..5f8960b9 100644 --- a/components/trade/PerpPositions.tsx +++ b/components/trade/PerpPositions.tsx @@ -11,7 +11,7 @@ import useMangoGroup from 'hooks/useMangoGroup' import useSelectedMarket from 'hooks/useSelectedMarket' import { useTranslation } from 'next-i18next' import { useCallback, useState } from 'react' -import { getDecimalCount, trimDecimals } from 'utils/numbers' +import { floorToDecimal, getDecimalCount } from 'utils/numbers' import { calculateLimitPriceForMarketOrder } from 'utils/tradeForm' import MarketCloseModal from './MarketCloseModal' import PerpSideBadge from './PerpSideBadge' @@ -91,10 +91,10 @@ const PerpPositions = () => { position.marketIndex ) const basePosition = position.getBasePositionUi(market) - const trimmedBasePosition = trimDecimals( + const floorBasePosition = floorToDecimal( basePosition, getDecimalCount(market.minOrderSize) - ) + ).toNumber() const isSelectedMarket = selectedMarket instanceof PerpMarket && selectedMarket.perpMarketIndex === position.marketIndex @@ -119,7 +119,7 @@ const PerpPositions = () => {

{isSelectedMarket ? ( handlePositionClick(trimmedBasePosition)} + onClick={() => handlePositionClick(floorBasePosition)} > { diff --git a/components/trade/PerpSlider.tsx b/components/trade/PerpSlider.tsx index 5ff3f531..e86d28f1 100644 --- a/components/trade/PerpSlider.tsx +++ b/components/trade/PerpSlider.tsx @@ -4,7 +4,7 @@ import mangoStore from '@store/mangoStore' import useMangoAccount from 'hooks/useMangoAccount' import useSelectedMarket from 'hooks/useSelectedMarket' import { useCallback, useMemo } from 'react' -import { trimDecimals } from 'utils/numbers' +import { floorToDecimal } from 'utils/numbers' const PerpSlider = ({ minOrderDecimals, @@ -61,20 +61,20 @@ const PerpSlider = ({ if (s.tradeForm.side === 'buy') { s.tradeForm.quoteSize = val if (Number(price)) { - s.tradeForm.baseSize = trimDecimals( + s.tradeForm.baseSize = floorToDecimal( parseFloat(val) / price, minOrderDecimals - ).toFixed(minOrderDecimals) + ).toString() } else { s.tradeForm.baseSize = '' } } else if (s.tradeForm.side === 'sell') { s.tradeForm.baseSize = val if (Number(price)) { - s.tradeForm.quoteSize = trimDecimals( + s.tradeForm.quoteSize = floorToDecimal( parseFloat(val) * price, tickDecimals - ).toFixed(tickDecimals) + ).toString() } } }) diff --git a/components/trade/SpotButtonGroup.tsx b/components/trade/SpotButtonGroup.tsx index 17af14d4..3657cb4a 100644 --- a/components/trade/SpotButtonGroup.tsx +++ b/components/trade/SpotButtonGroup.tsx @@ -3,7 +3,7 @@ import mangoStore from '@store/mangoStore' import useMangoAccount from 'hooks/useMangoAccount' import useSelectedMarket from 'hooks/useSelectedMarket' import { useCallback, useState } from 'react' -import { trimDecimals } from 'utils/numbers' +import { floorToDecimal } from 'utils/numbers' import { useSpotMarketMax } from './SpotSlider' const SpotButtonGroup = ({ @@ -29,28 +29,24 @@ const SpotButtonGroup = ({ set((s) => { if (s.tradeForm.side === 'buy') { - s.tradeForm.quoteSize = trimDecimals(size, tickDecimals).toFixed( - tickDecimals - ) + s.tradeForm.quoteSize = floorToDecimal(size, tickDecimals).toString() if (Number(s.tradeForm.price)) { - s.tradeForm.baseSize = trimDecimals( + s.tradeForm.baseSize = floorToDecimal( size / Number(s.tradeForm.price), minOrderDecimals - ).toFixed(minOrderDecimals) + ).toString() } else { s.tradeForm.baseSize = '' } } else if (s.tradeForm.side === 'sell') { - s.tradeForm.baseSize = trimDecimals(size, tickDecimals).toFixed( - minOrderDecimals - ) + s.tradeForm.baseSize = floorToDecimal(size, tickDecimals).toString() if (Number(s.tradeForm.price)) { - s.tradeForm.quoteSize = trimDecimals( + s.tradeForm.quoteSize = floorToDecimal( size * Number(s.tradeForm.price), tickDecimals - ).toFixed(tickDecimals) + ).toString() } } }) diff --git a/components/trade/SpotSlider.tsx b/components/trade/SpotSlider.tsx index 18c8f035..f7f4eac6 100644 --- a/components/trade/SpotSlider.tsx +++ b/components/trade/SpotSlider.tsx @@ -5,7 +5,7 @@ import useMangoAccount from 'hooks/useMangoAccount' import useSelectedMarket from 'hooks/useSelectedMarket' import { useCallback, useMemo } from 'react' import { GenericMarket } from 'types' -import { trimDecimals } from 'utils/numbers' +import { floorToDecimal } from 'utils/numbers' export const useSpotMarketMax = ( mangoAccount: MangoAccount | undefined, @@ -78,20 +78,20 @@ const SpotSlider = ({ if (s.tradeForm.side === 'buy') { s.tradeForm.quoteSize = val if (Number(price)) { - s.tradeForm.baseSize = trimDecimals( + s.tradeForm.baseSize = floorToDecimal( parseFloat(val) / price, minOrderDecimals - ).toFixed(minOrderDecimals) + ).toString() } else { s.tradeForm.baseSize = '' } } else if (s.tradeForm.side === 'sell') { s.tradeForm.baseSize = val if (Number(price)) { - s.tradeForm.quoteSize = trimDecimals( + s.tradeForm.quoteSize = floorToDecimal( parseFloat(val) * price, tickDecimals - ).toFixed(tickDecimals) + ).toString() } } }) diff --git a/utils/numbers.ts b/utils/numbers.ts index b282544a..3f748299 100644 --- a/utils/numbers.ts +++ b/utils/numbers.ts @@ -130,14 +130,6 @@ export const countLeadingZeros = (x: number) => { } } -export const trimDecimals = (n: number | Decimal, digits: number) => { - const number = Number(n) - const step = Math.pow(10, digits || 0) - const temp = Math.trunc(step * number) - - return temp / step -} - export const getDecimalCount = (value: number): number => { if ( !isNaN(value) &&