UNPKG

@ethersphere/swarm-cli

Version:
197 lines (196 loc) 8.43 kB
"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); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; 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 chalk_1 = __importDefault(require("chalk")); const furious_commander_1 = require("furious-commander"); const process_1 = require("process"); const chainsync_1 = require("../../utils/chainsync"); 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(); if (!(await (0, chainsync_1.isChainStateReady)(this.bee))) { this.console.error('Synchronization with the blockchain is not yet complete.'); this.console.error('Please wait until the Bee is fully synced before buying a postage stamp.'); this.console.error('You can check the synchronization status with the "status" command.'); return; } const chainState = await this.bee.getChainState(); const minimumAmount = BigInt(chainState.currentPrice) * 17280n; if (minimumAmount >= this.amount) { this.console.error('The amount is too low. The minimum amount is', (minimumAmount + 1n).toString()); return; } const estimatedCost = bee_js_1.Utils.getStampCost(this.depth, BigInt(this.amount)); const { bzzBalance } = await this.bee.getWalletBalance(); if (!this.dev && estimatedCost.gt(bzzBalance)) { this.console.error('You do not have enough BZZ to create this postage stamp.'); this.console.error(`Estimated cost: ${estimatedCost.toDecimalString()} xBZZ`); this.console.error(`Available balance: ${bzzBalance.toDecimalString()} xBZZ`); this.console.log(''); this.console.log('Visit the following link to learn how to fund your Bee node:'); this.console.log(chalk_1.default.blue('https://docs.ethswarm.org/docs/bee/installation/fund-your-node/')); (0, process_1.exit)(1); } 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(); } } } exports.Buy = Buy; __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);