Skip to content

Commit 443d145

Browse files
committed
feat: index core vault manager settings
1 parent 370d81e commit 443d145

File tree

11 files changed

+120
-37
lines changed

11 files changed

+120
-37
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { JsonRpcProvider, FetchRequest } from "ethers"
22
import { createOrm } from "../orm/mikro-orm.config"
33
import { ContractLookup } from "./lookup"
4-
import { IAssetManager__factory, IERC20__factory, IAgentOwnerRegistry__factory, IPriceReader__factory } from "../../chain/typechain"
5-
import type { IAssetManager, IERC20, IAgentOwnerRegistry, IPriceReader } from "../../chain/typechain"
4+
import { IAssetManager__factory, IERC20__factory, IAgentOwnerRegistry__factory, IPriceReader__factory, ICoreVaultManager__factory } from "../../chain/typechain"
5+
import type { IAssetManager, IERC20, IAgentOwnerRegistry, IPriceReader, ICoreVaultManager } from "../../chain/typechain"
66
import type { ORM } from "../orm/interface"
77
import type { ConfigLoader } from "../config/config"
88

@@ -32,6 +32,10 @@ export class Context extends ContractLookup {
3232
return IAssetManager__factory.connect(address, this.provider)
3333
}
3434

35+
getCoreVaultManagerContract(address: string): ICoreVaultManager {
36+
return ICoreVaultManager__factory.connect(address, this.provider)
37+
}
38+
3539
getERC20(address: string): IERC20 {
3640
return IERC20__factory.connect(address, this.provider)
3741
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class ContractLookup extends EventInterface {
1212
private fAssetTokenToFAsset__cache: Map<string, FAssetType> = new Map()
1313
private fAssetToFAssetToken__cache: Map<FAssetType, string> = new Map()
1414
private coreVaultToFAsset__cache: Map<string, FAssetType> = new Map()
15+
private fAssetToCoreVault__cache: Map<FAssetType, string> = new Map()
1516
// public
1617
public fassetTokens: string[] = []
1718
public contractInfos!: ContractInfo[]
@@ -86,6 +87,14 @@ export class ContractLookup extends EventInterface {
8687
}
8788
}
8889

90+
fassetTypeToCoreVaultManagerAddress(type: FAssetType): string {
91+
if (this.fAssetToCoreVault__cache.get(type)) {
92+
return this.fAssetToCoreVault__cache.get(type)!
93+
} else {
94+
throw new Error(`No core vault manager found for type ${type}`)
95+
}
96+
}
97+
8998
getContractAddress(name: string): string {
9099
for (const contract of this.contractInfos) {
91100
if (contract.name === name)
@@ -108,6 +117,7 @@ export class ContractLookup extends EventInterface {
108117
const fasset = this.contractNameToFAssetType(contract.name, 'CoreVaultManager_')
109118
if (fasset == null) continue
110119
this.coreVaultToFAsset__cache.set(contract.address, fasset)
120+
this.fAssetToCoreVault__cache.set(fasset, contract.address)
111121
}
112122
}
113123

packages/fasset-indexer-core/src/indexer/eventlib/event-storer.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ import type { TransferEvent } from "../../../chain/typechain/IERC20"
137137
import type { CurrentUnderlyingBlockUpdatedEvent, RedeemedInCollateralEvent } from "../../../chain/typechain/IAssetManager"
138138
import type { PricesPublishedEvent } from "../../../chain/typechain/IPriceChangeEmitter"
139139
import type { ORM } from "../../orm/interface"
140+
import { CoreVaultManagerSettings } from "../../orm/entities/state/settings"
140141

141142

142143
export class EventStorer {
@@ -969,11 +970,17 @@ export class EventStorer {
969970
// core vault manager
970971

971972
protected async onCoreVaultManagerSettingsUpdated(em: EntityManager, evmLog: EvmLog, logArgs: SettingsUpdatedEvent.OutputTuple):
972-
Promise<CoreVaultManagerSettingsUpdated>
973+
Promise<[CoreVaultManagerSettings, CoreVaultManagerSettingsUpdated]>
973974
{
974975
const fasset = this.lookup.coreVaultManagerToFAssetType(evmLog.address.hex)
975976
const [ escrowEndTimeSeconds, escrowAmount, minimalAmount, fee ] = logArgs
976-
return new CoreVaultManagerSettingsUpdated(evmLog, fasset, escrowEndTimeSeconds, escrowAmount, minimalAmount, fee)
977+
const settings = await em.findOneOrFail(CoreVaultManagerSettings, { fasset })
978+
settings.escrowEndTimeSeconds = Number(escrowEndTimeSeconds)
979+
settings.escrowAmount = escrowAmount
980+
settings.minimalAmount = minimalAmount
981+
settings.chainPaymentFee = fee
982+
const settingsUpdated = new CoreVaultManagerSettingsUpdated(evmLog, fasset, Number(escrowEndTimeSeconds), escrowAmount, minimalAmount, fee)
983+
return [settings, settingsUpdated]
977984
}
978985

979986
protected async onCustodianAddressUpdated(em: EntityManager, evmLog: EvmLog, logArgs: CustodianAddressUpdatedEvent.OutputTuple):

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import { UnderlyingAddress } from "../underlying/address"
1010
@Entity()
1111
export class CoreVaultManagerSettingsUpdated extends FAssetEventBound {
1212

13-
@Property({ type: new uint256() })
14-
escrowEndTimeSeconds: bigint
13+
@Property({ type: 'integer' })
14+
escrowEndTimeSeconds: number
1515

1616
@Property({ type: new uint256() })
1717
escrowAmount: bigint
@@ -23,7 +23,7 @@ export class CoreVaultManagerSettingsUpdated extends FAssetEventBound {
2323
fee: bigint
2424

2525
constructor(evmLog: EvmLog, fasset: FAssetType,
26-
escrowEndTimeSeconds: bigint, escrowAmount: bigint,
26+
escrowEndTimeSeconds: number, escrowAmount: bigint,
2727
minimalAmount: bigint, fee: bigint
2828
) {
2929
super(evmLog, fasset)

packages/fasset-indexer-core/src/orm/entities/state/settings.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,31 @@ export class AssetManagerSettings {
1616
this.lotSizeAmg = lotSizeAmg
1717
}
1818

19+
}
20+
21+
@Entity()
22+
export class CoreVaultManagerSettings {
23+
24+
@Enum({ type: () => FAssetType, primary: true })
25+
fasset: FAssetType
26+
27+
@Property({ type: new uint256() })
28+
escrowAmount: bigint
29+
30+
@Property({ type: new uint256() })
31+
minimalAmount: bigint
32+
33+
@Property({ type: 'integer' })
34+
escrowEndTimeSeconds: number
35+
36+
@Property({ type: new uint256() })
37+
chainPaymentFee: bigint
38+
39+
constructor(fasset: FAssetType, escrowAmount: bigint, minimalAmount: bigint, escrowEndTimeSeconds: number, chainPaymentFee: bigint) {
40+
this.fasset = fasset
41+
this.escrowAmount = escrowAmount
42+
this.minimalAmount = minimalAmount
43+
this.escrowEndTimeSeconds = escrowEndTimeSeconds
44+
this.chainPaymentFee = chainPaymentFee
45+
}
1946
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import {
4646
import { CoreVaultManagerCustodianAddressUpdated, CoreVaultManagerPaymentConfirmed, CoreVaultManagerSettingsUpdated, CoreVaultManagerTransferRequestCanceled, CoreVaultManagerTransferRequested, EscrowFinished, CoreVaultManagerEscrowInstructions, CoreVaultManagerNotAllEscrowsProcessed, CoreVaultManagerPaymentInstructions } from "./entities/events/core-vault-manager"
4747
import { AgentVaultInfo, AgentVaultSettings } from "./entities/state/agent"
4848
import { AgentManager, AgentOwner, AgentVault } from "./entities/agent"
49-
import { AssetManagerSettings } from "./entities"
49+
import { AssetManagerSettings, CoreVaultManagerSettings } from "./entities/state/settings"
5050
import { FtsoPrice } from "./entities/state/price"
5151
import { TokenBalance } from "./entities/state/balance"
5252
import { UnderlyingBlock } from "./entities/underlying/block"
@@ -84,7 +84,7 @@ export const ORM_OPTIONS: Options<AbstractSqlDriver> = defineConfig({
8484
CoreVaultManagerPaymentConfirmed, CoreVaultManagerPaymentInstructions, CoreVaultManagerEscrowInstructions,
8585
CoreVaultManagerTransferRequested, CoreVaultManagerTransferRequestCanceled, CoreVaultManagerNotAllEscrowsProcessed,
8686
EscrowFinished, CoreVaultManagerCustodianAddressUpdated,
87-
AssetManagerSettings,
87+
AssetManagerSettings, CoreVaultManagerSettings,
8888
// underlying
8989
UnderlyingBlock, UnderlyingVoutReference, UnderlyingAddress
9090
],
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Context } from "../context/context"
2+
import { AssetManagerSettings } from "../orm/entities"
3+
import { CoreVaultManagerSettings } from "../orm/entities/state/settings"
4+
import { FASSETS, FAssetType } from "../shared"
5+
6+
7+
export async function ensureData(context: Context) {
8+
await ensureAssetManagerSettings(context)
9+
await ensureCoreVaultManagerSettings(context)
10+
}
11+
12+
async function ensureAssetManagerSettings(context: Context) {
13+
for (const _fasset of FASSETS) {
14+
const fasset = FAssetType[_fasset]
15+
if (!context.supportsFAsset(fasset)) continue
16+
await context.orm.em.transactional(async em => {
17+
let assetManagerSettings = await em.findOne(AssetManagerSettings, { fasset })
18+
if (assetManagerSettings == null) {
19+
const assetManagerAddress = context.fAssetTypeToAssetManagerAddress(fasset)
20+
const assetManager = context.getAssetManagerContract(assetManagerAddress)
21+
const settings = await assetManager.getSettings()
22+
assetManagerSettings = new AssetManagerSettings(fasset, settings.lotSizeAMG)
23+
em.persist(assetManagerSettings)
24+
}
25+
})
26+
}
27+
}
28+
29+
async function ensureCoreVaultManagerSettings(context: Context) {
30+
const fasset = FAssetType.FXRP
31+
await context.orm.em.transactional(async em => {
32+
let coreVaultManagerSettings = await em.findOne(CoreVaultManagerSettings, { fasset })
33+
if (coreVaultManagerSettings == null) {
34+
const coreVaultManagerAddress = context.fassetTypeToCoreVaultManagerAddress(fasset)
35+
const coreVaultManager = context.getCoreVaultManagerContract(coreVaultManagerAddress)
36+
const settings = await coreVaultManager.getSettings()
37+
coreVaultManagerSettings = new CoreVaultManagerSettings(fasset,
38+
settings._escrowAmount, settings._minimalAmount,
39+
Number(settings._escrowEndTimeSeconds), settings._fee
40+
)
41+
em.persist(coreVaultManagerSettings)
42+
}
43+
})
44+
}

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

Lines changed: 0 additions & 25 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventIndexer } from "../indexer/indexer"
22
import { ensureConfigIntegrity } from "./integrity"
3-
import { ensureData } from "./ensure_data"
3+
import { ensureData } from "./ensure-data"
44
import { ConfigLoader } from "../config/config"
55
import { Context } from "../context/context"
66
import { logger } from "../logger"

packages/fasset-indexer-core/test/events.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
} from "../src/orm/entities/events/core-vault"
3939
import { CoreVaultManagerSettingsUpdated } from "../src/orm/entities/events/core-vault-manager"
4040
import { AssetManagerSettings } from "../src/orm/entities"
41+
import { CoreVaultManagerSettings } from "../src/orm/entities/state/settings"
4142

4243

4344
const ASSET_MANAGER_FXRP = "AssetManager_FTestXRP"
@@ -708,17 +709,24 @@ describe("FAsset evm events", () => {
708709
describe("core vault manager", () => {
709710
it("should store settings updated", async () => {
710711
const coreVaultManagerXrp = context.getContractAddress(CORE_VAULT_MANAGER_FXRP)
712+
await fixture.storeInitialCoreVaultManagerSettings()
711713
const em = context.orm.em.fork()
712714
const esu = await fixture.generateEvent(EVENTS.CORE_VAULT_MANAGER.SETTINGS_UPDATED, coreVaultManagerXrp)
713715
await storer.processEventUnsafe(em, esu)
714716
const su = await em.findOneOrFail(CoreVaultManagerSettingsUpdated,
715717
{ evmLog: { block: { index: esu.blockNumber }, index: esu.logIndex }}
716718
)
717719
expect(su.fasset).to.equal(FAssetType.FXRP)
718-
expect(su.escrowEndTimeSeconds).to.equal(esu.args[0])
720+
expect(su.escrowEndTimeSeconds).to.equal(Number(esu.args[0]))
719721
expect(su.escrowAmount).to.equal(esu.args[1])
720722
expect(su.minimalAmount).to.equal(esu.args[2])
721723
expect(su.fee).to.equal(esu.args[3])
724+
const cvms = await em.findOneOrFail(CoreVaultManagerSettings, { fasset: FAssetType.FXRP })
725+
expect(cvms.fasset).to.equal(FAssetType.FXRP)
726+
expect(cvms.escrowEndTimeSeconds).to.equal(su.escrowEndTimeSeconds)
727+
expect(cvms.escrowAmount).to.equal(su.escrowAmount)
728+
expect(cvms.minimalAmount).to.equal(su.minimalAmount)
729+
expect(cvms.chainPaymentFee).to.equal(su.fee)
722730
})
723731
})
724732

0 commit comments

Comments
 (0)