All files / src/NetworkInfo/DbSyncNetworkInfoProvider mappers.ts

100% Statements 24/24
100% Branches 26/26
100% Functions 5/5
100% Lines 13/13

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  36x                   36x         36x         36x           36x 4x 4x 4x 4x 4x     36x                                                                                                 2x                                                                                                                                                                 36x            
/* eslint-disable complexity */
import { Cardano, Seconds, SupplySummary } from '@cardano-sdk/core';
import { CostModelsParamModel, ProtocolParamsModel } from './types';
import { GenesisData } from '../../types';
import { LedgerTipModel } from '../../util/DbSyncProvider';
 
interface ToLovalaceSupplyInput {
  circulatingSupply: string;
  totalSupply: string;
}
 
export const networkIdMap = {
  Mainnet: Cardano.NetworkId.Mainnet,
  Testnet: Cardano.NetworkId.Testnet
};
 
export const toSupply = ({ circulatingSupply, totalSupply }: ToLovalaceSupplyInput): SupplySummary => ({
  circulating: BigInt(circulatingSupply),
  total: BigInt(totalSupply)
});
 
export const toLedgerTip = ({ block_no, slot_no, hash }: LedgerTipModel): Cardano.Tip => ({
  blockNo: Cardano.BlockNo(Number(block_no)),
  hash: hash.toString('hex') as unknown as Cardano.BlockId,
  slot: Cardano.Slot(Number(slot_no))
});
 
export const mapCostModels = (costs: CostModelsParamModel | null) => {
  const models: Cardano.CostModels = new Map();
  if (costs?.PlutusV1) models.set(Cardano.PlutusLanguageVersion.V1, costs.PlutusV1);
  if (costs?.PlutusV2) models.set(Cardano.PlutusLanguageVersion.V2, costs.PlutusV2);
  if (costs?.PlutusV3) models.set(Cardano.PlutusLanguageVersion.V3, costs.PlutusV3);
  return models;
};
 
export const toProtocolParams = ({
  coins_per_utxo_size,
  max_tx_size,
  max_val_size,
  max_collateral_inputs,
  min_pool_cost,
  pool_deposit,
  key_deposit,
  protocol_major,
  protocol_minor,
  min_fee_a,
  min_fee_b,
  max_block_size,
  max_bh_size,
  optimal_pool_count,
  influence,
  monetary_expand_rate,
  treasury_growth_rate,
  collateral_percent,
  price_mem,
  price_step,
  max_tx_ex_mem,
  max_tx_ex_steps,
  max_block_ex_mem,
  max_block_ex_steps,
  max_epoch,
  costs,
  pvt_motion_no_confidence,
  pvt_committee_normal,
  pvt_committee_no_confidence,
  pvt_hard_fork_initiation,
  pvtpp_security_group,
  dvt_motion_no_confidence,
  dvt_committee_normal,
  dvt_committee_no_confidence,
  dvt_update_to_constitution,
  dvt_hard_fork_initiation,
  dvt_p_p_network_group,
  dvt_p_p_economic_group,
  dvt_p_p_technical_group,
  dvt_p_p_gov_group,
  dvt_treasury_withdrawal,
  committee_min_size,
  committee_max_term_length,
  gov_action_lifetime,
  gov_action_deposit,
  drep_deposit,
  drep_activity,
  min_fee_ref_script_cost_per_byte
}: ProtocolParamsModel): Cardano.ProtocolParameters => ({
  coinsPerUtxoByte: Number(coins_per_utxo_size),
  collateralPercentage: collateral_percent,
  ...(committee_max_term_length && { committeeTermLimit: Cardano.EpochNo(committee_max_term_length) }),
  costModels: mapCostModels(costs),
  dRepDeposit: Number(drep_deposit),
  // CDDL represents it as `32: epoch  ; DRep inactivity period`
  ...(drep_activity && { dRepInactivityPeriod: Cardano.EpochNo(drep_activity) }),
  ...(dvt_committee_no_confidence &&
    dvt_committee_normal &&
    dvt_hard_fork_initiation &&
    dvt_motion_no_confidence &&
    dvt_p_p_economic_group &&
    dvt_p_p_gov_group &&
    dvt_p_p_network_group &&
    dvt_p_p_technical_group &&
    dvt_treasury_withdrawal &&
    dvt_update_to_constitution && {
      dRepVotingThresholds: {
        committeeNoConfidence: Cardano.FractionUtils.toFraction(dvt_committee_no_confidence),
        committeeNormal: Cardano.FractionUtils.toFraction(dvt_committee_normal),
        hardForkInitiation: Cardano.FractionUtils.toFraction(dvt_hard_fork_initiation),
        motionNoConfidence: Cardano.FractionUtils.toFraction(dvt_motion_no_confidence),
        ppEconomicGroup: Cardano.FractionUtils.toFraction(dvt_p_p_economic_group),
        ppGovernanceGroup: Cardano.FractionUtils.toFraction(dvt_p_p_gov_group),
        ppNetworkGroup: Cardano.FractionUtils.toFraction(dvt_p_p_network_group),
        ppTechnicalGroup: Cardano.FractionUtils.toFraction(dvt_p_p_technical_group),
        treasuryWithdrawal: Cardano.FractionUtils.toFraction(dvt_treasury_withdrawal),
        updateConstitution: Cardano.FractionUtils.toFraction(dvt_update_to_constitution)
      }
    }),
  desiredNumberOfPools: optimal_pool_count,
  governanceActionDeposit: Number(gov_action_deposit),
  ...(gov_action_lifetime && { governanceActionValidityPeriod: Cardano.EpochNo(gov_action_lifetime) }),
  maxBlockBodySize: max_block_size,
  maxBlockHeaderSize: max_bh_size,
  maxCollateralInputs: max_collateral_inputs,
  maxExecutionUnitsPerBlock: {
    memory: Number(max_block_ex_mem),
    steps: Number(max_block_ex_steps)
  },
  maxExecutionUnitsPerTransaction: {
    memory: Number(max_tx_ex_mem),
    steps: Number(max_tx_ex_steps)
  },
  maxTxSize: max_tx_size,
  maxValueSize: Number(max_val_size),
  minCommitteeSize: Number(committee_min_size),
  minFeeCoefficient: min_fee_a,
  minFeeConstant: min_fee_b,
  minFeeRefScriptCostPerByte: String(min_fee_ref_script_cost_per_byte),
  minPoolCost: Number(min_pool_cost),
  monetaryExpansion: String(monetary_expand_rate),
  poolDeposit: Number(pool_deposit),
  poolInfluence: String(influence),
  poolRetirementEpochBound: max_epoch,
  ...(pvt_committee_no_confidence &&
    pvt_committee_normal &&
    pvt_hard_fork_initiation &&
    pvt_motion_no_confidence &&
    pvtpp_security_group && {
      poolVotingThresholds: {
        committeeNoConfidence: Cardano.FractionUtils.toFraction(pvt_committee_no_confidence),
        committeeNormal: Cardano.FractionUtils.toFraction(pvt_committee_normal),
        hardForkInitiation: Cardano.FractionUtils.toFraction(pvt_hard_fork_initiation),
        motionNoConfidence: Cardano.FractionUtils.toFraction(pvt_motion_no_confidence),
        securityRelevantParamVotingThreshold: Cardano.FractionUtils.toFraction(pvtpp_security_group)
      }
    }),
  prices: {
    memory: price_mem,
    steps: price_step
  },
  protocolVersion: {
    major: protocol_major,
    minor: protocol_minor
  },
  stakeKeyDeposit: Number(key_deposit),
  treasuryExpansion: String(treasury_growth_rate)
});
 
export const toGenesisParams = (genesis: GenesisData): Cardano.CompactGenesis => ({
  ...genesis,
  networkId: networkIdMap[genesis.networkId],
  slotLength: Seconds(genesis.slotLength),
  systemStart: new Date(genesis.systemStart)
});