Skip to content

Commit d258040

Browse files
authored
feat: handle out of balance during postage batch creation (#625)
1 parent 8ef81af commit d258040

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/command/stamp/buy.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { BatchId, Utils } from '@ethersphere/bee-js'
22
import { Dates, Numbers } from 'cafe-utility'
3+
import chalk from 'chalk'
34
import { BigNumber } from 'ethers'
45
import { LeafCommand, Option } from 'furious-commander'
6+
import { exit } from 'process'
57
import { isChainStateReady } from '../../utils/chainsync'
68
import { createSpinner } from '../../utils/spinner'
79
import { createKeyValue } from '../../utils/text'
@@ -78,6 +80,20 @@ export class Buy extends StampCommand implements LeafCommand {
7880
}
7981

8082
const estimatedCost = Utils.getStampCost(this.depth, BigInt(this.amount))
83+
const { bzzBalance } = await this.bee.getWalletBalance()
84+
85+
if (estimatedCost.gt(bzzBalance)) {
86+
this.console.error('You do not have enough BZZ to create this postage stamp.')
87+
this.console.error(`Estimated cost: ${estimatedCost.toDecimalString()} xBZZ`)
88+
this.console.error(`Available balance: ${bzzBalance.toDecimalString()} xBZZ`)
89+
90+
this.console.log('')
91+
this.console.log('Visit the following link to learn how to fund your Bee node:')
92+
this.console.log(chalk.blue('https://docs.ethswarm.org/docs/bee/installation/fund-your-node/'))
93+
94+
exit(1)
95+
}
96+
8197
const estimatedCapacity = Numbers.convertBytes(Utils.getStampEffectiveBytes(this.depth))
8298
const estimatedTtl = Utils.getStampDuration(BigInt(this.amount), Number(chainState.currentPrice), 5)
8399

src/command/stamp/create.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { BatchId, Duration, Size } from '@ethersphere/bee-js'
22
import { Dates, Numbers } from 'cafe-utility'
3+
import chalk from 'chalk'
34
import { LeafCommand, Option } from 'furious-commander'
5+
import { exit } from 'process'
46
import { isChainStateReady } from '../../utils/chainsync'
57
import { createSpinner } from '../../utils/spinner'
68
import { createKeyValue } from '../../utils/text'
@@ -85,6 +87,18 @@ export class Create extends StampCommand implements LeafCommand {
8587
const estimatedCost = await this.bee.getStorageCost(size, duration)
8688
const { bzzBalance } = await this.bee.getWalletBalance()
8789

90+
if (estimatedCost.gt(bzzBalance)) {
91+
this.console.error('You do not have enough BZZ to create this postage stamp.')
92+
this.console.error(`Estimated cost: ${estimatedCost.toDecimalString()} xBZZ`)
93+
this.console.error(`Available balance: ${bzzBalance.toDecimalString()} xBZZ`)
94+
95+
this.console.log('')
96+
this.console.log('Visit the following link to learn how to fund your Bee node:')
97+
this.console.log(chalk.blue('https://docs.ethswarm.org/docs/bee/installation/fund-your-node/'))
98+
99+
exit(1)
100+
}
101+
88102
this.console.log('')
89103
this.console.log(createKeyValue('Cost', `${estimatedCost.toDecimalString()} xBZZ`))
90104
this.console.log(createKeyValue('Available', `${bzzBalance.toDecimalString()} xBZZ`))

0 commit comments

Comments
 (0)