@ethersphere/swarm-cli
Version:
CLI tool for Bee
176 lines (175 loc) • 7.14 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.Buy = void 0;
const bee_js_1 = require("@ethersphere/bee-js");
const cafe_utility_1 = require("cafe-utility");
const ethers_1 = require("ethers");
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 Buy extends stamp_command_1.StampCommand {
constructor() {
super(...arguments);
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'buy'
});
Object.defineProperty(this, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'Buy postage stamp based on depth and amount'
});
Object.defineProperty(this, "depth", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "amount", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "gasPrice", {
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, "waitUsable", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
// CLASS FIELDS
Object.defineProperty(this, "postageBatchId", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
async run() {
super.init();
const chainState = await this.bee.getChainState();
const minimumAmount = ethers_1.BigNumber.from(chainState.currentPrice).mul(17280);
if (minimumAmount.gte(ethers_1.BigNumber.from(this.amount))) {
this.console.error('The amount is too low. The minimum amount is', minimumAmount.add(1).toString());
return;
}
const estimatedCost = bee_js_1.Utils.getStampCost(this.depth, BigInt(this.amount));
const estimatedCapacity = cafe_utility_1.Numbers.convertBytes(bee_js_1.Utils.getStampEffectiveBytes(this.depth));
const estimatedTtl = bee_js_1.Utils.getStampDuration(BigInt(this.amount), Number(chainState.currentPrice), 5);
this.console.log((0, text_1.createKeyValue)('Estimated cost', `${estimatedCost.toDecimalString()} xBZZ`));
this.console.log((0, text_1.createKeyValue)('Estimated capacity', estimatedCapacity));
this.console.log((0, text_1.createKeyValue)('Estimated TTL', cafe_utility_1.Dates.secondsToHumanTime(estimatedTtl.toSeconds())));
this.console.log((0, text_1.createKeyValue)('Type', this.immutable ? 'Immutable' : 'Mutable'));
if (this.immutable) {
this.console.info('At full capacity, an immutable stamp no longer allows new content uploads.');
}
else {
this.console.info('At full capacity, a mutable stamp allows new content uploads, but overwrites old content.');
}
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.createPostageBatch(this.amount.toString(), this.depth, {
label: this.label,
gasPrice: this.gasPrice?.toString(),
immutableFlag: this.immutable,
waitForUsable: this.waitUsable === false ? false : 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: 'depth',
description: 'Depth of the postage stamp',
type: 'number',
required: true,
minimum: 17,
maximum: 255,
}),
__metadata("design:type", Number)
], Buy.prototype, "depth", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'amount',
description: 'Value per chunk in PLUR, deprecates over time with new blocks mined',
type: 'bigint',
required: true,
minimum: 1,
}),
__metadata("design:type", BigInt)
], Buy.prototype, "amount", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'gas-price',
description: 'Gas price of the transaction in wei',
type: 'bigint',
minimum: 0,
}),
__metadata("design:type", BigInt)
], Buy.prototype, "gasPrice", void 0);
__decorate([
(0, furious_commander_1.Option)({ key: 'immutable', description: 'Disable stamp reuse', type: 'boolean', default: true }),
__metadata("design:type", Boolean)
], Buy.prototype, "immutable", void 0);
__decorate([
(0, furious_commander_1.Option)({ key: 'label', description: 'Label of the postage stamp' }),
__metadata("design:type", String)
], Buy.prototype, "label", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'wait-usable',
description: 'Wait until the postage stamp becomes usable',
type: 'boolean',
default: true,
}),
__metadata("design:type", Boolean)
], Buy.prototype, "waitUsable", void 0);
exports.Buy = Buy;