Skip to content

Commit d2df6a6

Browse files
committed
chore: make entities inherit agent vault based events, integrate update tracking to api and integrity checks
1 parent b6b87cc commit d2df6a6

File tree

21 files changed

+142
-179
lines changed

21 files changed

+142
-179
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ To run the API, run `yarn run-api`.
6464

6565
## Docker Deployment
6666

67-
The docker image can be either built from this project, or one from github can be used. The repository also features the `compose.yaml` file that features a local postgres deployment and requires the following `.env` config:
67+
A local docker image can be either built from this repository, or one from github can be utilised. The repository also includes the `compose.yaml` file that features a local postgres deployment and requires the following `.env` config:
6868

6969
- **COMPOSE_PROJECT_NAME**: needs to be set to a unique name of your docker deployment;
7070
- **COMPOSE_PROFILES**: needs to be a list of options `core`, `api`, `ripple`, `watchdog`;
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { getVar, type ORM } from "fasset-indexer-core/orm"
2-
import {
3-
FIRST_UNHANDLED_EVENT_BLOCK_DB_KEY,
4-
backUpdateLastBlockName,
5-
backUpdateFirstUnhandledBlockName
6-
} from "fasset-indexer-core/config"
2+
import { updateDistance } from "fasset-indexer-core/utils"
3+
import { FIRST_UNHANDLED_EVENT_BLOCK_DB_KEY } from "fasset-indexer-core/config"
74

85

96
export class MetadataAnalytics {
@@ -14,16 +11,14 @@ export class MetadataAnalytics {
1411
return (v && v.value) ? parseInt(v.value) : null
1512
}
1613

17-
async blocksToBackSync(): Promise<number | null> {
18-
const currentUpdate = await getVar(this.orm.em.fork(), 'current_update')
19-
if (currentUpdate === null) {
20-
return null
14+
async blocksToBackSync(): Promise<[string | null, number | null]> {
15+
const em = this.orm.em.fork()
16+
const currentUpdate = await getVar(em, 'current_update')
17+
if (currentUpdate == null) {
18+
return [null, null]
2119
}
2220
const currentUpdateName = currentUpdate.value!
23-
const start = await getVar(this.orm.em.fork(), backUpdateFirstUnhandledBlockName(currentUpdateName))
24-
if (start === null || start.value === undefined) return null
25-
const end = await getVar(this.orm.em.fork(), backUpdateLastBlockName(currentUpdateName))
26-
if (end === null || end.value === undefined) return null
27-
return parseInt(end.value) - parseInt(start.value) + 1
21+
const currentUpdateDist = await updateDistance(em, currentUpdateName)
22+
return [currentUpdateName, currentUpdateDist]
2823
}
2924
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export class MetadataController {
1818
return apiResponse(this.service.currentBlock(), 200)
1919
}
2020

21-
@Get('/blocks-to-back-sync')
22-
getBlocksToBackSync(): Promise<ApiResponse<number | null>> {
21+
@Get('/blocks-for-current-update-sync')
22+
getBlocksToBackSync(): Promise<ApiResponse<[string | null, number | null]>> {
2323
return apiResponse(this.service.blocksToBackSync(), 200)
2424
}
2525

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ export const MAX_DATABASE_ENTRIES_FETCH = 200
1616
export const FIRST_UNHANDLED_EVENT_BLOCK_DB_KEY = "firstUnhandledEventBlock"
1717
export const MIN_EVM_BLOCK_NUMBER_DB_KEY = "minEvmBlockNumber"
1818

19-
// db back indexer names
20-
export function backUpdateFirstUnhandledBlockName(updateName: string): string {
21-
return `firstUnhandledEventBlock_${updateName}`
22-
}
23-
export function backUpdateLastBlockName(updateName: string): string {
24-
return `lastEventBlock_${updateName}`
25-
}
26-
2719
// event names
2820
// agent
2921
export const EVENTS = {
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
export {
22
MIN_EVM_BLOCK_NUMBER_DB_KEY, EVENTS,
33
MIN_DATABASE_POOL_CONNECTIONS, MAX_DATABASE_POOL_CONNECTIONS,
4-
FIRST_UNHANDLED_EVENT_BLOCK_DB_KEY,
5-
backUpdateLastBlockName,
6-
backUpdateFirstUnhandledBlockName
4+
FIRST_UNHANDLED_EVENT_BLOCK_DB_KEY
75
} from "./constants"

packages/fasset-indexer-core/src/indexer/reindexing/indexer-back-population.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getVar, setVar } from "../../orm/utils"
2-
import { backUpdateLastBlockName, backUpdateFirstUnhandledBlockName } from "../../config/constants"
2+
import { backUpdateLastBlockName, backUpdateFirstUnhandledBlockName } from "../../utils"
33
import { EventIndexer } from "../indexer"
44
import type { Context } from "../../context/context"
55

@@ -34,4 +34,3 @@ export class EventIndexerBackPopulation extends EventIndexer {
3434
await setVar(this.context.orm.em.fork(), this.firstUnhandledEventBlockForCurrentUpdateKey, blockNumber.toString())
3535
}
3636
}
37-

packages/fasset-indexer-core/src/indexer/reindexing/indexer-parallel-race-population.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from "../../utils"
1+
import { join, raceIndexerSyncedFlagName, raceUpdateFirstUnhandledBlockName } from "../../utils"
22
import { getVar, setVar } from "../../orm/utils"
33
import { EventIndexer } from "../indexer"
44
import type { Context } from "../../context/context"
@@ -20,9 +20,10 @@ export class EventIndexerParallelRacePopulation {
2020
frontInsertionEvents: string[]
2121
) {
2222
this.frontIndexer = new EventIndexer(context, frontInsertionEvents)
23-
this.backIndexer = new EventIndexer(context, backInsertionEvents, updateName)
23+
const firstUnhandledEventBlockName = raceUpdateFirstUnhandledBlockName(updateName)
24+
this.backIndexer = new EventIndexer(context, backInsertionEvents, firstUnhandledEventBlockName)
2425
this.indexer = new EventIndexer(context, join(frontInsertionEvents, backInsertionEvents))
25-
this.updateSynced = `front_indexing_synced_${updateName}`
26+
this.updateSynced = raceIndexerSyncedFlagName(updateName)
2627
}
2728

2829
async run(): Promise<EventIndexer | void> {

packages/fasset-indexer-core/src/orm/entities/events/_bound.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { OneToOne, Enum } from "@mikro-orm/core"
1+
import { OneToOne, Enum, ManyToOne } from "@mikro-orm/core"
22
import { FAssetType } from "../../../shared"
33
import { EvmLog } from "../evm/log"
4+
import { AgentVault } from "../agent"
45

56
export class EventBound {
67

@@ -12,4 +13,10 @@ export class FAssetEventBound extends EventBound {
1213

1314
@Enum(() => FAssetType)
1415
fasset!: FAssetType
16+
}
17+
18+
export class AgentEventBound extends FAssetEventBound {
19+
20+
@ManyToOne({ entity: () => AgentVault })
21+
agentVault!: AgentVault
1522
}

packages/fasset-indexer-core/src/orm/entities/events/agent.ts

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
1-
import { Entity, ManyToOne, OneToOne, Property } from "@mikro-orm/core"
1+
import { Entity, OneToOne, Property } from "@mikro-orm/core"
22
import { uint256, uint64 } from "../../custom/uint"
3-
import { FAssetEventBound } from "./_bound"
4-
import { AgentVault } from "../agent"
3+
import { AgentEventBound, FAssetEventBound } from "./_bound"
54

65

76
@Entity()
8-
export class AgentVaultCreated extends FAssetEventBound {
9-
10-
@OneToOne({ entity: () => AgentVault, owner: true })
11-
agentVault!: AgentVault
12-
}
7+
export class AgentVaultCreated extends AgentEventBound {}
138

149
@Entity()
15-
export class AgentVaultDestroyed extends FAssetEventBound {
16-
17-
@OneToOne({ entity: () => AgentVault, owner: true })
18-
agentVault!: AgentVault
19-
}
10+
export class AgentVaultDestroyed extends AgentEventBound {}
2011

2112
@Entity()
22-
export class AgentSettingChanged extends FAssetEventBound {
23-
24-
@ManyToOne({ entity: () => AgentVault })
25-
agentVault!: AgentVault
13+
export class AgentSettingChanged extends AgentEventBound {
2614

2715
@Property({ type: 'text' })
2816
name!: string
@@ -32,20 +20,14 @@ export class AgentSettingChanged extends FAssetEventBound {
3220
}
3321

3422
@Entity()
35-
export class SelfClose extends FAssetEventBound {
36-
37-
@ManyToOne({ entity: () => AgentVault })
38-
agentVault!: AgentVault
23+
export class SelfClose extends AgentEventBound {
3924

4025
@Property({ type: new uint256() })
4126
valueUBA!: bigint
4227
}
4328

4429
@Entity()
45-
export class VaultCollateralWithdrawalAnnounced extends FAssetEventBound {
46-
47-
@ManyToOne({ entity: () => AgentVault })
48-
agentVault!: AgentVault
30+
export class VaultCollateralWithdrawalAnnounced extends AgentEventBound {
4931

5032
@Property({ type: new uint256() })
5133
amountWei!: bigint
@@ -55,10 +37,7 @@ export class VaultCollateralWithdrawalAnnounced extends FAssetEventBound {
5537
}
5638

5739
@Entity()
58-
export class PoolTokenRedemptionAnnounced extends FAssetEventBound {
59-
60-
@ManyToOne({ entity: () => AgentVault })
61-
agentVault!: AgentVault
40+
export class PoolTokenRedemptionAnnounced extends AgentEventBound {
6241

6342
@Property({ type: new uint256() })
6443
amountWei!: bigint
@@ -68,10 +47,7 @@ export class PoolTokenRedemptionAnnounced extends FAssetEventBound {
6847
}
6948

7049
@Entity()
71-
export class UnderlyingWithdrawalAnnounced extends FAssetEventBound {
72-
73-
@ManyToOne({ entity: () => AgentVault })
74-
agentVault!: AgentVault
50+
export class UnderlyingWithdrawalAnnounced extends AgentEventBound {
7551

7652
@Property({ type: new uint64() })
7753
announcementId!: bigint
@@ -101,10 +77,7 @@ export class UnderlyingWithdrawalCancelled extends FAssetEventBound {
10177
}
10278

10379
@Entity()
104-
export class UnderlyingBalanceToppedUp extends FAssetEventBound {
105-
106-
@ManyToOne({ entity: () => AgentVault })
107-
agentVault!: AgentVault
80+
export class UnderlyingBalanceToppedUp extends AgentEventBound {
10881

10982
@Property({ type: 'text' })
11083
transactionHash!: string
@@ -114,20 +87,14 @@ export class UnderlyingBalanceToppedUp extends FAssetEventBound {
11487
}
11588

11689
@Entity()
117-
export class UnderlyingBalanceChanged extends FAssetEventBound {
118-
119-
@ManyToOne({ entity: () => AgentVault })
120-
agentVault!: AgentVault
90+
export class UnderlyingBalanceChanged extends AgentEventBound {
12191

12292
@Property({ type: new uint256() })
12393
balanceUBA!: bigint
12494
}
12595

12696
@Entity()
127-
export class DustChanged extends FAssetEventBound {
128-
129-
@ManyToOne({ entity: () => AgentVault })
130-
agentVault!: AgentVault
97+
export class DustChanged extends AgentEventBound {
13198

13299
@Property({ type: new uint256() })
133100
dustUBA!: bigint

packages/fasset-indexer-core/src/orm/entities/events/challenge.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
1-
import { Entity, ManyToOne, Property } from '@mikro-orm/core'
1+
import { Entity, Property } from '@mikro-orm/core'
22
import { uint256 } from '../../custom/uint'
3-
import { FAssetEventBound } from './_bound'
4-
import { AgentVault } from '../agent'
3+
import { AgentEventBound } from './_bound'
54
import { BYTES32_LENGTH } from '../../../config/constants'
65

76

87
@Entity()
9-
export class IllegalPaymentConfirmed extends FAssetEventBound {
10-
11-
@ManyToOne({ entity: () => AgentVault })
12-
agentVault!: AgentVault
8+
export class IllegalPaymentConfirmed extends AgentEventBound {
139

1410
@Property({ type: "text", length: BYTES32_LENGTH, unique: true })
1511
transactionHash!: string
1612
}
1713

1814
@Entity()
19-
export class DuplicatePaymentConfirmed extends FAssetEventBound {
20-
21-
@ManyToOne({ entity: () => AgentVault })
22-
agentVault!: AgentVault
15+
export class DuplicatePaymentConfirmed extends AgentEventBound {
2316

2417
@Property({ type: "text", length: BYTES32_LENGTH, unique: true })
2518
transactionHash1!: string
@@ -29,10 +22,7 @@ export class DuplicatePaymentConfirmed extends FAssetEventBound {
2922
}
3023

3124
@Entity()
32-
export class UnderlyingBalanceTooLow extends FAssetEventBound {
33-
34-
@ManyToOne({ entity: () => AgentVault })
35-
agentVault!: AgentVault
25+
export class UnderlyingBalanceTooLow extends AgentEventBound {
3626

3727
@Property({ type: new uint256() })
3828
balance!: bigint

0 commit comments

Comments
 (0)