All files / src/StakePool/DbSyncStakePoolProvider DbSyncStakePoolProvider.ts

92.85% Statements 130/140
70.83% Branches 34/48
97.22% Functions 35/36
92.12% Lines 117/127

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 41136x               36x                                         36x   36x 36x 36x   36x 36x                                                                         36x 6x 6x 6x 6x 6x 6x 6x 6x 6x   36x           6x           6x 6x       6x 6x 6x 6x 6x               6x               86x     86x     7x 7x     8x 3x 3x 3x       5x     71x         86x 411x                                           623x 623x 86x 86x   86x         411x 86x 2270x   86x   86x 15x 15x   71x     86x 86x               128x   602x   373x     373x         373x     373x   373x                                         128x 76x 373x 373x 76x                     76x       137x   137x 1x             136x 1x             135x 1x                 134x 6x                 137x 137x   137x   128x           128x 86x       128x     70x   70x                 128x   128x 128x     128x 86x     128x 128x 124x       77x       128x 602x     128x 128x 602x               128x 128x   128x 602x 373x     128x   128x                         128x   128x   128x   128x       4x 4x       2x       2x       2x 2x      
import {
  APY_EPOCHS_BACK_LIMIT_DEFAULT,
  IDS_NAMESPACE,
  StakePoolsSubQuery,
  emptyPoolsExtraInfo,
  getStakePoolSortType,
  queryCacheKey
} from './util';
import {
  Cardano,
  CardanoNodeUtil,
  Paginated,
  ProviderError,
  ProviderFailure,
  QueryStakePoolsArgs,
  SortField,
  StakePoolProvider,
  StakePoolStats
} from '@cardano-sdk/core';
import {
  CommonPoolInfo,
  OrderedResult,
  PoolAPY,
  PoolData,
  PoolMetrics,
  PoolSortType,
  PoolUpdate,
  StakePoolResults
} from './types';
import { DbSyncProvider, DbSyncProviderDependencies, Disposer, EpochMonitor } from '../../util';
import { GenesisData } from '../../types';
import { InMemoryCache, UNLIMITED_CACHE_TTL } from '../../InMemoryCache';
import { PromiseOrValue, RunnableModule, resolveObjectValues } from '@cardano-sdk/util';
import { StakePoolBuilder } from './StakePoolBuilder';
import { StakePoolMetadataService } from '../types';
import { toStakePoolResults } from './mappers';
import merge from 'lodash/merge.js';
 
/** Properties that are need to create DbSyncStakePoolProvider */
export interface StakePoolProviderProps {
  /** Pagination page size limit used for provider methods constraint. */
  paginationPageSizeLimit: number;
 
  /** Configure the response optional fields */
  responseConfig?: {
    search?: {
      metrics?: {
        apy?: boolean;
      };
    };
  };
 
  /** Enables Blockfrost hybrid cache. */
  useBlockfrost: boolean;
}
 
/** Dependencies that are need to create DbSyncStakePoolProvider */
export interface StakePoolProviderDependencies extends DbSyncProviderDependencies {
  /** The in memory cache engine. */
  cache: DbSyncProviderDependencies['cache'] & {
    db: InMemoryCache;
  };
 
  /** Monitor the epoch rollover through db polling. */
  epochMonitor: EpochMonitor;
 
  /** The genesis data loaded from the genesis file. */
  genesisData: GenesisData;
 
  /** The Stake Pool extended metadata service. */
  metadataService: StakePoolMetadataService;
}
 
export class DbSyncStakePoolProvider extends DbSyncProvider(RunnableModule) implements StakePoolProvider {
  #builder: StakePoolBuilder;
  #cache: InMemoryCache;
  #epochLength: number;
  #epochMonitor: EpochMonitor;
  #epochRolloverDisposer: Disposer;
  #metadataService: StakePoolMetadataService;
  #paginationPageSizeLimit: number;
  #responseConfig: StakePoolProviderProps['responseConfig'];
  #useBlockfrost: boolean;
 
  static notSupportedSortFields: SortField[] = ['blocks', 'lastRos', 'liveStake', 'margin', 'pledge', 'ros'];
 
  constructor(
    { paginationPageSizeLimit, responseConfig, useBlockfrost }: StakePoolProviderProps,
    { cache, dbPools, cardanoNode, genesisData, metadataService, logger, epochMonitor }: StakePoolProviderDependencies
  ) {
    super(
      { cache: { healthCheck: cache.healthCheck }, cardanoNode, dbPools, logger },
      'DbSyncStakePoolProvider',
      logger
    );
 
    this.#builder = new StakePoolBuilder(dbPools.main, logger);
    this.#cache = cache.db;
    // epochLength can change, so it should come from EraSummaries instead of from CompactGenesis.
    // Then we would need to look up the length of the specific epoch based on slot number.
    // However it would add a lot of complexity to the queries, so for now we use this simple approach.
    this.#epochLength = genesisData.epochLength * 1000;
    this.#epochMonitor = epochMonitor;
    this.#metadataService = metadataService;
    this.#paginationPageSizeLimit = paginationPageSizeLimit;
    this.#responseConfig = merge({
      search: {
        metrics: {
          apy: true
        }
      },
      ...responseConfig
    });
    this.#useBlockfrost = useBlockfrost;
  }
 
  private getQueryBySortType(
    sortType: PoolSortType,
    queryArgs: { hashesIds: number[]; updatesIds: number[]; totalStake: string | null },
    useBlockfrost: boolean
  ) {
    const { hashesIds, updatesIds, totalStake } = queryArgs;
    // Identify which query to use to order and paginate the result
    // Should be the only one to get the sort options, rest should be ordered by their own defaults
    switch (sortType) {
      // Add more cases as more sort types are supported
      case 'metrics':
        return (options: QueryStakePoolsArgs) =>
          this.#builder.queryPoolMetrics(hashesIds, totalStake, useBlockfrost, options);
      case 'apy':
        // HACK: If the client request sort by APY default to normal sorting.
        if (this.#responseConfig?.search?.metrics?.apy === false) {
          return async (options: QueryStakePoolsArgs) => {
            options.sort = undefined;
            return await this.#builder.queryPoolData(updatesIds, useBlockfrost, options);
          };
        }
 
        return (options: QueryStakePoolsArgs) => this.#builder.queryPoolAPY(hashesIds, this.#epochLength, options);
      case 'data':
      default:
        return (options: QueryStakePoolsArgs) => this.#builder.queryPoolData(updatesIds, useBlockfrost, options);
    }
  }
 
  private async attachExtendedMetadata(poolData: PoolData[]): Promise<void> {
    for (const pool of poolData) {
      Iif (pool.metadata?.extDataUrl || pool.metadata?.extended) {
        try {
          pool.metadata.ext = await this.#metadataService.getStakePoolExtendedMetadata(pool.metadata);
        } catch (error) {
          if (CardanoNodeUtil.isProviderError(error) && error.reason === ProviderFailure.ConnectionFailure) {
            pool.metadata.ext = undefined;
          } else if (CardanoNodeUtil.isProviderError(error) && error.reason === ProviderFailure.NotFound) {
            pool.metadata.ext = null;
          } else {
            throw error;
          }
        }
      }
    }
  }
 
  private async getPoolsDataOrdered(
    poolUpdates: PoolUpdate[],
    totalStake: string | null,
    useBlockfrost: boolean,
    options: QueryStakePoolsArgs
  ) {
    const hashesIds = poolUpdates.map(({ id }) => id);
    const updatesIds = poolUpdates.map(({ updateId }) => updateId);
    this.logger.debug(`${hashesIds.length} pools found`);
    const sortType = options.sort?.field ? getStakePoolSortType(options.sort.field) : 'data';
 
    const orderedResult = await this.getQueryBySortType(
      sortType,
      { hashesIds, totalStake, updatesIds },
      useBlockfrost
    )(options);
    const orderedResultHashIds = (orderedResult as CommonPoolInfo[]).map(({ hashId }) => hashId);
    const orderedResultUpdateIds = orderedResultHashIds.map(
      (id) => poolUpdates[poolUpdates.findIndex((item) => item.id === id)].updateId
    );
    let poolDatas: PoolData[] = [];
 
    if (sortType !== 'data') {
      this.logger.debug('About to query stake pools data');
      poolDatas = await this.#builder.queryPoolData(orderedResultUpdateIds, useBlockfrost);
    } else {
      poolDatas = orderedResult as PoolData[];
    }
 
    await this.attachExtendedMetadata(poolDatas);
    return { hashesIds, orderedResult, orderedResultHashIds, orderedResultUpdateIds, poolDatas, sortType };
  }
 
  private cacheStakePools(
    cachedPromises: { [k: string]: PromiseOrValue<Cardano.StakePool | undefined> },
    resultPromise: Promise<StakePoolResults>,
    rewardsHistoryKey: string
  ) {
    for (const [hashId, promise] of Object.entries(cachedPromises)) {
      // If the pool was already cached, there is nothing to do
      if (promise) continue;
 
      const cacheKey = `${IDS_NAMESPACE}/${rewardsHistoryKey}/${hashId}`;
 
      // Cache a promise which will resolve with the pool when resultPromise will be resolved
      this.#cache.set(
        cacheKey,
        resultPromise.then(
          ({ poolsToCache }) => {
            // Once the resultPromise is resolved, pick the right pool from it
            const pool = poolsToCache[hashId as unknown as number];
 
            // Replace the cached promise with the pool
            this.#cache.set(cacheKey, pool, UNLIMITED_CACHE_TTL);
 
            return pool;
          },
          (error) => {
            // In case of error, reset the cached value to let next request to start a new query
            this.#cache.set(cacheKey, undefined, UNLIMITED_CACHE_TTL);
 
            throw error;
          }
        ),
        UNLIMITED_CACHE_TTL
      );
    }
  }
 
  private async queryExtraPoolsData(
    idsToFetch: PoolUpdate[],
    sortType: PoolSortType,
    totalStake: string | null,
    orderedResult: OrderedResult,
    useBlockfrost: boolean
  ) {
    if (idsToFetch.length === 0) return emptyPoolsExtraInfo;
    this.logger.debug('About to query stake pool extra information');
    const orderedResultHashIds = idsToFetch.map(({ id }) => id);
    const orderedResultUpdateIds = idsToFetch.map(({ updateId }) => updateId);
    const [poolRelays, poolOwners, poolRegistrations, poolRetirements, poolMetrics] = await Promise.all([
      // TODO: it would be easier and make the code cleaner if all queries had the same id as argument
      //       (either hash or update id)
      this.#builder.queryPoolRelays(orderedResultUpdateIds),
      useBlockfrost ? [] : this.#builder.queryPoolOwners(orderedResultUpdateIds),
      useBlockfrost ? [] : this.#builder.queryRegistrations(orderedResultHashIds),
      useBlockfrost ? [] : this.#builder.queryRetirements(orderedResultHashIds),
      sortType === 'metrics'
        ? (orderedResult as PoolMetrics[])
        : this.#builder.queryPoolMetrics(orderedResultHashIds, totalStake, useBlockfrost)
    ]);
    return { poolMetrics, poolOwners, poolRegistrations, poolRelays, poolRetirements };
  }
 
  public queryStakePoolsChecks(options: QueryStakePoolsArgs) {
    const { filters, pagination, sort } = options;
 
    if (pagination.limit > this.#paginationPageSizeLimit) {
      throw new ProviderError(
        ProviderFailure.BadRequest,
        undefined,
        `Page size of ${pagination.limit} can not be greater than ${this.#paginationPageSizeLimit}`
      );
    }
 
    if (filters?.text) {
      throw new ProviderError(
        ProviderFailure.NotImplemented,
        undefined,
        'DbSyncStakePoolProvider does not support text filter'
      );
    }
 
    if (filters?.identifier && filters.identifier.values.length > this.#paginationPageSizeLimit) {
      throw new ProviderError(
        ProviderFailure.BadRequest,
        undefined,
        `Filter identifiers of ${filters.identifier.values.length} can not be greater than ${
          this.#paginationPageSizeLimit
        }`
      );
    }
 
    if (DbSyncStakePoolProvider.notSupportedSortFields.includes(sort?.field || 'name')) {
      throw new ProviderError(
        ProviderFailure.NotImplemented,
        undefined,
        `DbSyncStakePoolProvider doesn't support sort by ${sort?.field} `
      );
    }
  }
 
  public async queryStakePools(options: QueryStakePoolsArgs): Promise<Paginated<Cardano.StakePool>> {
    const { filters, apyEpochsBackLimit = APY_EPOCHS_BACK_LIMIT_DEFAULT } = options;
    const useBlockfrost = this.#useBlockfrost;
 
    this.queryStakePoolsChecks(options);
 
    const { params, query } = useBlockfrost
      ? this.#builder.buildBlockfrostQuery(filters)
      : filters?._condition === 'or'
      ? this.#builder.buildOrQuery(filters)
      : this.#builder.buildAndQuery(filters);
    // Get pool updates/hashes cached
    const poolUpdates = await this.#cache.get(queryCacheKey(StakePoolsSubQuery.POOL_HASHES, options), () =>
      this.#builder.queryPoolHashes(query, params)
    );
    // 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
      ? null
      : await this.#cache.get(queryCacheKey(StakePoolsSubQuery.TOTAL_STAKE), async () => {
          const distribution = await this.cardanoNode.stakeDistribution();
 
          for (const [_, value] of distribution) return value.stake.supply.toString();
 
          throw new ProviderError(
            ProviderFailure.InvalidResponse,
            undefined,
            'Got an empty distribution response from OgmiosCardanoNode'
          );
        });
    // Get total stake pools count
    const totalCount = poolUpdates.length;
    // Get last epoch data
    const lastEpoch = await this.#builder.getLastEpochWithData();
    const { no: lastEpochNo } = lastEpoch;
    // Get stake pools data cached
    const { orderedResultHashIds, orderedResultUpdateIds, orderedResult, poolDatas, hashesIds, sortType } =
      await this.#cache.get(queryCacheKey(StakePoolsSubQuery.POOLS_DATA_ORDERED, options), () =>
        this.getPoolsDataOrdered(poolUpdates, totalStake, useBlockfrost, options)
      );
    // Get stake pools APYs cached
    let poolAPYs = [] as PoolAPY[];
    if (this.#responseConfig?.search?.metrics?.apy === true) {
      poolAPYs =
        sortType === 'apy'
          ? (orderedResult as PoolAPY[])
          : await this.#cache.get(queryCacheKey(StakePoolsSubQuery.APY, hashesIds, options), () =>
              this.#builder.queryPoolAPY(hashesIds, this.#epochLength, { apyEpochsBackLimit })
            );
    }
    // Create lookup table with pool ids: (hashId:updateId)
    const hashIdsMap = Object.fromEntries(
      orderedResultHashIds.map((hashId, idx) => [hashId, orderedResultUpdateIds[idx]])
    );
    // Create a lookup table with cached pools: (hashId:Cardano.StakePool)
    const rewardsHistoryKey = JSON.stringify(apyEpochsBackLimit);
    const cachedPromises = Object.fromEntries(
      orderedResultHashIds.map((hashId) => [
        hashId,
        this.#cache.getVal<PromiseOrValue<Cardano.StakePool | undefined>>(
          `${IDS_NAMESPACE}/${rewardsHistoryKey}/${hashId}`
        )
      ])
    );
 
    const queryExtraPoolsDataMissingFromCacheAndMap = async () => {
      const fromCache = await resolveObjectValues(cachedPromises);
      // Compute ids to fetch from db
      const idsToFetch = Object.entries(fromCache)
        .filter(([_, pool]) => pool === undefined)
        .map(([hashId, _]) => ({ id: Number(hashId), updateId: hashIdsMap[hashId] }));
      // Get stake pools extra information
      const { poolRelays, poolOwners, poolRegistrations, poolRetirements, poolMetrics } =
        await this.queryExtraPoolsData(idsToFetch, sortType, totalStake, orderedResult, useBlockfrost);
 
      return toStakePoolResults(orderedResultHashIds, fromCache, useBlockfrost, {
        lastEpochNo: Cardano.EpochNo(lastEpochNo),
        poolAPYs,
        poolDatas,
        poolMetrics,
        poolOwners,
        poolRegistrations,
        poolRelays,
        poolRetirements,
        totalCount
      });
    };
 
    const resultPromise = queryExtraPoolsDataMissingFromCacheAndMap();
 
    this.cacheStakePools(cachedPromises, resultPromise, rewardsHistoryKey);
 
    const { results } = await resultPromise;
 
    return results;
  }
 
  public async stakePoolStats(): Promise<StakePoolStats> {
    this.logger.debug('About to query pool stats');
    return await this.#cache.get(queryCacheKey(StakePoolsSubQuery.STATS), () => this.#builder.queryPoolStats());
  }
 
  async initializeImpl() {
    return Promise.resolve();
  }
 
  async startImpl() {
    this.#epochRolloverDisposer = this.#epochMonitor.onEpochRollover(() => this.#cache.clear());
  }
 
  async shutdownImpl() {
    this.#cache.shutdown();
    this.#epochRolloverDisposer();
  }
}