Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/tests-rc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ jobs:

- name: Deposit to chequebook
run: |
swarm-cli cheque deposit 100000000000000000
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:11633
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:21633
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:31633
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:41633
swarm-cli cheque deposit 10
swarm-cli cheque deposit 10 --bee-api-url http://localhost:11633
swarm-cli cheque deposit 10 --bee-api-url http://localhost:21633
swarm-cli cheque deposit 10 --bee-api-url http://localhost:31633
swarm-cli cheque deposit 10 --bee-api-url http://localhost:41633

- name: Print swarm-cli status
continue-on-error: true
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ jobs:

- name: Deposit to chequebook
run: |
swarm-cli cheque deposit 100000000000000000
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:11633
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:21633
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:31633
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:41633
swarm-cli cheque deposit 10
swarm-cli cheque deposit 10 --bee-api-url http://localhost:11633
swarm-cli cheque deposit 10 --bee-api-url http://localhost:21633
swarm-cli cheque deposit 10 --bee-api-url http://localhost:31633
swarm-cli cheque deposit 10 --bee-api-url http://localhost:41633

- name: Print swarm-cli status
continue-on-error: true
Expand Down
24 changes: 2 additions & 22 deletions src/command/cheque/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Argument, LeafCommand, Option } from 'furious-commander'
import { BZZ } from '@ethersphere/bee-js'
import { Context } from 'madlad'
import { createKeyValue } from '../../utils/text'
import { ChequeCommand } from './cheque-command'
import { validateTokenAmount } from '../../utils/validate'

export class Deposit extends ChequeCommand implements LeafCommand {
public readonly name = 'deposit'
Expand All @@ -16,27 +16,7 @@ export class Deposit extends ChequeCommand implements LeafCommand {
description: 'Amount of tokens to deposit',
type: 'decimal-string',
required: true,
validate: (value: unknown, context: Context): string[] => {
if (context.options.unit === 'bzz') {
const amount = parseFloat(value as string)

if (isNaN(amount) || amount <= 0) {
return [`Invalid amount '${value}'. Amount must be a positive number.`]
}
} else {
try {
const amount = BigInt(value as string)

if (amount <= BigInt(0)) {
return [`Invalid amount '${value}'. Amount must be a positive integer.`]
}
} catch (e) {
return [`Invalid amount '${value}'. Amount must be a positive integer.`]
}
}

return []
},
validate: validateTokenAmount,
})
public amount!: string

Expand Down
24 changes: 2 additions & 22 deletions src/command/cheque/withdraw.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Argument, LeafCommand, Option } from 'furious-commander'
import { BZZ } from '@ethersphere/bee-js'
import { Context } from 'madlad'
import { createKeyValue } from '../../utils/text'
import { ChequeCommand } from './cheque-command'
import { validateTokenAmount } from '../../utils/validate'

export class Withdraw extends ChequeCommand implements LeafCommand {
public readonly name = 'withdraw'
Expand All @@ -16,27 +16,7 @@ export class Withdraw extends ChequeCommand implements LeafCommand {
type: 'string',
description: 'Amount of tokens to withdraw',
required: true,
validate: (value: unknown, context: Context): string[] => {
if (context.options.unit === 'bzz') {
const amount = parseFloat(value as string)

if (isNaN(amount) || amount <= 0) {
return [`Invalid amount '${value}'. Amount must be a positive number.`]
}
} else {
try {
const amount = BigInt(value as string)

if (amount <= BigInt(0)) {
return [`Invalid amount '${value}'. Amount must be a positive integer.`]
}
} catch (e) {
return [`Invalid amount '${value}'. Amount must be a positive integer.`]
}
}

return []
},
validate: validateTokenAmount,
})
public amount!: string

Expand Down
35 changes: 16 additions & 19 deletions src/command/stake/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { BZZ } from '@ethersphere/bee-js'
import { LeafCommand, Option } from 'furious-commander'
import { Argument, LeafCommand, Option } from 'furious-commander'
import { exit } from 'process'
import { createSpinner } from '../../utils/spinner'
import { RootCommand } from '../root-command'
import { VerbosityLevel } from '../root-command/command-log'
import { validateTokenAmount } from '../../utils/validate'

const MIN_DEPOSIT = BZZ.fromDecimalString('10')

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

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

@Option({
key: 'bzz',
description: "Amount of BZZ to add to the node's stake",
type: 'string',
conflicts: 'plur',
@Argument({
key: 'amount',
description: 'Amount of tokens to deposit',
type: 'decimal-string',
required: true,
validate: validateTokenAmount,
})
public amountBzz!: string | undefined
public amount!: string

@Option({
key: 'plur',
description: "Amount of PLUR to add to the node's stake",
type: 'bigint',
minimum: BigInt(1),
conflicts: 'bzz',
required: true,
key: 'unit',
type: 'enum',
description: 'Unit of the amount',
enum: ['bzz', 'plur'],
default: 'bzz',
})
public amountPlur!: bigint | undefined
public unit!: string

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

if (this.amountPlur) {
await this.deposit(BZZ.fromPLUR(this.amountPlur))
} else if (this.amountBzz) {
await this.deposit(BZZ.fromDecimalString(this.amountBzz))
}
const amountBzz = this.unit === 'bzz' ? BZZ.fromDecimalString(this.amount) : 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.')
Expand Down
23 changes: 23 additions & 0 deletions src/utils/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Context } from 'madlad'

export function validateTokenAmount(value: unknown, context: Context): string[] {
if (context.options.unit === 'bzz') {
const amount = parseFloat(value as string)

if (isNaN(amount) || amount <= 0) {
return [`Invalid amount '${value}'. Amount must be a positive number.`]
}
} else {
try {
const amount = BigInt(value as string)

if (amount <= BigInt(0)) {
return [`Invalid amount '${value}'. Amount must be a positive integer.`]
}
} catch (e) {
return [`Invalid amount '${value}'. Amount must be a positive integer.`]
}
}

return []
}
4 changes: 2 additions & 2 deletions test/command/stake.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ expect.extend({
describeCommand('Test Stake command', ({ consoleMessages }) => {
test('should stake with bzz, plur, and print stake', async () => {
await invokeTestCli(['stake', 'status', ...getBeeDevOption()])
await invokeTestCli(['stake', 'deposit', '--bzz', '10', '--yes', ...getBeeDevOption()])
await invokeTestCli(['stake', 'deposit', '--plur', '10', '--yes', ...getBeeDevOption()])
await invokeTestCli(['stake', 'deposit', '10', '--unit', 'bzz', '--yes', ...getBeeDevOption()])
await invokeTestCli(['stake', 'deposit', '10', '--unit', 'plur', '--yes', ...getBeeDevOption()])
await invokeTestCli(['stake', 'status', ...getBeeDevOption()])
expect(consoleMessages).toMatchLinesInOrder([
['Staked xBZZ', '0.0000000000000000'],
Expand Down
9 changes: 5 additions & 4 deletions test/coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{"total": {"lines":{"total":2863,"covered":2186,"skipped":0,"pct":76.35},"statements":{"total":2884,"covered":2200,"skipped":0,"pct":76.28},"functions":{"total":343,"covered":271,"skipped":0,"pct":79},"branches":{"total":621,"covered":353,"skipped":0,"pct":56.84},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
{"total": {"lines":{"total":2855,"covered":2180,"skipped":0,"pct":76.35},"statements":{"total":2876,"covered":2194,"skipped":0,"pct":76.28},"functions":{"total":342,"covered":270,"skipped":0,"pct":78.94},"branches":{"total":614,"covered":347,"skipped":0,"pct":56.51},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/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}}
,"/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}}
,"/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}}
Expand All @@ -12,11 +12,11 @@
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
Expand Down Expand Up @@ -67,7 +67,7 @@
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
,"/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}}
Expand Down Expand Up @@ -109,4 +109,5 @@
,"/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}}
,"/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}}
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/text.ts": {"lines":{"total":21,"covered":18,"skipped":0,"pct":85.71},"functions":{"total":9,"covered":7,"skipped":0,"pct":77.77},"statements":{"total":22,"covered":19,"skipped":0,"pct":86.36},"branches":{"total":5,"covered":3,"skipped":0,"pct":60}}
,"/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}}
}