Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nav management card value in Drawer #2442

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 31 additions & 55 deletions centrifuge-app/src/pages/NavManagement/NavManagementAssetTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import { useSuitableAccounts } from '../../utils/usePermissions'
import { usePool, usePoolAccountOrders, usePoolFees } from '../../utils/usePools'
import { usePoolsForWhichAccountIsFeeder } from '../../utils/usePoolsForWhichAccountIsFeeder'
import { positiveNumber } from '../../utils/validation'
import { nonNegativeNumber } from '../../utils/validation'
import { isCashLoan, isExternalLoan } from '../Loan/utils'

type FormValues = {
Expand Down Expand Up @@ -202,20 +202,23 @@
)
}, [poolFees, pool.currency.decimals])

const poolReserve = pool.reserve.total.toDecimal().toNumber() || 0
const newNavExternal = form.values.feed.reduce(
(acc, cur) => acc + cur.quantity * (isEditing && cur.value ? cur.value : cur.oldValue),
0
)
const newNavInternal =
allLoans?.reduce(
(acc, cur) => acc + (!isExternalLoan(cur) && 'presentValue' in cur ? cur.presentValue.toFloat() : 0),
0
) || 0
const newNavCash = cashLoans.reduce((acc, cur) => acc + cur.outstandingDebt.toFloat(), 0)
const newNav = newNavExternal + newNavInternal + newNavCash + poolReserve - pendingFees.toFloat()
const changeInValuation = React.useMemo(() => {
return (externalLoans as ActiveLoan[]).reduce((prev, curr) => {
const price = curr.currentPrice.toDecimal()
const quantity = (curr as ExternalLoan).pricing.outstandingQuantity.toDecimal()
const updatedPrice = Dec(form.values.feed.find((p) => p.id === curr.id)?.value || 0)
return CurrencyBalance.fromFloat(
prev.toDecimal().add(updatedPrice.sub(price).mul(quantity)).toString(),
pool.currency.decimals
)
}, new CurrencyBalance(0, pool.currency.decimals))
}, [externalLoans, pool?.nav, form.values.feed])

Check warning on line 215 in centrifuge-app/src/pages/NavManagement/NavManagementAssetTable.tsx

View workflow job for this annotation

GitHub Actions / build-app

React Hook React.useMemo has a missing dependency: 'pool.currency.decimals'. Either include it or remove the dependency array

Check warning on line 215 in centrifuge-app/src/pages/NavManagement/NavManagementAssetTable.tsx

View workflow job for this annotation

GitHub Actions / ff-prod / build-app

React Hook React.useMemo has a missing dependency: 'pool.currency.decimals'. Either include it or remove the dependency array

const totalAum = pool.nav.aum.toDecimal().add(pool.reserve.available.toDecimal())
const pendingNav = totalAum.add(changeInValuation.toDecimal()).sub(pendingFees.toDecimal())

// Only for single tranche pools
const newPrice = newNav / pool.tranches[0].totalIssuance.toFloat()
const newPrice = pendingNav.toNumber() / pool.tranches[0].totalIssuance.toFloat()
const isTinlakePool = poolId.startsWith('0x')

const columns = [
Expand Down Expand Up @@ -276,7 +279,7 @@
header: 'New price',
cell: (row: Row) => {
return 'oldValue' in row && row.id !== 'reserve' && isEditing ? (
<Field name={`feed.${row.formIndex}.value`} validate={positiveNumber()}>
<Field name={`feed.${row.formIndex}.value`} validate={nonNegativeNumber()}>
{({ field, meta, form }: FieldProps) => (
<CurrencyInput
{...field}
Expand Down Expand Up @@ -316,7 +319,7 @@
return (
<>
<LayoutSection pt={3}>
<NavOverviewCard poolId={pool.id} updatedPrices={form.values.feed} />
<NavOverviewCard poolId={pool.id} changeInValuation={changeInValuation.toDecimal().toNumber()} />
</LayoutSection>

<Stack pb={8}>
Expand All @@ -327,10 +330,10 @@
<Text variant="heading3">Confirm NAV</Text>
<VisualNavCard
currency={pool.currency}
current={pool.nav.total.toFloat()}
change={newNav - pool.nav.total.toFloat()}
aum={totalAum.toNumber()}
change={changeInValuation.toDecimal().toNumber()}
pendingFees={pendingFees.toFloat()}
pendingNav={newNav}
pendingNav={pendingNav.toNumber()}
/>
</Stack>
{pool.tranches.length === 1 && (
Expand Down Expand Up @@ -408,7 +411,7 @@
)
}

export function NavOverviewCard({ poolId, updatedPrices }: { poolId: string; updatedPrices: FormValues['feed'] }) {
export function NavOverviewCard({ poolId, changeInValuation }: { poolId: string; changeInValuation: number }) {
const pool = usePool(poolId)
const poolFees = usePoolFees(poolId)
const today = new Date()
Expand All @@ -421,55 +424,28 @@
)
}, [poolFees, pool.currency.decimals])

const [allLoans] = useCentrifugeQuery(['loans', poolId], (cent) => cent.pools.getLoans([poolId]), {
enabled: !!poolId && !!pool,
})

const externalLoans = React.useMemo(
() =>
(allLoans?.filter(
// Keep external loans, except ones that are fully repaid
(l) =>
isExternalLoan(l) &&
l.status !== 'Closed' &&
l.status !== 'Created' &&
(!('presentValue' in l) || !l.presentValue.isZero())
) as ActiveLoan[]) ?? [],
[allLoans]
)

const changeInValuation = React.useMemo(() => {
return externalLoans.reduce((prev, curr) => {
const price = curr.currentPrice.toDecimal()
const quantity = (curr as ExternalLoan).pricing.outstandingQuantity.toDecimal()
const updatedPrice = Dec(updatedPrices.find((p) => p.id === curr.id)?.value || 0)
return CurrencyBalance.fromFloat(
prev.toDecimal().add(price.sub(updatedPrice).mul(quantity)).toString(),
pool.currency.decimals
)
}, new CurrencyBalance(0, pool.currency.decimals))
}, [externalLoans, pool?.nav, updatedPrices])
const totalAum = pool.nav.aum.toDecimal().add(pool.reserve.available.toDecimal())

return (
<VisualNavCard
currency={pool.currency}
current={pool.nav.total.toFloat()}
change={changeInValuation ? changeInValuation.toDecimal().toNumber() : 0}
aum={totalAum.toNumber()}
change={changeInValuation ?? 0}
pendingFees={pendingFees.toFloat()}
pendingNav={changeInValuation.toDecimal().add(pool.nav.total.toDecimal()).sub(pendingFees.toDecimal()).toNumber()}
pendingNav={totalAum.add(changeInValuation).sub(pendingFees.toDecimal()).toNumber()}
/>
)
}

export function VisualNavCard({
currency,
current,
aum,
change,
pendingFees,
pendingNav,
}: {
currency: Pick<CurrencyMetadata, 'displayName' | 'decimals'>
current: number
aum: number
change: number
pendingFees: number
pendingNav: number
Expand All @@ -478,9 +454,9 @@
<Stack p={2} maxWidth="444px" bg="backgroundTertiary" gap={2}>
<Shelf justifyContent="space-between">
<Text variant="body2" color="textPrimary">
Current NAV
AUM
</Text>
<Text variant="body2">{formatBalance(current, currency.displayName, 2)}</Text>
<Text variant="body2">{formatBalance(aum, currency.displayName, 2)}</Text>
</Shelf>
<Divider borderColor="statusInfoBg" />
<Stack gap={1}>
Expand Down
Loading