All files / src/StakePool/TypeormStakePoolProvider mappers.ts

96% Statements 24/25
66.66% Branches 22/33
100% Functions 8/8
95.23% Lines 20/21

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 14541x   41x 41x                                                                                                     41x       353x 259x             41x           353x           259x                   353x                                   41x 353x                         353x           62x           41x 2x 8x     2x 8x       2x    
import { Cardano, Paginated } from '@cardano-sdk/core';
import { Hash32ByteBase16 } from '@cardano-sdk/crypto';
import { Percent, isNotNil } from '@cardano-sdk/util';
import { RelayModel, mapRelay } from '../DbSyncStakePoolProvider';
 
export type Margin = {
  numerator: number;
  denominator: number;
};
 
export type PoolModel = {
  pool_id: string;
  pool_status: string;
  params_pledge: string;
  params_cost: string;
  params_margin: Margin;
  params_relays: RelayModel[];
  params_owners: string[];
  params_vrf: string;
  params_reward_account: string;
  params_metadata_url: string | null;
  params_metadata_hash: string | null;
  metadata_ticker: string | null;
  metadata_name: string | null;
  metadata_homepage: string | null;
  metadata_description: string | null;
  metadata_ext: JSON | null;
  metrics_minted_blocks: number;
  metrics_live_delegators: number;
  metrics_active_stake: string;
  metrics_live_stake: string;
  metrics_active_size: string;
  metrics_live_size: string;
  metrics_live_saturation: string;
  metrics_live_pledge: string;
  metrics_ros: string | null;
  metrics_last_ros: string | null;
  total_count: string;
};
 
export type PoolStatsModel = {
  status: string;
  count: number;
};
 
export type PoolStats = {
  activating: number;
  active: number;
  retiring: number;
  retired: number;
};
 
export type SortOrder = 'ASC' | 'DESC' | undefined;
 
const mapMetadataJson = ({
  params_metadata_url,
  params_metadata_hash
}: PoolModel): Cardano.PoolMetadataJson | undefined => {
  if (params_metadata_url && params_metadata_hash) {
    return {
      hash: params_metadata_hash as Hash32ByteBase16,
      url: params_metadata_url
    };
  }
};
 
const mapMetadata = ({
  metadata_name,
  metadata_ticker,
  metadata_homepage,
  metadata_description
}: PoolModel): Cardano.StakePoolMetadata | undefined => {
  if (
    isNotNil(metadata_name) &&
    isNotNil(metadata_ticker) &&
    isNotNil(metadata_description) &&
    isNotNil(metadata_homepage)
  ) {
    return {
      description: metadata_description,
      homepage: metadata_homepage,
      name: metadata_name,
      ticker: metadata_ticker
    };
  }
};
 
// eslint-disable-next-line complexity
const mapMetrics = (pool: PoolModel): Cardano.StakePoolMetrics => ({
  apy: Percent(Number.parseFloat(pool.metrics_last_ros || '0')),
  blocksCreated: pool.metrics_minted_blocks || 0,
  delegators: pool.metrics_live_delegators || 0,
  lastRos: Percent(Number.parseFloat(pool.metrics_last_ros || '0')),
  livePledge: BigInt(pool.metrics_live_pledge || 0n),
  ros: Percent(Number.parseFloat(pool.metrics_ros || '0')),
  saturation: Percent(Number.parseFloat(pool.metrics_live_saturation || '0')),
  size: {
    active: Percent(Number.parseFloat(pool.metrics_active_size || '0')),
    live: Percent(Number.parseFloat(pool.metrics_live_size || '0'))
  },
  stake: {
    active: BigInt(pool.metrics_active_stake || 0),
    live: BigInt(pool.metrics_live_stake || 0)
  }
});
 
export const mapStakePoolsResult = (rawResult: PoolModel[]): Paginated<Cardano.StakePool> => {
  const pageResults: Cardano.StakePool[] = rawResult.map((poolModel) => ({
    cost: BigInt(poolModel.params_cost),
    hexId: Cardano.PoolIdHex(Cardano.PoolId.toKeyHash(poolModel.pool_id as Cardano.PoolId)),
    id: Cardano.PoolId(poolModel.pool_id),
    margin: {
      denominator: Number(poolModel.params_margin.denominator),
      numerator: Number(poolModel.params_margin.numerator)
    } as Cardano.Fraction,
    metadata: mapMetadata(poolModel),
    metadataJson: mapMetadataJson(poolModel),
    metrics: mapMetrics(poolModel),
    owners: poolModel.params_owners as Cardano.RewardAccount[],
    pledge: BigInt(poolModel.params_pledge),
    relays: poolModel.params_relays.map(mapRelay).map((r) => r.relay),
    rewardAccount: Cardano.RewardAccount(poolModel.params_reward_account),
    status: poolModel.pool_status as Cardano.StakePoolStatus,
    vrf: poolModel.params_vrf as Cardano.VrfVkHex
  }));
 
  return {
    pageResults,
    totalResultCount: rawResult.length > 0 ? Number(rawResult[0].total_count) : 0
  };
};
 
export const mapPoolStats = (rawResult: PoolStatsModel[]): PoolStats => {
  const result = rawResult.reduce(
    (acc, curr) => (acc = { ...acc, [curr.status]: Number(curr.count) }),
    {} as PoolStats
  );
  for (const status of Object.values(Cardano.StakePoolStatus)) {
    Iif (!(status in result)) {
      result[status] = 0;
    }
  }
  return result;
};