remove trimdecimals
This commit is contained in:
parent
f8f022fe9a
commit
6a9a40f95e
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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 = () => {
|
|||
<p className="flex justify-end">
|
||||
{isSelectedMarket ? (
|
||||
<LinkButton
|
||||
onClick={() => handlePositionClick(trimmedBasePosition)}
|
||||
onClick={() => handlePositionClick(floorBasePosition)}
|
||||
>
|
||||
<FormatNumericValue
|
||||
value={Math.abs(basePosition)}
|
||||
|
@ -136,7 +136,7 @@ const PerpPositions = () => {
|
|||
</Td>
|
||||
<Td className="text-right font-mono">
|
||||
<FormatNumericValue
|
||||
value={trimmedBasePosition * market._uiPrice}
|
||||
value={floorBasePosition * market._uiPrice}
|
||||
decimals={2}
|
||||
isUsd
|
||||
/>
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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) &&
|
||||
|
|
Loading…
Reference in New Issue