Skip to content

Commit 23ce0e1

Browse files
committed
feat: previous collateral, debt, ltv and health to form
1 parent 1fb7656 commit 23ce0e1

File tree

3 files changed

+88
-15
lines changed

3 files changed

+88
-15
lines changed

apps/main/src/llamalend/features/manage-loan/components/AddCollateralForm.tsx

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import { useMemo } from 'react'
12
import { useLoanToValueFromUserState } from '@/llamalend/features/manage-loan/hooks/useLoanToValueFromUserState'
3+
import { useHealthQueries } from '@/llamalend/hooks/useHealthQueries'
24
import type { LlamaMarketTemplate, NetworkDict } from '@/llamalend/llamalend.types'
35
import type { AddCollateralOptions } from '@/llamalend/mutations/add-collateral.mutation'
46
import { useMarketRates } from '@/llamalend/queries/market-rates'
7+
import { getUserHealthOptions } from '@/llamalend/queries/user-health.query'
8+
import { mapQuery } from '@/llamalend/queries/utils'
9+
import type { Query } from '@/llamalend/widgets/manage-loan/loan.types'
510
import { LoanFormAlerts } from '@/llamalend/widgets/manage-loan/LoanFormAlerts'
611
import { LoanFormTokenInput } from '@/llamalend/widgets/manage-loan/LoanFormTokenInput'
712
import { LoanFormWrapper } from '@/llamalend/widgets/manage-loan/LoanFormWrapper'
@@ -11,9 +16,15 @@ import Button from '@mui/material/Button'
1116
import Stack from '@mui/material/Stack'
1217
import { useSwitch } from '@ui-kit/hooks/useSwitch'
1318
import { t } from '@ui-kit/lib/i18n'
19+
import { decimal } from '@ui-kit/utils'
1420
import { InputDivider } from '../../../widgets/InputDivider'
1521
import { useAddCollateralForm } from '../hooks/useAddCollateralForm'
1622

23+
const withTokenSymbol = <T,>(query: Query<T | null>, tokenSymbol?: string) => ({
24+
...query,
25+
tokenSymbol,
26+
})
27+
1728
export const AddCollateralForm = <ChainId extends IChainId>({
1829
market,
1930
networks,
@@ -37,15 +48,14 @@ export const AddCollateralForm = <ChainId extends IChainId>({
3748
action,
3849
params,
3950
values,
40-
bands,
4151
health,
42-
prices,
4352
gas,
4453
isApproved,
4554
formErrors,
4655
collateralToken,
4756
borrowToken,
4857
txHash,
58+
userState,
4959
} = useAddCollateralForm({
5060
market,
5161
network,
@@ -54,30 +64,74 @@ export const AddCollateralForm = <ChainId extends IChainId>({
5464
onAdded,
5565
})
5666

57-
const marketRates = useMarketRates(params, isOpen)
67+
const prevCollateral = useMemo(
68+
() =>
69+
withTokenSymbol(
70+
mapQuery(userState, (state) => state?.collateral),
71+
collateralToken?.symbol,
72+
),
73+
[collateralToken?.symbol, userState],
74+
)
75+
const prevDebt = useMemo(
76+
() =>
77+
withTokenSymbol(
78+
mapQuery(userState, (state) => state?.debt),
79+
borrowToken?.symbol,
80+
),
81+
[borrowToken?.symbol, userState],
82+
)
83+
const prevLoanToValue = useLoanToValueFromUserState({
84+
chainId,
85+
marketId: params.marketId,
86+
userAddress: params.userAddress,
87+
collateralToken,
88+
borrowToken,
89+
enabled: isOpen,
90+
})
91+
const prevHealth = useHealthQueries((isFull) => getUserHealthOptions({ ...params, isFull }, undefined))
5892

93+
const collateral = useMemo(
94+
() =>
95+
withTokenSymbol(
96+
{
97+
...mapQuery(userState, (state) => state?.collateral),
98+
data: decimal(
99+
values.userCollateral
100+
? +values.userCollateral + (userState.data?.collateral ? +userState.data?.collateral : 0)
101+
: null,
102+
),
103+
},
104+
collateralToken?.symbol,
105+
),
106+
[collateralToken?.symbol, userState, values.userCollateral],
107+
)
108+
const marketRates = useMarketRates(params, isOpen)
109+
const loanToValue = useLoanToValueFromUserState({
110+
chainId: params.chainId!,
111+
marketId: params.marketId,
112+
userAddress: params.userAddress,
113+
collateralToken,
114+
borrowToken,
115+
enabled: !!enabled && !!values.userCollateral,
116+
collateralDelta: values.userCollateral,
117+
})
59118
return (
60119
<LoanFormWrapper
61120
{...form}
62121
onSubmit={onSubmit}
63122
infoAccordion={
64-
<LoanInfoAccordion // todo: prevHealth, prevRates, debt, prevDebt
123+
<LoanInfoAccordion // todo: prevRates
65124
isOpen={isOpen}
66125
toggle={toggle}
126+
prevHealth={prevHealth}
67127
health={health}
68-
bands={bands}
69-
prices={prices}
70128
rates={marketRates}
71-
loanToValue={useLoanToValueFromUserState({
72-
chainId: params.chainId!,
73-
marketId: params.marketId,
74-
userAddress: params.userAddress,
75-
collateralToken,
76-
borrowToken,
77-
enabled: isOpen,
78-
collateralDelta: values.userCollateral,
79-
})}
129+
prevLoanToValue={prevLoanToValue}
130+
loanToValue={loanToValue}
131+
prevDebt={prevDebt}
80132
gas={gas}
133+
prevCollateral={prevCollateral}
134+
collateral={collateral}
81135
/>
82136
}
83137
>

apps/main/src/llamalend/features/manage-loan/hooks/useAddCollateralForm.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useAddCollateralBands } from '@/llamalend/queries/add-collateral/add-co
1111
import { useAddCollateralEstimateGas } from '@/llamalend/queries/add-collateral/add-collateral-gas-estimate.query'
1212
import { getAddCollateralHealthOptions } from '@/llamalend/queries/add-collateral/add-collateral-health.query'
1313
import { useAddCollateralPrices } from '@/llamalend/queries/add-collateral/add-collateral-prices.query'
14+
import { useUserState } from '@/llamalend/queries/user-state.query'
1415
import type { CollateralParams } from '@/llamalend/queries/validation/manage-loan.types'
1516
import {
1617
collateralFormValidationSuite,
@@ -81,6 +82,7 @@ export const useAddCollateralForm = <ChainId extends LlamaChainId>({
8182

8283
useCallbackAfterFormUpdate(form, action.reset)
8384

85+
const userState = useUserState(params, enabled)
8486
const bands = useAddCollateralBands(params, enabled)
8587
const health = useHealthQueries((isFull) => getAddCollateralHealthOptions({ ...params, isFull }, enabled))
8688
const prices = useAddCollateralPrices(params, enabled)
@@ -104,5 +106,6 @@ export const useAddCollateralForm = <ChainId extends LlamaChainId>({
104106
collateralToken,
105107
borrowToken,
106108
txHash: action.data?.hash,
109+
userState,
107110
}
108111
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { Query } from '../widgets/manage-loan/loan.types'
2+
3+
/**
4+
* Maps a Query type to extract partial data from it.
5+
* Preserves error and loading states while transforming the data.
6+
*/
7+
export function mapQuery<TSource, TResult>(
8+
query: Query<TSource>,
9+
selector: (data: TSource) => TResult,
10+
): Query<TResult> {
11+
return {
12+
data: query.data === undefined ? undefined : selector(query.data),
13+
isLoading: query.isLoading,
14+
error: query.error,
15+
}
16+
}

0 commit comments

Comments
 (0)