@@ -7,7 +7,7 @@ import { fassetToUsdPrice } from "./utils/prices"
77import { SharedAnalytics } from "./shared"
88import { AgentStatistics } from "./statistics"
99import { MAX_BIPS , PRICE_FACTOR } from "../config/constants"
10- import { COLLATERAL_POOL_PORTFOLIO_SQL } from "./utils/raw-sql"
10+ import { COLLATERAL_POOL_PORTFOLIO_SQL , UNDERLYING_AGENT_BALANCE } from "./utils/raw-sql"
1111import type {
1212 AmountResult ,
1313 TimeSeries , Timespan , FAssetTimeSeries , FAssetTimespan ,
@@ -103,7 +103,7 @@ export class DashboardAnalytics extends SharedAnalytics {
103103 return this . convertOrmResultToFAssetValueResult ( settings , 'lotSizeAmg' )
104104 }
105105
106- async trackedUnderlyingBacking ( ) : Promise < FAssetValueResult > {
106+ async trackedAgentUnderlyingBacking ( ) : Promise < FAssetValueResult > {
107107 return this . trackedAgentBackingAt ( this . orm . em . fork ( ) , unixnow ( ) )
108108 }
109109
@@ -497,22 +497,9 @@ export class DashboardAnalytics extends SharedAnalytics {
497497 }
498498
499499 protected async trackedAgentBackingAt ( em : EntityManager , timestamp : number ) : Promise < FAssetValueResult > {
500- const knex = em . getKnex ( )
501- const subquery = em . createQueryBuilder ( Entities . UnderlyingBalanceChanged , 'ubc' )
502- . select ( [ 'ubc.fasset' , 'ubc.agentVault' , raw ( 'max(ubc.balance_uba) as agent_balance' ) ] )
503- . join ( 'ubc.evmLog' , 'el' )
504- . join ( 'el.block' , 'bl' )
505- . where ( { 'bl.timestamp' : { $lt : timestamp } } )
506- . groupBy ( [ 'ubc.fasset' , 'ubc.agentVault' ] )
507- . getKnexQuery ( )
508- const mainquery = knex
509- . from ( knex . raw ( '(?) as sub' , [ subquery ] ) )
510- . select ( [ 'sub.fasset' , knex . raw ( 'sum(sub.agent_balance) as total_agent_balance' ) ] )
511- . groupBy ( 'sub.fasset' )
512- const { sql, bindings } = mainquery . toSQL ( )
513- const result = await em . getConnection ( ) . execute ( sql , bindings as any ) as {
514- fasset : FAssetType , total_agent_balance : string } [ ]
515- return this . convertOrmResultToFAssetValueResult ( result , 'total_agent_balance' )
500+ const res = await em . getConnection ( 'read' ) . execute ( UNDERLYING_AGENT_BALANCE , [ timestamp ] ) as {
501+ fasset : FAssetType , total_balance : string } [ ]
502+ return this . convertOrmResultToFAssetValueResult ( res , 'total_balance' )
516503 }
517504
518505 protected async coreVaultInflowAt ( em : EntityManager , timestamp : number ) : Promise < FAssetValueResult > {
0 commit comments