Skip to content

Commit

Permalink
refactor!: remove unusable fields from StakePool core type
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

- remove `epochRewards` and type `StakePoolEpochRewards`
- remove `transactions` and type `StakePoolTransactions`
  • Loading branch information
Ivaylo Andonov authored and rhyslbw committed Jun 2, 2023
1 parent c5b49f8 commit a7aa17f
Show file tree
Hide file tree
Showing 14 changed files with 12 additions and 317 deletions.
Expand Up @@ -28,7 +28,7 @@ import {
queryCacheKey
} from './util';
import { InMemoryCache, UNLIMITED_CACHE_TTL } from '../../InMemoryCache';
import { PromiseOrValue, RunnableModule, isNotNil, resolveObjectValues } from '@cardano-sdk/util';
import { PromiseOrValue, RunnableModule, resolveObjectValues } from '@cardano-sdk/util';
import { StakePoolBuilder } from './StakePoolBuilder';
import { StakePoolMetadataService } from '../types';
import { toStakePoolResults } from './mappers';
Expand Down Expand Up @@ -336,12 +336,6 @@ export class DbSyncStakePoolProvider extends DbSyncProvider(RunnableModule) impl
this.#builder.queryPoolAPY(hashesIds, this.#epochLength, { rewardsHistoryLimit })
);
}
// Get stake pools rewards cached
const poolRewards = await this.#cache.get(
queryCacheKey(StakePoolsSubQuery.REWARDS, orderedResultHashIds, options),
() => this.#builder.queryPoolRewards(orderedResultHashIds, this.#epochLength, rewardsHistoryLimit),
UNLIMITED_CACHE_TTL
);
// Create lookup table with pool ids: (hashId:updateId)
const hashIdsMap = Object.fromEntries(
orderedResultHashIds.map((hashId, idx) => [hashId, orderedResultUpdateIds[idx]])
Expand Down Expand Up @@ -376,7 +370,6 @@ export class DbSyncStakePoolProvider extends DbSyncProvider(RunnableModule) impl
poolRegistrations,
poolRelays,
poolRetirements,
poolRewards: poolRewards.filter(isNotNil),
totalCount
});
};
Expand Down
Expand Up @@ -2,7 +2,6 @@
import {
BlockfrostPoolMetricsModel,
EpochModel,
EpochRewardModel,
OrderByOptions,
OwnerAddressModel,
PoolAPY,
Expand Down Expand Up @@ -31,7 +30,6 @@ import {
mapAddressOwner,
mapBlockfrostPoolMetrics,
mapEpoch,
mapEpochReward,
mapPoolAPY,
mapPoolData,
mapPoolMetrics,
Expand Down Expand Up @@ -89,16 +87,6 @@ export class StakePoolBuilder {
return result.rows.length > 0 ? result.rows.map(mapAddressOwner) : [];
}

public async queryPoolRewards(hashesIds: number[], epochLength: number, limit?: number) {
this.#logger.debug('About to query pool rewards');

const result = await this.#db.query<EpochRewardModel>(Queries.findPoolEpochRewards(epochLength, limit), [
hashesIds
]);

return result.rows.map(mapEpochReward);
}

public async queryPoolAPY(hashesIds: number[], epochLength: number, options?: QueryPoolsApyArgs): Promise<PoolAPY[]> {
this.#logger.debug('About to query pools APY');
const defaultSort: OrderByOptions[] = [{ field: 'apy', order: 'desc' }];
Expand Down
Expand Up @@ -4,8 +4,6 @@ import {
BlockfrostPoolMetricsModel,
Epoch,
EpochModel,
EpochReward,
EpochRewardModel,
HashIdStakePoolMap,
OwnerAddressModel,
PoolAPY,
Expand Down Expand Up @@ -36,7 +34,7 @@ const getPoolStatus = (
lastPoolRegistration: PoolRegistration,
lastEpoch: number,
lastPoolRetirement?: PoolRetirement
) => {
): Cardano.StakePoolStatus => {
if (lastPoolRetirement === undefined || lastPoolRetirement.retiringEpoch <= lastPoolRegistration.activeEpochNo) {
if (lastPoolRegistration.activeEpochNo > lastEpoch) return Cardano.StakePoolStatus.Activating;
return Cardano.StakePoolStatus.Active;
Expand All @@ -51,7 +49,6 @@ interface ToCoreStakePoolInput {
poolRegistrations: PoolRegistration[];
poolRelays: PoolRelay[];
poolRetirements: PoolRetirement[];
poolRewards: EpochReward[];
lastEpochNo: Cardano.EpochNo;
poolMetrics: PoolMetrics[];
totalCount: number;
Expand Down Expand Up @@ -89,7 +86,6 @@ export const toStakePoolResults = (
poolRegistrations,
poolRelays,
poolRetirements,
poolRewards,
lastEpochNo,
poolMetrics,
totalCount,
Expand All @@ -106,16 +102,12 @@ export const toStakePoolResults = (
const poolData = poolDatas.find((data) => data.hashId === hashId);
if (!poolData) return;

const epochRewards = poolRewards
.filter((r) => r.hashId === poolData.hashId)
.map((reward) => reward.epochReward);

// Get the cached value if given hash id persist in the in-memory cache
if (fromCache[hashId]) return { ...fromCache[hashId], epochRewards } as Cardano.StakePool;
if (fromCache[hashId]) return fromCache[hashId];

const apy = poolAPYs.find((pool) => pool.hashId === hashId)?.apy;
const registrations = poolRegistrations.filter((r) => r.hashId === poolData.hashId);
const retirements = poolRetirements.filter((r) => r.hashId === poolData.hashId);
const registration = poolRegistrations.find((r) => r.hashId === poolData.hashId);
const retirement = poolRetirements.find((r) => r.hashId === poolData.hashId);
const poolMetric = (poolMetrics as BlockfrostPoolMetrics[]).find(
(metric) => metric.hashId === poolData.hashId
);
Expand All @@ -126,7 +118,6 @@ export const toStakePoolResults = (
}
const coreStakePool: Cardano.StakePool = {
cost: poolData.cost,
epochRewards,
hexId: poolData.hexId,
id: poolData.id,
margin: poolData.margin,
Expand All @@ -138,20 +129,12 @@ export const toStakePoolResults = (
? {
owners: poolMetric?.owners || [],
rewardAccount: poolMetric?.rewardAccount || ('' as Cardano.RewardAccount),
status: poolMetric?.status || Cardano.StakePoolStatus.Retired,
transactions: {
registration: poolMetric?.registration || [],
retirement: poolMetric?.retirement || []
}
status: poolMetric?.status || Cardano.StakePoolStatus.Retired
}
: {
owners: poolOwners.filter((o) => o.hashId === poolData.hashId).map((o) => o.address),
rewardAccount: poolData.rewardAccount,
status: getPoolStatus(registrations[0], lastEpochNo, retirements[0]),
transactions: {
registration: registrations.map((r) => r.transactionId),
retirement: retirements.map((r) => r.transactionId)
}
status: getPoolStatus(registration!, lastEpochNo, retirement)
})
};
if (poolData.metadata) coreStakePool.metadata = poolData.metadata;
Expand Down Expand Up @@ -241,19 +224,6 @@ export const mapEpoch = ({ no, optimal_pool_count }: EpochModel): Epoch => ({
optimalPoolCount: optimal_pool_count
});

export const mapEpochReward = (epochRewardModel: EpochRewardModel): EpochReward => ({
epochReward: {
activeStake: BigInt(epochRewardModel.active_stake),
epoch: Cardano.EpochNo(epochRewardModel.epoch_no),
epochLength: Number(epochRewardModel.epoch_length),
leaderRewards: BigInt(epochRewardModel.leader_rewards),
memberROI: Cardano.Percent(epochRewardModel.member_roi),
memberRewards: BigInt(epochRewardModel.member_rewards),
pledge: BigInt(epochRewardModel.pledge)
},
hashId: Number(epochRewardModel.hash_id)
});

export const mapAddressOwner = (ownerAddressModel: OwnerAddressModel): PoolOwner => ({
address: ownerAddressModel.address as unknown as Cardano.RewardAccount,
hashId: Number(ownerAddressModel.hash_id)
Expand Down
Expand Up @@ -321,21 +321,6 @@ epoch_rewards AS (
)
)`;

export const findPoolEpochRewards = (epochLength: number, limit?: number) => `
${epochRewardsSubqueries(epochLength, limit)}
SELECT
active_stake,
epoch_length::TEXT,
epoch_no,
hash_id,
leader_rewards,
member_rewards,
member_roi,
pledge
FROM epoch_rewards
ORDER BY epoch_no desc
`;

export const findPoolAPY = (epochLength: number, limit?: number) => `
${epochRewardsSubqueries(epochLength, limit)}
SELECT
Expand Down Expand Up @@ -881,7 +866,6 @@ const Queries = {
findLastEpoch,
findLastEpochWithData,
findPoolAPY,
findPoolEpochRewards,
findPoolStats,
findPools,
findPoolsData,
Expand Down
Expand Up @@ -62,22 +62,6 @@ export interface EpochModel {
optimal_pool_count?: number;
}

export interface EpochReward {
hashId: number;
epochReward: Cardano.StakePoolEpochRewards;
}

export interface EpochRewardModel {
active_stake: string;
epoch_no: number;
epoch_length: string;
hash_id: number;
leader_rewards: string;
member_rewards: string;
member_roi: number;
pledge: string;
}

export interface OwnerAddressModel {
address: string;
hash_id: string;
Expand Down
52 changes: 0 additions & 52 deletions packages/cardano-services/src/StakePool/openApi.json
Expand Up @@ -223,58 +223,6 @@
},
"status": {
"$ref": "#/components/schemas/StakePoolStatus"
},
"transactions": {
"$ref": "#/components/schemas/StakePoolTransactions"
},
"epochRewards": {
"type": "array",
"items": {
"$ref": "#/components/schemas/EpochRewards"
}
}
}
},
"EpochRewards": {
"type": "object",
"properties": {
"activeStake": {
"$ref": "#/components/schemas/BigInt"
},
"epoch": {
"type": "number"
},
"epochLength": {
"type": "number"
},
"leaderRewards": {
"$ref": "#/components/schemas/BigInt"
},
"memberROI": {
"type": "number"
},
"memberRewards": {
"$ref": "#/components/schemas/BigInt"
},
"pledge": {
"$ref": "#/components/schemas/BigInt"
}
}
},
"StakePoolTransactions": {
"type": "object",
"properties": {
"registration": {
"type": "array",
"items": {
"type": "string"
}
},
"retirement": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down
Expand Up @@ -62,14 +62,6 @@ describe('StakePoolBuilder', () => {
expect(registrations[0]).toMatchShapeOf(DataMocks.Pool.registration);
});
});
describe('queryPoolRewards', () => {
it('queryPoolRewards', async () => {
const epochRewards = await builder.queryPoolRewards(hashIds, epochLength);

expect(epochRewards.filter(({ epochReward: { epoch } }) => epoch === Cardano.EpochNo(12))).toHaveLength(3);
expect(epochRewards[0]).toMatchShapeOf(DataMocks.Pool.epochReward);
});
});
describe('queryPoolRelays', () => {
it('queryPoolRelays', async () => {
const relays = await builder.queryPoolRelays(updateIds);
Expand Down

0 comments on commit a7aa17f

Please sign in to comment.