diff --git a/src/command/stamp/buy.ts b/src/command/stamp/buy.ts index b00bba5f..46144917 100644 --- a/src/command/stamp/buy.ts +++ b/src/command/stamp/buy.ts @@ -1,7 +1,9 @@ import { BatchId, Utils } from '@ethersphere/bee-js' import { Dates, Numbers } from 'cafe-utility' +import chalk from 'chalk' import { BigNumber } from 'ethers' import { LeafCommand, Option } from 'furious-commander' +import { exit } from 'process' import { isChainStateReady } from '../../utils/chainsync' import { createSpinner } from '../../utils/spinner' import { createKeyValue } from '../../utils/text' @@ -78,6 +80,20 @@ export class Buy extends StampCommand implements LeafCommand { } const estimatedCost = Utils.getStampCost(this.depth, BigInt(this.amount)) + const { bzzBalance } = await this.bee.getWalletBalance() + + if (estimatedCost.gt(bzzBalance)) { + this.console.error('You do not have enough BZZ to create this postage stamp.') + this.console.error(`Estimated cost: ${estimatedCost.toDecimalString()} xBZZ`) + this.console.error(`Available balance: ${bzzBalance.toDecimalString()} xBZZ`) + + this.console.log('') + this.console.log('Visit the following link to learn how to fund your Bee node:') + this.console.log(chalk.blue('https://docs.ethswarm.org/docs/bee/installation/fund-your-node/')) + + exit(1) + } + const estimatedCapacity = Numbers.convertBytes(Utils.getStampEffectiveBytes(this.depth)) const estimatedTtl = Utils.getStampDuration(BigInt(this.amount), Number(chainState.currentPrice), 5) diff --git a/src/command/stamp/create.ts b/src/command/stamp/create.ts index 77492108..7a826ad3 100644 --- a/src/command/stamp/create.ts +++ b/src/command/stamp/create.ts @@ -1,6 +1,8 @@ import { BatchId, Duration, Size } from '@ethersphere/bee-js' import { Dates, Numbers } from 'cafe-utility' +import chalk from 'chalk' import { LeafCommand, Option } from 'furious-commander' +import { exit } from 'process' import { isChainStateReady } from '../../utils/chainsync' import { createSpinner } from '../../utils/spinner' import { createKeyValue } from '../../utils/text' @@ -85,6 +87,18 @@ export class Create extends StampCommand implements LeafCommand { const estimatedCost = await this.bee.getStorageCost(size, duration) const { bzzBalance } = await this.bee.getWalletBalance() + if (estimatedCost.gt(bzzBalance)) { + this.console.error('You do not have enough BZZ to create this postage stamp.') + this.console.error(`Estimated cost: ${estimatedCost.toDecimalString()} xBZZ`) + this.console.error(`Available balance: ${bzzBalance.toDecimalString()} xBZZ`) + + this.console.log('') + this.console.log('Visit the following link to learn how to fund your Bee node:') + this.console.log(chalk.blue('https://docs.ethswarm.org/docs/bee/installation/fund-your-node/')) + + exit(1) + } + this.console.log('') this.console.log(createKeyValue('Cost', `${estimatedCost.toDecimalString()} xBZZ`)) this.console.log(createKeyValue('Available', `${bzzBalance.toDecimalString()} xBZZ`))