Skip to content

Commit 5d533be

Browse files
committed
feat: fxrp launch api controller
1 parent c343d69 commit 5d533be

File tree

5 files changed

+89
-6
lines changed

5 files changed

+89
-6
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,24 +746,24 @@ export class DashboardAnalytics extends SharedAnalytics {
746746
)) as FAssetAmountResult
747747
}
748748

749-
private convertOrmResultToFAssetValueResult<K extends string>(
749+
protected convertOrmResultToFAssetValueResult<K extends string>(
750750
result: ({ fasset: number } & { [key in K]: string | bigint })[], key: K
751751
): FAssetValueResult {
752752
const ret = {} as FAssetValueResult
753753
for (const x of result) {
754754
ret[FAssetType[x.fasset] as FAsset] = { value: BigInt(x?.[key] ?? 0) }
755755
}
756-
return ret
756+
return this.complementFAssetValueResult(ret)
757757
}
758758

759-
private convertOrmResultToFAssetAmountResult<K extends string>(
759+
protected convertOrmResultToFAssetAmountResult<K extends string>(
760760
result: ({ fasset: number } & { [key in K]: string | number })[], key: K
761761
): FAssetAmountResult {
762762
const ret = {} as FAssetAmountResult
763763
for (const x of result) {
764764
ret[FAssetType[x.fasset] as FAsset] = { amount: Number(x?.[key] ?? 0) }
765765
}
766-
return ret
766+
return this.complementFAssetAmountResult(ret)
767767
}
768768

769769
private convertFAssetTimeSeriesToFAssetTimespan(timeseries: FAssetTimeSeries<bigint>): FAssetTimespan<bigint> {
@@ -782,4 +782,22 @@ export class DashboardAnalytics extends SharedAnalytics {
782782
return timespan
783783
}
784784

785+
private complementFAssetValueResult(result: FAssetValueResult): FAssetValueResult {
786+
for (const fasset of this.supportedFAssets) {
787+
if (result[fasset] == null) {
788+
result[fasset] = { value: BigInt(0) }
789+
}
790+
}
791+
return result
792+
}
793+
794+
private complementFAssetAmountResult(result: FAssetAmountResult): FAssetAmountResult {
795+
for (const fasset of this.supportedFAssets) {
796+
if (result[fasset] == null) {
797+
result[fasset] = { amount: 0 }
798+
}
799+
}
800+
return result
801+
}
802+
785803
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { FAssetType } from "fasset-indexer-core"
2+
import { EntityManager, raw } from "fasset-indexer-core/orm"
3+
import * as Entities from "fasset-indexer-core/entities"
4+
import { DashboardAnalytics } from "./dashboard"
5+
import type { FAssetValueResult } from "./interface"
6+
7+
export class FxrpLaunchAnalytics extends DashboardAnalytics {
8+
9+
public mintedByUserFromTimestamp(user: string, from: number): Promise<FAssetValueResult> {
10+
return this._mintedByUserFromTimestamp(this.orm.em.fork(), user, from)
11+
}
12+
13+
protected async _mintedByUserFromTimestamp(em: EntityManager, user: string, from: number): Promise<FAssetValueResult> {
14+
const minted = await em.createQueryBuilder(Entities.MintingExecuted, 'me')
15+
.select(['me.fasset', raw('sum(cr.value_uba) as value')])
16+
.join('me.collateralReserved', 'cr')
17+
.join('me.evmLog', 'el')
18+
.join('el.block', 'block')
19+
.join('cr.minter', 'cm')
20+
.where({ 'block.timestamp': { $gte: from }, 'cm.hex': user })
21+
.groupBy('fasset')
22+
.execute() as { fasset: FAssetType, value: string }[]
23+
return this.convertOrmResultToFAssetValueResult(minted, 'value')
24+
}
25+
}

packages/fasset-indexer-api/src/app.module.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { StatisticsController } from './controllers/statistics.controller'
1010
import { MetadataController } from './controllers/metadata.controller'
1111
import { ExplorerService } from './services/explorer.service'
1212
import { ExplorerController } from './controllers/explorer.controller'
13+
import { FxrpLaunchService } from './services/fxrp-launch.service'
14+
import { FxrpLaunch } from './controllers/fxrp-launch.controller'
1315
import { ApiConfigLoader } from './config/config'
1416
import { ApiContext } from './config/context'
1517
import { CACHE_MAX_ENTRIES, CACHE_TTL_MS } from './config/constants'
@@ -33,15 +35,17 @@ const apiContextProvider = {
3335
ExplorerController,
3436
NotificationController,
3537
MetadataController,
36-
StatisticsController
38+
StatisticsController,
39+
FxrpLaunch
3740
],
3841
providers: [
3942
apiContextProvider,
4043
DashboardService,
4144
ExplorerService,
4245
NotificationService,
4346
MetadataService,
44-
StatisticsService
47+
StatisticsService,
48+
FxrpLaunchService
4549
]
4650
})
4751
export class FAssetIndexerModule {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { CacheInterceptor } from "@nestjs/cache-manager"
2+
import { ApiOperation, ApiParam, ApiTags } from "@nestjs/swagger"
3+
import { Controller, Get, Param, UseInterceptors } from "@nestjs/common"
4+
import { FxrpLaunchService } from "../services/fxrp-launch.service"
5+
import { ApiResponse, apiResponse } from "../shared/api-response"
6+
import { FAssetValueResult } from "../analytics/interface"
7+
8+
@ApiTags('FXRP Launch')
9+
@UseInterceptors(CacheInterceptor)
10+
@Controller('api/fxrp-launch')
11+
export class FxrpLaunch {
12+
constructor(private readonly service: FxrpLaunchService) { }
13+
14+
@Get('/total-minted/:user/:from')
15+
@ApiOperation({ summary: 'Amount of FAsset user has minted from given timestamp on' })
16+
@ApiParam({ name: 'user', type: String, description: 'minting user' })
17+
@ApiParam({ name: "from", type: Number, description: 'time of first allowed minting' })
18+
getPoolCollateralDiff(
19+
@Param('user') user: string,
20+
@Param('from') from: number
21+
): Promise<ApiResponse<FAssetValueResult>> {
22+
return apiResponse(this.service.mintedByUserFromTimestamp(user, from), 200)
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Injectable, Inject } from '@nestjs/common'
2+
import { FxrpLaunchAnalytics } from '../analytics/fxrp-launch'
3+
import type { ApiContext } from '../config/context'
4+
5+
6+
@Injectable()
7+
export class FxrpLaunchService extends FxrpLaunchAnalytics {
8+
9+
constructor(@Inject('apiContext') config: ApiContext) {
10+
super(config.orm, config.chain, config.loader.addressesJson)
11+
}
12+
}

0 commit comments

Comments
 (0)