UNPKG

@ethersphere/swarm-cli

Version:
53 lines (52 loc) 2.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.printStamp = exports.pickStamp = void 0; const cafe_utility_1 = require("cafe-utility"); const process_1 = require("process"); const text_1 = require("../../utils/text"); /** * Displays an interactive stamp picker to select a Stamp ID. * * A typical use case is to prompt the user for a stamp, when * the command requires one, but was not specified in `argv`. * * Makes a request via `bee` to fetch possible stamps. * * @returns {Promise<string>} Hex representation of the Stamp ID. */ async function pickStamp(bee, console) { const stamps = await bee.getAllPostageBatch(); const choices = stamps .filter(stamp => stamp.usable || stamp.duration.toSeconds() > 1) .map(stamp => `${stamp.batchID} ${stamp.remainingSize.toFormattedString()} remaining, TTL ${cafe_utility_1.Dates.secondsToHumanTime(stamp.duration.toSeconds())}`); if (!choices.length) { console.error('You need to have at least one stamp for this action.'); (0, process_1.exit)(1); } const value = await console.promptList(choices, 'Please select a stamp for this action'); const [hex] = value.split(' '); return hex; } exports.pickStamp = pickStamp; function printStamp(stamp, console, settings) { const batchId = settings?.shortenBatchId ? stamp.batchID.toHex().slice(0, 8) : stamp.batchID.toHex(); console.log((0, text_1.createKeyValue)('Stamp ID', batchId)); if (stamp.label) { console.log((0, text_1.createKeyValue)('Label', stamp.label)); } console.log((0, text_1.createKeyValue)('Usage', stamp.usageText)); console.log((0, text_1.createKeyValue)(stamp.immutableFlag ? 'Capacity (immutable)' : 'Capacity (mutable)', `${stamp.remainingSize.toFormattedString()} remaining out of ${stamp.size.toFormattedString()}`)); if (settings?.showTtl) { const ttl = cafe_utility_1.Dates.secondsToHumanTime(stamp.duration.toSeconds()); const expires = stamp.duration.toEndDate().toISOString().slice(0, 10); console.log((0, text_1.createKeyValue)('TTL', `${ttl} (${expires})`)); } console.verbose((0, text_1.createKeyValue)('Depth', stamp.depth)); console.verbose((0, text_1.createKeyValue)('Bucket Depth', stamp.bucketDepth)); console.verbose((0, text_1.createKeyValue)('Amount', stamp.amount)); console.verbose((0, text_1.createKeyValue)('Usable', stamp.usable)); console.verbose((0, text_1.createKeyValue)('Utilization', stamp.utilization)); console.verbose((0, text_1.createKeyValue)('Block Number', stamp.blockNumber)); console.quiet(settings?.printUsageInQuiet ? `${batchId} ${stamp.usageText}` : batchId); } exports.printStamp = printStamp;