Skip to content

Commit d6a25cd

Browse files
slapec93Gergely Békésigithub-actions[bot]
authored
feat: use amount argument and unit option for stake deposit (#707)
* feat: use amount argument and unit option for stake deposit * test: update test coverage * refactor: make validation logic reusable * ci: update deposit command * test: update test coverage * test: update test coverage --------- Co-authored-by: Gergely Békési <gergely.bekesi@ethswarm.org> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 9c32f68 commit d6a25cd

6 files changed

Lines changed: 50 additions & 69 deletions

File tree

src/command/cheque/deposit.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Argument, LeafCommand, Option } from 'furious-commander'
22
import { BZZ } from '@ethersphere/bee-js'
3-
import { Context } from 'madlad'
43
import { createKeyValue } from '../../utils/text'
54
import { ChequeCommand } from './cheque-command'
5+
import { validateTokenAmount } from '../../utils/validate'
66

77
export class Deposit extends ChequeCommand implements LeafCommand {
88
public readonly name = 'deposit'
@@ -16,27 +16,7 @@ export class Deposit extends ChequeCommand implements LeafCommand {
1616
description: 'Amount of tokens to deposit',
1717
type: 'decimal-string',
1818
required: true,
19-
validate: (value: unknown, context: Context): string[] => {
20-
if (context.options.unit === 'bzz') {
21-
const amount = parseFloat(value as string)
22-
23-
if (isNaN(amount) || amount <= 0) {
24-
return [`Invalid amount '${value}'. Amount must be a positive number.`]
25-
}
26-
} else {
27-
try {
28-
const amount = BigInt(value as string)
29-
30-
if (amount <= BigInt(0)) {
31-
return [`Invalid amount '${value}'. Amount must be a positive integer.`]
32-
}
33-
} catch (e) {
34-
return [`Invalid amount '${value}'. Amount must be a positive integer.`]
35-
}
36-
}
37-
38-
return []
39-
},
19+
validate: validateTokenAmount,
4020
})
4121
public amount!: string
4222

src/command/cheque/withdraw.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Argument, LeafCommand, Option } from 'furious-commander'
22
import { BZZ } from '@ethersphere/bee-js'
3-
import { Context } from 'madlad'
43
import { createKeyValue } from '../../utils/text'
54
import { ChequeCommand } from './cheque-command'
5+
import { validateTokenAmount } from '../../utils/validate'
66

77
export class Withdraw extends ChequeCommand implements LeafCommand {
88
public readonly name = 'withdraw'
@@ -16,27 +16,7 @@ export class Withdraw extends ChequeCommand implements LeafCommand {
1616
type: 'string',
1717
description: 'Amount of tokens to withdraw',
1818
required: true,
19-
validate: (value: unknown, context: Context): string[] => {
20-
if (context.options.unit === 'bzz') {
21-
const amount = parseFloat(value as string)
22-
23-
if (isNaN(amount) || amount <= 0) {
24-
return [`Invalid amount '${value}'. Amount must be a positive number.`]
25-
}
26-
} else {
27-
try {
28-
const amount = BigInt(value as string)
29-
30-
if (amount <= BigInt(0)) {
31-
return [`Invalid amount '${value}'. Amount must be a positive integer.`]
32-
}
33-
} catch (e) {
34-
return [`Invalid amount '${value}'. Amount must be a positive integer.`]
35-
}
36-
}
37-
38-
return []
39-
},
19+
validate: validateTokenAmount,
4020
})
4121
public amount!: string
4222

src/command/stake/deposit.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { BZZ } from '@ethersphere/bee-js'
2-
import { LeafCommand, Option } from 'furious-commander'
2+
import { Argument, LeafCommand, Option } from 'furious-commander'
33
import { exit } from 'process'
44
import { createSpinner } from '../../utils/spinner'
55
import { RootCommand } from '../root-command'
66
import { VerbosityLevel } from '../root-command/command-log'
7+
import { validateTokenAmount } from '../../utils/validate'
78

89
const MIN_DEPOSIT = BZZ.fromDecimalString('10')
910

@@ -12,33 +13,29 @@ export class Deposit extends RootCommand implements LeafCommand {
1213

1314
public readonly description = 'Stake xBZZ for the storage incentives'
1415

15-
@Option({
16-
key: 'bzz',
17-
description: "Amount of BZZ to add to the node's stake",
18-
type: 'string',
19-
conflicts: 'plur',
16+
@Argument({
17+
key: 'amount',
18+
description: 'Amount of tokens to deposit',
19+
type: 'decimal-string',
2020
required: true,
21+
validate: validateTokenAmount,
2122
})
22-
public amountBzz!: string | undefined
23+
public amount!: string
2324

2425
@Option({
25-
key: 'plur',
26-
description: "Amount of PLUR to add to the node's stake",
27-
type: 'bigint',
28-
minimum: BigInt(1),
29-
conflicts: 'bzz',
30-
required: true,
26+
key: 'unit',
27+
type: 'enum',
28+
description: 'Unit of the amount',
29+
enum: ['bzz', 'plur'],
30+
default: 'bzz',
3131
})
32-
public amountPlur!: bigint | undefined
32+
public unit!: string
3333

3434
public async run(): Promise<void> {
3535
super.init()
3636

37-
if (this.amountPlur) {
38-
await this.deposit(BZZ.fromPLUR(this.amountPlur))
39-
} else if (this.amountBzz) {
40-
await this.deposit(BZZ.fromDecimalString(this.amountBzz))
41-
}
37+
const amountBzz = this.unit === 'bzz' ? BZZ.fromDecimalString(this.amount) : BZZ.fromPLUR(this.amount)
38+
await this.deposit(amountBzz)
4239

4340
this.console.log('Stake deposited successfully!')
4441
this.console.log('Run `swarm-cli stake status` to check your stake status.')

src/utils/validate.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Context } from 'madlad'
2+
3+
export function validateTokenAmount(value: unknown, context: Context): string[] {
4+
if (context.options.unit === 'bzz') {
5+
const amount = parseFloat(value as string)
6+
7+
if (isNaN(amount) || amount <= 0) {
8+
return [`Invalid amount '${value}'. Amount must be a positive number.`]
9+
}
10+
} else {
11+
try {
12+
const amount = BigInt(value as string)
13+
14+
if (amount <= BigInt(0)) {
15+
return [`Invalid amount '${value}'. Amount must be a positive integer.`]
16+
}
17+
} catch (e) {
18+
return [`Invalid amount '${value}'. Amount must be a positive integer.`]
19+
}
20+
}
21+
22+
return []
23+
}

test/command/stake.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ expect.extend({
99
describeCommand('Test Stake command', ({ consoleMessages }) => {
1010
test('should stake with bzz, plur, and print stake', async () => {
1111
await invokeTestCli(['stake', 'status', ...getBeeDevOption()])
12-
await invokeTestCli(['stake', 'deposit', '--bzz', '10', '--yes', ...getBeeDevOption()])
13-
await invokeTestCli(['stake', 'deposit', '--plur', '10', '--yes', ...getBeeDevOption()])
12+
await invokeTestCli(['stake', 'deposit', '10', '--unit', 'bzz', '--yes', ...getBeeDevOption()])
13+
await invokeTestCli(['stake', 'deposit', '10', '--unit', 'plur', '--yes', ...getBeeDevOption()])
1414
await invokeTestCli(['stake', 'status', ...getBeeDevOption()])
1515
expect(consoleMessages).toMatchLinesInOrder([
1616
['Staked xBZZ', '0.0000000000000000'],

test/coverage/coverage-summary.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{"total": {"lines":{"total":2863,"covered":2188,"skipped":0,"pct":76.42},"statements":{"total":2884,"covered":2202,"skipped":0,"pct":76.35},"functions":{"total":343,"covered":272,"skipped":0,"pct":79.3},"branches":{"total":621,"covered":355,"skipped":0,"pct":57.16},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
1+
{"total": {"lines":{"total":2855,"covered":2182,"skipped":0,"pct":76.42},"statements":{"total":2876,"covered":2196,"skipped":0,"pct":76.35},"functions":{"total":342,"covered":271,"skipped":0,"pct":79.23},"branches":{"total":614,"covered":349,"skipped":0,"pct":56.84},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
22
,"/home/runner/work/swarm-cli/swarm-cli/src/application.ts": {"lines":{"total":2,"covered":0,"skipped":0,"pct":0},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":2,"covered":0,"skipped":0,"pct":0},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
33
,"/home/runner/work/swarm-cli/swarm-cli/src/config.ts": {"lines":{"total":33,"covered":32,"skipped":0,"pct":96.96},"functions":{"total":1,"covered":0,"skipped":0,"pct":0},"statements":{"total":33,"covered":32,"skipped":0,"pct":96.96},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
44
,"/home/runner/work/swarm-cli/swarm-cli/src/curl.ts": {"lines":{"total":24,"covered":24,"skipped":0,"pct":100},"functions":{"total":7,"covered":7,"skipped":0,"pct":100},"statements":{"total":25,"covered":25,"skipped":0,"pct":100},"branches":{"total":13,"covered":12,"skipped":0,"pct":92.3}}
@@ -12,11 +12,11 @@
1212
,"/home/runner/work/swarm-cli/swarm-cli/src/command/upload.ts": {"lines":{"total":223,"covered":157,"skipped":0,"pct":70.4},"functions":{"total":16,"covered":14,"skipped":0,"pct":87.5},"statements":{"total":224,"covered":158,"skipped":0,"pct":70.53},"branches":{"total":101,"covered":58,"skipped":0,"pct":57.42}}
1313
,"/home/runner/work/swarm-cli/swarm-cli/src/command/cheque/cashout.ts": {"lines":{"total":32,"covered":30,"skipped":0,"pct":93.75},"functions":{"total":4,"covered":4,"skipped":0,"pct":100},"statements":{"total":32,"covered":30,"skipped":0,"pct":93.75},"branches":{"total":2,"covered":2,"skipped":0,"pct":100}}
1414
,"/home/runner/work/swarm-cli/swarm-cli/src/command/cheque/cheque-command.ts": {"lines":{"total":25,"covered":21,"skipped":0,"pct":84},"functions":{"total":5,"covered":5,"skipped":0,"pct":100},"statements":{"total":26,"covered":22,"skipped":0,"pct":84.61},"branches":{"total":2,"covered":0,"skipped":0,"pct":0}}
15-
,"/home/runner/work/swarm-cli/swarm-cli/src/command/cheque/deposit.ts": {"lines":{"total":25,"covered":23,"skipped":0,"pct":92},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":25,"covered":23,"skipped":0,"pct":92},"branches":{"total":8,"covered":7,"skipped":0,"pct":87.5}}
15+
,"/home/runner/work/swarm-cli/swarm-cli/src/command/cheque/deposit.ts": {"lines":{"total":16,"covered":16,"skipped":0,"pct":100},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":16,"covered":16,"skipped":0,"pct":100},"branches":{"total":2,"covered":2,"skipped":0,"pct":100}}
1616
,"/home/runner/work/swarm-cli/swarm-cli/src/command/cheque/index.ts": {"lines":{"total":9,"covered":9,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":9,"covered":9,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
1717
,"/home/runner/work/swarm-cli/swarm-cli/src/command/cheque/list.ts": {"lines":{"total":13,"covered":13,"skipped":0,"pct":100},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":14,"covered":14,"skipped":0,"pct":100},"branches":{"total":1,"covered":1,"skipped":0,"pct":100}}
1818
,"/home/runner/work/swarm-cli/swarm-cli/src/command/cheque/withdraw-all.ts": {"lines":{"total":15,"covered":6,"skipped":0,"pct":40},"functions":{"total":2,"covered":1,"skipped":0,"pct":50},"statements":{"total":15,"covered":6,"skipped":0,"pct":40},"branches":{"total":1,"covered":0,"skipped":0,"pct":0}}
19-
,"/home/runner/work/swarm-cli/swarm-cli/src/command/cheque/withdraw.ts": {"lines":{"total":25,"covered":23,"skipped":0,"pct":92},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":25,"covered":23,"skipped":0,"pct":92},"branches":{"total":8,"covered":7,"skipped":0,"pct":87.5}}
19+
,"/home/runner/work/swarm-cli/swarm-cli/src/command/cheque/withdraw.ts": {"lines":{"total":16,"covered":16,"skipped":0,"pct":100},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":16,"covered":16,"skipped":0,"pct":100},"branches":{"total":2,"covered":2,"skipped":0,"pct":100}}
2020
,"/home/runner/work/swarm-cli/swarm-cli/src/command/feed/feed-command.ts": {"lines":{"total":49,"covered":49,"skipped":0,"pct":100},"functions":{"total":5,"covered":5,"skipped":0,"pct":100},"statements":{"total":49,"covered":49,"skipped":0,"pct":100},"branches":{"total":13,"covered":13,"skipped":0,"pct":100}}
2121
,"/home/runner/work/swarm-cli/swarm-cli/src/command/feed/index.ts": {"lines":{"total":7,"covered":7,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":7,"covered":7,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
2222
,"/home/runner/work/swarm-cli/swarm-cli/src/command/feed/print.ts": {"lines":{"total":80,"covered":26,"skipped":0,"pct":32.5},"functions":{"total":4,"covered":2,"skipped":0,"pct":50},"statements":{"total":81,"covered":26,"skipped":0,"pct":32.09},"branches":{"total":21,"covered":4,"skipped":0,"pct":19.04}}
@@ -67,7 +67,7 @@
6767
,"/home/runner/work/swarm-cli/swarm-cli/src/command/root-command/command-log.ts": {"lines":{"total":78,"covered":60,"skipped":0,"pct":76.92},"functions":{"total":9,"covered":6,"skipped":0,"pct":66.66},"statements":{"total":78,"covered":60,"skipped":0,"pct":76.92},"branches":{"total":11,"covered":7,"skipped":0,"pct":63.63}}
6868
,"/home/runner/work/swarm-cli/swarm-cli/src/command/root-command/index.ts": {"lines":{"total":44,"covered":40,"skipped":0,"pct":90.9},"functions":{"total":4,"covered":4,"skipped":0,"pct":100},"statements":{"total":44,"covered":40,"skipped":0,"pct":90.9},"branches":{"total":9,"covered":5,"skipped":0,"pct":55.55}}
6969
,"/home/runner/work/swarm-cli/swarm-cli/src/command/root-command/printer.ts": {"lines":{"total":9,"covered":9,"skipped":0,"pct":100},"functions":{"total":6,"covered":6,"skipped":0,"pct":100},"statements":{"total":9,"covered":9,"skipped":0,"pct":100},"branches":{"total":1,"covered":1,"skipped":0,"pct":100}}
70-
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stake/deposit.ts": {"lines":{"total":40,"covered":31,"skipped":0,"pct":77.5},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":40,"covered":31,"skipped":0,"pct":77.5},"branches":{"total":17,"covered":11,"skipped":0,"pct":64.7}}
70+
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stake/deposit.ts": {"lines":{"total":39,"covered":30,"skipped":0,"pct":76.92},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":39,"covered":30,"skipped":0,"pct":76.92},"branches":{"total":16,"covered":10,"skipped":0,"pct":62.5}}
7171
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stake/index.ts": {"lines":{"total":8,"covered":8,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":8,"covered":8,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
7272
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stake/recover.ts": {"lines":{"total":22,"covered":10,"skipped":0,"pct":45.45},"functions":{"total":2,"covered":1,"skipped":0,"pct":50},"statements":{"total":22,"covered":10,"skipped":0,"pct":45.45},"branches":{"total":1,"covered":0,"skipped":0,"pct":0}}
7373
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stake/status.ts": {"lines":{"total":11,"covered":11,"skipped":0,"pct":100},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":11,"covered":11,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
@@ -109,4 +109,5 @@
109109
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/rpc.ts": {"lines":{"total":37,"covered":9,"skipped":0,"pct":24.32},"functions":{"total":6,"covered":0,"skipped":0,"pct":0},"statements":{"total":37,"covered":9,"skipped":0,"pct":24.32},"branches":{"total":5,"covered":0,"skipped":0,"pct":0}}
110110
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/spinner.ts": {"lines":{"total":15,"covered":15,"skipped":0,"pct":100},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":15,"covered":15,"skipped":0,"pct":100},"branches":{"total":5,"covered":3,"skipped":0,"pct":60}}
111111
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/text.ts": {"lines":{"total":21,"covered":19,"skipped":0,"pct":90.47},"functions":{"total":9,"covered":7,"skipped":0,"pct":77.77},"statements":{"total":22,"covered":20,"skipped":0,"pct":90.9},"branches":{"total":5,"covered":4,"skipped":0,"pct":80}}
112+
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/validate.ts": {"lines":{"total":11,"covered":9,"skipped":0,"pct":81.81},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":11,"covered":9,"skipped":0,"pct":81.81},"branches":{"total":6,"covered":5,"skipped":0,"pct":83.33}}
112113
}

0 commit comments

Comments
 (0)