11import { cloneDeep } from 'lodash'
22import { zeroAddress } from 'viem'
3- import type { MintMarketTemplate } from '@curvefi/llamalend-api/lib/mintMarkets'
43import { BN } from '@ui/utils'
54import { requireLib } from '@ui-kit/features/connect-wallet'
65import { queryFactory , rootKeys , type UserMarketParams , type UserMarketQuery } from '@ui-kit/lib/model'
@@ -27,7 +26,6 @@ const DEFAULT_USER_STATE = {
2726}
2827
2928export type UserLoanDetails = {
30- loading : boolean
3129 healthFull : string
3230 healthNotFull : string
3331 userBands : number [ ]
@@ -81,112 +79,87 @@ function getLiquidationStatus(healthNotFull: string, userIsCloseToLiquidation: b
8179 return userStatus
8280}
8381
84- async function userLoanPartialInfo ( params : { market : MintMarketTemplate ; userAddress : string ; loanExists : boolean } ) {
85- const { market, userAddress, loanExists } = params
86-
87- const [
88- healthFullResult ,
89- healthNotFullResult ,
90- userBandsResult ,
91- userStateResult ,
92- liquidatingBandResult ,
93- oraclePriceBandResult ,
94- ] = await Promise . allSettled ( [
95- loanExists ? market . userHealth ( true , userAddress ) : Promise . resolve ( '' ) ,
96- loanExists ? market . userHealth ( false , userAddress ) : Promise . resolve ( '' ) ,
97- loanExists ? market . userBands ( userAddress ) : Promise . resolve ( [ 0 , 0 ] ) ,
98- loanExists ? market . userState ( userAddress ) : Promise . resolve ( DEFAULT_USER_STATE ) ,
99- loanExists ? market . stats . liquidatingBand ( ) : Promise . resolve ( null ) ,
100- loanExists ? market . oraclePriceBand ( ) : Promise . resolve ( null ) ,
101- ] )
102-
103- const healthFull = fulfilledValue ( healthFullResult ) ?? ''
104- const healthNotFull = fulfilledValue ( healthNotFullResult ) ?? ''
105- const userBands = fulfilledValue ( userBandsResult ) ?? ( [ 0 , 0 ] as [ number , number ] )
106- const userState = fulfilledValue ( userStateResult ) ?? DEFAULT_USER_STATE
107- const userLiquidationBand = fulfilledValue ( liquidatingBandResult ) ?? null
108- const oraclePriceBand = fulfilledValue ( oraclePriceBandResult ) ?? null
109-
110- const reversedUserBands = reverseBands ( userBands )
111- const userIsCloseToLiquidation = getIsUserCloseToLiquidation (
112- reversedUserBands [ 0 ] ,
113- userLiquidationBand ,
114- oraclePriceBand ,
115- )
116-
117- const fetchedUserDetails : {
118- healthFull : UserLoanDetails [ 'healthFull' ]
119- healthNotFull : UserLoanDetails [ 'healthNotFull' ]
120- userBands : UserLoanDetails [ 'userBands' ]
121- userHealth : UserLoanDetails [ 'userHealth' ]
122- userIsCloseToLiquidation : UserLoanDetails [ 'userIsCloseToLiquidation' ]
123- userState : UserLoanDetails [ 'userState' ]
124- userLiquidationBand : UserLoanDetails [ 'userLiquidationBand' ]
125- } = {
126- healthFull,
127- healthNotFull,
128- userBands : reversedUserBands ,
129- userHealth : + healthNotFull < 0 ? healthNotFull : healthFull ,
130- userIsCloseToLiquidation,
131- userState,
132- userLiquidationBand,
133- }
134-
135- return fetchedUserDetails
136- }
137-
138- async function userLoanInfo ( params : { market : MintMarketTemplate ; userAddress : string } ) {
139- const { market, userAddress } = params
140- const loanExists = await market . loanExists ( )
141- const fetchedPartialUserLoanInfo = await userLoanPartialInfo ( { ...params , loanExists } )
142-
143- const [ userBandsRangeResult , userPricesResult , userLossResult , userBandsBalancesResult ] = await Promise . allSettled ( [
144- loanExists ? market . userRange ( userAddress ) : Promise . resolve ( 0 ) ,
145- loanExists ? market . userPrices ( userAddress ) : Promise . resolve ( [ ] as string [ ] ) ,
146- loanExists ? market . userLoss ( userAddress ) : Promise . resolve ( DEFAULT_USER_LOSS ) ,
147- loanExists ? market . userBandsBalances ( userAddress ) : Promise . resolve ( DEFAULT_BAND_BALANCES ) ,
148- ] )
149-
150- const userBandsRange = fulfilledValue ( userBandsRangeResult ) ?? null
151- const userPrices = fulfilledValue ( userPricesResult ) ?? ( [ ] as string [ ] )
152- const userLoss = fulfilledValue ( userLossResult ) ?? DEFAULT_USER_LOSS
153- const userBandsBalances = fulfilledValue ( userBandsBalancesResult ) ?? DEFAULT_BAND_BALANCES
154-
155- const { healthNotFull, userState, userIsCloseToLiquidation, userLiquidationBand } = fetchedPartialUserLoanInfo
156-
157- const parsedBandsBalances = await getChartBandBalancesData ( sortBands ( userBandsBalances ) , userLiquidationBand , market )
158-
159- const fetchedUserDetails : UserLoanDetails = {
160- loading : false ,
161- ...fetchedPartialUserLoanInfo ,
162- userBandsBalances : parsedBandsBalances ,
163- userBandsRange,
164- userBandsPct : userBandsRange ? market . calcRangePct ( userBandsRange ) : '0' ,
165- userPrices,
166- userLoss : parseUserLoss ( userLoss ) ,
167- userStatus : getLiquidationStatus ( healthNotFull , userIsCloseToLiquidation , userState . stablecoin ) ,
168- }
169-
170- return fetchedUserDetails
171- }
172-
17382export const {
17483 useQuery : useUserLoanDetails ,
17584 fetchQuery : fetchUserLoanDetails ,
17685 getQueryData : getUserLoanDetails ,
17786 invalidate : invalidateUserLoanDetails ,
17887} = queryFactory ( {
17988 queryKey : ( params : UserMarketParams ) => [ ...rootKeys . userMarket ( params ) , 'user-loan-details' ] as const ,
180- queryFn : async ( { marketId, userAddress } : UserMarketQuery ) => {
89+ queryFn : async ( { marketId, userAddress : rawUserAddress } : UserMarketQuery ) => {
18190 const api = requireLib ( 'llamaApi' )
18291 const market = api . getMintMarket ( marketId )
18392
184- return userLoanInfo ( {
93+ // Because of the validation suite we can't pass an empty address ('' or undefined),
94+ // so the caller has to explicitly set it to the zero address instead.
95+ const userAddress = rawUserAddress === zeroAddress ? '' : rawUserAddress
96+
97+ const loanExists = await market . loanExists ( userAddress )
98+
99+ const [
100+ healthFullResult ,
101+ healthNotFullResult ,
102+ userBandsResult ,
103+ userStateResult ,
104+ liquidatingBandResult ,
105+ oraclePriceBandResult ,
106+ userBandsRangeResult ,
107+ userPricesResult ,
108+ userLossResult ,
109+ userBandsBalancesResult ,
110+ ] = await Promise . allSettled ( [
111+ loanExists ? market . userHealth ( true , userAddress ) : Promise . resolve ( '' ) ,
112+ loanExists ? market . userHealth ( false , userAddress ) : Promise . resolve ( '' ) ,
113+ loanExists ? market . userBands ( userAddress ) : Promise . resolve ( [ 0 , 0 ] ) ,
114+ loanExists ? market . userState ( userAddress ) : Promise . resolve ( DEFAULT_USER_STATE ) ,
115+ loanExists ? market . stats . liquidatingBand ( ) : Promise . resolve ( null ) ,
116+ loanExists ? market . oraclePriceBand ( ) : Promise . resolve ( null ) ,
117+ loanExists ? market . userRange ( userAddress ) : Promise . resolve ( 0 ) ,
118+ loanExists ? market . userPrices ( userAddress ) : Promise . resolve ( [ ] as string [ ] ) ,
119+ loanExists ? market . userLoss ( userAddress ) : Promise . resolve ( DEFAULT_USER_LOSS ) ,
120+ loanExists ? market . userBandsBalances ( userAddress ) : Promise . resolve ( DEFAULT_BAND_BALANCES ) ,
121+ ] )
122+
123+ const healthFull = fulfilledValue ( healthFullResult ) ?? ''
124+ const healthNotFull = fulfilledValue ( healthNotFullResult ) ?? ''
125+ const userBands = fulfilledValue ( userBandsResult ) ?? ( [ 0 , 0 ] as [ number , number ] )
126+ const userState = fulfilledValue ( userStateResult ) ?? DEFAULT_USER_STATE
127+ const userLiquidationBand = fulfilledValue ( liquidatingBandResult ) ?? null
128+ const oraclePriceBand = fulfilledValue ( oraclePriceBandResult ) ?? null
129+ const userBandsRange = fulfilledValue ( userBandsRangeResult ) ?? null
130+ const userPrices = fulfilledValue ( userPricesResult ) ?? ( [ ] as string [ ] )
131+ const userLoss = fulfilledValue ( userLossResult ) ?? DEFAULT_USER_LOSS
132+ const userBandsBalances = fulfilledValue ( userBandsBalancesResult ) ?? DEFAULT_BAND_BALANCES
133+
134+ const parsedBandsBalances = await getChartBandBalancesData (
135+ sortBands ( userBandsBalances ) ,
136+ userLiquidationBand ,
185137 market ,
186- // Because of the validation suite we can't pass an empty address ('' or undefined),
187- // so the caller has to explicitly set it to the zero address instead.
188- userAddress : userAddress === zeroAddress ? '' : userAddress ,
189- } )
138+ )
139+ const reversedUserBands = reverseBands ( userBands )
140+ const userIsCloseToLiquidation = getIsUserCloseToLiquidation (
141+ reversedUserBands [ 0 ] ,
142+ userLiquidationBand ,
143+ oraclePriceBand ,
144+ )
145+
146+ const fetchedUserDetails : UserLoanDetails = {
147+ healthFull,
148+ healthNotFull,
149+ userBands : reversedUserBands ,
150+ userHealth : + healthNotFull < 0 ? healthNotFull : healthFull ,
151+ userIsCloseToLiquidation,
152+ userState,
153+ userLiquidationBand,
154+ userBandsBalances : parsedBandsBalances ,
155+ userBandsRange,
156+ userBandsPct : userBandsRange ? market . calcRangePct ( userBandsRange ) : '0' ,
157+ userPrices,
158+ userLoss : parseUserLoss ( userLoss ) ,
159+ userStatus : getLiquidationStatus ( healthNotFull , userIsCloseToLiquidation , userState . stablecoin ) ,
160+ }
161+
162+ return fetchedUserDetails
190163 } ,
191164 staleTime : '1m' ,
192165 validationSuite : userMarketValidationSuite ,
0 commit comments