UNPKG

@substrate/api-sidecar

Version:

REST service that makes it easy to interact with blockchain nodes built using Substrate's FRAME framework.

204 lines 9.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sortByCore = sortByCore; exports.hexToBin = hexToBin; exports.processHexMask = processHexMask; exports.extractWorkloadInfo = extractWorkloadInfo; exports.extractWorkplanInfo = extractWorkplanInfo; exports.extractReservationInfo = extractReservationInfo; exports.extractPotentialRenewalInfo = extractPotentialRenewalInfo; exports.extractLeaseInfo = extractLeaseInfo; exports.extractSaleInfo = extractSaleInfo; exports.extractStatusInfo = extractStatusInfo; exports.extractRegionInfo = extractRegionInfo; exports.extractConfigInfo = extractConfigInfo; exports.extractCoreDescriptorInfo = extractCoreDescriptorInfo; exports.extractParachainLifecycleInfo = extractParachainLifecycleInfo; const util_1 = require("@polkadot/util"); function sortByCore(dataArray) { if (!dataArray) { return []; } const sanitized = Array.isArray(dataArray) ? dataArray : [dataArray]; return sanitized.sort((a, b) => a.core - b.core); } function hexToBin(hex) { return parseInt(hex, 16).toString(2); } function processHexMask(mask) { if (!mask) { return []; } const trimmedHex = mask.toHex().slice(2); const arr = trimmedHex.split(''); const buffArr = []; arr.forEach((bit) => { hexToBin(bit) .split('') .forEach((v) => buffArr.push(v)); }); buffArr.filter((v) => v === '1'); return buffArr; } function extractWorkloadInfo(info, core) { return { core, info: info.map((c) => ({ isPool: c.assignment.isPool, isTask: c.assignment.isTask, mask: c.mask.toHex(), task: c.assignment.isTask ? c.assignment.asTask.toString() : c.assignment.isPool ? 'Pool' : 'Idle', }))[0], }; } function extractWorkplanInfo(info, core, timeslice) { const workplanInfo = info.isSome ? info.unwrap() : []; return { core: core, timeslice: timeslice, info: workplanInfo === null || workplanInfo === void 0 ? void 0 : workplanInfo.map((c) => ({ mask: c.mask.toHex(), isPool: c.assignment.isPool, isTask: c.assignment.isTask, task: c.assignment.isTask ? c.assignment.asTask.toString() : c.assignment.isPool ? 'Pool' : 'Idle', })), }; } function extractReservationInfo(info) { var _a, _b, _c, _d, _e, _f, _g; return { mask: (_a = info[0]) === null || _a === void 0 ? void 0 : _a.mask.toHex(), task: ((_c = (_b = info[0]) === null || _b === void 0 ? void 0 : _b.assignment) === null || _c === void 0 ? void 0 : _c.isTask) ? (_e = (_d = info[0]) === null || _d === void 0 ? void 0 : _d.assignment) === null || _e === void 0 ? void 0 : _e.asTask.toString() : ((_g = (_f = info[0]) === null || _f === void 0 ? void 0 : _f.assignment) === null || _g === void 0 ? void 0 : _g.isPool) ? 'Pool' : '', }; } function extractPotentialRenewalInfo(info, item) { const unwrapped = info.isSome ? info.unwrap() : null; let mask = null; let task = ''; const completion = unwrapped === null || unwrapped === void 0 ? void 0 : unwrapped.completion; if (completion === null || completion === void 0 ? void 0 : completion.isComplete) { const complete = completion === null || completion === void 0 ? void 0 : completion.asComplete[0]; task = complete.assignment.isTask ? complete === null || complete === void 0 ? void 0 : complete.assignment.asTask.toString() : (complete === null || complete === void 0 ? void 0 : complete.assignment.isPool) ? 'Pool' : 'Idle'; mask = complete.mask.toHex(); } else if (completion === null || completion === void 0 ? void 0 : completion.isPartial) { mask = completion === null || completion === void 0 ? void 0 : completion.asPartial.toHex(); task = ''; } else { mask = null; } return { completion: completion === null || completion === void 0 ? void 0 : completion.type, core: item.args[0].core.toNumber(), mask, price: unwrapped === null || unwrapped === void 0 ? void 0 : unwrapped.price.toBn(), task, when: item.args[0].when.toNumber(), }; } function extractLeaseInfo(info, core) { return { ...(core ? { core } : {}), task: info.task.toString(), until: info.until.toNumber(), }; } function extractSaleInfo(info) { var _a; return { saleStart: info.saleStart.toNumber(), leadinLength: info.leadinLength.toNumber(), endPrice: info.endPrice.toBn(), regionBegin: info.regionBegin.toNumber(), regionEnd: info.regionEnd.toNumber(), idealCoresSold: info.idealCoresSold.toNumber(), coresOffered: info.coresOffered.toNumber(), firstCore: info.firstCore.toNumber(), selloutPrice: info.selloutPrice.isSome ? (_a = info.selloutPrice.unwrapOr(undefined)) === null || _a === void 0 ? void 0 : _a.toBn() : undefined, coresSold: info.coresSold.toNumber(), }; } function extractStatusInfo(info) { const unwrapped = info.isSome ? info.unwrap() : null; return { coreCount: unwrapped === null || unwrapped === void 0 ? void 0 : unwrapped.coreCount.toNumber(), privatePoolSize: unwrapped === null || unwrapped === void 0 ? void 0 : unwrapped.privatePoolSize.toNumber(), systemPoolSize: unwrapped === null || unwrapped === void 0 ? void 0 : unwrapped.systemPoolSize.toNumber(), lastCommittedTimeslice: unwrapped === null || unwrapped === void 0 ? void 0 : unwrapped.lastCommittedTimeslice.toNumber(), lastTimeslice: unwrapped === null || unwrapped === void 0 ? void 0 : unwrapped.lastTimeslice.toNumber(), }; } function extractRegionInfo(info) { const regionInfo = info[0].args[0]; const value = info[1].isSome ? info[1].unwrap() : null; return { core: regionInfo.core.toNumber(), begin: regionInfo.begin.toNumber(), end: value === null || value === void 0 ? void 0 : value.end.toNumber(), owner: value === null || value === void 0 ? void 0 : value.owner.toString(), paid: (value === null || value === void 0 ? void 0 : value.paid.isSome) ? value === null || value === void 0 ? void 0 : value.paid.unwrap().toNumber() : undefined, mask: regionInfo.mask.toHex(), }; } function extractConfigInfo(info) { var _a; return { advanceNotice: info.unwrap().advanceNotice.toNumber(), interludeLength: info.unwrap().interludeLength.toNumber(), leadinLength: info.unwrap().leadinLength.toNumber(), regionLength: info.unwrap().regionLength.toNumber(), idealBulkProportion: info.unwrap().idealBulkProportion.toNumber(), limitCoresOffered: (_a = info.unwrap().limitCoresOffered.unwrapOr(undefined)) === null || _a === void 0 ? void 0 : _a.toNumber(), renewalBump: info.unwrap().renewalBump.toNumber(), contributionTimeout: info.unwrap().contributionTimeout.toNumber(), }; } function extractCoreDescriptorInfo(_key, info) { var _a; const currentWork = (info === null || info === void 0 ? void 0 : info.currentWork.isSome) ? info.currentWork.unwrap() : null; const queue = (info === null || info === void 0 ? void 0 : info.queue.isSome) ? info.queue.unwrap() : null; const assignments = (currentWork === null || currentWork === void 0 ? void 0 : currentWork.assignments) || []; return { info: { currentWork: { assignments: assignments === null || assignments === void 0 ? void 0 : assignments.map((assgn) => { var _a, _b, _c, _d, _e, _f, _g; return { isPool: (_a = assgn[0]) === null || _a === void 0 ? void 0 : _a.isPool, isTask: (_b = assgn[0]) === null || _b === void 0 ? void 0 : _b.isTask, ratio: (_c = assgn[1]) === null || _c === void 0 ? void 0 : _c.ratio.toNumber(), remaining: (_d = assgn[1]) === null || _d === void 0 ? void 0 : _d.remaining.toNumber(), task: ((_e = assgn[0]) === null || _e === void 0 ? void 0 : _e.isTask) ? (_f = assgn[0]) === null || _f === void 0 ? void 0 : _f.asTask.toString() : ((_g = assgn[0]) === null || _g === void 0 ? void 0 : _g.isPool) ? 'Pool' : 'Idle', }; }), endHint: (currentWork === null || currentWork === void 0 ? void 0 : currentWork.endHint.isSome) ? (_a = currentWork === null || currentWork === void 0 ? void 0 : currentWork.endHint) === null || _a === void 0 ? void 0 : _a.unwrap().toBn() : null, pos: (currentWork === null || currentWork === void 0 ? void 0 : currentWork.pos.toNumber()) || 0, step: (currentWork === null || currentWork === void 0 ? void 0 : currentWork.step.toNumber()) || 0, }, queue: { first: (queue === null || queue === void 0 ? void 0 : queue.first.toBn()) || new util_1.BN(0), last: (queue === null || queue === void 0 ? void 0 : queue.last.toBn()) || new util_1.BN(0), }, }, }; } function extractParachainLifecycleInfo(key, val) { return { paraId: key.args[0].toNumber(), type: val.isSome ? val.unwrap().toString() : null, }; } //# sourceMappingURL=util.js.map