Skip to content

Commit

Permalink
fixup! refactor(cardano-services): skips a not used query when using …
Browse files Browse the repository at this point in the history
…blockfrost
  • Loading branch information
iccicci committed Mar 22, 2023
1 parent 60b0946 commit abb9b01
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
Expand Up @@ -27,18 +27,11 @@ import {
getStakePoolSortType,
queryCacheKey
} from './util';
import { PromiseOrValue, RunnableModule, isNotNil, resolveObjectOfPromises } from '@cardano-sdk/util';
import { PromiseOrValue, RunnableModule, isNotNil, resolveObjectValues } from '@cardano-sdk/util';
import { StakePoolBuilder } from './StakePoolBuilder';
import { toStakePoolResults } from './mappers';
import merge from 'lodash/merge';

/**
* The total stake amount is used to compute the saturation.
* When Blockfrost cache is enabled the saturation is one of the cached values:
* the query to get the value can be skipped
*/
const NOT_USED_TOTAL_STAKE = '1';

/**
* Properties that are need to create DbSyncStakePoolProvider
*/
Expand Down Expand Up @@ -130,7 +123,7 @@ export class DbSyncStakePoolProvider extends DbSyncProvider(RunnableModule) impl

private getQueryBySortType(
sortType: PoolSortType,
queryArgs: { hashesIds: number[]; updatesIds: number[]; totalStake: string },
queryArgs: { hashesIds: number[]; updatesIds: number[]; totalStake: string | null },
useBlockfrost: boolean
) {
const { hashesIds, updatesIds, totalStake } = queryArgs;
Expand Down Expand Up @@ -177,7 +170,7 @@ export class DbSyncStakePoolProvider extends DbSyncProvider(RunnableModule) impl

private async getPoolsDataOrdered(
poolUpdates: PoolUpdate[],
totalStake: string,
totalStake: string | null,
useBlockfrost: boolean,
options?: QueryStakePoolsArgs
) {
Expand Down Expand Up @@ -239,7 +232,7 @@ export class DbSyncStakePoolProvider extends DbSyncProvider(RunnableModule) impl
private async queryExtraPoolsData(
idsToFetch: PoolUpdate[],
sortType: PoolSortType,
totalStake: string,
totalStake: string | null,
orderedResult: OrderedResult,
useBlockfrost: boolean
) {
Expand Down Expand Up @@ -292,9 +285,10 @@ export class DbSyncStakePoolProvider extends DbSyncProvider(RunnableModule) impl
const poolUpdates = await this.#cache.get(queryCacheKey(StakePoolsSubQuery.POOL_HASHES, options), () =>
this.#builder.queryPoolHashes(query, params)
);
// Get cached total staked amount
// Get cached total staked amount used to compute the saturation
// When Blockfrost cache is enabled the saturation is one of the cached values: the query can be skipped
const totalStake = this.#useBlockfrost
? NOT_USED_TOTAL_STAKE
? null
: await this.#cache.get(queryCacheKey(StakePoolsSubQuery.TOTAL_STAKE), async () => {
const distribution = await this.cardanoNode.stakeDistribution();

Expand Down Expand Up @@ -348,7 +342,7 @@ export class DbSyncStakePoolProvider extends DbSyncProvider(RunnableModule) impl
);

const queryExtraPoolsDataMissingFromCacheAndMap = async () => {
const fromCache = await resolveObjectOfPromises(cachedPromises);
const fromCache = await resolveObjectValues(cachedPromises);
// Compute ids to fetch from db
const idsToFetch = Object.entries(fromCache)
.filter(([_, pool]) => pool === undefined)
Expand Down
Expand Up @@ -133,7 +133,7 @@ export class StakePoolBuilder {

public async queryPoolMetrics(
hashesIds: number[],
totalStake: string,
totalStake: string | null,
useBlockfrost: boolean,
options?: QueryStakePoolsArgs
) {
Expand Down

0 comments on commit abb9b01

Please sign in to comment.