Skip to content

Commit bcfdd29

Browse files
committed
feat: additional settings in json configs
1 parent aed6501 commit bcfdd29

File tree

14 files changed

+121
-97
lines changed

14 files changed

+121
-97
lines changed

configs/dashboard.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
{
2+
"events": {
3+
"logFetchBlockBatchSize": 29,
4+
"logFetchCycleSleepMs": 20000,
5+
"logFetchBlockHeightOffset": 10
6+
},
7+
"watchdog": {
8+
"cycleSleepMs": 60000
9+
},
210
"indexEvents": [
311
"AgentVaultCreated",
412
"AgentSettingChanged",

configs/everything.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
{
2+
"events": {
3+
"logFetchBlockBatchSize": 29,
4+
"logFetchCycleSleepMs": 10000,
5+
"logFetchBlockHeightOffset": 10
6+
},
7+
"watchdog": {
8+
"cycleSleepMs": 30000
9+
},
210
"indexEvents": [
311
"AgentVaultCreated",
412
"AgentSettingChanged",

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

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -401,84 +401,91 @@ export class ExplorerAnalytics {
401401
const ret: ExplorerType.GenericTransactionClassification = []
402402
const logs = await em.find(Entities.EvmLog, { transaction: { hash } }, { populate: [ 'transaction' ]})
403403
for (const log of logs) {
404-
let oglog: Entities.EvmLog | null = null
405-
if (
406-
log.name == EVENTS.ASSET_MANAGER.REDEMPTION_REQUESTED
407-
|| log.name == EVENTS.ASSET_MANAGER.COLLATERAL_RESERVED
408-
|| log.name == EVENTS.ASSET_MANAGER.TRANSFER_TO_CORE_VAULT_STARTED
409-
|| log.name == EVENTS.ASSET_MANAGER.RETURN_FROM_CORE_VAULT_REQUESTED
410-
|| log.name == EVENTS.ASSET_MANAGER.SELF_MINT
411-
|| log.name == EVENTS.ASSET_MANAGER.UNDERLYING_BALANCE_TOPPED_UP
412-
|| log.name == EVENTS.ASSET_MANAGER.UNDERLYING_WITHDRAWAL_ANNOUNCED
413-
) {
414-
oglog = log
415-
// core vault transfers
416-
} else if (log.name == EVENTS.ASSET_MANAGER.TRANSFER_TO_CORE_VAULT_SUCCESSFUL) {
417-
oglog = await em.findOneOrFail(Entities.TransferToCoreVaultSuccessful,
404+
const oglog = await this.nativeEventClassification(em, log)
405+
if (oglog == null) continue
406+
ret.push({
407+
eventName: log.name,
408+
transactionHash: oglog.transaction.hash
409+
})
410+
}
411+
return ret
412+
}
413+
414+
protected async nativeEventClassification(em: EntityManager, log: Entities.EvmLog): Promise<Entities.EvmLog | null> {
415+
switch (log.name) {
416+
case EVENTS.ASSET_MANAGER.COLLATERAL_RESERVED:
417+
return log
418+
case EVENTS.ASSET_MANAGER.REDEMPTION_REQUESTED:
419+
return log
420+
case EVENTS.ASSET_MANAGER.TRANSFER_TO_CORE_VAULT_STARTED:
421+
return log
422+
case EVENTS.ASSET_MANAGER.RETURN_FROM_CORE_VAULT_REQUESTED:
423+
return log
424+
case EVENTS.ASSET_MANAGER.SELF_MINT:
425+
return log
426+
case EVENTS.ASSET_MANAGER.UNDERLYING_BALANCE_TOPPED_UP:
427+
return log
428+
case EVENTS.ASSET_MANAGER.UNDERLYING_WITHDRAWAL_ANNOUNCED:
429+
return log
430+
case EVENTS.ASSET_MANAGER.TRANSFER_TO_CORE_VAULT_SUCCESSFUL:
431+
return em.findOneOrFail(Entities.TransferToCoreVaultSuccessful,
418432
{ evmLog: log }, { populate: [ 'transferToCoreVaultStarted.evmLog.transaction' ]}
419433
).then(x => x.transferToCoreVaultStarted.evmLog)
420-
} else if (log.name == EVENTS.ASSET_MANAGER.TRANSFER_TO_CORE_VAULT_DEFAULTED) {
421-
oglog = await em.findOneOrFail(Entities.TransferToCoreVaultDefaulted,
434+
case EVENTS.ASSET_MANAGER.TRANSFER_TO_CORE_VAULT_DEFAULTED:
435+
return em.findOneOrFail(Entities.TransferToCoreVaultDefaulted,
422436
{ evmLog: log }, { populate: [ 'transferToCoreVaultStarted.evmLog.transaction' ]}
423437
).then(x => x.transferToCoreVaultStarted.evmLog)
424-
// core vault returns
425-
} else if (log.name == EVENTS.ASSET_MANAGER.RETURN_FROM_CORE_VAULT_CONFIRMED) {
426-
oglog = await em.findOneOrFail(Entities.ReturnFromCoreVaultConfirmed,
438+
case EVENTS.ASSET_MANAGER.RETURN_FROM_CORE_VAULT_CONFIRMED:
439+
return em.findOneOrFail(Entities.ReturnFromCoreVaultConfirmed,
427440
{ evmLog: log }, { populate: [ 'returnFromCoreVaultRequested.evmLog.transaction' ] }
428441
).then(x => x.returnFromCoreVaultRequested.evmLog)
429-
} else if (log.name == EVENTS.ASSET_MANAGER.RETURN_FROM_CORE_VAULT_CANCELLED) {
430-
oglog = await em.findOneOrFail(Entities.ReturnFromCoreVaultCancelled,
442+
case EVENTS.ASSET_MANAGER.RETURN_FROM_CORE_VAULT_CANCELLED:
443+
return em.findOneOrFail(Entities.ReturnFromCoreVaultCancelled,
431444
{ evmLog: log }, { populate: [ 'returnFromCoreVaultRequested.evmLog.transaction' ] }
432445
).then(x => x.returnFromCoreVaultRequested.evmLog)
433-
// mintings
434-
} else if (log.name == EVENTS.ASSET_MANAGER.MINTING_EXECUTED) {
435-
oglog = await em.findOneOrFail(Entities.MintingExecuted,
446+
case EVENTS.ASSET_MANAGER.MINTING_EXECUTED:
447+
return em.findOneOrFail(Entities.MintingExecuted,
436448
{ evmLog: log }, { populate: [ 'collateralReserved.evmLog.transaction' ] }
437449
).then(x => x.collateralReserved.evmLog)
438-
} else if (log.name == EVENTS.ASSET_MANAGER.MINTING_PAYMENT_DEFAULT) {
439-
oglog = await em.findOneOrFail(Entities.MintingPaymentDefault,
450+
case EVENTS.ASSET_MANAGER.MINTING_PAYMENT_DEFAULT:
451+
return em.findOneOrFail(Entities.MintingPaymentDefault,
440452
{ evmLog: log }, { populate: [ 'collateralReserved.evmLog.transaction' ] }
441453
).then(x => x.collateralReserved.evmLog)
442-
} else if (log.name == EVENTS.ASSET_MANAGER.COLLATERAL_RESERVATION_DELETED) {
443-
oglog = await em.findOneOrFail(Entities.CollateralReservationDeleted,
454+
case EVENTS.ASSET_MANAGER.COLLATERAL_RESERVATION_DELETED:
455+
return em.findOneOrFail(Entities.CollateralReservationDeleted,
444456
{ evmLog: log }, { populate: [ 'collateralReserved.evmLog.transaction' ] }
445457
).then(x => x.collateralReserved.evmLog)
446-
// redemptions
447-
} else if (log.name == EVENTS.ASSET_MANAGER.REDEMPTION_PERFORMED) {
448-
oglog = await em.findOneOrFail(Entities.RedemptionPerformed,
458+
case EVENTS.ASSET_MANAGER.REDEMPTION_PERFORMED:
459+
return em.findOneOrFail(Entities.RedemptionPerformed,
449460
{ evmLog: log }, { populate: [ 'redemptionRequested.evmLog.transaction' ] }
450461
).then(x => x.redemptionRequested.evmLog)
451-
} else if (log.name == EVENTS.ASSET_MANAGER.REDEMPTION_DEFAULT) {
452-
oglog = await em.findOneOrFail(Entities.RedemptionDefault,
462+
case EVENTS.ASSET_MANAGER.REDEMPTION_DEFAULT:
463+
return em.findOneOrFail(Entities.RedemptionDefault,
453464
{ evmLog: log }, { populate: [ 'redemptionRequested.evmLog.transaction' ] }
454465
).then(x => x.redemptionRequested.evmLog)
455-
} else if (log.name == EVENTS.ASSET_MANAGER.REDEMPTION_REJECTED) {
456-
oglog = await em.findOneOrFail(Entities.RedemptionRejected,
466+
case EVENTS.ASSET_MANAGER.REDEMPTION_REJECTED:
467+
return em.findOneOrFail(Entities.RedemptionRejected,
457468
{ evmLog: log }, { populate: [ 'redemptionRequested.evmLog.transaction' ] }
458469
).then(x => x.redemptionRequested.evmLog)
459-
} else if (log.name == EVENTS.ASSET_MANAGER.REDEMPTION_PAYMENT_FAILED) {
460-
oglog = await em.findOneOrFail(Entities.RedemptionPaymentFailed,
470+
case EVENTS.ASSET_MANAGER.REDEMPTION_PAYMENT_FAILED:
471+
return em.findOneOrFail(Entities.RedemptionPaymentFailed,
461472
{ evmLog: log }, { populate: [ 'redemptionRequested.evmLog.transaction' ] }
462473
).then(x => x.redemptionRequested.evmLog)
463-
} else if (log.name == EVENTS.ASSET_MANAGER.REDEMPTION_PAYMENT_BLOCKED) {
464-
oglog = await em.findOneOrFail(Entities.RedemptionPaymentBlocked,
474+
case EVENTS.ASSET_MANAGER.REDEMPTION_PAYMENT_BLOCKED:
475+
return em.findOneOrFail(Entities.RedemptionPaymentBlocked,
465476
{ evmLog: log }, { populate: [ 'redemptionRequested.evmLog.transaction' ] }
466477
).then(x => x.redemptionRequested.evmLog)
467-
// agent withdrawal
468-
} else if (log.name == EVENTS.ASSET_MANAGER.UNDERLYING_WITHDRAWAL_CONFIRMED) {
469-
oglog = await em.findOneOrFail(Entities.UnderlyingWithdrawalConfirmed,
478+
case EVENTS.ASSET_MANAGER.UNDERLYING_WITHDRAWAL_CONFIRMED:
479+
return em.findOneOrFail(Entities.UnderlyingWithdrawalConfirmed,
470480
{ evmLog: log }, { populate: [ 'underlyingWithdrawalAnnounced.evmLog.transaction' ] }
471481
).then(x => x.underlyingWithdrawalAnnounced.evmLog)
472-
} else if (log.name == EVENTS.ASSET_MANAGER.UNDERLYING_WITHDRAWAL_CANCELLED) {
473-
oglog = await em.findOneOrFail(Entities.UnderlyingWithdrawalCancelled,
482+
case EVENTS.ASSET_MANAGER.UNDERLYING_WITHDRAWAL_CANCELLED:
483+
return em.findOneOrFail(Entities.UnderlyingWithdrawalCancelled,
474484
{ evmLog: log }, { populate: [ 'underlyingWithdrawalAnnounced.evmLog.transaction' ] }
475485
).then(x => x.underlyingWithdrawalAnnounced.evmLog)
476-
} else {
477-
continue
478-
}
479-
ret.push({ eventName: log.name, transactionHash: oglog.transaction.hash })
486+
default:
487+
return null
480488
}
481-
return ret
482489
}
483490

484491
protected async rippleTransactionClassification(em: EntityManager, hash: string): Promise<ExplorerType.GenericTransactionClassification> {

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

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@ import { CacheInterceptor } from '@nestjs/cache-manager'
33
import { ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger'
44
import { ExplorerService } from '../services/explorer.service'
55
import { apiResponse, ApiResponse } from '../shared/api-response'
6-
import {
7-
type MintTransactionDetails,
8-
type RedeemTransactionDetails,
9-
type ReturnFromCoreVaultTransactionDetails,
10-
type TransferToCoreVaultTransactionDetails,
11-
type TransactionsInfo,
12-
type GenericTransactionClassification,
13-
TransactionType,
14-
SelfMintTransactionDetails,
15-
BalanceTopupTransactionDetails,
16-
WithdrawalTransactionDetails
17-
} from '../analytics/types'
18-
import { get } from 'http'
6+
import * as Types from '../analytics/types'
197

208

219
@ApiTags('FAsset Explorer')
@@ -43,7 +31,7 @@ export class ExplorerController {
4331
@Query('start', new ParseIntPipe({ optional: true })) start?: number,
4432
@Query('end', new ParseIntPipe({ optional: true })) end?: number,
4533
@Query('types') types?: string | string[]
46-
): Promise<ApiResponse<TransactionsInfo>> {
34+
): Promise<ApiResponse<Types.TransactionsInfo>> {
4735
if (types != null && typeof types == 'string') types = [types]
4836
const transactionTypes = types != null ? this.parseTransactionTypes(types as string[]) : undefined
4937
return apiResponse(this.service.transactions(limit, offset, user, agent, start, end, asc, transactionTypes), 200)
@@ -54,7 +42,7 @@ export class ExplorerController {
5442
@ApiQuery({ name: 'hash', type: String })
5543
getTransactionClassification(
5644
@Query('hash') hash: string
57-
): Promise<ApiResponse<GenericTransactionClassification>> {
45+
): Promise<ApiResponse<Types.GenericTransactionClassification>> {
5846
return apiResponse(this.service.transactionClassification(hash), 200)
5947
}
6048

@@ -63,7 +51,7 @@ export class ExplorerController {
6351
@ApiQuery({ name: 'hash', type: String })
6452
getMintingTransactionDetails(
6553
@Query('hash') hash: string,
66-
): Promise<ApiResponse<MintTransactionDetails>> {
54+
): Promise<ApiResponse<Types.MintTransactionDetails>> {
6755
return apiResponse(this.service.mintingTransactionDetails(hash), 200)
6856
}
6957

@@ -72,7 +60,7 @@ export class ExplorerController {
7260
@ApiQuery({ name: 'hash', type: String })
7361
getRedemptionTransactionDetails(
7462
@Query('hash') hash: string,
75-
): Promise<ApiResponse<RedeemTransactionDetails>> {
63+
): Promise<ApiResponse<Types.RedeemTransactionDetails>> {
7664
return apiResponse(this.service.redemptionTransactionDetails(hash), 200)
7765
}
7866

@@ -81,7 +69,7 @@ export class ExplorerController {
8169
@ApiQuery({ name: 'hash', type: String })
8270
getCoreVaultTransferTransactionDetails(
8371
@Query('hash') hash: string,
84-
): Promise<ApiResponse<TransferToCoreVaultTransactionDetails>> {
72+
): Promise<ApiResponse<Types.TransferToCoreVaultTransactionDetails>> {
8573
return apiResponse(this.service.transferToCoreVaultTransactionDetails(hash), 200)
8674
}
8775

@@ -90,7 +78,7 @@ export class ExplorerController {
9078
@ApiQuery({ name: 'hash', type: String })
9179
getReturnFromCoreVaultTransactionDetails(
9280
@Query('hash') hash: string,
93-
): Promise<ApiResponse<ReturnFromCoreVaultTransactionDetails>> {
81+
): Promise<ApiResponse<Types.ReturnFromCoreVaultTransactionDetails>> {
9482
return apiResponse(this.service.returnFromCoreVaultTransactionDetails(hash), 200)
9583
}
9684

@@ -99,7 +87,7 @@ export class ExplorerController {
9987
@ApiQuery({ name: 'hash', type: String })
10088
getSelfMintTransactionDetails(
10189
@Query('hash') hash: string
102-
): Promise<ApiResponse<SelfMintTransactionDetails>> {
90+
): Promise<ApiResponse<Types.SelfMintTransactionDetails>> {
10391
return apiResponse(this.service.selfMintTransactionDetails(hash), 200)
10492
}
10593

@@ -108,7 +96,7 @@ export class ExplorerController {
10896
@ApiQuery({ name: 'hash', type: String })
10997
getUnderlyingBalanceTopupTransactionDetails(
11098
@Query('hash') hash: string
111-
): Promise<ApiResponse<BalanceTopupTransactionDetails>> {
99+
): Promise<ApiResponse<Types.BalanceTopupTransactionDetails>> {
112100
return apiResponse(this.service.underlyingBalanceToppedUpTransactionDetails(hash), 200)
113101
}
114102

@@ -117,11 +105,11 @@ export class ExplorerController {
117105
@ApiQuery({ name: 'hash', type: String })
118106
getAgentBalanceWithdrawalTransactionDetails(
119107
@Query('hash') hash: string
120-
): Promise<ApiResponse<WithdrawalTransactionDetails>> {
108+
): Promise<ApiResponse<Types.WithdrawalTransactionDetails>> {
121109
return apiResponse(this.service.underlyingWithdrawalTransactionDetails(hash), 200)
122110
}
123111

124-
private parseTransactionTypes(types: string[]): TransactionType[] {
125-
return types.map(t => TransactionType[t])
112+
private parseTransactionTypes(types: string[]): Types.TransactionType[] {
113+
return types.map(t => Types.TransactionType[t])
126114
}
127115
}

packages/fasset-indexer-btc/src/run/run-indexer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { BtcConfigLoader } from "../config/config"
33
import { BtcContext } from "../context"
44
import { BtcIndexer } from "../indexer/indexer"
55

6+
const SLEEP_CYCLE_MS = 2000
67

78
async function runIndexer(): Promise<void> {
89
const config = new BtcConfigLoader()
910
const context = await BtcContext.create(config)
1011
const indexer = new BtcIndexer(context)
1112
const runner = new IndexerRunner(indexer, 'doge')
12-
await runner.run(config.btcMinBlockNumber)
13+
await runner.run(SLEEP_CYCLE_MS, config.btcMinBlockNumber)
1314
}
1415

1516
runIndexer()

packages/fasset-indexer-core/src/config/config.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,9 @@ import { SqliteDriver } from "@mikro-orm/sqlite"
44
import { PostgreSqlDriver } from "@mikro-orm/postgresql"
55
import { getContractInfo } from "./contracts"
66
import { SchemaUpdate } from "../orm/interface"
7-
import type { ContractInfo } from "./interface"
7+
import type { ContractInfo, ConfigJson, ReindexConfig } from "./interface"
88

99

10-
interface ConfigJson {
11-
indexEvents: string[]
12-
}
13-
14-
interface ReindexConfig {
15-
type: "back" | "race"
16-
diff: string[]
17-
name: string
18-
}
19-
2010
export class ConfigLoader {
2111
private _configJson: ConfigJson | undefined
2212

packages/fasset-indexer-core/src/config/constants.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// chain call config
2-
export const CHAIN_FETCH_RETRY_LIMIT = 20
3-
export const SLEEP_AFTER_ERROR_MS = 100
2+
export const SLEEP_AFTER_ERROR_MS = 3000
43

54
// evm event scrape config
6-
export const EVM_LOG_FETCH_SLEEP_MS = 30 * 1000 // collect logs every 30 seconds
5+
export const EVM_LOG_FETCH_SLEEP_MS = 10 * 1000 // collect logs every 10 seconds
76
export const EVM_STATE_UPDATE_SLEEP_MS = 60 * 1000 // collect state every one minute
87
export const EVM_BLOCK_HEIGHT_OFFSET = 10 // log collection offset from the current block height
98

packages/fasset-indexer-core/src/config/interface.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,22 @@ export type ContractInfo = {
22
name: string
33
contractName: string
44
address: string
5+
}
6+
7+
export interface ConfigJson {
8+
events: {
9+
logFetchBlockBatchSize: number
10+
logFetchCycleSleepMs: number,
11+
logFetchBlockHeightOffset: number
12+
}
13+
watchdog: {
14+
cycleSleepMs: number
15+
}
16+
indexEvents: string[]
17+
}
18+
19+
export interface ReindexConfig {
20+
type: "back" | "race"
21+
diff: string[]
22+
name: string
523
}

packages/fasset-indexer-core/src/indexer/indexer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ export class EventIndexer {
4949

5050
async lastBlockToHandle(): Promise<number> {
5151
const blockHeight = await this.context.provider.getBlockNumber()
52-
return blockHeight - EVM_BLOCK_HEIGHT_OFFSET
52+
const offset = this.context.config.json?.events.logFetchBlockHeightOffset ?? EVM_BLOCK_HEIGHT_OFFSET
53+
return blockHeight - offset
5354
}
5455

5556
async firstUnhandledBlock(startBlock?: number): Promise<number> {

packages/fasset-indexer-core/src/indexer/runner.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { sleep } from "../utils/general"
22
import { logger } from "../logger"
3-
import { SLEEP_AFTER_ERROR_MS, EVM_LOG_FETCH_SLEEP_MS } from "../config/constants"
3+
import { SLEEP_AFTER_ERROR_MS } from "../config/constants"
44

55

66
interface Indexer {
@@ -14,7 +14,7 @@ export class IndexerRunner {
1414
public name: string
1515
) { }
1616

17-
async run(startBlock?: number, layered: boolean = false) {
17+
async run(cyclesleep: number, startBlock?: number, layered: boolean = false) {
1818
while (true) {
1919
try {
2020
const resp = await this.indexer.run(startBlock)
@@ -30,7 +30,7 @@ export class IndexerRunner {
3030
continue
3131
}
3232
if (!layered) {
33-
await sleep(EVM_LOG_FETCH_SLEEP_MS)
33+
await sleep(cyclesleep)
3434
}
3535
}
3636
}

0 commit comments

Comments
 (0)