UNPKG

@ethersphere/swarm-cli

Version:
90 lines (89 loc) 4.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Extend = void 0; const bee_js_1 = require("@ethersphere/bee-js"); const cafe_utility_1 = require("cafe-utility"); const process_1 = require("process"); const stamp_1 = require("../../service/stamp"); const spinner_1 = require("../../utils/spinner"); const command_log_1 = require("../root-command/command-log"); const stamp_command_1 = require("./stamp-command"); class Extend extends stamp_command_1.StampCommand { constructor() { super(...arguments); Object.defineProperty(this, "name", { enumerable: true, configurable: true, writable: true, value: 'extend' }); Object.defineProperty(this, "description", { enumerable: true, configurable: true, writable: true, value: 'Extend storage size or duration' }); } async run() { super.init(); const batches = await this.bee.getAllPostageBatch(); const batchId = await (0, stamp_1.pickStamp)(this.bee, this.console); const batch = batches.find(b => b.batchID.toHex() === batchId); if (!batch) { throw Error(`Batch with ID ${batchId} not found`); } const { bzzBalance } = await this.bee.getWalletBalance(); const mode = await this.console.promptList(['Size', 'Duration'], 'What do you want to extend?'); this.console.log(`Current balance is ${bzzBalance.toDecimalString()} BZZ`); if (mode === 'Size') { this.console.log(`Current size is ${batch.size.toFormattedString()}`); const wantedSize = await this.console.askForValue('New size'); const size = bee_js_1.Size.fromBytes(cafe_utility_1.Numbers.makeStorage(wantedSize)); const cost = await this.bee.getSizeExtensionCost(batchId, size); if (cost.gt(bzzBalance)) { this.console.error(`Need ${cost.toDecimalString()} BZZ to extend the size to ${size.toFormattedString()}`); (0, process_1.exit)(1); } const confirm = await this.console.confirm(`Do you want to extend the size to ${size.toFormattedString()} bytes for ${cost.toDecimalString()} BZZ?`); if (confirm) { const spinner = (0, spinner_1.createSpinner)('This may take a few minutes.'); if (this.verbosity !== command_log_1.VerbosityLevel.Quiet && !this.curl) { spinner.start(); } try { await this.bee.extendStorageSize(batchId, size); } finally { spinner.stop(); } } } else { this.console.log(`Current duration is ${batch.duration.toDays().toFixed(2)} days`); const wantedLength = await this.console.askForValue('Add duration'); const addedDuration = bee_js_1.Duration.fromMilliseconds(cafe_utility_1.Dates.make(wantedLength)); const totalDuration = bee_js_1.Duration.fromMilliseconds(cafe_utility_1.Dates.make(wantedLength) + batch.duration.toSeconds() * 1000); const cost = await this.bee.getDurationExtensionCost(batchId, addedDuration); if (cost.gt(bzzBalance)) { this.console.error(`Need ${cost.toDecimalString()} BZZ to extend the duration to ${totalDuration.toDays().toFixed(2)} days`); (0, process_1.exit)(1); } const confirm = await this.console.confirm(`Do you want to extend the duration to a total of ${totalDuration .toDays() .toFixed(2)} days for ${cost.toDecimalString()} BZZ?`); if (confirm) { const spinner = (0, spinner_1.createSpinner)('This may take a few minutes.'); if (this.verbosity !== command_log_1.VerbosityLevel.Quiet && !this.curl) { spinner.start(); } try { await this.bee.extendStorageDuration(batchId, addedDuration); } finally { spinner.stop(); } } } } } exports.Extend = Extend;