All files / src/StakePool/DbSyncStakePoolProvider mappers.ts

96.22% Statements 102/106
67.74% Branches 21/31
96.15% Functions 25/26
95.77% Lines 68/71

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                                                      41x   41x 41x   41x         377x 254x 215x   123x 70x                                     41x 379x 379x 379x 379x       379x       379x 379x 379x     41x                               133x 133x           2560x 607x     607x   1632x 1611x 793x 377x 1611x   377x   377x 377x   377x             2845x                 2845x         377x 377x     377x   377x               664x         41x                     41x     1312x   41x 453x 453x 453x                     453x 328x         453x 328x 328x   453x     41x   733x 1x 732x 1x         731x             733x     129x         378x         716x           126x           386x                             41x                           41x                   405x        
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
  BlockfrostPoolMetrics,
  BlockfrostPoolMetricsModel,
  Epoch,
  EpochModel,
  HashIdStakePoolMap,
  OwnerAddressModel,
  PoolAPY,
  PoolAPYModel,
  PoolData,
  PoolDataModel,
  PoolMetrics,
  PoolMetricsModel,
  PoolOwner,
  PoolRegistration,
  PoolRegistrationModel,
  PoolRelay,
  PoolRetirement,
  PoolRetirementModel,
  PoolUpdate,
  PoolUpdateModel,
  PoolsToCache,
  RelayModel,
  StakePoolResults,
  StakePoolStatsModel
} from './types';
import { Cardano, StakePoolStats } from '@cardano-sdk/core';
import { Hash32ByteBase16 } from '@cardano-sdk/crypto';
import { Percent, bufferToHexString, isNotNil } from '@cardano-sdk/util';
import Fraction from 'fraction.js';
 
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;
  }
  if (lastPoolRetirement.retiringEpoch > lastEpoch) return Cardano.StakePoolStatus.Retiring;
  return Cardano.StakePoolStatus.Retired;
};
 
interface ToCoreStakePoolInput {
  poolOwners: PoolOwner[];
  poolDatas: PoolData[];
  poolRegistrations: PoolRegistration[];
  poolRelays: PoolRelay[];
  poolRetirements: PoolRetirement[];
  lastEpochNo: Cardano.EpochNo;
  poolMetrics: PoolMetrics[];
  totalCount: number;
  poolAPYs: PoolAPY[];
}
 
/**
 * Calculates metrics that depends on Node's retrieved data.
 * Since some metrics are obtained from the Node they have to be calculated outside db queries
 */
export const calcNodeMetricsValues = (metrics: PoolMetrics['metrics'], apy?: number): Cardano.StakePoolMetrics => {
  const { activeStake, liveStake, activeStakePercentage, ...rest } = metrics;
  const stakePoolMetrics = { ...rest, apy } as unknown as Cardano.StakePoolMetrics;
  const isZeroStake = liveStake === 0n;
  const size: Cardano.StakePoolMetricsSize = {
    active: activeStakePercentage,
    live: Percent(!isZeroStake ? 1 - activeStakePercentage : 0)
  };
  const stake: Cardano.StakePoolMetricsStake = {
    active: activeStake,
    live: liveStake
  };
  stakePoolMetrics.size = size;
  stakePoolMetrics.stake = stake;
  return stakePoolMetrics;
};
 
export const toStakePoolResults = (
  poolHashIds: number[],
  fromCache: HashIdStakePoolMap,
  useBlockfrost: boolean,
  {
    poolOwners,
    poolDatas,
    poolRegistrations,
    poolRelays,
    poolRetirements,
    lastEpochNo,
    poolMetrics,
    totalCount,
    poolAPYs
  }: ToCoreStakePoolInput
): StakePoolResults => {
  const poolsToCache: PoolsToCache = {};
  return {
    poolsToCache,
    results: {
      pageResults: poolHashIds
        // eslint-disable-next-line complexity
        .map((hashId) => {
          const poolData = poolDatas.find((data) => data.hashId === hashId);
          Iif (!poolData) return;
 
          // Get the cached value if given hash id persist in the in-memory cache
          if (fromCache[hashId]) return fromCache[hashId];
 
          const apy = poolAPYs.find((pool) => pool.hashId === hashId)?.apy;
          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
          );
          const partialMetrics = poolMetric?.metrics;
          let metrics: Cardano.StakePoolMetrics | undefined;
          if (partialMetrics) {
            metrics = calcNodeMetricsValues(partialMetrics, apy);
          }
          const coreStakePool: Cardano.StakePool = {
            cost: poolData.cost,
            hexId: poolData.hexId,
            id: poolData.id,
            margin: poolData.margin,
            metrics: metrics ? metrics : ({} as Cardano.StakePoolMetrics),
            pledge: poolData.pledge,
            relays: poolRelays.filter((r) => r.updateId === poolData.updateId).map((r) => r.relay),
            vrf: poolData.vrfKeyHash,
            ...(useBlockfrost
              ? {
                  owners: poolMetric?.owners || [],
                  rewardAccount: poolMetric?.rewardAccount || ('' as Cardano.RewardAccount),
                  status: poolMetric?.status || Cardano.StakePoolStatus.Retired
                }
              : {
                  owners: poolOwners.filter((o) => o.hashId === poolData.hashId).map((o) => o.address),
                  rewardAccount: poolData.rewardAccount,
                  status: getPoolStatus(registration!, lastEpochNo, retirement)
                })
          };
          if (poolData.metadata) coreStakePool.metadata = poolData.metadata;
          if (poolData.metadataJson) coreStakePool.metadataJson = poolData.metadataJson;
 
          // Mark stake pool as pool to cache
          poolsToCache[hashId] = coreStakePool;
 
          return coreStakePool;
        })
        .filter(isNotNil),
      totalResultCount: Number(totalCount)
    }
  };
};
 
export const mapPoolUpdate = (poolUpdateModel: PoolUpdateModel): PoolUpdate => ({
  id: Number(poolUpdateModel.id),
  updateId: Number(poolUpdateModel.update_id)
});
 
const metadataKeys = new Set([
  'ticker',
  'name',
  'description',
  'homepage',
  'extended',
  'extDataUrl',
  'extSigUrl',
  'extVkey'
]);
 
const isOfflineMetadata = (
  _object: any
): _object is Cardano.StakePoolMainMetadataFields & Cardano.Cip6MetadataFields & Cardano.APMetadataFields =>
  Object.keys(_object).every((k) => metadataKeys.has(k) && typeof _object[k] === 'string');
 
export const mapPoolData = (poolDataModel: PoolDataModel): PoolData => {
  const vrfAsHexString = bufferToHexString(poolDataModel.vrf_key_hash);
  const { n: numerator, d: denominator } = new Fraction(poolDataModel.margin);
  const toReturn: PoolData = {
    cost: BigInt(poolDataModel.fixed_cost),
    hashId: Number(poolDataModel.hash_id),
    hexId: bufferToHexString(poolDataModel.pool_hash) as unknown as Cardano.PoolIdHex,
    id: poolDataModel.pool_id as unknown as Cardano.PoolId,
    margin: { denominator, numerator },
    pledge: BigInt(poolDataModel.pledge),
    rewardAccount: poolDataModel.reward_address as unknown as Cardano.RewardAccount,
    updateId: Number(poolDataModel.update_id),
    vrfKeyHash: vrfAsHexString as unknown as Cardano.VrfVkHex
  };
  if (poolDataModel.metadata_hash) {
    toReturn.metadataJson = {
      hash: bufferToHexString(poolDataModel.metadata_hash) as unknown as Hash32ByteBase16,
      url: poolDataModel.metadata_url
    };
  }
  if (poolDataModel.offline_data) {
    const parsedData = poolDataModel.offline_data;
    if (isOfflineMetadata(parsedData)) toReturn.metadata = parsedData;
  }
  return toReturn;
};
 
export const mapRelay = (relayModel: RelayModel): PoolRelay => {
  let relay: Cardano.Relay;
  if (relayModel.hostname) {
    relay = { __typename: 'RelayByName', hostname: relayModel.hostname, port: relayModel.port };
  } else if (relayModel.dns_name) {
    relay = {
      __typename: 'RelayByNameMultihost',
      dnsName: relayModel.dns_name
    };
  } else
    relay = {
      __typename: 'RelayByAddress',
      ipv4: relayModel.ipv4,
      ipv6: relayModel.ipv6,
      port: relayModel.port
    };
 
  return { hashId: Number(relayModel.hash_id), relay, updateId: Number(relayModel.update_id) };
};
 
export const mapEpoch = ({ no, optimal_pool_count }: EpochModel): Epoch => ({
  no,
  optimalPoolCount: optimal_pool_count
});
 
export const mapAddressOwner = (ownerAddressModel: OwnerAddressModel): PoolOwner => ({
  address: ownerAddressModel.address as unknown as Cardano.RewardAccount,
  hashId: Number(ownerAddressModel.hash_id)
});
 
export const mapPoolRegistration = (poolRegistrationModel: PoolRegistrationModel): PoolRegistration => ({
  activeEpochNo: poolRegistrationModel.active_epoch_no,
  hashId: Number(poolRegistrationModel.hash_id),
  transactionId: bufferToHexString(poolRegistrationModel.tx_hash) as unknown as Cardano.TransactionId
});
 
export const mapPoolRetirement = (poolRetirementModel: PoolRetirementModel): PoolRetirement => ({
  hashId: Number(poolRetirementModel.hash_id),
  retiringEpoch: poolRetirementModel.retiring_epoch,
  transactionId: bufferToHexString(poolRetirementModel.tx_hash) as unknown as Cardano.TransactionId
});
 
export const mapPoolMetrics = (poolMetricsModel: PoolMetricsModel): PoolMetrics => ({
  hashId: Number(poolMetricsModel.pool_hash_id),
  metrics: {
    activeStake: BigInt(poolMetricsModel.active_stake),
    activeStakePercentage: Percent(Number(poolMetricsModel.active_stake_percentage)),
    blocksCreated: poolMetricsModel.blocks_created,
    delegators: poolMetricsModel.delegators,
    lastRos: Percent(0),
    livePledge: BigInt(poolMetricsModel.live_pledge),
    liveStake: BigInt(poolMetricsModel.live_stake),
    ros: Percent(0),
    saturation: Percent(Number.parseFloat(poolMetricsModel.saturation))
  }
});
 
export const mapBlockfrostPoolMetrics = (poolMetricsModel: BlockfrostPoolMetricsModel): BlockfrostPoolMetrics => {
  const { extra, reward_address, status } = poolMetricsModel;
  const [owners, registration, retirement] = JSON.parse(extra);
 
  return {
    ...mapPoolMetrics(poolMetricsModel),
    owners,
    registration,
    retirement,
    rewardAccount: reward_address as unknown as Cardano.RewardAccount,
    status: status as unknown as Cardano.StakePoolStatus
  };
};
 
export const mapPoolStats = (poolStats: StakePoolStatsModel): StakePoolStats => ({
  qty: {
    // There is no need of resolving this for db-sync provider, will be deprecated soon with the optimized postgres one
    activating: 0,
    active: Number(poolStats.active),
    retired: Number(poolStats.retired),
    retiring: Number(poolStats.retiring)
  }
});
 
export const mapPoolAPY = (poolAPYModel: PoolAPYModel): PoolAPY => ({
  apy: poolAPYModel.apy,
  hashId: Number(poolAPYModel.hash_id)
});