From 2ba18d2f05de60648d68da3cb5aa29b9bd6d570d Mon Sep 17 00:00:00 2001 From: Linkie Link Date: Wed, 20 Sep 2023 11:16:47 +0200 Subject: [PATCH] Max margin trade fix (#490) * fix: margin trading fix * fix: accountsBalancesTable fix * fix: show detailed APR where we have the space * fix: smallerThan not biggerThan * fix: be more precise on the decimals * tidy: refactor --- .../Account/AccountBalancesTable/index.tsx | 12 ++++++----- src/components/Account/AccountComposition.tsx | 6 +++--- src/components/AmountAndValue.tsx | 8 ++++++-- src/components/DisplayCurrency.tsx | 20 +++++++++++-------- src/components/FormattedNumber.tsx | 12 ++++++++++- src/components/Portfolio/Account/Summary.tsx | 11 +++++++--- .../Trade/TradeModule/SwapForm/index.tsx | 6 ++++-- src/constants/math.ts | 4 ++++ 8 files changed, 55 insertions(+), 24 deletions(-) diff --git a/src/components/Account/AccountBalancesTable/index.tsx b/src/components/Account/AccountBalancesTable/index.tsx index 03b26ef9..37622ab8 100644 --- a/src/components/Account/AccountBalancesTable/index.tsx +++ b/src/components/Account/AccountBalancesTable/index.tsx @@ -20,6 +20,7 @@ import { FormattedNumber } from 'components/FormattedNumber' import { SortAsc, SortDesc, SortNone } from 'components/Icons' import Text from 'components/Text' import { ASSETS } from 'constants/assets' +import { MAX_AMOUNT_DECIMALS, MIN_AMOUNT } from 'constants/math' import { ORACLE_DENOM } from 'constants/oracle' import useCurrentAccount from 'hooks/useCurrentAccount' import useMarketAssets from 'hooks/useMarketAssets' @@ -99,23 +100,24 @@ export default function Index(props: Props) { row.original.amount, getAssetByDenom(row.original.denom) ?? ASSETS[0], ) - if (amount >= 0.01) + if (amount >= 1) return ( ) - const formattedAmount = formatAmountToPrecision(amount, 1) + const formattedAmount = formatAmountToPrecision(amount, MAX_AMOUNT_DECIMALS) return ( @@ -152,7 +152,7 @@ function Item(props: ItemProps) { {props.isPercentage ? ( diff --git a/src/components/AmountAndValue.tsx b/src/components/AmountAndValue.tsx index eae90b8f..67fb6368 100644 --- a/src/components/AmountAndValue.tsx +++ b/src/components/AmountAndValue.tsx @@ -1,6 +1,8 @@ import DisplayCurrency from 'components/DisplayCurrency' import { FormattedNumber } from 'components/FormattedNumber' +import { MAX_AMOUNT_DECIMALS, MIN_AMOUNT } from 'constants/math' import { BNCoin } from 'types/classes/BNCoin' +import { demagnify } from 'utils/formatters' interface Props { asset: Asset @@ -8,11 +10,13 @@ interface Props { } export default function AmountAndValue(props: Props) { + const amount = demagnify(props.amount.toString(), props.asset) return (
{ const coinValue = getCoinValue(props.coin, prices) @@ -55,13 +49,23 @@ export default function DisplayCurrency(props: Props) { return coinValue.div(displayPrice).toNumber() }, [displayCurrency, displayCurrencyAsset.decimals, prices, props.coin]) + const isLessThanACent = isUSD && amount < 0.01 && amount > 0 + const smallerThanPrefix = isLessThanACent ? '< ' : '' + + const prefix = isUSD + ? `${props.isApproximation ? '~ ' : smallerThanPrefix}$` + : `${props.isApproximation ? '~ ' : ''}` + const suffix = isUSD + ? '' + : ` ${displayCurrencyAsset.symbol ? ` ${displayCurrencyAsset.symbol}` : ''}` + return ( (0) + let { options, smallerThanThreshold } = props + + if (smallerThanThreshold) { + if (!options) options = { prefix: '< ' } + if (options.prefix && options.prefix.substring(0, 1) !== '<') + options.prefix = `< ${options.prefix}` + else options.prefix = '< ' + } + useEffect(() => { if (prevAmountRef.current !== props.amount) prevAmountRef.current = props.amount }, [props.amount]) @@ -47,7 +57,7 @@ export const FormattedNumber = React.memo( props.className, )} > - {formatValue(props.amount.toString(), props.options)} + {formatValue(props.amount.toString(), options)}

) diff --git a/src/components/Portfolio/Account/Summary.tsx b/src/components/Portfolio/Account/Summary.tsx index bed2f243..3e4de18c 100644 --- a/src/components/Portfolio/Account/Summary.tsx +++ b/src/components/Portfolio/Account/Summary.tsx @@ -8,6 +8,7 @@ import { Heart } from 'components/Icons' import Loading from 'components/Loading' import Text from 'components/Text' import TitleAndSubCell from 'components/TitleAndSubCell' +import { MAX_AMOUNT_DECIMALS } from 'constants/math' import { ORACLE_DENOM } from 'constants/oracle' import useAccount from 'hooks/useAccount' import useBorrowMarketAssetsTableData from 'hooks/useBorrowMarketAssetsTableData' @@ -70,7 +71,11 @@ function Content(props: Props) { }, { title: ( - + ), sub: STATS[3].sub, }, @@ -105,7 +110,7 @@ interface SkeletonProps extends Props { function Skeleton(props: SkeletonProps) { return ( -
+
Credit Account {props.accountId}
@@ -115,7 +120,7 @@ function Skeleton(props: SkeletonProps) {
{props.stats.map((stat) => ( - + } sub={stat.sub} diff --git a/src/components/Trade/TradeModule/SwapForm/index.tsx b/src/components/Trade/TradeModule/SwapForm/index.tsx index c10c50b8..1ee14959 100644 --- a/src/components/Trade/TradeModule/SwapForm/index.tsx +++ b/src/components/Trade/TradeModule/SwapForm/index.tsx @@ -12,7 +12,7 @@ import { AvailableOrderType } from 'components/Trade/TradeModule/SwapForm/OrderT import TradeSummary from 'components/Trade/TradeModule/SwapForm/TradeSummary' import { DEFAULT_SETTINGS } from 'constants/defaultSettings' import { SLIPPAGE_KEY } from 'constants/localStore' -import { BN_ZERO } from 'constants/math' +import { BN_ZERO, MARGIN_TRADE_BUFFER } from 'constants/math' import useAutoLend from 'hooks/useAutoLend' import useCurrentAccount from 'hooks/useCurrentAccount' import useHealthComputer from 'hooks/useHealthComputer' @@ -27,7 +27,7 @@ import { BNCoin } from 'types/classes/BNCoin' import { byDenom } from 'utils/array' import { defaultFee } from 'utils/constants' import { getCapLeftWithBuffer } from 'utils/generic' -import { asyncThrottle, BN } from 'utils/helpers' +import { BN, asyncThrottle } from 'utils/helpers' interface Props { buyAsset: Asset @@ -108,6 +108,8 @@ export default function SwapForm(props: Props) { const [maxSellAmount, sellSideMarginThreshold, marginRatio] = useMemo(() => { const maxAmount = computeMaxSwapAmount(sellAsset.denom, buyAsset.denom, 'default') const maxAmountOnMargin = computeMaxSwapAmount(sellAsset.denom, buyAsset.denom, 'margin') + .multipliedBy(MARGIN_TRADE_BUFFER) + .integerValue() const marginRatio = maxAmount.dividedBy(maxAmountOnMargin) estimateExactIn( diff --git a/src/constants/math.ts b/src/constants/math.ts index 1e899593..80110d2e 100644 --- a/src/constants/math.ts +++ b/src/constants/math.ts @@ -2,3 +2,7 @@ import { BN } from 'utils/helpers' export const BN_ZERO = BN(0) export const BN_ONE = BN(1) + +export const MARGIN_TRADE_BUFFER = 0.9 +export const MIN_AMOUNT = 0.000001 +export const MAX_AMOUNT_DECIMALS = 6