Skip to content

Commit eafd864

Browse files
committed
fix: setting previously null value to uint256 database value due to updated "redemptionPoolFeeShareBIPS" agent setting
1 parent 941e6e3 commit eafd864

File tree

8 files changed

+62
-14
lines changed

8 files changed

+62
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ export class EventStorer {
575575
agentSettings.redemptionPoolFeeShareBIPS = BigInt(value)
576576
break
577577
} default: {
578-
break
578+
throw new Error(`agent has no setting ${name}`)
579579
}
580580
}
581581
}

packages/fasset-indexer-core/src/orm/custom/uint.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import { Type } from '@mikro-orm/core'
22

33

4-
export class uint256 extends Type<bigint, string> {
4+
export class uint256 extends Type<bigint | null | undefined, string | null | undefined> {
55

6-
convertToDatabaseValue(value: bigint): string {
7-
return value.toString()
6+
convertToDatabaseValue(value: bigint | null | undefined): string | null {
7+
if (value === null || value === undefined) {
8+
return null;
9+
}
10+
return value.toString();
811
}
912

10-
convertToJSValue(value: string): bigint {
11-
return BigInt(value)
13+
convertToJSValue(value: string | null | undefined): bigint | null {
14+
if (value === null || value === undefined) {
15+
return null;
16+
}
17+
return BigInt(value);
1218
}
1319

1420
getColumnType() {
15-
return `NUMERIC(78)`
21+
return `NUMERIC(78)`; // or DECIMAL(78), depending on your DB
1622
}
1723
}
1824

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ export { createOrm } from "./mikro-orm.config"
22
export { raw } from "@mikro-orm/core"
33
export { EntityManager, SelectQueryBuilder } from "@mikro-orm/knex"
44
export { ZeroAddress } from 'ethers' // for database lookup
5-
export { getVar, setVar, findOrCreateUnderlyingAddress, findOrCreateUnderlyingTransaction, updateUnderlyingBalance } from "./utils"
5+
export {
6+
getVar, setVar, findOrCreateUnderlyingAddress,
7+
findOrCreateUnderlyingTransaction, updateUnderlyingBalance,
8+
findOrCreateUnderlyingBlock
9+
} from "./utils"
610
export { OrmOptions, ORM, AddressType } from "./interface"

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ export async function findOrCreateUnderlyingAddress(em: EntityManager, address:
4848
return underlyingAddress
4949
}
5050

51+
export async function findOrCreateUnderlyingBlock(em: EntityManager, index: number, hash: string, timestamp: number): Promise<UnderlyingBlock> {
52+
let underlyingBlock = await em.findOne(UnderlyingBlock, { height: index })
53+
if (!underlyingBlock) {
54+
underlyingBlock = new UnderlyingBlock(hash, index, timestamp)
55+
em.persist(underlyingBlock)
56+
}
57+
return underlyingBlock
58+
}
59+
5160
export async function findOrCreateUnderlyingTransaction(
5261
em: EntityManager, hash: string, block: UnderlyingBlock, value: bigint,
5362
source: UnderlyingAddress, target?: UnderlyingAddress

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,10 @@ describe("FAsset evm events", () => {
125125

126126
it("should store agent created event", async () => {
127127
const assetManager = context.getContractAddress(ASSET_MANAGER_FXRP)
128-
await fixture.storeInitialAgents()
129128
const em = context.orm.em.fork()
130129
// add initial collateral token type
131130
const collateralTypeAddedEvent = await fixture.generateEvent(EVENTS.ASSET_MANAGER.COLLATERAL_TYPE_ADDED, assetManager)
132-
await storer.processEventUnsafe(em, collateralTypeAddedEvent)
131+
await storer.processEvent(collateralTypeAddedEvent)
133132
const collateralTypeAdded = await em.findOneOrFail(CollateralTypeAdded,
134133
{ evmLog: { index: collateralTypeAddedEvent.logIndex, block: { index: collateralTypeAddedEvent.blockNumber }}},
135134
{ populate: ['evmLog.block', 'address'] })
@@ -138,6 +137,8 @@ describe("FAsset evm events", () => {
138137
expect(collateralTypeAdded.evmLog.block.index).to.equal(collateralTypeAddedEvent.blockNumber)
139138
expect(collateralTypeAdded.address.hex).to.equal(collateralTypeAddedEvent.args[1])
140139
expect(collateralTypeAdded.fasset).to.equal(FAssetType.FXRP)
140+
// store agents
141+
await fixture.storeInitialAgents(FAssetType.FXRP, true)
141142
// create agent
142143
const agentVaultCreatedEvent = await fixture.generateEvent(EVENTS.ASSET_MANAGER.AGENT_VAULT_CREATED, assetManager)
143144
await storer.processEventUnsafe(em, agentVaultCreatedEvent)
@@ -860,4 +861,17 @@ describe("FAsset evm events", () => {
860861
})
861862
})
862863

864+
describe("edge cases", async () => {
865+
it("should fetch null bigint when storing agent settings updated", async () => {
866+
const assetManager = context.getContractAddress(ASSET_MANAGER_FXRP)
867+
// add initial collateral token type
868+
const collateralTypeAddedEvent = await fixture.generateEvent(EVENTS.ASSET_MANAGER.COLLATERAL_TYPE_ADDED, assetManager)
869+
await storer.processEvent(collateralTypeAddedEvent)
870+
// store
871+
await fixture.storeInitialAgents(FAssetType.FXRP, true)
872+
const settingChanged = await fixture.generateEvent(EVENTS.ASSET_MANAGER.AGENT_SETTING_CHANGED, assetManager)
873+
await storer.processEvent(settingChanged)
874+
})
875+
})
876+
863877
})

packages/fasset-indexer-core/test/fixtures/constants.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ export const AGENT_SETTINGS = [
1111
'mintingPoolCollateralRatioBIPS',
1212
'buyFAssetByAgentFactorBIPS',
1313
'poolExitCollateralRatioBIPS',
14-
'poolTopupCollateralRatioBIPS',
15-
'poolTopupTokenPriceFactorBIPS'
14+
'redemptionPoolFeeShareBIPS'
1615
]

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { EventGeneration } from "./generate"
99
import { EVENTS } from "../../../src/config"
1010
import type { Event } from "../../../src/indexer/eventlib/event-scraper"
1111
import type { EventNameToEventArgs } from "./types"
12+
import { AgentVaultSettings, CollateralTypeAdded } from "../../../src/orm/entities"
1213

1314

1415
export class EventFixture extends EventGeneration {
@@ -22,7 +23,7 @@ export class EventFixture extends EventGeneration {
2223
})
2324
}
2425

25-
async storeInitialAgents(fasset: FAssetType = FAssetType.FXRP): Promise<AgentVault[]> {
26+
async storeInitialAgents(fasset: FAssetType = FAssetType.FXRP, settings = false): Promise<AgentVault[]> {
2627
const agents: AgentVault[] = []
2728
await this.orm.em.transactional(async (em) => {
2829
const managerAddress = new EvmAddress(randomNativeAddress(), 1)
@@ -42,6 +43,21 @@ export class EventFixture extends EventGeneration {
4243
collateralPoolTokenAddress, agentOwner, false
4344
)
4445
em.persist(agentVault)
46+
if (settings) {
47+
const collateralTypeAdded = await em.findOneOrFail(CollateralTypeAdded, { collateralClass: 1 })
48+
const agentVaultSettings = new AgentVaultSettings(
49+
agentVault,
50+
collateralTypeAdded,
51+
BigInt(0),
52+
BigInt(0),
53+
BigInt(0),
54+
BigInt(0),
55+
BigInt(0),
56+
BigInt(0),
57+
BigInt(0)
58+
)
59+
em.persist(agentVaultSettings)
60+
}
4561
agents.push(agentVault)
4662
})
4763
return agents

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class EventGeneration {
8686
transactionIndex: randomNumber(1, 1e6),
8787
logIndex: randomNumber(1, 1e6),
8888
source: source ?? randomChoice(ASSET_MANAGERS),
89-
blockTimestamp: Date.now(),
89+
blockTimestamp: Math.floor(Date.now() / 1000),
9090
transactionHash: randomHash(),
9191
transactionSource: randomNativeAddress(),
9292
transactionTarget: randomNativeAddress()

0 commit comments

Comments
 (0)