Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into chore/lw-6514-hardware-trezor-package
- Loading branch information
Showing
39 changed files
with
707 additions
and
115 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { PgBossQueue, WorkerHandlerFactory } from './types'; | ||
import { STAKE_POOL_METADATA_QUEUE, STAKE_POOL_METRICS_UPDATE } from '@cardano-sdk/projection-typeorm'; | ||
import { stakePoolMetadataHandlerFactory } from './stakePoolMetadataHandler'; | ||
import { stakePoolMetricsHandlerFactory } from './stakePoolMetricsHandler'; | ||
|
||
export * from './stakePoolMetadataHandler'; | ||
export * from './stakePoolMetricsHandler'; | ||
export * from './types'; | ||
export * from './util'; | ||
|
||
/** | ||
* Defines the handler for each pg-boss queue | ||
*/ | ||
export const queueHandlers: Record<PgBossQueue, WorkerHandlerFactory> = { | ||
[STAKE_POOL_METADATA_QUEUE]: stakePoolMetadataHandlerFactory, | ||
[STAKE_POOL_METRICS_UPDATE]: stakePoolMetricsHandlerFactory | ||
}; |
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
79 changes: 79 additions & 0 deletions
79
packages/cardano-services/src/PgBoss/stakePoolMetricsHandler.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,79 @@ | ||
import { Cardano, StakePoolProvider } from '@cardano-sdk/core'; | ||
import { CurrentPoolMetricsEntity, StakePoolEntity, StakePoolMetricsUpdateJob } from '@cardano-sdk/projection-typeorm'; | ||
import { DataSource } from 'typeorm'; | ||
import { Logger } from 'ts-log'; | ||
import { WorkerHandlerFactory } from './types'; | ||
import { isErrorWithConstraint } from './util'; | ||
import { stakePoolHttpProvider } from '@cardano-sdk/cardano-services-client'; | ||
|
||
interface RefreshPoolMetricsOptions { | ||
dataSource: DataSource; | ||
id: Cardano.PoolId; | ||
logger: Logger; | ||
provider: StakePoolProvider; | ||
slot: Cardano.Slot; | ||
} | ||
|
||
export const savePoolMetrics = async (options: RefreshPoolMetricsOptions & { metrics: Cardano.StakePoolMetrics }) => { | ||
const { dataSource, id, metrics, slot } = options; | ||
const repos = dataSource.getRepository(CurrentPoolMetricsEntity); | ||
const entity = { | ||
activeSize: metrics.size.active, | ||
activeStake: metrics.stake.active, | ||
apy: metrics.apy || 0, | ||
id, | ||
liveDelegators: metrics.delegators, | ||
livePledge: metrics.livePledge, | ||
liveSaturation: metrics.saturation, | ||
liveSize: metrics.size.live, | ||
liveStake: metrics.stake.live, | ||
mintedBlocks: metrics.blocksCreated, | ||
slot, | ||
stakePool: { id } | ||
}; | ||
|
||
try { | ||
await repos.upsert(entity, ['stakePool']); | ||
} catch (error) { | ||
// If no poolRegistration record is present, it was rolled back: do nothing | ||
if (isErrorWithConstraint(error) && error.constraint === 'FK_current_pool_metrics_stake_pool_id') return; | ||
|
||
throw error; | ||
} | ||
}; | ||
|
||
export const refreshPoolMetrics = async (options: RefreshPoolMetricsOptions) => { | ||
const { id, logger, provider } = options; | ||
|
||
logger.info(`Refreshing metrics for stake pool ${id}`); | ||
|
||
try { | ||
const { pageResults, totalResultCount } = await provider.queryStakePools({ | ||
filters: { identifier: { values: [{ id }] } }, | ||
pagination: { limit: 1, startAt: 0 } | ||
}); | ||
|
||
if (totalResultCount === 0) return logger.warn(`No data fetched for stake pool ${id}`); | ||
|
||
const { metrics } = pageResults[0]; | ||
|
||
await savePoolMetrics({ ...options, metrics }); | ||
} catch (error) { | ||
logger.error(`Error while refreshing metrics for stake pool ${id}`, error); | ||
} | ||
}; | ||
|
||
export const stakePoolMetricsHandlerFactory: WorkerHandlerFactory = (options) => { | ||
const { dataSource, logger, stakePoolProviderUrl } = options; | ||
const provider = stakePoolHttpProvider({ baseUrl: stakePoolProviderUrl, logger }); | ||
|
||
return async (data: StakePoolMetricsUpdateJob) => { | ||
const { slot } = data; | ||
|
||
logger.info('Starting stake pools metrics job'); | ||
|
||
const pools = await dataSource.getRepository(StakePoolEntity).find({ select: { id: true } }); | ||
|
||
for (const { id } of pools) await refreshPoolMetrics({ dataSource, id: id!, logger, provider, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
import { DataSource } from 'typeorm'; | ||
import { Logger } from 'ts-log'; | ||
import { STAKE_POOL_METADATA_QUEUE } from '@cardano-sdk/projection-typeorm'; | ||
import { Pool } from 'pg'; | ||
import { STAKE_POOL_METADATA_QUEUE, STAKE_POOL_METRICS_UPDATE } from '@cardano-sdk/projection-typeorm'; | ||
|
||
export const workerQueues = [STAKE_POOL_METADATA_QUEUE] as const; | ||
export const workerQueues = [STAKE_POOL_METADATA_QUEUE, STAKE_POOL_METRICS_UPDATE] as const; | ||
|
||
export type PgBossQueue = typeof workerQueues[number]; | ||
|
||
export interface WorkerHandlerFactoryOptions { | ||
dataSource: DataSource; | ||
db: Pool; | ||
logger: Logger; | ||
stakePoolProviderUrl: string; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type WorkerHandler = (data: any) => Promise<void>; | ||
export type WorkerHandlerFactory = (dataSource: DataSource, logger: Logger) => WorkerHandler; | ||
export type WorkerHandlerFactory = (options: WorkerHandlerFactoryOptions) => WorkerHandler; |
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 |
---|---|---|
@@ -1,12 +1,6 @@ | ||
import { PgBossQueue, WorkerHandlerFactory, workerQueues } from './types'; | ||
import { STAKE_POOL_METADATA_QUEUE } from '@cardano-sdk/projection-typeorm'; | ||
import { stakePoolMetadataHandlerFactory } from './stakePoolMetadataHandler'; | ||
import { PgBossQueue, workerQueues } from './types'; | ||
|
||
export const isValidQueue = (queue: string): queue is PgBossQueue => workerQueues.includes(queue as PgBossQueue); | ||
|
||
/** | ||
* Defines the handler for each pg-boss queue | ||
*/ | ||
export const queueHandlers: Record<PgBossQueue, WorkerHandlerFactory> = { | ||
[STAKE_POOL_METADATA_QUEUE]: stakePoolMetadataHandlerFactory | ||
}; | ||
export const isErrorWithConstraint = (error: unknown): error is Error & { constraint: unknown } => | ||
error instanceof Error && 'constraint' in error; |
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
Oops, something went wrong.