@ethersphere/swarm-cli
Version:
CLI tool for Bee
89 lines (77 loc) • 2.54 kB
text/typescript
import { BZZ } from '@ethersphere/bee-js'
import chalk from 'chalk'
import { LeafCommand, Option } from 'furious-commander'
import { createKeyValue } from '../../utils/text'
import { ChequeCommand } from './cheque-command'
export class Cashout extends ChequeCommand implements LeafCommand {
public readonly name = 'cashout'
public readonly alias = 'co'
public readonly description = 'Cashout one or all pending cheques'
public peer!: string | null
public all!: boolean
public minimum!: bigint
public gasLimit!: bigint
public gasPrice!: bigint
public async run(): Promise<void> {
super.init()
if (this.all) {
await this.cashoutAll()
}
if (this.peer) {
const amount = await this.getUncashedAmount(this.peer)
await this.cashoutOne(this.peer, amount)
}
}
private async cashoutAll(): Promise<void> {
this.console.info(`Collecting cheques with value at least ${this.minimum} PLUR...`)
const cheques = await this.getFilteredCheques(this.minimum)
this.console.info('Found ' + cheques.length + ' cheques.')
for (const { amount, address } of cheques) {
await this.cashoutOne(address, amount)
}
}
private async cashoutOne(address: string, amount: BZZ): Promise<void> {
try {
this.console.log(chalk.green('Cashing out:'))
this.printCheque({ address, amount })
const transaction = await this.bee.cashoutLastCheque(address, {
gasLimit: this.gasLimit,
gasPrice: this.gasPrice,
})
this.console.log(createKeyValue('Tx', transaction.toHex()))
this.console.quiet(transaction.toHex())
} catch (error) {
this.console.error('Could not cashout ' + address)
this.console.printBeeError(error, { notFoundMessage: 'No peer found with that address.' })
}
}
}