@ardier16/q-js-sdk
Version:
Typescript Library to interact with Q System Contracts
92 lines • 3.71 kB
JavaScript
;
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 => block.attributes.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 => block.attributes.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) {
var _a;
try {
const response = await this._axios.get('/blocks', { params: options });
return response.data.data;
}
catch (err) {
console.error((_a = err.response) === null || _a === void 0 ? void 0 : _a.data.error);
return [];
}
}
async getMetrics(options) {
var _a;
try {
const response = await this._axios.get('/metrics', { params: options });
return response.data.data;
}
catch (err) {
console.error((_a = err.response) === null || _a === void 0 ? void 0 : _a.data.error);
return [];
}
}
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
};
}
}
exports.Indexer = Indexer;
//# sourceMappingURL=indexer.js.map