From 44beed2c0216565fd9090de83370c4da856c875b Mon Sep 17 00:00:00 2001 From: Cafe137 Date: Wed, 2 Jul 2025 22:52:14 +0200 Subject: [PATCH] feat: handle out of balance during postage batch creation --- src/command/stamp/buy.ts | 16 ++++++++++++++++ src/command/stamp/create.ts | 14 ++++++++++++++ 2 files changed, 30 insertions(+) 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`))