All files / src/StakePool/DbSyncStakePoolProvider util.ts

97.22% Statements 35/36
100% Branches 6/6
75% Functions 3/4
96.55% Lines 28/29

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  43x               43x   43x   43x 77x 37x 18x 3x 1x     43x 43x 43x   43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x     43x 505x   43x                 43x    
import { PoolSortType } from './types';
import {
  ProviderError,
  ProviderFailure,
  isPoolAPYSortField,
  isPoolDataSortField,
  isPoolMetricsSortField,
  isPoolROSSortField
} from '@cardano-sdk/core';
import BigNumber from 'bignumber.js';
 
export const APY_EPOCHS_BACK_LIMIT_DEFAULT = 3;
 
export const getStakePoolSortType = (field: string): PoolSortType => {
  if (isPoolDataSortField(field)) return 'data';
  if (isPoolMetricsSortField(field)) return 'metrics';
  if (isPoolAPYSortField(field)) return 'apy';
  if (isPoolROSSortField(field)) return 'ros';
  throw new ProviderError(ProviderFailure.Unknown, null, `Invalid sort field "${field}"`);
};
 
export const QUERIES_NAMESPACE = 'StakePoolQueries';
export const IDS_NAMESPACE = 'StakePoolIds';
export const LIVE_STAKE_CACHE_KEY = 'StakePoolLiveStake';
 
export enum StakePoolsSubQuery {
  APY = 'apy',
  STATS = 'stats',
  METRICS = 'metrics',
  REWARDS = 'rewards',
  RELAYS = 'relays',
  REGISTRATIONS = 'registrations',
  OWNERS = 'owners',
  RETIREMENTS = 'retirements',
  TOTAL_STAKE = 'total_stake',
  POOL_HASHES = 'pool_hashes',
  POOLS_DATA_ORDERED = 'pools_data_ordered'
}
 
export const queryCacheKey = (queryName: StakePoolsSubQuery, ...args: unknown[]) =>
  `${QUERIES_NAMESPACE}/${queryName}/${JSON.stringify(args)}`;
 
export const emptyPoolsExtraInfo = {
  poolMetrics: [],
  poolOwners: [],
  poolRegistrations: [],
  poolRelays: [],
  poolRetirements: [],
  poolRewards: []
};
 
export const divideBigIntToFloat = (numerator: bigint, denominator: bigint) =>
  new BigNumber(numerator.toString()).dividedBy(new BigNumber(denominator.toString())).toString();