Skip to content

Commit

Permalink
Bugfix/various fee bugs (#769)
Browse files Browse the repository at this point in the history
* fix: hero stats (#766)

* fix: some initial issues

* fix: withdraw liquidity bugs
  • Loading branch information
jeffywu authored Apr 21, 2024
1 parent df7f9fc commit 4a4c212
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,36 @@ import statsImg from '../images/stats_overlay.svg';
import { FormattedMessage } from 'react-intl';
import { formatNumber } from '@notional-finance/helpers';
import { useNotionalContext } from '@notional-finance/notionable-hooks';
import { SupportedNetworks } from '@notional-finance/util';

const oneMillion = 1_000_000;

export const HeroStats = () => {
const theme = useTheme();
const {
globalState: { heroStats },
globalState: { heroStats, activeAccounts },
} = useNotionalContext();
const isReady = SupportedNetworks.every(
(n) => activeAccounts && activeAccounts[n]
);

return (
<StatsContainer>
<ImgContainer>
<StatsContent>
{heroStats?.totalAccounts ? (
{isReady && heroStats ? (
<div>
<LargeInputText>
{`$${formatNumber(
heroStats.totalValueLocked / oneMillion,
0
2
)}M`}
<BodySecondary>
<FormattedMessage defaultMessage={'Total Value Locked'} />
</BodySecondary>
</LargeInputText>
<LargeInputText sx={{ marginTop: theme.spacing(6) }}>
{`$${formatNumber(heroStats.totalOpenDebt / oneMillion, 0)}M`}
{`$${formatNumber(heroStats.totalOpenDebt / oneMillion, 1)}M`}
<BodySecondary>
<FormattedMessage defaultMessage={'Total Open Debt'} />
</BodySecondary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,10 @@ export class AnalyticsRegistryClient extends ClientRegistry<unknown> {
Registry.getTokenRegistry()
.getAllTokens(n)
.filter(
(t) => t.tokenType === 'PrimeDebt' || t.tokenType === 'PrimeCash'
(t) =>
t.tokenType === 'PrimeDebt' ||
t.tokenType === 'PrimeCash' ||
t.tokenType === 'VaultShare'
)
.reduce(
(acc, token) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,6 @@ export const useLiquidityDetails = () => {
greenOnArrowUp: true,
},
},
{
label: 'Net Worth',
current: currentHoldings?.presentValue.toDisplayStringWithSymbol() || '-',
updated: {
value: newNetWorth?.toDisplayStringWithSymbol() || '-',
arrowUp:
getChangeType(
currentHoldings?.presentValue.toFloat(),
newNetWorth?.toFloat()
) === 'increase',
checkmark:
getChangeType(
currentHoldings?.presentValue.toFloat(),
newNetWorth?.toFloat()
) === 'cleared',
greenOnCheckmark: false,
greenOnArrowUp: true,
},
},
{
label: 'Leverage Ratio',
current: currentHoldings?.leverageRatio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const CreateOrIncreasePosition = () => {
{currentPosition ? (
<ManageTerms
borrowType={
currentPosition.debt.tokenType === 'PrimeDebt'
currentPosition.debt.balance.tokenType === 'PrimeDebt'
? 'Variable'
: 'Fixed'
}
Expand Down
15 changes: 2 additions & 13 deletions packages/features/portfolio/src/side-drawers/convert-asset.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Box, useTheme } from '@mui/material';
import { Box } from '@mui/material';
import { TokenBalance } from '@notional-finance/core-entities';
import { SideBarSubHeader, useCurrencyInputRef } from '@notional-finance/mui';
import { useCurrencyInputRef } from '@notional-finance/mui';
import { useTradeContext } from '@notional-finance/notionable-hooks';
import { AssetInput } from '@notional-finance/trade';
import { PORTFOLIO_ACTIONS } from '@notional-finance/util';
import { defineMessage } from 'react-intl';
import { PortfolioSideDrawer } from './components/portfolio-side-drawer';
import { SelectConvertAsset } from './components/select-convert-asset';
import { messages } from './messages';
import { useState } from 'react';

export const ConvertAsset = () => {
const theme = useTheme();
const context = useTradeContext('ConvertAsset');
const { currencyInputRef } = useCurrencyInputRef();
const {
Expand Down Expand Up @@ -41,15 +39,6 @@ export const ConvertAsset = () => {
</Box>
{collateral && (
<PortfolioSideDrawer context={context}>
<SideBarSubHeader
callback={() => updateState({ collateral: undefined })}
sx={{
position: 'absolute',
top: 0,
background: theme.palette.background.paper,
}}
titleText={defineMessage({ defaultMessage: 'Back' })}
/>
<AssetInput
ref={currencyInputRef}
debtOrCollateral="Debt"
Expand Down
15 changes: 2 additions & 13 deletions packages/features/portfolio/src/side-drawers/roll-debt.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Box, useTheme } from '@mui/material';
import { Box } from '@mui/material';
import { TokenBalance } from '@notional-finance/core-entities';
import { SideBarSubHeader, useCurrencyInputRef } from '@notional-finance/mui';
import { useCurrencyInputRef } from '@notional-finance/mui';
import { useTradeContext } from '@notional-finance/notionable-hooks';
import { AssetInput } from '@notional-finance/trade';
import { PORTFOLIO_ACTIONS } from '@notional-finance/util';
import { defineMessage } from 'react-intl';
import { PortfolioSideDrawer } from './components/portfolio-side-drawer';
import { SelectConvertAsset } from './components/select-convert-asset';
import { messages } from './messages';
import { useState } from 'react';

export const RollDebt = () => {
const theme = useTheme();
const context = useTradeContext('RollDebt');
const { currencyInputRef } = useCurrencyInputRef();
const {
Expand Down Expand Up @@ -44,15 +42,6 @@ export const RollDebt = () => {
context={context}
enablePrimeBorrow={debt?.tokenType === 'PrimeDebt'}
>
<SideBarSubHeader
callback={() => updateState({ debt: undefined })}
sx={{
position: 'absolute',
top: 0,
background: theme.palette.background.paper,
}}
titleText={defineMessage({ defaultMessage: 'Back' })}
/>
<AssetInput
ref={currencyInputRef}
debtOrCollateral="Collateral"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,9 @@ function getRollDebtOrConvertAssetSummary(
if (netRealizedDebtBalance && debtBalance) {
summary.push(
getTradeDetail(
netRealizedDebtBalance.abs().neg(),
tradeType === 'RollDebt'
? netRealizedDebtBalance.abs()
: netRealizedDebtBalance.neg(),
tradeType === 'RollDebt' ? 'Debt' : 'Asset',
tradeType === 'RollDebt' ? 'none' : 'withdraw',
intl,
Expand All @@ -519,7 +521,9 @@ function getRollDebtOrConvertAssetSummary(
if (netRealizedCollateralBalance && collateralBalance)
summary.push(
getTradeDetail(
netRealizedCollateralBalance,
tradeType === 'RollDebt'
? netRealizedCollateralBalance.neg()
: netRealizedCollateralBalance.abs(),
tradeType === 'RollDebt' ? 'Debt' : 'Asset',
tradeType === 'RollDebt' ? 'repay' : 'none',
intl,
Expand Down
11 changes: 8 additions & 3 deletions packages/transaction/src/calculate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function exchangeToLocalPrime(
return {
localPrime: tokensOut[0].toToken(outToken),
fees: feesPaid[0],
netRealized: tokensOut[0].sub(feesPaid[0]).toUnderlying(),
netRealized: tokensOut[0].add(feesPaid[0]).toUnderlying(),
};
} else if (token.tokenType === 'nToken' && balance.isPositive()) {
if (!pool) throw Error('Pool is undefined');
Expand All @@ -73,7 +73,7 @@ export function exchangeToLocalPrime(
return {
localPrime: tokensIn[0].toToken(outToken),
fees: feesPaid[0],
netRealized: balance.sub(feesPaid[0]).toUnderlying(),
netRealized: balance.toPrimeCash().sub(feesPaid[0]).toUnderlying(),
};
} else if (token.tokenType === 'fCash') {
if (!pool) throw Error('Pool is undefined');
Expand Down Expand Up @@ -275,7 +275,11 @@ export function calculateDebt({
debtBalance: lpTokens.neg(),
debtFee: feesPaid[0],
collateralFee,
netRealizedDebtBalance: totalDebtPrime.sub(feesPaid[0]).toUnderlying(),
// Ensure netRealizedDebtBalance is positive
netRealizedDebtBalance: totalDebtPrime
.sub(feesPaid[0])
.neg()
.toUnderlying(),
netRealizedCollateralBalance,
};
} else if (debt.tokenType === 'fCash') {
Expand Down Expand Up @@ -676,6 +680,7 @@ export function calculateDebtCollateralGivenDepositRiskLimit({
debtBalance: results.debtBalance.add(debtDeposit.debtBalance),
debtFee: results.debtFee.add(debtDeposit.debtFee),
netRealizedDebtBalance: results.netRealizedDebtBalance.add(
// TODO: this is negative
debtDeposit.netRealizedDebtBalance
),
}
Expand Down

0 comments on commit 4a4c212

Please sign in to comment.