@ethersphere/swarm-cli
Version:
CLI tool for Bee
155 lines (154 loc) • 6.54 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Create = void 0;
const bee_js_1 = require("@ethersphere/bee-js");
const cafe_utility_1 = require("cafe-utility");
const furious_commander_1 = require("furious-commander");
const spinner_1 = require("../../utils/spinner");
const text_1 = require("../../utils/text");
const command_log_1 = require("../root-command/command-log");
const stamp_command_1 = require("./stamp-command");
class Create extends stamp_command_1.StampCommand {
constructor() {
super(...arguments);
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'create'
});
Object.defineProperty(this, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Create postage stamp in a simple way'
});
Object.defineProperty(this, "capacity", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "ttl", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "immutable", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "label", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "postageBatchId", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
async run() {
super.init();
if (!this.capacity) {
this.console.log('Please provide the total capacity of the postage stamp batch');
this.console.log('This represents the total size of data that can be uploaded');
this.console.log('Example: 1GB');
this.capacity = await this.console.askForValue('Capacity');
this.console.log('');
}
const size = bee_js_1.Size.fromBytes(cafe_utility_1.Numbers.makeStorage(this.capacity));
if (!this.ttl) {
this.console.log('Please provide the time-to-live (TTL) of the postage stamps');
this.console.log('Defines the duration after which the stamp will expire');
this.console.log('Example: 1d, 1w, 1month');
this.ttl = await this.console.askForValue('TTL');
this.console.log('');
}
const duration = bee_js_1.Duration.fromMilliseconds(cafe_utility_1.Dates.make(this.ttl) + cafe_utility_1.Dates.seconds(5));
if (duration.toDays() < 1) {
this.console.error('The minimum TTL is 1 day');
return;
}
this.console.log('You have provided the following parameters:');
this.console.log((0, text_1.createKeyValue)('Capacity', size.toFormattedString()));
this.console.log((0, text_1.createKeyValue)('TTL', cafe_utility_1.Dates.secondsToHumanTime(duration.toSeconds())));
const estimatedCost = await this.bee.getStorageCost(size, duration);
const { bzzBalance } = await this.bee.getWalletBalance();
this.console.log('');
this.console.log((0, text_1.createKeyValue)('Cost', `${estimatedCost.toDecimalString()} xBZZ`));
this.console.log((0, text_1.createKeyValue)('Available', `${bzzBalance.toDecimalString()} xBZZ`));
this.console.log((0, text_1.createKeyValue)('Type', this.immutable ? 'Immutable' : 'Mutable'));
if (!this.quiet && !this.yes) {
this.yes = await this.console.confirm('Confirm the purchase');
}
if (!this.yes && !this.quiet) {
return;
}
const spinner = (0, spinner_1.createSpinner)('Creating postage batch. This may take up to 5 minutes.');
if (this.verbosity !== command_log_1.VerbosityLevel.Quiet && !this.curl) {
spinner.start();
}
try {
const batchId = await this.bee.buyStorage(size, duration, {
label: this.label,
immutableFlag: this.immutable,
waitForUsable: true,
});
spinner.stop();
this.console.quiet(batchId.toHex());
this.console.log((0, text_1.createKeyValue)('Stamp ID', batchId.toHex()));
this.postageBatchId = batchId;
}
finally {
spinner.stop();
}
}
}
__decorate([
(0, furious_commander_1.Option)({
key: 'capacity',
description: 'Size of data, e.g. 1GB',
type: 'string',
required: false,
}),
__metadata("design:type", String)
], Create.prototype, "capacity", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'ttl',
description: 'Time to live of the postage stamp, e.g. 1d, 1w, 1month',
type: 'string',
required: false,
}),
__metadata("design:type", String)
], Create.prototype, "ttl", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'immutable',
description: 'At full capacity, immutable prevents; mutable allows further uploads, overwriting old data',
type: 'boolean',
default: true,
}),
__metadata("design:type", Boolean)
], Create.prototype, "immutable", void 0);
__decorate([
(0, furious_commander_1.Option)({ key: 'label', description: 'Label of the postage stamp' }),
__metadata("design:type", String)
], Create.prototype, "label", void 0);
exports.Create = Create;