remove trimdecimals

This commit is contained in:
saml33 2023-01-25 14:11:42 +11:00
parent f8f022fe9a
commit 6a9a40f95e
8 changed files with 45 additions and 56 deletions

View File

@ -12,7 +12,6 @@ import {
floorToDecimal, floorToDecimal,
formatNumericValue, formatNumericValue,
getDecimalCount, getDecimalCount,
trimDecimals,
} from 'utils/numbers' } from 'utils/numbers'
import { breakpoints } from 'utils/theme' import { breakpoints } from 'utils/theme'
import { calculateLimitPriceForMarketOrder } from 'utils/tradeForm' import { calculateLimitPriceForMarketOrder } from 'utils/tradeForm'
@ -250,29 +249,32 @@ const Balance = ({ bank }: { bank: Bank }) => {
} }
let minOrderDecimals: number let minOrderDecimals: number
let tickSize: number let tickDecimals: number
if (selectedMarket instanceof Serum3Market) { if (selectedMarket instanceof Serum3Market) {
const market = group.getSerum3ExternalMarket( const market = group.getSerum3ExternalMarket(
selectedMarket.serumMarketExternal selectedMarket.serumMarketExternal
) )
minOrderDecimals = getDecimalCount(market.minOrderSize) minOrderDecimals = getDecimalCount(market.minOrderSize)
tickSize = getDecimalCount(market.tickSize) tickDecimals = getDecimalCount(market.tickSize)
} else { } else {
minOrderDecimals = getDecimalCount(selectedMarket.minOrderSize) minOrderDecimals = getDecimalCount(selectedMarket.minOrderSize)
tickSize = getDecimalCount(selectedMarket.tickSize) tickDecimals = getDecimalCount(selectedMarket.tickSize)
} }
if (type === 'quote') { if (type === 'quote') {
const trimmedBalance = trimDecimals(balance, tickSize) const floorBalance = floorToDecimal(balance, tickDecimals).toNumber()
const baseSize = trimDecimals(trimmedBalance / price, minOrderDecimals) const baseSize = floorToDecimal(
const quoteSize = trimDecimals(baseSize * price, tickSize) floorBalance / price,
minOrderDecimals
).toNumber()
const quoteSize = floorToDecimal(baseSize * price, tickDecimals)
set((s) => { set((s) => {
s.tradeForm.baseSize = baseSize.toString() s.tradeForm.baseSize = baseSize.toString()
s.tradeForm.quoteSize = quoteSize.toString() s.tradeForm.quoteSize = quoteSize.toString()
}) })
} else { } else {
const baseSize = trimDecimals(balance, minOrderDecimals) const baseSize = floorToDecimal(balance, minOrderDecimals).toNumber()
const quoteSize = trimDecimals(baseSize * price, tickSize) const quoteSize = floorToDecimal(baseSize * price, tickDecimals)
set((s) => { set((s) => {
s.tradeForm.baseSize = baseSize.toString() s.tradeForm.baseSize = baseSize.toString()
s.tradeForm.quoteSize = quoteSize.toString() s.tradeForm.quoteSize = quoteSize.toString()

View File

@ -13,7 +13,7 @@ import { IconButton, LinkButton } from '../shared/Button'
import { Transition } from '@headlessui/react' import { Transition } from '@headlessui/react'
import SheenLoader from '../shared/SheenLoader' import SheenLoader from '../shared/SheenLoader'
import mangoStore from '@store/mangoStore' import mangoStore from '@store/mangoStore'
import { countLeadingZeros, trimDecimals } from '../../utils/numbers' import { countLeadingZeros, floorToDecimal } from '../../utils/numbers'
import useLocalStorageState from 'hooks/useLocalStorageState' import useLocalStorageState from 'hooks/useLocalStorageState'
import { PAGINATION_PAGE_LENGTH, PREFERRED_EXPLORER_KEY } from 'utils/constants' import { PAGINATION_PAGE_LENGTH, PREFERRED_EXPLORER_KEY } from 'utils/constants'
import Tooltip from '@components/shared/Tooltip' import Tooltip from '@components/shared/Tooltip'
@ -100,7 +100,7 @@ const SwapHistoryTable = () => {
} = h } = h
const borrowAmount = const borrowAmount =
loan > 0 loan > 0
? `${trimDecimals(loan, countLeadingZeros(loan) + 2)}` ? `${floorToDecimal(loan, countLeadingZeros(loan) + 2)}`
: 0 : 0
const borrowFee = const borrowFee =
swap_in_loan_origination_fee > 0 swap_in_loan_origination_fee > 0
@ -273,7 +273,7 @@ const SwapHistoryTable = () => {
const borrowAmount = const borrowAmount =
loan > 0 loan > 0
? `${trimDecimals(loan, countLeadingZeros(loan) + 2)}` ? `${floorToDecimal(loan, countLeadingZeros(loan) + 2)}`
: 0 : 0
const borrowFee = const borrowFee =
swap_in_loan_origination_fee > 0 swap_in_loan_origination_fee > 0

View File

@ -4,7 +4,7 @@ import mangoStore from '@store/mangoStore'
import useMangoAccount from 'hooks/useMangoAccount' import useMangoAccount from 'hooks/useMangoAccount'
import useSelectedMarket from 'hooks/useSelectedMarket' import useSelectedMarket from 'hooks/useSelectedMarket'
import { useCallback, useMemo, useState } from 'react' import { useCallback, useMemo, useState } from 'react'
import { trimDecimals } from 'utils/numbers' import { floorToDecimal } from 'utils/numbers'
const PerpButtonGroup = ({ const PerpButtonGroup = ({
minOrderDecimals, minOrderDecimals,
@ -50,28 +50,27 @@ const PerpButtonGroup = ({
set((s) => { set((s) => {
if (s.tradeForm.side === 'buy') { if (s.tradeForm.side === 'buy') {
s.tradeForm.quoteSize = trimDecimals(size, tickDecimals).toFixed( s.tradeForm.quoteSize = floorToDecimal(size, tickDecimals).toString()
tickDecimals
)
if (Number(s.tradeForm.price)) { if (Number(s.tradeForm.price)) {
s.tradeForm.baseSize = trimDecimals( s.tradeForm.baseSize = floorToDecimal(
size / Number(s.tradeForm.price), size / Number(s.tradeForm.price),
minOrderDecimals minOrderDecimals
).toFixed(minOrderDecimals) ).toString()
} else { } else {
s.tradeForm.baseSize = '' s.tradeForm.baseSize = ''
} }
} else if (s.tradeForm.side === 'sell') { } else if (s.tradeForm.side === 'sell') {
s.tradeForm.baseSize = trimDecimals(size, minOrderDecimals).toFixed( s.tradeForm.baseSize = floorToDecimal(
size,
minOrderDecimals minOrderDecimals
) ).toString()
if (Number(s.tradeForm.price)) { if (Number(s.tradeForm.price)) {
s.tradeForm.quoteSize = trimDecimals( s.tradeForm.quoteSize = floorToDecimal(
size * Number(s.tradeForm.price), size * Number(s.tradeForm.price),
tickDecimals tickDecimals
).toFixed(tickDecimals) ).toString()
} }
} }
}) })

View File

@ -11,7 +11,7 @@ import useMangoGroup from 'hooks/useMangoGroup'
import useSelectedMarket from 'hooks/useSelectedMarket' import useSelectedMarket from 'hooks/useSelectedMarket'
import { useTranslation } from 'next-i18next' import { useTranslation } from 'next-i18next'
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
import { getDecimalCount, trimDecimals } from 'utils/numbers' import { floorToDecimal, getDecimalCount } from 'utils/numbers'
import { calculateLimitPriceForMarketOrder } from 'utils/tradeForm' import { calculateLimitPriceForMarketOrder } from 'utils/tradeForm'
import MarketCloseModal from './MarketCloseModal' import MarketCloseModal from './MarketCloseModal'
import PerpSideBadge from './PerpSideBadge' import PerpSideBadge from './PerpSideBadge'
@ -91,10 +91,10 @@ const PerpPositions = () => {
position.marketIndex position.marketIndex
) )
const basePosition = position.getBasePositionUi(market) const basePosition = position.getBasePositionUi(market)
const trimmedBasePosition = trimDecimals( const floorBasePosition = floorToDecimal(
basePosition, basePosition,
getDecimalCount(market.minOrderSize) getDecimalCount(market.minOrderSize)
) ).toNumber()
const isSelectedMarket = const isSelectedMarket =
selectedMarket instanceof PerpMarket && selectedMarket instanceof PerpMarket &&
selectedMarket.perpMarketIndex === position.marketIndex selectedMarket.perpMarketIndex === position.marketIndex
@ -119,7 +119,7 @@ const PerpPositions = () => {
<p className="flex justify-end"> <p className="flex justify-end">
{isSelectedMarket ? ( {isSelectedMarket ? (
<LinkButton <LinkButton
onClick={() => handlePositionClick(trimmedBasePosition)} onClick={() => handlePositionClick(floorBasePosition)}
> >
<FormatNumericValue <FormatNumericValue
value={Math.abs(basePosition)} value={Math.abs(basePosition)}
@ -136,7 +136,7 @@ const PerpPositions = () => {
</Td> </Td>
<Td className="text-right font-mono"> <Td className="text-right font-mono">
<FormatNumericValue <FormatNumericValue
value={trimmedBasePosition * market._uiPrice} value={floorBasePosition * market._uiPrice}
decimals={2} decimals={2}
isUsd isUsd
/> />

View File

@ -4,7 +4,7 @@ import mangoStore from '@store/mangoStore'
import useMangoAccount from 'hooks/useMangoAccount' import useMangoAccount from 'hooks/useMangoAccount'
import useSelectedMarket from 'hooks/useSelectedMarket' import useSelectedMarket from 'hooks/useSelectedMarket'
import { useCallback, useMemo } from 'react' import { useCallback, useMemo } from 'react'
import { trimDecimals } from 'utils/numbers' import { floorToDecimal } from 'utils/numbers'
const PerpSlider = ({ const PerpSlider = ({
minOrderDecimals, minOrderDecimals,
@ -61,20 +61,20 @@ const PerpSlider = ({
if (s.tradeForm.side === 'buy') { if (s.tradeForm.side === 'buy') {
s.tradeForm.quoteSize = val s.tradeForm.quoteSize = val
if (Number(price)) { if (Number(price)) {
s.tradeForm.baseSize = trimDecimals( s.tradeForm.baseSize = floorToDecimal(
parseFloat(val) / price, parseFloat(val) / price,
minOrderDecimals minOrderDecimals
).toFixed(minOrderDecimals) ).toString()
} else { } else {
s.tradeForm.baseSize = '' s.tradeForm.baseSize = ''
} }
} else if (s.tradeForm.side === 'sell') { } else if (s.tradeForm.side === 'sell') {
s.tradeForm.baseSize = val s.tradeForm.baseSize = val
if (Number(price)) { if (Number(price)) {
s.tradeForm.quoteSize = trimDecimals( s.tradeForm.quoteSize = floorToDecimal(
parseFloat(val) * price, parseFloat(val) * price,
tickDecimals tickDecimals
).toFixed(tickDecimals) ).toString()
} }
} }
}) })

View File

@ -3,7 +3,7 @@ import mangoStore from '@store/mangoStore'
import useMangoAccount from 'hooks/useMangoAccount' import useMangoAccount from 'hooks/useMangoAccount'
import useSelectedMarket from 'hooks/useSelectedMarket' import useSelectedMarket from 'hooks/useSelectedMarket'
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
import { trimDecimals } from 'utils/numbers' import { floorToDecimal } from 'utils/numbers'
import { useSpotMarketMax } from './SpotSlider' import { useSpotMarketMax } from './SpotSlider'
const SpotButtonGroup = ({ const SpotButtonGroup = ({
@ -29,28 +29,24 @@ const SpotButtonGroup = ({
set((s) => { set((s) => {
if (s.tradeForm.side === 'buy') { if (s.tradeForm.side === 'buy') {
s.tradeForm.quoteSize = trimDecimals(size, tickDecimals).toFixed( s.tradeForm.quoteSize = floorToDecimal(size, tickDecimals).toString()
tickDecimals
)
if (Number(s.tradeForm.price)) { if (Number(s.tradeForm.price)) {
s.tradeForm.baseSize = trimDecimals( s.tradeForm.baseSize = floorToDecimal(
size / Number(s.tradeForm.price), size / Number(s.tradeForm.price),
minOrderDecimals minOrderDecimals
).toFixed(minOrderDecimals) ).toString()
} else { } else {
s.tradeForm.baseSize = '' s.tradeForm.baseSize = ''
} }
} else if (s.tradeForm.side === 'sell') { } else if (s.tradeForm.side === 'sell') {
s.tradeForm.baseSize = trimDecimals(size, tickDecimals).toFixed( s.tradeForm.baseSize = floorToDecimal(size, tickDecimals).toString()
minOrderDecimals
)
if (Number(s.tradeForm.price)) { if (Number(s.tradeForm.price)) {
s.tradeForm.quoteSize = trimDecimals( s.tradeForm.quoteSize = floorToDecimal(
size * Number(s.tradeForm.price), size * Number(s.tradeForm.price),
tickDecimals tickDecimals
).toFixed(tickDecimals) ).toString()
} }
} }
}) })

View File

@ -5,7 +5,7 @@ import useMangoAccount from 'hooks/useMangoAccount'
import useSelectedMarket from 'hooks/useSelectedMarket' import useSelectedMarket from 'hooks/useSelectedMarket'
import { useCallback, useMemo } from 'react' import { useCallback, useMemo } from 'react'
import { GenericMarket } from 'types' import { GenericMarket } from 'types'
import { trimDecimals } from 'utils/numbers' import { floorToDecimal } from 'utils/numbers'
export const useSpotMarketMax = ( export const useSpotMarketMax = (
mangoAccount: MangoAccount | undefined, mangoAccount: MangoAccount | undefined,
@ -78,20 +78,20 @@ const SpotSlider = ({
if (s.tradeForm.side === 'buy') { if (s.tradeForm.side === 'buy') {
s.tradeForm.quoteSize = val s.tradeForm.quoteSize = val
if (Number(price)) { if (Number(price)) {
s.tradeForm.baseSize = trimDecimals( s.tradeForm.baseSize = floorToDecimal(
parseFloat(val) / price, parseFloat(val) / price,
minOrderDecimals minOrderDecimals
).toFixed(minOrderDecimals) ).toString()
} else { } else {
s.tradeForm.baseSize = '' s.tradeForm.baseSize = ''
} }
} else if (s.tradeForm.side === 'sell') { } else if (s.tradeForm.side === 'sell') {
s.tradeForm.baseSize = val s.tradeForm.baseSize = val
if (Number(price)) { if (Number(price)) {
s.tradeForm.quoteSize = trimDecimals( s.tradeForm.quoteSize = floorToDecimal(
parseFloat(val) * price, parseFloat(val) * price,
tickDecimals tickDecimals
).toFixed(tickDecimals) ).toString()
} }
} }
}) })

View File

@ -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 => { export const getDecimalCount = (value: number): number => {
if ( if (
!isNaN(value) && !isNaN(value) &&