Added leverage change to Trade (#457)

This commit is contained in:
Bob van der Helm 2023-09-12 09:37:28 +02:00 committed by GitHub
parent f517a94b92
commit 00da894375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 13 deletions

View File

@ -0,0 +1,54 @@
import classNames from 'classnames'
import { FormattedNumber } from 'components/FormattedNumber'
import { ArrowRight } from 'components/Icons'
interface Props {
leverage: BigNumber
updatedLeverage: BigNumber | null
}
export default function AccountDetailsLeverage(props: Props) {
const { leverage, updatedLeverage } = props
if (!updatedLeverage) {
return (
<FormattedNumber
className={'w-full text-center text-2xs'}
amount={isNaN(leverage.toNumber()) ? 0 : leverage.toNumber()}
options={{
maxDecimals: 2,
minDecimals: 2,
suffix: 'x',
}}
animate
/>
)
}
return (
<div className='flex'>
<FormattedNumber
className={'w-full text-center text-2xs'}
amount={isNaN(leverage.toNumber()) ? 0 : leverage.toNumber()}
options={{
maxDecimals: 1,
minDecimals: 1,
rounded: true,
}}
animate
/>
<ArrowRight width={12} />
<FormattedNumber
className={classNames(
'w-full text-center text-2xs',
updatedLeverage.gt(leverage) && 'text-loss',
updatedLeverage.lt(leverage) && 'text-profit',
)}
amount={isNaN(updatedLeverage.toNumber()) ? 0 : updatedLeverage.toNumber()}
options={{ maxDecimals: 1, minDecimals: 1, rounded: true }}
animate
/>
</div>
)
}

View File

@ -3,6 +3,7 @@ import { useMemo } from 'react'
import AccountBalancesTable from 'components/Account/AccountBalancesTable' import AccountBalancesTable from 'components/Account/AccountBalancesTable'
import AccountComposition from 'components/Account/AccountComposition' import AccountComposition from 'components/Account/AccountComposition'
import AccountDetailsLeverage from 'components/Account/AccountDetails/AccountDetailsLeverage'
import { glowElement } from 'components/Button/utils' import { glowElement } from 'components/Button/utils'
import Card from 'components/Card' import Card from 'components/Card'
import DisplayCurrency from 'components/DisplayCurrency' import DisplayCurrency from 'components/DisplayCurrency'
@ -55,10 +56,15 @@ function AccountDetails(props: Props) {
[updatedAccount, account, prices], [updatedAccount, account, prices],
) )
const coin = BNCoin.fromDenomAndBigNumber(ORACLE_DENOM, accountBalanceValue) const coin = BNCoin.fromDenomAndBigNumber(ORACLE_DENOM, accountBalanceValue)
const leverage = useMemo( const leverage = useMemo(() => calculateAccountLeverage(account, prices), [account, prices])
() => calculateAccountLeverage(updatedAccount ?? account, prices), const updatedLeverage = useMemo(() => {
[account, updatedAccount, prices], if (!updatedAccount) return null
) const updatedLeverage = calculateAccountLeverage(updatedAccount, prices)
if (updatedLeverage.eq(leverage)) return null
return updatedLeverage
}, [updatedAccount, prices, leverage])
const { availableAssets: borrowAvailableAssets, accountBorrowedAssets } = const { availableAssets: borrowAvailableAssets, accountBorrowedAssets } =
useBorrowMarketAssetsTableData() useBorrowMarketAssetsTableData()
const { availableAssets: lendingAvailableAssets, accountLentAssets } = const { availableAssets: lendingAvailableAssets, accountLentAssets } =
@ -75,6 +81,7 @@ function AccountDetails(props: Props) {
() => calculateAccountApr(account, borrowAssetsData, lendingAssetsData, prices), () => calculateAccountApr(account, borrowAssetsData, lendingAssetsData, prices),
[account, borrowAssetsData, lendingAssetsData, prices], [account, borrowAssetsData, lendingAssetsData, prices],
) )
return ( return (
<div <div
data-testid='account-details' data-testid='account-details'
@ -104,12 +111,7 @@ function AccountDetails(props: Props) {
<Text size='2xs' className='mb-0.5 w-full text-center text-white/50'> <Text size='2xs' className='mb-0.5 w-full text-center text-white/50'>
Leverage Leverage
</Text> </Text>
<FormattedNumber <AccountDetailsLeverage leverage={leverage} updatedLeverage={updatedLeverage} />
className={'w-full text-center text-2xs'}
amount={isNaN(leverage.toNumber()) ? 0 : leverage.toNumber()}
options={{ maxDecimals: 2, minDecimals: 2, suffix: 'x' }}
animate
/>
</div> </div>
<div className='w-full py-4 border-t border-white/20'> <div className='w-full py-4 border-t border-white/20'>
<Text size='2xs' className='mb-0.5 w-full text-center text-white/50'> <Text size='2xs' className='mb-0.5 w-full text-center text-white/50'>

View File

@ -26,7 +26,7 @@ import { BNCoin } from 'types/classes/BNCoin'
import { byDenom } from 'utils/array' import { byDenom } from 'utils/array'
import { formatPercent, formatValue } from 'utils/formatters' import { formatPercent, formatValue } from 'utils/formatters'
import { BN } from 'utils/helpers' import { BN } from 'utils/helpers'
import {getDebtAmountWithInterest} from 'utils/tokens' import { getDebtAmountWithInterest } from 'utils/tokens'
function getDebtAmount(modal: BorrowModal) { function getDebtAmount(modal: BorrowModal) {
return BN((modal.marketData as BorrowMarketTableData)?.debt ?? 0).toString() return BN((modal.marketData as BorrowMarketTableData)?.debt ?? 0).toString()
@ -264,4 +264,4 @@ function BorrowModal(props: Props) {
</div> </div>
</Modal> </Modal>
) )
} }

View File

@ -23,4 +23,4 @@ export function getTokenPrice(denom: string, prices: BNCoin[]): BigNumber {
export function getDebtAmountWithInterest(debt: BigNumber, apr: number) { export function getDebtAmountWithInterest(debt: BigNumber, apr: number) {
return debt.times(1 + apr / 365 / 24).integerValue() return debt.times(1 + apr / 365 / 24).integerValue()
} }