Skip to content

Commit 47e484b

Browse files
committed
chore: code improvement
1 parent aec795c commit 47e484b

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { unixnow } from "../shared/utils"
66
import { fassetToUsdPrice } from "./utils/prices"
77
import { SharedAnalytics } from "./shared"
88
import { AgentStatistics } from "./statistics"
9-
import { PRICE_FACTOR } from "../config/constants"
9+
import { MAX_BIPS, PRICE_FACTOR } from "../config/constants"
1010
import { COLLATERAL_POOL_PORTFOLIO_SQL } from "./utils/raw-sql"
1111
import type {
1212
AmountResult,
@@ -16,6 +16,10 @@ import type {
1616
} from "./interface"
1717

1818

19+
const add = (x: bigint, y: bigint) => x + y
20+
const sub = (x: bigint, y: bigint) => x - y
21+
const rat = (x: bigint, y: bigint) => Number(MAX_BIPS * x / y) / Number(MAX_BIPS)
22+
1923
/**
2024
* DashboardAnalytics provides a set of analytics functions for the FAsset UI's dashboard.
2125
* It is seperated in case of UI's opensource release, and subsequent simplified indexer deployment.
@@ -280,10 +284,8 @@ export class DashboardAnalytics extends SharedAnalytics {
280284
const trackedAgentBalance = await this.trackedAgentBackingTimespan(timestamps)
281285
const coreVaultBalance = await this.coreVaultBalanceTimespan(timestamps)
282286
const fassetSupply = await this.fAssetSupplyTimespan(timestamps)
283-
const underlyingBacking = this.transformFAssetTimespan
284-
(trackedAgentBalance, coreVaultBalance, (x, y) => x + y)
285-
const transformer = (x, y) => Number(PRICE_FACTOR * x / y) / Number(PRICE_FACTOR)
286-
return this.transformFAssetTimespan(underlyingBacking, fassetSupply, transformer)
287+
const underlyingBacking = this.transformFAssetTimespan(trackedAgentBalance, coreVaultBalance, add)
288+
return this.transformFAssetTimespan(underlyingBacking, fassetSupply, rat)
287289
}
288290

289291
//////////////////////////////////////////////////////////////////////
@@ -300,7 +302,7 @@ export class DashboardAnalytics extends SharedAnalytics {
300302
return this.transformTimeSeries(
301303
await this.aggregateTimeSeries(redeemedTs),
302304
await this.aggregateTimeSeries(transferredTs),
303-
(x, y) => x - y
305+
sub
304306
)
305307
}
306308

@@ -322,13 +324,13 @@ export class DashboardAnalytics extends SharedAnalytics {
322324
async coreVaultOutflowAggregateTimeSeries(end: number, npoints: number, start?: number): Promise<TimeSeries<bigint>> {
323325
const ts1 = await this.coreVaultReturnOutflowAggregateTimeSeries(end, npoints, start)
324326
const ts2 = await this.coreVaultRedeemOutflowAggregateTimeSeries(end, npoints, start)
325-
return this.transformTimeSeries(ts1, ts2, (x, y) => x + y)
327+
return this.transformTimeSeries(ts1, ts2, add)
326328
}
327329

328330
async coreVaultBalanceAggregateTimeSeries(end: number, npoints: number, start?: number): Promise<TimeSeries<bigint>> {
329331
const ts1 = await this.coreVaultInflowAggregateTimeSeries(end, npoints, start)
330332
const ts2 = await this.coreVaultOutflowAggregateTimeSeries(end, npoints, start)
331-
return this.transformTimeSeries(ts1, ts2, (x, y) => x - y)
333+
return this.transformTimeSeries(ts1, ts2, sub)
332334
}
333335

334336
async mintedTimeSeries(end: number, npoints: number, start?: number): Promise<FAssetTimeSeries<bigint>> {
@@ -422,7 +424,7 @@ export class DashboardAnalytics extends SharedAnalytics {
422424
return this.transformFAssetValueResults(
423425
this.convertOrmResultToFAssetValueResult(redeemed, 'value'),
424426
this.convertOrmResultToFAssetValueResult(transferred, 'value'),
425-
(x, y) => x - y)
427+
sub)
426428
}
427429

428430
protected async coreVaultTransferredDuring(em: EntityManager, from: number, to: number): Promise<FAssetValueResult> {
@@ -472,16 +474,16 @@ export class DashboardAnalytics extends SharedAnalytics {
472474
protected async coreVaultOutflowDuring(em: EntityManager, from: number, to: number): Promise<FAssetValueResult> {
473475
const returns = await this.coreVaultReturnOutflowDuring(em, from, to)
474476
const redeems = await this.coreVaultRedeemOutflowDuring(em, from, to)
475-
return this.transformFAssetValueResults(returns, redeems, (x, y) => x + y)
477+
return this.transformFAssetValueResults(returns, redeems, add)
476478
}
477479

478480
protected async coreVaultBalanceDuring(em: EntityManager, from: number, to: number): Promise<FAssetValueResult> {
479481
const inflow = await this.coreVaultInflowDuring(em, from, to)
480482
const outflow = await this.coreVaultOutflowDuring(em, from, to)
481-
return this.transformFAssetValueResults(inflow, outflow, (x, y) => x - y)
483+
return this.transformFAssetValueResults(inflow, outflow, sub)
482484
}
483485

484-
protected async trackedAgentBackingDuring(em: EntityManager, from: number, to: number): Promise<FAssetValueResult> {
486+
async trackedAgentBackingDuring(em: EntityManager, from: number, to: number): Promise<FAssetValueResult> {
485487
const knex = em.getKnex()
486488
const subquery = em.createQueryBuilder(Entities.UnderlyingBalanceChanged, 'ubc')
487489
.select(['ubc.fasset', 'ubc.agentVault', raw('max(ubc.balance_uba) as agent_balance')])

0 commit comments

Comments
 (0)