UNPKG

@ethersphere/swarm-cli

Version:
110 lines (109 loc) 4.7 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Deposit = void 0; const bee_js_1 = require("@ethersphere/bee-js"); const furious_commander_1 = require("furious-commander"); const process_1 = require("process"); const spinner_1 = require("../../utils/spinner"); const root_command_1 = require("../root-command"); const command_log_1 = require("../root-command/command-log"); const validate_1 = require("../../utils/validate"); const MIN_DEPOSIT = bee_js_1.BZZ.fromDecimalString('10'); class Deposit extends root_command_1.RootCommand { constructor() { super(...arguments); Object.defineProperty(this, "name", { enumerable: true, configurable: true, writable: true, value: 'deposit' }); Object.defineProperty(this, "description", { enumerable: true, configurable: true, writable: true, value: 'Stake xBZZ for the storage incentives' }); Object.defineProperty(this, "amount", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "unit", { enumerable: true, configurable: true, writable: true, value: void 0 }); } async run() { super.init(); const amountBzz = this.unit === 'bzz' ? bee_js_1.BZZ.fromDecimalString(this.amount) : bee_js_1.BZZ.fromPLUR(this.amount); await this.deposit(amountBzz); this.console.log('Stake deposited successfully!'); this.console.log('Run `swarm-cli stake status` to check your stake status.'); this.console.log(''); this.console.log('Do note it may take a few minutes for the stake to be reflected in the node status.'); } async deposit(amount) { const currentStake = await this.bee.getStake(); if (currentStake.lt(MIN_DEPOSIT) && amount.lt(MIN_DEPOSIT)) { if (this.quiet) { throw new Error(`Insufficient deposit! Initial deposit has to be at least ${MIN_DEPOSIT.toSignificantDigits(1)} xBZZ!`); } if (!(await this.console.confirm(`Insufficient deposit! Initial deposit has to be at least ${MIN_DEPOSIT.toSignificantDigits(1)} xBZZ! Do you want to increase the deposit to ${MIN_DEPOSIT.toSignificantDigits(1)} xBZZ?`))) { throw new Error(`Insufficient deposit! Initial deposit has to be at least ${MIN_DEPOSIT.toSignificantDigits(1)} xBZZ!`); } amount = MIN_DEPOSIT; } if (!this.quiet && !this.yes) { this.yes = await this.console.confirm(`You are about to deposit a non-refundable stake of ${amount.toDecimalString()} xBZZ, are you sure you wish to proceed?`); } if (!this.yes && !this.quiet) { (0, process_1.exit)(1); } const spinner = (0, spinner_1.createSpinner)('Depositing stake'); if (this.verbosity !== command_log_1.VerbosityLevel.Quiet && !this.curl) { spinner.start(); } try { await this.bee.depositStake(amount); spinner.stop(); } catch (e) { spinner.stop(); throw e; } } } exports.Deposit = Deposit; __decorate([ (0, furious_commander_1.Argument)({ key: 'amount', description: 'Amount of tokens to deposit', type: 'decimal-string', required: true, validate: validate_1.validateTokenAmount, }), __metadata("design:type", String) ], Deposit.prototype, "amount", void 0); __decorate([ (0, furious_commander_1.Option)({ key: 'unit', type: 'enum', description: 'Unit of the amount', enum: ['bzz', 'plur'], default: 'bzz', }), __metadata("design:type", String) ], Deposit.prototype, "unit", void 0);