Skip to content

Commit 5b2f467

Browse files
authored
feat: check chainsync status before creating batch (#614)
1 parent 07b7b7e commit 5b2f467

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/command/stamp/buy.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { BatchId, Utils } from '@ethersphere/bee-js'
22
import { Dates, Numbers } from 'cafe-utility'
33
import { BigNumber } from 'ethers'
44
import { LeafCommand, Option } from 'furious-commander'
5+
import { isChainStateReady } from '../../utils/chainsync'
56
import { createSpinner } from '../../utils/spinner'
67
import { createKeyValue } from '../../utils/text'
78
import { VerbosityLevel } from '../root-command/command-log'
@@ -59,6 +60,14 @@ export class Buy extends StampCommand implements LeafCommand {
5960
public async run(): Promise<void> {
6061
super.init()
6162

63+
if (!(await isChainStateReady(this.bee))) {
64+
this.console.error('Synchronization with the blockchain is not yet complete.')
65+
this.console.error('Please wait until the Bee is fully synced before buying a postage stamp.')
66+
this.console.error('You can check the synchronization status with the "status" command.')
67+
68+
return
69+
}
70+
6271
const chainState = await this.bee.getChainState()
6372
const minimumAmount = BigNumber.from(chainState.currentPrice).mul(17280)
6473

src/command/stamp/create.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BatchId, Duration, Size } from '@ethersphere/bee-js'
22
import { Dates, Numbers } from 'cafe-utility'
33
import { LeafCommand, Option } from 'furious-commander'
4+
import { isChainStateReady } from '../../utils/chainsync'
45
import { createSpinner } from '../../utils/spinner'
56
import { createKeyValue } from '../../utils/text'
67
import { VerbosityLevel } from '../root-command/command-log'
@@ -43,6 +44,14 @@ export class Create extends StampCommand implements LeafCommand {
4344
public async run(): Promise<void> {
4445
super.init()
4546

47+
if (!(await isChainStateReady(this.bee))) {
48+
this.console.error('Synchronization with the blockchain is not yet complete.')
49+
this.console.error('Please wait until the Bee is fully synced before buying a postage stamp.')
50+
this.console.error('You can check the synchronization status with the "status" command.')
51+
52+
return
53+
}
54+
4655
if (!this.capacity) {
4756
this.console.log('Please provide the total capacity of the postage stamp batch')
4857
this.console.log('This represents the total size of data that can be uploaded')

src/utils/chainsync.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Bee } from '@ethersphere/bee-js'
2+
3+
const THRESHOLD = 10_000
4+
5+
export async function isChainStateReady(bee: Bee): Promise<boolean> {
6+
const chainState = await bee.getChainState()
7+
8+
return chainState.chainTip > chainState.block - THRESHOLD
9+
}

0 commit comments

Comments
 (0)