Skip to content

Commit e4da0e3

Browse files
committed
refactor!: hoist Cardano.Percent to util package
1 parent 19cabfe commit e4da0e3

File tree

16 files changed

+55
-44
lines changed

16 files changed

+55
-44
lines changed

packages/cardano-services/src/StakePool/DbSyncStakePoolProvider/mappers.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
} from './types';
2828
import { Cardano, StakePoolStats } from '@cardano-sdk/core';
2929
import { Hash32ByteBase16 } from '@cardano-sdk/crypto';
30-
import { bufferToHexString, isNotNil } from '@cardano-sdk/util';
30+
import { Percent, bufferToHexString, isNotNil } from '@cardano-sdk/util';
3131
import Fraction from 'fraction.js';
3232

3333
const getPoolStatus = (
@@ -65,7 +65,7 @@ export const calcNodeMetricsValues = (metrics: PoolMetrics['metrics'], apy?: num
6565
const isZeroStake = liveStake === 0n;
6666
const size: Cardano.StakePoolMetricsSize = {
6767
active: activeStakePercentage,
68-
live: Cardano.Percent(!isZeroStake ? 1 - activeStakePercentage : 0)
68+
live: Percent(!isZeroStake ? 1 - activeStakePercentage : 0)
6969
};
7070
const stake: Cardano.StakePoolMetricsStake = {
7171
active: activeStake,
@@ -245,12 +245,12 @@ export const mapPoolMetrics = (poolMetricsModel: PoolMetricsModel): PoolMetrics
245245
hashId: Number(poolMetricsModel.pool_hash_id),
246246
metrics: {
247247
activeStake: BigInt(poolMetricsModel.active_stake),
248-
activeStakePercentage: Cardano.Percent(Number(poolMetricsModel.active_stake_percentage)),
248+
activeStakePercentage: Percent(Number(poolMetricsModel.active_stake_percentage)),
249249
blocksCreated: poolMetricsModel.blocks_created,
250250
delegators: poolMetricsModel.delegators,
251251
livePledge: BigInt(poolMetricsModel.live_pledge),
252252
liveStake: BigInt(poolMetricsModel.live_stake),
253-
saturation: Cardano.Percent(Number.parseFloat(poolMetricsModel.saturation))
253+
saturation: Percent(Number.parseFloat(poolMetricsModel.saturation))
254254
}
255255
});
256256

packages/cardano-services/src/StakePool/DbSyncStakePoolProvider/types.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Cardano, Paginated, QueryStakePoolsArgs } from '@cardano-sdk/core';
2+
import { Percent } from '@cardano-sdk/util';
23
export interface PoolUpdateModel {
34
id: string; // pool hash id
45
update_id: string;
@@ -130,8 +131,8 @@ export interface PoolMetrics extends CommonPoolInfo {
130131
livePledge: Cardano.Lovelace;
131132
activeStake: Cardano.Lovelace;
132133
liveStake: Cardano.Lovelace;
133-
activeStakePercentage: Cardano.Percent;
134-
saturation: Cardano.Percent;
134+
activeStakePercentage: Percent;
135+
saturation: Percent;
135136
delegators: number;
136137
};
137138
}

packages/cardano-services/src/StakePool/TypeormStakePoolProvider/mappers.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Cardano, Paginated } from '@cardano-sdk/core';
22
import { Hash32ByteBase16 } from '@cardano-sdk/crypto';
3+
import { Percent, isNotNil } from '@cardano-sdk/util';
34
import { RelayModel, mapRelay } from '../DbSyncStakePoolProvider';
4-
import { isNotNil } from '@cardano-sdk/util';
55

66
export type Margin = {
77
numerator: number;
@@ -90,10 +90,10 @@ const defaultMetrics: Cardano.StakePoolMetrics = {
9090
blocksCreated: 0,
9191
delegators: 0,
9292
livePledge: 0n,
93-
saturation: Cardano.Percent(0),
93+
saturation: Percent(0),
9494
size: {
95-
active: Cardano.Percent(0),
96-
live: Cardano.Percent(0)
95+
active: Percent(0),
96+
live: Percent(0)
9797
},
9898
stake: {
9999
active: 0n,
@@ -105,14 +105,14 @@ const mapMetrics = (pool: PoolModel): Cardano.StakePoolMetrics => {
105105
if (pool.metrics_live_pledge === null) return defaultMetrics;
106106

107107
return {
108-
apy: pool.metrics_apy ? Cardano.Percent(Number.parseFloat(pool.metrics_apy)) : undefined,
108+
apy: pool.metrics_apy ? Percent(Number.parseFloat(pool.metrics_apy)) : undefined,
109109
blocksCreated: pool.metrics_minted_blocks,
110110
delegators: pool.metrics_live_delegators,
111111
livePledge: BigInt(pool.metrics_live_pledge),
112-
saturation: Cardano.Percent(Number.parseFloat(pool.metrics_live_saturation)),
112+
saturation: Percent(Number.parseFloat(pool.metrics_live_saturation)),
113113
size: {
114-
active: Cardano.Percent(Number.parseFloat(pool.metrics_active_size)),
115-
live: Cardano.Percent(Number.parseFloat(pool.metrics_live_size))
114+
active: Percent(Number.parseFloat(pool.metrics_active_size)),
115+
live: Percent(Number.parseFloat(pool.metrics_live_size))
116116
},
117117
stake: {
118118
active: BigInt(pool.metrics_active_stake),

packages/cardano-services/test/PgBoss/stakePoolMetricsHandler.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Cardano, StakePoolProvider } from '@cardano-sdk/core';
22
import { CurrentPoolMetricsEntity } from '@cardano-sdk/projection-typeorm';
33
import { DataSource } from 'typeorm';
4+
import { Percent } from '@cardano-sdk/util';
45
import { initHandlerTest, poolId } from './util';
56
import { logger } from '@cardano-sdk/util-dev';
67
import { savePoolMetrics } from '../../src/PgBoss';
@@ -30,8 +31,8 @@ describe('stakePoolMetricsHandler', () => {
3031
blocksCreated: 23,
3132
delegators: 15,
3233
livePledge: 23_000_000n,
33-
saturation: Cardano.Percent(0.002),
34-
size: { active: Cardano.Percent(0.0005), live: Cardano.Percent(0.0005) },
34+
saturation: Percent(0.002),
35+
size: { active: Percent(0.0005), live: Percent(0.0005) },
3536
stake: { active: 42_000_000n, live: 42_000_000n }
3637
};
3738

packages/core/src/Cardano/types/StakePool/StakePool.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
import { EpochNo } from '../Block';
22
import { Lovelace } from '../Value';
3-
import { OpaqueNumber } from '@cardano-sdk/util';
3+
import { Percent } from '@cardano-sdk/util';
44
import { PoolIdHex } from './primitives';
55
import { PoolParameters } from './PoolParameters';
66

7-
/**
8-
* The Percentage is a relative value that indicates the hundredth parts of any quantity.
9-
*
10-
* One percent 1% (0.01) represents the one hundredth, 2 percent 2% (0.02) represents two hundredths,
11-
* 100% (1.0) represents the whole, 200% (2.0) twice the given quantity and so on…
12-
*/
13-
export type Percent = OpaqueNumber<'Percent'>;
14-
export const Percent = (value: number): Percent => value as unknown as Percent;
15-
167
/**
178
* Stake quantities for a Stake Pool.
189
*/

packages/core/src/Cardano/util/estimateStakePoolAPY.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Percent, StakePoolEpochRewards } from '../types';
1+
import { Percent } from '@cardano-sdk/util';
2+
import { StakePoolEpochRewards } from '../types';
23

34
const MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;
45

packages/core/src/Provider/Provider.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Cardano } from '..';
2-
import { Percent } from '../Cardano';
2+
33
// eslint-disable-next-line import/no-extraneous-dependencies
44
import { Logger } from 'ts-log';
5+
import { Percent } from '@cardano-sdk/util';
56
import { Tip } from '@cardano-ogmios/schema';
67

78
export type HealthCheckResponse = {

packages/core/test/Cardano/util/estimateStakePoolAPY.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Cardano } from '../../../src';
2+
import { Percent } from '@cardano-sdk/util';
23

34
describe('estimateStakePoolAPY', () => {
45
const rewards = {
56
activeStake: 10_365_739_303_707n,
67
epochLength: 432_000_000,
7-
memberROI: Cardano.Percent(0.000_829_950_248_854_788),
8+
memberROI: Percent(0.000_829_950_248_854_788),
89
memberRewards: 8_579_404_603n,
910
pledge: 28_487_625_262n
1011
} as Cardano.StakePoolEpochRewards;

packages/core/test/CardanoNode/mocks.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Cardano, EraSummary, HealthCheckResponse, Milliseconds, StakeDistribution } from '../../src';
2+
import { Percent } from '@cardano-sdk/util';
23

34
const mockEraSummaries: EraSummary[] = [
45
{
@@ -55,7 +56,7 @@ export const healthCheckResponseMock = (opts?: {
5556
blockNo?: number;
5657
slot?: number;
5758
hash?: string;
58-
networkSync?: Cardano.Percent;
59+
networkSync?: Percent;
5960
withTip?: boolean;
6061
projectedTip?: {
6162
blockNo?: number;
@@ -69,7 +70,7 @@ export const healthCheckResponseMock = (opts?: {
6970
hash: opts?.hash ?? '9ef43ab6e234fcf90d103413096c7da752da2f45b15e1259f43d476afd12932c',
7071
slot: opts?.slot ?? 52_819_355
7172
},
72-
networkSync: opts?.networkSync ?? Cardano.Percent(0.999)
73+
networkSync: opts?.networkSync ?? Percent(0.999)
7374
},
7475
ok: true,
7576
...(opts?.withTip === false

packages/ogmios/src/util.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Cardano } from '@cardano-sdk/core';
21
import {
32
ConnectionConfig,
43
InteractionContext,
@@ -7,6 +6,7 @@ import {
76
createInteractionContext
87
} from '@cardano-ogmios/client';
98
import { Logger } from 'ts-log';
9+
import { Percent } from '@cardano-sdk/util';
1010

1111
/**
1212
* Converts an Ogmios connection URL to an Ogmios ConnectionConfig Object
@@ -56,7 +56,7 @@ export const createInteractionContextWithLogger = (
5656
export const ogmiosServerHealthToHealthCheckResponse = ({ lastKnownTip, networkSynchronization }: ServerHealth) => ({
5757
localNode: {
5858
ledgerTip: lastKnownTip,
59-
networkSync: Cardano.Percent(networkSynchronization)
59+
networkSync: Percent(networkSynchronization)
6060
},
6161
ok: networkSynchronization > 0.99
6262
});

packages/ogmios/test/util.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Cardano } from '@cardano-sdk/core';
21
import { HEALTH_RESPONSE_BODY } from './mocks/util';
32
import { InteractionContext, ServerHealth } from '@cardano-ogmios/client';
43
import { Logger } from 'ts-log';
4+
import { Percent } from '@cardano-sdk/util';
55
import { createInteractionContextWithLogger, ogmiosServerHealthToHealthCheckResponse } from '../src';
66
import { createLogger } from '@cardano-sdk/util-dev';
77
import { createMockOgmiosServer, listenPromise, serverClosePromise } from './mocks/mockOgmiosServer';
@@ -91,7 +91,7 @@ describe('util', () => {
9191
expect(ogmiosServerHealthToHealthCheckResponse({ ...serverHealth, networkSynchronization })).toEqual({
9292
localNode: {
9393
ledgerTip: serverHealth.lastKnownTip,
94-
networkSync: Cardano.Percent(networkSynchronization)
94+
networkSync: Percent(networkSynchronization)
9595
},
9696
ok: true
9797
});

packages/projection-typeorm/src/entity/CurrentPoolMetrics.entity.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BigIntColumnOptions, DeleteCascadeRelationOptions } from './util';
22
import { Cardano } from '@cardano-sdk/core';
33
import { Column, Entity, JoinColumn, OneToOne, PrimaryColumn } from 'typeorm';
4+
import { Percent } from '@cardano-sdk/util';
45
import { StakePoolEntity } from './StakePool.entity';
56
import { float } from './transformers';
67

@@ -33,14 +34,14 @@ export class CurrentPoolMetricsEntity {
3334
livePledge?: Cardano.Lovelace;
3435

3536
@Column({ transformer: float, type: 'numeric' })
36-
liveSaturation?: Cardano.Percent;
37+
liveSaturation?: Percent;
3738

3839
@Column({ transformer: float, type: 'numeric' })
39-
activeSize?: Cardano.Percent;
40+
activeSize?: Percent;
4041

4142
@Column({ transformer: float, type: 'numeric' })
42-
liveSize?: Cardano.Percent;
43+
liveSize?: Percent;
4344

4445
@Column({ transformer: float, type: 'numeric' })
45-
apy?: Cardano.Percent;
46+
apy?: Percent;
4647
}

packages/projection-typeorm/src/entity/PoolRegistration.entity.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { BigIntColumnOptions, DeleteCascadeRelationOptions } from './util';
33
import { BlockEntity } from './Block.entity';
44
import { Cardano } from '@cardano-sdk/core';
55
import { Column, Entity, JoinColumn, ManyToOne, OneToOne, PrimaryColumn } from 'typeorm';
6+
import { Percent } from '@cardano-sdk/util';
67
import { PoolMetadataEntity } from './PoolMetadata.entity';
78
import { StakePoolEntity } from './StakePool.entity';
89

@@ -26,7 +27,7 @@ export class PoolRegistrationEntity {
2627
@Column({ type: 'jsonb' })
2728
margin?: Cardano.Fraction;
2829
@Column({ type: 'float4' })
29-
marginPercent?: Cardano.Percent;
30+
marginPercent?: Percent;
3031
@Column('jsonb')
3132
relays?: Cardano.Relay[];
3233
@Column('jsonb')

packages/util-dev/src/mockProviders/mockRewardsProvider.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Cardano, Paginated, StakePoolProvider } from '@cardano-sdk/core';
2+
import { Percent } from '@cardano-sdk/util';
23
import { epochRewards, rewardAccountBalance, rewardAccountBalance2, rewardsHistory, rewardsHistory2 } from './mockData';
34
import { getRandomTxId } from './mockChainHistoryProvider';
45
import delay from 'delay';
@@ -41,14 +42,14 @@ export const generateStakePools = (qty: number): Cardano.StakePool[] =>
4142
url: 'https://git.io/JJ7wm'
4243
},
4344
metrics: {
44-
apy: Cardano.Percent(0),
45+
apy: Percent(0),
4546
blocksCreated: 0,
4647
delegators: 1,
4748
livePledge: 495_463_149n,
48-
saturation: Cardano.Percent(0.000_035_552_103_558_591_88),
49+
saturation: Percent(0.000_035_552_103_558_591_88),
4950
size: {
50-
active: Cardano.Percent(1),
51-
live: Cardano.Percent(0)
51+
active: Percent(1),
52+
live: Percent(0)
5253
},
5354
stake: {
5455
active: 2_986_376_991n,

packages/util/src/Percent.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { OpaqueNumber } from './opaqueTypes';
2+
3+
/**
4+
* The Percentage is a relative value that indicates the hundredth parts of any quantity.
5+
*
6+
* One percent 1% (0.01) represents the one hundredth, 2 percent 2% (0.02) represents two hundredths,
7+
* 100% (1.0) represents the whole, 200% (2.0) twice the given quantity and so on…
8+
*/
9+
export type Percent = OpaqueNumber<'Percent'>;
10+
export const Percent = (value: number): Percent => value as unknown as Percent;

packages/util/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ export * from './environment';
1717
export * from './patchObject';
1818
export * from './isPromise';
1919
export * from './transformer';
20+
export * from './Percent';
2021
export { PromiseOrValue, resolveObjectValues } from './util';

0 commit comments

Comments
 (0)