Skip to content

Commit b627072

Browse files
committed
fix: fix tracked-agent-backing query
1 parent 0f3ceec commit b627072

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

packages/fasset-indexer-api/src/analytics/dashboard.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { fassetToUsdPrice } from "./utils/prices"
77
import { SharedAnalytics } from "./shared"
88
import { AgentStatistics } from "./statistics"
99
import { 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"
1111
import 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> {

packages/fasset-indexer-api/src/analytics/utils/raw-sql.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,18 @@ left join underlying_vout_reference dvr on rr.payment_reference = dvr.reference
8383
left join redemption_default rd on rd.redemption_requested_evm_log_id = rr.evm_log_id
8484
where rr.fasset=${FAssetType.FDOGE} and eb.timestamp > ? and dvr.id is null and rd.evm_log_id is null
8585
limit ?`
86+
87+
// postgres-specific query
88+
export const UNDERLYING_AGENT_BALANCE = `
89+
SELECT t.fasset, SUM(t.balance_uba) AS total_balance FROM (
90+
SELECT DISTINCT ON (ubc.fasset, ubc.agent_vault_address_id)
91+
ubc.fasset, ubc.agent_vault_address_id, ubc.balance_uba, el.block_index
92+
FROM underlying_balance_changed ubc
93+
JOIN evm_log el ON ubc.evm_log_id = el.id
94+
JOIN evm_block eb ON el.block_index = eb.index
95+
WHERE eb.timestamp <= ?
96+
ORDER BY ubc.fasset, ubc.agent_vault_address_id, el.block_index DESC
97+
) t
98+
GROUP BY t.fasset
99+
ORDER BY t.fasset
100+
`

packages/fasset-indexer-api/src/controllers/dashboard.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ export class DashboardController {
6464
return apiResponse(this.service.redemptionDefault(id, FAssetType[fasset]), 200)
6565
}
6666

67-
@Get('/tracked-underlying-backing?')
67+
@Get('/total-tracked-underlying-agent-backing?')
6868
@ApiOperation({ summary: 'Total underlying assets tracked by the contracts' })
6969
getTrackedUnderlyingBacking(): Promise<ApiResponse<FAssetValueResult>>{
70-
return apiResponse(this.service.trackedUnderlyingBacking(), 200)
70+
return apiResponse(this.service.trackedAgentUnderlyingBacking(), 200)
7171
}
7272

7373
//////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)