feat: wait for postage stamp to be usable when bying it (#352)
* feat: wait for postage stamp to be usable when bying it * refactor: simplified the waitUntilStampUsable function
This commit is contained in:
@@ -9,7 +9,13 @@ import { SwarmTextInput } from '../../components/SwarmTextInput'
|
||||
import { Context as BeeContext } from '../../providers/Bee'
|
||||
import { Context as SettingsContext } from '../../providers/Settings'
|
||||
import { Context as StampsContext } from '../../providers/Stamps'
|
||||
import { calculateStampPrice, convertAmountToSeconds, convertDepthToBytes, secondsToTimeString } from '../../utils'
|
||||
import {
|
||||
calculateStampPrice,
|
||||
convertAmountToSeconds,
|
||||
convertDepthToBytes,
|
||||
secondsToTimeString,
|
||||
waitUntilStampUsable,
|
||||
} from '../../utils'
|
||||
import { getHumanReadableFileSize } from '../../utils/file'
|
||||
|
||||
interface FormValues {
|
||||
@@ -82,7 +88,8 @@ export function PostageStampCreation({ onFinished }: Props): ReactElement {
|
||||
const amount = BigInt(values.amount)
|
||||
const depth = Number.parseInt(values.depth)
|
||||
const options = values.label ? { label: values.label } : undefined
|
||||
await beeDebugApi.createPostageBatch(amount.toString(), depth, options)
|
||||
const batch = await beeDebugApi.createPostageBatch(amount.toString(), depth, options)
|
||||
await waitUntilStampUsable(batch, beeDebugApi)
|
||||
actions.resetForm()
|
||||
await refresh()
|
||||
onFinished()
|
||||
|
||||
@@ -2,6 +2,7 @@ import { BigNumber } from 'bignumber.js'
|
||||
import { Token } from '../models/Token'
|
||||
import { decodeCid } from '@ethersphere/swarm-cid'
|
||||
import { BZZ_LINK_DOMAIN } from '../constants'
|
||||
import { BatchId, BeeDebug, PostageBatch } from '@ethersphere/bee-js'
|
||||
|
||||
/**
|
||||
* Test if value is an integer
|
||||
@@ -216,3 +217,29 @@ export function shortenText(text: string, length = 20, separator = '[…]'): str
|
||||
|
||||
return `${text.slice(0, length)}${separator}${text.slice(-length)}`
|
||||
}
|
||||
|
||||
const DEFAULT_POLLING_FREQUENCY = 1_000
|
||||
const DEFAULT_STAMP_USABLE_TIMEOUT = 120_000
|
||||
|
||||
interface Options {
|
||||
pollingFrequency?: number
|
||||
timeout?: number
|
||||
}
|
||||
|
||||
export async function waitUntilStampUsable(
|
||||
batchId: BatchId,
|
||||
beeDebug: BeeDebug,
|
||||
options?: Options,
|
||||
): Promise<PostageBatch> {
|
||||
const timeout = options?.timeout || DEFAULT_STAMP_USABLE_TIMEOUT
|
||||
const pollingFrequency = options?.pollingFrequency || DEFAULT_POLLING_FREQUENCY
|
||||
|
||||
for (let i = 0; i < timeout; i += pollingFrequency) {
|
||||
const stamp = await beeDebug.getPostageBatch(batchId)
|
||||
|
||||
if (stamp.usable) return stamp
|
||||
await sleepMs(pollingFrequency)
|
||||
}
|
||||
|
||||
throw new Error('Wait until stamp usable timeout has been reached')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user