@@ -67,41 +67,77 @@ async function getMarketsInfo(clients: ContractClients, chainConfig: ChainConfig
6767 } ) ,
6868 )
6969
70- // For Neutron USDC, fetch the actual bank balance of the redBank contract
71- // This is needed because there's bad debt that makes the contract state inaccurate
70+ // For Neutron USDC, fetch the actual bank balance AND true total deposits
71+ // This is needed because there's bad debt that makes the Red Bank's internal state inaccurate
7272 let usdcActualBalance : BigNumber | null = null
73+ let usdcTrueDeposits : BigNumber | null = null
7374 if ( chainConfig . id === ChainInfoID . Neutron1 ) {
75+ // Fetch actual USDC balance in the redBank contract
7476 usdcActualBalance = await getRedBankBalance (
7577 chainConfig . endpoints . rest ,
7678 chainConfig . contracts . redBank ,
7779 NEUTRON_USDC_DENOM ,
7880 )
81+ // Fetch true total deposits from params contract (not affected by bad debt accounting)
82+ try {
83+ const totalDeposit = await clients . params . totalDeposit ( { denom : NEUTRON_USDC_DENOM } )
84+ usdcTrueDeposits = BN ( totalDeposit . amount )
85+ } catch ( error ) {
86+ console . error ( 'Failed to fetch USDC total deposits:' , error )
87+ }
7988 }
8089
8190 const [ debts , liquidity ] = await Promise . all ( [ Promise . all ( debts$ ) , Promise . all ( liquidities$ ) ] )
8291
8392 return markets . map ( ( market , index ) => {
84- const deposits = BN ( liquidity [ index ] )
93+ const redBankDeposits = BN ( liquidity [ index ] )
94+ const contractDebt = BN ( debts [ index ] )
8595
86- // For USDC on Neutron, use actual bank balance as liquidity and derive debt from it
87- if ( chainConfig . id === ChainInfoID . Neutron1 && market . denom === NEUTRON_USDC_DENOM && usdcActualBalance !== null ) {
88- const actualLiquidity = BigNumber . max ( usdcActualBalance , BN_ZERO )
89- const derivedDebt = BigNumber . max ( deposits . minus ( actualLiquidity ) , BN_ZERO )
96+ // For USDC on Neutron, use actual bank balance as liquidity and derive debt from true deposits
97+ // The Red Bank's internal accounting has been impacted by bad debt, so we use params contract's
98+ // totalDeposit as the "true" deposits to correctly calculate the borrowed amount
99+ const isNeutronUsdc = chainConfig . id === ChainInfoID . Neutron1 && market . denom === NEUTRON_USDC_DENOM
100+ const shouldAdjustForBadDebt = isNeutronUsdc && usdcActualBalance && usdcActualBalance . isGreaterThan ( 0 ) && usdcTrueDeposits
101+
102+ if ( shouldAdjustForBadDebt ) {
103+ // Use true deposits from params contract for accurate debt calculation
104+ const trueDeposits = usdcTrueDeposits !
105+ const actualLiquidity = usdcActualBalance !
106+ // Derived debt = true deposits - actual balance in contract
107+ const derivedDebt = BigNumber . max ( trueDeposits . minus ( actualLiquidity ) , BN_ZERO )
90108
91109 return {
92- ...market ,
110+ borrow_index : market . borrow_index ,
111+ borrow_rate : market . borrow_rate ,
112+ collateral_total_scaled : market . collateral_total_scaled ,
113+ debt_total_scaled : market . debt_total_scaled ,
114+ denom : market . denom ,
115+ indexes_last_updated : market . indexes_last_updated ,
116+ interest_rate_model : market . interest_rate_model ,
117+ liquidity_index : market . liquidity_index ,
118+ liquidity_rate : market . liquidity_rate ,
119+ reserve_factor : market . reserve_factor ,
93120 debt : derivedDebt ,
94- deposits,
121+ deposits : trueDeposits ,
95122 liquidity : actualLiquidity ,
96123 }
97124 }
98125
99126 // Default logic for all other markets
100127 return {
101- ...market ,
102- debt : BN ( debts [ index ] ) ,
103- deposits,
104- liquidity : BigNumber . max ( deposits . minus ( debts [ index ] ) , BN_ZERO ) ,
128+ borrow_index : market . borrow_index ,
129+ borrow_rate : market . borrow_rate ,
130+ collateral_total_scaled : market . collateral_total_scaled ,
131+ debt_total_scaled : market . debt_total_scaled ,
132+ denom : market . denom ,
133+ indexes_last_updated : market . indexes_last_updated ,
134+ interest_rate_model : market . interest_rate_model ,
135+ liquidity_index : market . liquidity_index ,
136+ liquidity_rate : market . liquidity_rate ,
137+ reserve_factor : market . reserve_factor ,
138+ debt : contractDebt ,
139+ deposits : redBankDeposits ,
140+ liquidity : BigNumber . max ( redBankDeposits . minus ( contractDebt ) , BN_ZERO ) ,
105141 }
106142 } )
107143}
0 commit comments