perps: add tradingfee percentage tooltip (#787)

This commit is contained in:
Bob van der Helm 2024-02-09 10:35:09 +01:00 committed by GitHub
parent c037dc5d62
commit ba8758aa91
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 32 additions and 14 deletions

View File

@ -1,6 +1,9 @@
import classNames from 'classnames' import classNames from 'classnames'
import React from 'react' import React from 'react'
import Text from 'components/common/Text'
import { Tooltip } from 'components/common/Tooltip'
const infoLineClasses = 'flex flex-row justify-between flex-1 mb-1 text-xs text-white' const infoLineClasses = 'flex flex-row justify-between flex-1 mb-1 text-xs text-white'
interface SummaryLineProps { interface SummaryLineProps {
@ -8,11 +11,20 @@ interface SummaryLineProps {
className?: string className?: string
contentClassName?: string contentClassName?: string
label: string label: string
tooltip?: string
} }
export default function SummaryLine(props: SummaryLineProps) { export default function SummaryLine(props: SummaryLineProps) {
return ( return (
<div className={classNames(infoLineClasses, props.className)}> <div className={classNames(infoLineClasses, props.className)}>
<span className='opacity-40'>{props.label}</span> {props.tooltip ? (
<Tooltip content={<span className='text-sm'>{props.tooltip}</span>} type='info' underline>
<Text size='xs' className='opacity-40'>
{props.label}
</Text>
</Tooltip>
) : (
<span className='opacity-40'>{props.label}</span>
)}
<span className={props.contentClassName}>{props.children}</span> <span className={props.contentClassName}>{props.children}</span>
</div> </div>
) )

View File

@ -51,8 +51,8 @@ export const Tooltip = (props: Props) => {
<span <span
className={classNames( className={classNames(
props.underline && props.underline &&
'border-b-1 hover:cursor-pointer border border-x-0 border-t-0 border-dashed border-white/20 pb-1', 'border-b hover:cursor-help border-dashed border-white/20 pb-1 hover:border-transparent',
!reduceMotion && 'transition-all', !reduceMotion && 'transition-all duration-200',
props.className, props.className,
)} )}
> >

View File

@ -104,7 +104,7 @@ export function PerpsModule() {
/> />
<AssetAmountInput <AssetAmountInput
label='Amount' label='Amount'
max={BN(1000000)} // TODO: Implement max calculation max={BN(100000000)} // TODO: Implement max calculation
amount={amount.abs()} amount={amount.abs()}
setAmount={onChangeAmount} setAmount={onChangeAmount}
asset={perpsAsset} asset={perpsAsset}

View File

@ -7,9 +7,10 @@ import { ArrowRight } from 'components/common/Icons'
import SummaryLine from 'components/common/SummaryLine' import SummaryLine from 'components/common/SummaryLine'
import Text from 'components/common/Text' import Text from 'components/common/Text'
import TradeDirection from 'components/perps/BalancesTable/Columns/TradeDirection' import TradeDirection from 'components/perps/BalancesTable/Columns/TradeDirection'
import OpeningFee from 'components/perps/Module/OpeningFee' import TradingFee from 'components/perps/Module/TradingFee'
import { BN_ZERO } from 'constants/math' import { BN_ZERO } from 'constants/math'
import useCurrentAccount from 'hooks/accounts/useCurrentAccount' import useCurrentAccount from 'hooks/accounts/useCurrentAccount'
import useTradingFee from 'hooks/perps/useTradingFee'
import useStore from 'store' import useStore from 'store'
import { BNCoin } from 'types/classes/BNCoin' import { BNCoin } from 'types/classes/BNCoin'
import { formatLeverage } from 'utils/formatters' import { formatLeverage } from 'utils/formatters'
@ -31,6 +32,7 @@ export default function PerpsSummary(props: Props) {
const modifyPerpPosition = useStore((s) => s.modifyPerpPosition) const modifyPerpPosition = useStore((s) => s.modifyPerpPosition)
const closePerpPosition = useStore((s) => s.closePerpPosition) const closePerpPosition = useStore((s) => s.closePerpPosition)
const currentAccount = useCurrentAccount() const currentAccount = useCurrentAccount()
const { data: tradingFee, isLoading } = useTradingFee(props.asset.denom, props.amount)
const newAmount = useMemo( const newAmount = useMemo(
() => (props.previousAmount ?? BN_ZERO).plus(props.amount), () => (props.previousAmount ?? BN_ZERO).plus(props.amount),
@ -74,8 +76,11 @@ export default function PerpsSummary(props: Props) {
Summary Summary
</Text> </Text>
<SummaryLine label='Expected Price'>-</SummaryLine> <SummaryLine label='Expected Price'>-</SummaryLine>
<SummaryLine label='Fees'> <SummaryLine
<OpeningFee denom={props.asset.denom} amount={props.amount} /> label='Fees'
tooltip={`${tradingFee ? tradingFee.rate.times(100) + '% ' : ''}Trading Fees`}
>
<TradingFee denom={props.asset.denom} amount={props.amount} />
</SummaryLine> </SummaryLine>
<SummaryLine label='Total'>-</SummaryLine> <SummaryLine label='Total'>-</SummaryLine>
</div> </div>

View File

@ -2,18 +2,18 @@ import BigNumber from 'bignumber.js'
import { CircularProgress } from 'components/common/CircularProgress' import { CircularProgress } from 'components/common/CircularProgress'
import DisplayCurrency from 'components/common/DisplayCurrency' import DisplayCurrency from 'components/common/DisplayCurrency'
import useOpeningFee from 'hooks/perps/useOpeningFee' import useTradingFee from 'hooks/perps/useTradingFee'
type Props = { type Props = {
denom: string denom: string
amount: BigNumber amount: BigNumber
} }
export default function OpeningFee(props: Props) { export default function TradingFee(props: Props) {
const { data: openingFee, isLoading } = useOpeningFee(props.denom, props.amount) const { data: openingFee, isLoading } = useTradingFee(props.denom, props.amount)
if (isLoading) return <CircularProgress className='h-full' size={12} /> if (isLoading) return <CircularProgress className='h-full' size={12} />
if (props.amount.isZero() || !openingFee) return '-' if (props.amount.isZero() || !openingFee) return '-'
return <DisplayCurrency coin={openingFee} /> return <DisplayCurrency coin={openingFee.fee} />
} }

View File

@ -5,16 +5,17 @@ import useChainConfig from 'hooks/useChainConfig'
import useClients from 'hooks/useClients' import useClients from 'hooks/useClients'
import useDebounce from 'hooks/useDebounce' import useDebounce from 'hooks/useDebounce'
import { BNCoin } from 'types/classes/BNCoin' import { BNCoin } from 'types/classes/BNCoin'
import { BN } from 'utils/helpers'
export default function useOpeningFee(denom: string, amount: BigNumber) { export default function useTradingFee(denom: string, amount: BigNumber) {
const chainConfig = useChainConfig() const chainConfig = useChainConfig()
const debouncedAmount = useDebounce<string>(amount.toString(), 500) const debouncedAmount = useDebounce<string>(amount.toString(), 500)
const clients = useClients() const clients = useClients()
const enabled = !amount.isZero() && clients const enabled = !amount.isZero() && clients
return useSWR(enabled && `${chainConfig.id}/perps/${denom}/openingFee/${debouncedAmount}`, () => return useSWR(enabled && `${chainConfig.id}/perps/${denom}/tradingFee/${debouncedAmount}`, () =>
clients!.perps clients!.perps
.openingFee({ denom, size: amount as any }) .openingFee({ denom, size: amount as any })
.then((resp) => BNCoin.fromCoin(resp.fee)), .then((resp) => ({ rate: BN(resp.rate), fee: BNCoin.fromCoin(resp.fee) })),
) )
} }