Skip to content

Commit 59b50a1

Browse files
authored
feat: allow specifying bzz as staking unit (#615)
1 parent 5b2f467 commit 59b50a1

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

src/command/stake.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { BZZ } from '@ethersphere/bee-js'
12
import { LeafCommand, Option } from 'furious-commander'
23
import { createSpinner } from '../utils/spinner'
34
import { createKeyValue } from '../utils/text'
45
import { RootCommand } from './root-command'
56
import { VerbosityLevel } from './root-command/command-log'
67

7-
const MIN_INITIAL_STAKE_PLUR = BigInt('100000000000000000')
8-
const MIN_INITIAL_STAKE_BZZ = 10
8+
const MIN_DEPOSIT = BZZ.fromDecimalString('10')
99

1010
export class Stake extends RootCommand implements LeafCommand {
1111
public readonly name = 'stake'
@@ -18,30 +18,58 @@ export class Stake extends RootCommand implements LeafCommand {
1818
type: 'bigint',
1919
minimum: BigInt(1),
2020
})
21-
public amount!: bigint | undefined
21+
public amountPlur!: bigint | undefined
2222

23-
private async deposit(amount: bigint): Promise<void> {
24-
const currentStake = (await this.bee.getStake()).toPLURBigInt()
23+
@Option({
24+
key: 'deposit-bzz',
25+
description: "Amount of BZZ to add to the node's stake",
26+
type: 'string',
27+
})
28+
public amountBzz!: string | undefined
29+
30+
public async run(): Promise<void> {
31+
super.init()
32+
33+
if (this.amountPlur) {
34+
await this.deposit(BZZ.fromPLUR(this.amountPlur))
35+
} else if (this.amountBzz) {
36+
await this.deposit(BZZ.fromDecimalString(this.amountBzz))
37+
}
38+
39+
const stake = await this.bee.getStake()
40+
41+
this.console.log(createKeyValue('Staked xBZZ', stake.toDecimalString()))
42+
this.console.quiet(stake.toDecimalString())
43+
}
2544

26-
if (!currentStake && amount < MIN_INITIAL_STAKE_PLUR) {
45+
private async deposit(amount: BZZ): Promise<void> {
46+
const currentStake = await this.bee.getStake()
47+
48+
if (currentStake.lt(MIN_DEPOSIT) && amount.lt(MIN_DEPOSIT)) {
2749
if (this.quiet) {
28-
throw new Error(`Insufficient deposit! Initial deposit has to be at least ${MIN_INITIAL_STAKE_BZZ} xBZZ!`)
50+
throw new Error(
51+
`Insufficient deposit! Initial deposit has to be at least ${MIN_DEPOSIT.toSignificantDigits(1)} xBZZ!`,
52+
)
2953
}
3054

3155
if (
3256
!(await this.console.confirm(
33-
`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?`,
57+
`Insufficient deposit! Initial deposit has to be at least ${MIN_DEPOSIT.toSignificantDigits(
58+
1,
59+
)} xBZZ! Do you want to increase the deposit to ${MIN_DEPOSIT.toSignificantDigits(1)} xBZZ?`,
3460
))
3561
) {
36-
throw new Error(`Insufficient deposit! Initial deposit has to be at least ${MIN_INITIAL_STAKE_BZZ} xBZZ!`)
62+
throw new Error(
63+
`Insufficient deposit! Initial deposit has to be at least ${MIN_DEPOSIT.toSignificantDigits(1)} xBZZ!`,
64+
)
3765
}
3866

39-
amount = MIN_INITIAL_STAKE_PLUR
67+
amount = MIN_DEPOSIT
4068
}
4169

4270
if (!this.quiet && !this.yes) {
4371
this.yes = await this.console.confirm(
44-
'Depositing stake is irreversible! It can not be withdrawn later on. Do you want to continue?',
72+
`You are about to deposit a non-refundable stake of ${amount.toDecimalString()} xBZZ, are you sure you wish to proceed?`,
4573
)
4674
}
4775

@@ -59,23 +87,12 @@ export class Stake extends RootCommand implements LeafCommand {
5987
await this.bee.depositStake(amount.toString())
6088
spinner.stop()
6189

62-
this.console.log('PLUR successfully staked!')
90+
this.console.log(
91+
'Successfully staked! It may take a few minutes for the stake to be reflected in the node status.',
92+
)
6393
} catch (e) {
6494
spinner.stop()
6595
throw e
6696
}
6797
}
68-
69-
public async run(): Promise<void> {
70-
super.init()
71-
72-
if (this.amount) {
73-
await this.deposit(this.amount)
74-
}
75-
76-
const stake = await this.bee.getStake()
77-
78-
this.console.log(createKeyValue('Staked xBZZ', stake.toDecimalString()))
79-
this.console.quiet(stake.toDecimalString())
80-
}
8198
}

0 commit comments

Comments
 (0)