diff --git a/src/command/stake.ts b/src/command/stake.ts index 94ddf76b..6b9bbbef 100644 --- a/src/command/stake.ts +++ b/src/command/stake.ts @@ -1,11 +1,11 @@ +import { BZZ } from '@ethersphere/bee-js' import { LeafCommand, Option } from 'furious-commander' import { createSpinner } from '../utils/spinner' import { createKeyValue } from '../utils/text' import { RootCommand } from './root-command' import { VerbosityLevel } from './root-command/command-log' -const MIN_INITIAL_STAKE_PLUR = BigInt('100000000000000000') -const MIN_INITIAL_STAKE_BZZ = 10 +const MIN_DEPOSIT = BZZ.fromDecimalString('10') export class Stake extends RootCommand implements LeafCommand { public readonly name = 'stake' @@ -18,30 +18,58 @@ export class Stake extends RootCommand implements LeafCommand { type: 'bigint', minimum: BigInt(1), }) - public amount!: bigint | undefined + public amountPlur!: bigint | undefined - private async deposit(amount: bigint): Promise { - const currentStake = (await this.bee.getStake()).toPLURBigInt() + @Option({ + key: 'deposit-bzz', + description: "Amount of BZZ to add to the node's stake", + type: 'string', + }) + public amountBzz!: string | undefined + + public async run(): Promise { + super.init() + + if (this.amountPlur) { + await this.deposit(BZZ.fromPLUR(this.amountPlur)) + } else if (this.amountBzz) { + await this.deposit(BZZ.fromDecimalString(this.amountBzz)) + } + + const stake = await this.bee.getStake() + + this.console.log(createKeyValue('Staked xBZZ', stake.toDecimalString())) + this.console.quiet(stake.toDecimalString()) + } - if (!currentStake && amount < MIN_INITIAL_STAKE_PLUR) { + private async deposit(amount: BZZ): Promise { + 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_INITIAL_STAKE_BZZ} xBZZ!`) + 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_INITIAL_STAKE_BZZ} xBZZ. Do you want to increase the deposit to ${MIN_INITIAL_STAKE_BZZ} xBZZ?`, + `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_INITIAL_STAKE_BZZ} xBZZ!`) + throw new Error( + `Insufficient deposit! Initial deposit has to be at least ${MIN_DEPOSIT.toSignificantDigits(1)} xBZZ!`, + ) } - amount = MIN_INITIAL_STAKE_PLUR + amount = MIN_DEPOSIT } if (!this.quiet && !this.yes) { this.yes = await this.console.confirm( - 'Depositing stake is irreversible! It can not be withdrawn later on. Do you want to continue?', + `You are about to deposit a non-refundable stake of ${amount.toDecimalString()} xBZZ, are you sure you wish to proceed?`, ) } @@ -59,23 +87,12 @@ export class Stake extends RootCommand implements LeafCommand { await this.bee.depositStake(amount.toString()) spinner.stop() - this.console.log('PLUR successfully staked!') + this.console.log( + 'Successfully staked! It may take a few minutes for the stake to be reflected in the node status.', + ) } catch (e) { spinner.stop() throw e } } - - public async run(): Promise { - super.init() - - if (this.amount) { - await this.deposit(this.amount) - } - - const stake = await this.bee.getStake() - - this.console.log(createKeyValue('Staked xBZZ', stake.toDecimalString())) - this.console.quiet(stake.toDecimalString()) - } }