UNPKG

@q-dev/q-js-sdk

Version:

Typescript Library to interact with Q System Contracts

176 lines 7.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Indexer = void 0; const axios_1 = __importDefault(require("axios")); class Indexer { constructor(baseURL) { this.baseURL = baseURL; this._axios = axios_1.default.create({ baseURL }); } async getValidatorStats(signers = [], options = { sort: '-id', 'page[limit]': 1000 }) { if (!options['filter[inturnsigner]'] && !signers) throw new Error('Signers list is null!'); if (signers && options['filter[inturnsigner]']) throw new Error('Cannot filter for inturn signer, already have list!'); const blocks = await this.getBlocks(options); return signers.map(signer => { const tblocks = blocks.filter(block => { const { mainaccount, miner } = block.attributes; return (mainaccount || miner) === signer.toLowerCase(); }); return this.countValidatorStats(tblocks); }); } async getInactiveValidators(signers) { const blocks = await this.getBlocks({ sort: '-id', 'page[limit]': 101 }); return signers .filter(signer => !blocks.some(block => { const { mainaccount, miner } = block.attributes; return (mainaccount || miner) === signer.toLowerCase(); })) .length; } async getValidatorMetrics(cycles) { var _a; const PAGE_LIMIT = 100; const metrics = []; let tempMetrics; do { tempMetrics = await this.getMetrics({ 'filter[cycles]': cycles, 'page[limit]': PAGE_LIMIT, 'page[cursor]': (_a = metrics[metrics.length - 1]) === null || _a === void 0 ? void 0 : _a.id }); metrics.push(...tempMetrics); } while (tempMetrics.length === PAGE_LIMIT); return metrics.map(metric => this.countValidatorMetric(metric)); } async getBlocks(options) { const response = await this._axios.get('/blocks', { params: options }); return response.data.data; } async getMetrics(options) { const response = await this._axios.get('/metrics', { params: options }); return response.data.data; } async getOnchainRootNodeMetrics(options) { const response = await this._axios.get('/root-node-metrics/onchain-membership', { params: options }); return response.data.data; } async getVotings(options) { const response = await this._axios.get('/votings', { params: { ...(options.filterType ? { 'filter[type]': options.filterType } : {}), ...(options.filterStatus ? { 'filter[status]': options.filterStatus } : {}), sort: options.sort || '-voting_end_time', ...(options.pageCursor ? { 'page[cursor]': options.pageCursor } : {}), 'page[limit]': options.pageLimit || 10, } }); return response.data; } async getVotingsByCursor(cursor) { const response = await this._axios.get(cursor); return response.data; } async getL0RootList(status) { const { data: { data: { attributes } } } = await this._axios.get('/root-list', { params: { status } }); return { status: attributes.status, timestamp: attributes.timestamp, signers: attributes.signers.map(i => ({ signer: i.signer, mainAccount: i.main_account })), roots: attributes.roots.map(i => ({ signer: i.signer, mainAccount: i.main_account })), activeRootPercentage: attributes.active_root_percentage }; } async getL0ExclusionList(status) { const { data: { data: { attributes } } } = await this._axios.get('/exclusion-list', { params: { status } }); return { status: attributes.status, timestamp: attributes.timestamp, signers: attributes.signers.map(i => ({ signer: i.signer, mainAccount: i.main_account })), exclusions: attributes.exclusions.map(i => ({ signer: i.signer, mainAccount: i.main_account })), activeRootPercentage: attributes.active_root_percentage }; } async getRootNodeMetrics(options) { const { data: { data: { attributes } } } = await this._axios.get('root-node-metrics', { params: { ...(options.cycles !== undefined ? { 'filter[cycles]': options.cycles } : {}), ...(options.rootAddress ? { 'filter[rootAddress]': options.rootAddress } : {}), ...(options.blocksDelay !== undefined ? { 'filter[blocksDelay]': options.blocksDelay } : {}), sort: options.sort || 'desc', 'page[limit]': 100, } }); return attributes; } async getRNQTHVotingsAggregated(options) { const { data: { data: { attributes } } } = await this._axios.get('root-node-metrics/q-token-holder-votings/aggregated', { params: this.buildRootNodeAggregatedParams(options) }); return attributes; } async getRNVotingsAggregated(options) { const { data: { data: { attributes } } } = await this._axios.get('root-node-metrics/rootnode-votings/aggregated', { params: this.buildRootNodeAggregatedParams(options) }); return attributes; } async getRNProposalsAggregated(options) { const { data: { data: { attributes } } } = await this._axios.get('root-node-metrics/rootnode-proposals/aggregated', { params: this.buildRootNodeAggregatedParams(options) }); return attributes; } countValidatorStats(blocks) { var _a, _b, _c, _d; return { lastBlockValidated: Number((_b = (_a = blocks[0]) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0), lastBlockValidatedTime: new Date(((_d = (_c = blocks[0]) === null || _c === void 0 ? void 0 : _c.attributes.timestamp) !== null && _d !== void 0 ? _d : 0) * 1000), lastAvailability: blocks.length / 10 }; } countValidatorMetric(metric) { const attrs = metric.attributes; return { address: attrs.MainAccount, dueBlocks: attrs.DueBlocks, inTurnBlocks: attrs.InTurnBlocks, outOfTurnBlocks: attrs.OutOfTurnBlocks, inTurnAvailability: attrs.DueBlocks > 0 ? attrs.InTurnBlocks / attrs.DueBlocks : 0, totalAvailability: attrs.DueBlocks > 0 ? (attrs.InTurnBlocks + attrs.OutOfTurnBlocks) / attrs.DueBlocks : 0 }; } buildRootNodeAggregatedParams(options) { return { ...((options === null || options === void 0 ? void 0 : options.rootAddress) ? { 'filter[rootAddress]': options.rootAddress } : {}), ...((options === null || options === void 0 ? void 0 : options.startBlock) !== undefined ? { 'filter[startBlock]': options.startBlock } : {}), sort: (options === null || options === void 0 ? void 0 : options.sort) || 'desc', 'page[limit]': 100, }; } } exports.Indexer = Indexer; //# sourceMappingURL=indexer.js.map