UNPKG

@blockfrost/blockfrost-cardano-cli

Version:

Drop-in(ish) replacement for cardano-cli powered by Blockfrost

66 lines (65 loc) 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StakeDistribution = void 0; /* eslint-disable camelcase */ const bignumber_js_1 = require("bignumber.js"); const cli_ux_1 = require("cli-ux"); const base_command_1 = require("../../helpers/base-command"); class StakeDistribution extends base_command_1.BaseCommand { constructor() { super(...arguments); this.prettyPrint = (res) => { const calculatedData = res.map(stake => ({ pool_id: stake.pool_id, frac: new bignumber_js_1.default(stake.numerator.toString()) .div(stake.denominator.toString()) .toExponential(3), })); this.log(); cli_ux_1.cli.table(calculatedData, { pool_id: { header: 'PoolId', minWidth: 30, }, frac: { header: 'Stake frac', }, }, { printLine: line => this.log(line), 'no-truncate': true }); this.log(); }; this.doWork = async () => { const client = await this.getClient(); const poolsExtended = await client.poolsExtendedAll({ batchSize: 50 }); let stakesSum = BigInt(0); const allStakes = poolsExtended.map(res => { const liveStake = BigInt(res.live_stake); stakesSum += liveStake; return { pool_id: res.pool_id, pool_hex: res.hex, live_stake: liveStake, }; }); const formattedStakes = allStakes // sort by pool hex .sort((a, b) => new bignumber_js_1.default('0x' + a.pool_hex, 16).comparedTo(new bignumber_js_1.default('0x' + b.pool_hex, 16))) .map(stake => ({ pool_id: stake.pool_id, numerator: stake.live_stake, denominator: stakesSum, })); return formattedStakes; }; } async toFile(data) { const stakePerPool = {}; data.forEach(stake => { stakePerPool[stake.pool_id] = { numerator: stake.numerator, denominator: stake.denominator, }; }); super.toFile(stakePerPool); } } exports.StakeDistribution = StakeDistribution;