Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/command/cheque/cheque-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface Cashable {
amount: BZZ
}

export const VALID_UNITS = ['bzz', 'plur']

export class ChequeCommand extends RootCommand {
protected async getFilteredCheques(minimum: bigint): Promise<Cashable[]> {
const cheques = await this.getCashableCheques()
Expand Down
31 changes: 26 additions & 5 deletions src/command/cheque/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Argument, LeafCommand } from 'furious-commander'
import { createKeyValue } from '../../utils/text'
import { ChequeCommand } from './cheque-command'
import { Argument, LeafCommand, Option } from 'furious-commander'
import { createKeyValue, errorText } from '../../utils/text'
import { ChequeCommand, VALID_UNITS } from './cheque-command'
import { BZZ } from '@ethersphere/bee-js'
import { exit } from 'process'

export class Deposit extends ChequeCommand implements LeafCommand {
public readonly name = 'deposit'
Expand All @@ -11,18 +13,37 @@ export class Deposit extends ChequeCommand implements LeafCommand {

@Argument({
key: 'amount',
description: 'Amount of tokens to deposit',
type: 'bigint',
description: 'Amount of tokens to deposit in PLUR',
required: true,
minimum: BigInt(1),
})
public amount!: bigint
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably needs to have type string

With type bigint it probably won't accept decimal values such as 0.5 (truncates to 0, invalid) or 1.5 (loses decimal part)


@Option({
key: 'unit',
type: 'string',
description: `Unit of the amount; choices: ${VALID_UNITS.join(', ')}`,
default: 'bzz',
})
public unit!: string

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

const response = await this.bee.depositTokens(this.amount.toString())
this.validateUnit()
const amountInPlur =
this.unit === 'bzz' ? BZZ.fromDecimalString(this.amount.toString()).toPLURBigInt() : this.amount

const response = await this.bee.depositTokens(amountInPlur.toString())
this.console.log(createKeyValue('Tx', response.toHex()))
this.console.quiet(response.toHex())
}

private validateUnit() {
if (!VALID_UNITS.includes(this.unit)) {
this.console.error(errorText(`Invalid unit '${this.unit}'. Valid units are: ${VALID_UNITS.join(', ')}`))
exit(1)
}
}
}
31 changes: 26 additions & 5 deletions src/command/cheque/withdraw.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Argument, LeafCommand } from 'furious-commander'
import { createKeyValue } from '../../utils/text'
import { ChequeCommand } from './cheque-command'
import { Argument, LeafCommand, Option } from 'furious-commander'
import { createKeyValue, errorText } from '../../utils/text'
import { ChequeCommand, VALID_UNITS } from './cheque-command'
import { exit } from 'process'
import { BZZ } from '@ethersphere/bee-js'

export class Withdraw extends ChequeCommand implements LeafCommand {
public readonly name = 'withdraw'
Expand All @@ -12,17 +14,36 @@ export class Withdraw extends ChequeCommand implements LeafCommand {
@Argument({
key: 'amount',
type: 'bigint',
description: 'Amount of tokens to withdraw in PLUR',
description: 'Amount of tokens to withdraw',
required: true,
minimum: BigInt(1),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably needs to have type string

With type bigint it probably won't accept decimal values such as 0.5 (truncates to 0, invalid) or 1.5 (loses decimal part)

})
public amount!: bigint

@Option({
key: 'unit',
type: 'string',
description: `Unit of the amount; choices: ${VALID_UNITS.join(', ')}`,
default: 'bzz',
})
public unit!: string

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

const response = await this.bee.withdrawTokens(this.amount.toString())
this.validateUnit()
const amountInPlur =
this.unit === 'bzz' ? BZZ.fromDecimalString(this.amount.toString()).toPLURBigInt() : this.amount

const response = await this.bee.withdrawTokens(amountInPlur.toString())
this.console.log(createKeyValue('Tx', response.toHex()))
this.console.quiet(response.toHex())
}

private validateUnit() {
if (!VALID_UNITS.includes(this.unit)) {
this.console.error(errorText(`Invalid unit '${this.unit}'. Valid units are: ${VALID_UNITS.join(', ')}`))
exit(1)
}
}
}
4 changes: 4 additions & 0 deletions src/utils/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export function warningText(string: string): string {
return chalk.yellow(string)
}

export function errorText(string: string): string {
return chalk.red(string)
}

export function deletePreviousLine(): void {
process.stdout.write('\r' + goUpOneRow() + deleteWholeRow())
}
Expand Down
10 changes: 5 additions & 5 deletions test/coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{"total": {"lines":{"total":2774,"covered":2069,"skipped":0,"pct":74.58},"statements":{"total":2795,"covered":2083,"skipped":0,"pct":74.52},"functions":{"total":333,"covered":256,"skipped":0,"pct":76.87},"branches":{"total":583,"covered":313,"skipped":0,"pct":53.68},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
{"total": {"lines":{"total":2793,"covered":2077,"skipped":0,"pct":74.36},"statements":{"total":2814,"covered":2091,"skipped":0,"pct":74.3},"functions":{"total":336,"covered":256,"skipped":0,"pct":76.19},"branches":{"total":589,"covered":313,"skipped":0,"pct":53.14},"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 @@ -11,12 +11,12 @@
,"/home/runner/work/swarm-cli/swarm-cli/src/command/status.ts": {"lines":{"total":79,"covered":79,"skipped":0,"pct":100},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":79,"covered":79,"skipped":0,"pct":100},"branches":{"total":11,"covered":11,"skipped":0,"pct":100}}
,"/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":99,"covered":56,"skipped":0,"pct":56.56}}
,"/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":12,"covered":8,"skipped":0,"pct":66.66},"functions":{"total":2,"covered":1,"skipped":0,"pct":50},"statements":{"total":12,"covered":8,"skipped":0,"pct":66.66},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/swarm-cli/swarm-cli/src/command/cheque/cheque-command.ts": {"lines":{"total":26,"covered":22,"skipped":0,"pct":84.61},"functions":{"total":5,"covered":5,"skipped":0,"pct":100},"statements":{"total":27,"covered":23,"skipped":0,"pct":85.18},"branches":{"total":2,"covered":0,"skipped":0,"pct":0}}
,"/home/runner/work/swarm-cli/swarm-cli/src/command/cheque/deposit.ts": {"lines":{"total":20,"covered":11,"skipped":0,"pct":55},"functions":{"total":3,"covered":1,"skipped":0,"pct":33.33},"statements":{"total":20,"covered":11,"skipped":0,"pct":55},"branches":{"total":3,"covered":0,"skipped":0,"pct":0}}
,"/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":12,"covered":8,"skipped":0,"pct":66.66},"functions":{"total":2,"covered":1,"skipped":0,"pct":50},"statements":{"total":12,"covered":8,"skipped":0,"pct":66.66},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/swarm-cli/swarm-cli/src/command/cheque/withdraw.ts": {"lines":{"total":20,"covered":11,"skipped":0,"pct":55},"functions":{"total":3,"covered":1,"skipped":0,"pct":33.33},"statements":{"total":20,"covered":11,"skipped":0,"pct":55},"branches":{"total":3,"covered":0,"skipped":0,"pct":0}}
,"/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 @@ -105,5 +105,5 @@
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/option.ts": {"lines":{"total":3,"covered":3,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":3,"covered":3,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/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":19,"covered":16,"skipped":0,"pct":84.21},"functions":{"total":8,"covered":6,"skipped":0,"pct":75},"statements":{"total":20,"covered":17,"skipped":0,"pct":85},"branches":{"total":5,"covered":3,"skipped":0,"pct":60}}
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/text.ts": {"lines":{"total":21,"covered":17,"skipped":0,"pct":80.95},"functions":{"total":9,"covered":6,"skipped":0,"pct":66.66},"statements":{"total":22,"covered":18,"skipped":0,"pct":81.81},"branches":{"total":5,"covered":3,"skipped":0,"pct":60}}
}
4 changes: 2 additions & 2 deletions test/misc/monetary-units.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ describeCommand('Test Monetary units', ({ consoleMessages }) => {

it('should show units in help: cheque withdraw', async () => {
await invokeTestCli(['cheque', 'withdraw', '--help'])
expectSubstringsPrinted('amount', 'in PLUR')
expectSubstringsPrinted('amount', 'Amount of tokens to withdraw')
})

it('should show units in help: cheque deposit', async () => {
await invokeTestCli(['cheque', 'deposit', '--help'])
expectSubstringsPrinted('amount', 'in PLUR')
expectSubstringsPrinted('amount', 'Amount of tokens to deposit')
})

it('should show units after running: cheque list', async () => {
Expand Down