Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(projection-typeorm): add stake pools metrics computation job sch…
…edule
- Loading branch information
Showing
11 changed files
with
170 additions
and
7 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
packages/projection-typeorm/src/entity/CurrentPoolMetrics.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { BigIntColumnOptions, DeleteCascadeRelationOptions } from './util'; | ||
import { Cardano } from '@cardano-sdk/core'; | ||
import { Column, Entity, JoinColumn, OneToOne, PrimaryColumn } from 'typeorm'; | ||
import { StakePoolEntity } from './StakePool.entity'; | ||
import { float } from './transformers'; | ||
|
||
@Entity() | ||
export class CurrentPoolMetricsEntity { | ||
// Using the same column for both primary and foreign key | ||
@PrimaryColumn({ length: 56, type: 'char' }) | ||
stakePoolId?: Cardano.PoolId; | ||
|
||
@Column() | ||
slot: Cardano.Slot; | ||
|
||
@OneToOne(() => StakePoolEntity, DeleteCascadeRelationOptions) | ||
@JoinColumn() | ||
stakePool?: StakePoolEntity; | ||
|
||
@Column({ type: 'integer' }) | ||
mintedBlocks?: number; | ||
|
||
@Column({ type: 'integer' }) | ||
liveDelegators?: number; | ||
|
||
@Column(BigIntColumnOptions) | ||
activeStake?: Cardano.Lovelace; | ||
|
||
@Column(BigIntColumnOptions) | ||
liveStake?: Cardano.Lovelace; | ||
|
||
@Column(BigIntColumnOptions) | ||
livePledge?: Cardano.Lovelace; | ||
|
||
@Column({ transformer: float, type: 'numeric' }) | ||
liveSaturation?: Cardano.Percent; | ||
|
||
@Column({ transformer: float, type: 'numeric' }) | ||
activeSize?: Cardano.Percent; | ||
|
||
@Column({ transformer: float, type: 'numeric' }) | ||
liveSize?: Cardano.Percent; | ||
|
||
@Column({ transformer: float, type: 'numeric' }) | ||
apy?: Cardano.Percent; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/projection-typeorm/src/operators/storePoolMetricsUpdateJob.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Cardano, ChainSyncEventType } from '@cardano-sdk/core'; | ||
import { STAKE_POOL_METRICS_UPDATE, StakePoolMetricsUpdateJob } from '../pgBoss'; | ||
import { WithPgBoss } from './withTypeormTransaction'; | ||
import { typeormOperator } from './util'; | ||
|
||
export const createStorePoolMetricsUpdateJob = (jobFrequency = 1000) => { | ||
// Remember the blockNo of last sent job in order to no resend another job in case of rollback | ||
let lastSentBlock: Cardano.BlockNo | undefined; | ||
let reachedTheTip = false; | ||
|
||
return typeormOperator<WithPgBoss>(async ({ eventType, pgBoss, block: { header }, tip }) => { | ||
let insertFirstJob = false; | ||
|
||
if (eventType === ChainSyncEventType.RollBackward) return; | ||
if (!reachedTheTip) insertFirstJob = reachedTheTip = tip.slot === header.slot; | ||
|
||
const { blockNo, slot } = header; | ||
|
||
if (insertFirstJob || (blockNo % jobFrequency === 0 && blockNo !== lastSentBlock && reachedTheTip)) { | ||
const task: StakePoolMetricsUpdateJob = { slot }; | ||
|
||
lastSentBlock = blockNo; | ||
await pgBoss.send(STAKE_POOL_METRICS_UPDATE, task, { slot }); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/projection-typeorm/test/operators/storePoolMetricsUpdateJob.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { OperatorFunction, of } from 'rxjs'; | ||
import { STAKE_POOL_METRICS_UPDATE, createStorePoolMetricsUpdateJob } from '../../src'; | ||
|
||
const testPromise = () => { | ||
let resolvePromise: Function; | ||
const promise = new Promise<void>((resolve) => (resolvePromise = resolve)); | ||
return [promise, resolvePromise!] as const; | ||
}; | ||
|
||
describe('createStorePoolMetricsUpdateJob', () => { | ||
it('sends jobs only at expected blocks', async () => { | ||
let counter = 0; | ||
const [promise, resolver] = testPromise(); | ||
const send = jest.fn(() => { | ||
if (++counter === 3) resolver(); | ||
return Promise.resolve(); | ||
}); | ||
const createEvent = (blockNo: number, tipSlot?: number) => ({ | ||
block: { header: { blockNo, slot: blockNo * 10 } }, | ||
eventType: 0, | ||
pgBoss: { send }, | ||
tip: { slot: tipSlot ?? blockNo * 10 } | ||
}); | ||
|
||
of( | ||
createEvent(2, 80), | ||
createEvent(3, 80), | ||
createEvent(4, 80), | ||
createEvent(5, 80), // doesn't generate event since tip is not reached | ||
createEvent(6, 80), | ||
createEvent(7, 80), | ||
createEvent(8), // generates first event once tip is reached | ||
createEvent(9), | ||
createEvent(10), // generates a std event | ||
createEvent(11), | ||
createEvent(10), // doesn't generate event due to rollback | ||
createEvent(11), | ||
createEvent(12), | ||
createEvent(13), | ||
createEvent(14), | ||
createEvent(15), // generates a std event | ||
createEvent(16) | ||
) | ||
.pipe( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
createStorePoolMetricsUpdateJob(5)() as OperatorFunction<any, any> | ||
) | ||
.subscribe(); | ||
|
||
await promise; | ||
|
||
expect(send).toBeCalledTimes(3); | ||
expect(send).toBeCalledWith(STAKE_POOL_METRICS_UPDATE, { slot: 80 }, { slot: 80 }); | ||
expect(send).toBeCalledWith(STAKE_POOL_METRICS_UPDATE, { slot: 100 }, { slot: 100 }); | ||
expect(send).toBeCalledWith(STAKE_POOL_METRICS_UPDATE, { slot: 150 }, { slot: 150 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters