@@ -31,6 +31,7 @@ import {
3131 adaptAccountStateFromSDK ,
3232 parseAssetName ,
3333} from '../utils/hyperLiquidAdapter' ;
34+ import { calculateWeightedReturnOnEquity } from '../utils/accountUtils' ;
3435import type { HyperLiquidClientService } from './HyperLiquidClientService' ;
3536import type { HyperLiquidWalletService } from './HyperLiquidWalletService' ;
3637import type { CaipAccountId } from '@metamask/utils' ;
@@ -476,6 +477,12 @@ export class HyperLiquidSubscriptionService {
476477 let totalMarginUsed = 0 ;
477478 let totalUnrealizedPnl = 0 ;
478479
480+ // Collect account states for weighted ROE calculation
481+ const accountStatesForROE : {
482+ unrealizedPnl : string ;
483+ returnOnEquity : string ;
484+ } [ ] = [ ] ;
485+
479486 // Aggregate all cached account states
480487 Array . from ( this . dexAccountCache . entries ( ) ) . forEach (
481488 ( [ currentDex , state ] ) => {
@@ -488,20 +495,21 @@ export class HyperLiquidSubscriptionService {
488495 totalBalance += parseFloat ( state . totalBalance ) ;
489496 totalMarginUsed += parseFloat ( state . marginUsed ) ;
490497 totalUnrealizedPnl += parseFloat ( state . unrealizedPnl ) ;
498+
499+ // Collect data for weighted ROE calculation
500+ accountStatesForROE . push ( {
501+ unrealizedPnl : state . unrealizedPnl ,
502+ returnOnEquity : state . returnOnEquity ,
503+ } ) ;
491504 } ,
492505 ) ;
493506
494507 // Use first DEX's account state as base and override aggregated values
495508 const firstDexAccount =
496509 this . dexAccountCache . values ( ) . next ( ) . value || ( { } as AccountState ) ;
497510
498- // Calculate returnOnEquity across all DEXs (same formula as HyperLiquidProvider.getAccountState)
499- let returnOnEquity = '0' ;
500- if ( totalMarginUsed > 0 ) {
501- returnOnEquity = ( ( totalUnrealizedPnl / totalMarginUsed ) * 100 ) . toFixed (
502- 1 ,
503- ) ;
504- }
511+ // Calculate weighted returnOnEquity across all DEXs
512+ const returnOnEquity = calculateWeightedReturnOnEquity ( accountStatesForROE ) ;
505513
506514 return {
507515 ...firstDexAccount ,
0 commit comments