From 1e2face10e93818f281526d8245f84834e5ecb86 Mon Sep 17 00:00:00 2001 From: Vojtech Simetka Date: Fri, 29 Apr 2022 16:42:29 +0500 Subject: [PATCH] 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 --- src/pages/stamps/PostageStampCreation.tsx | 11 +++++++-- src/utils/index.ts | 27 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/pages/stamps/PostageStampCreation.tsx b/src/pages/stamps/PostageStampCreation.tsx index 6347d57..89d2e9e 100644 --- a/src/pages/stamps/PostageStampCreation.tsx +++ b/src/pages/stamps/PostageStampCreation.tsx @@ -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() diff --git a/src/utils/index.ts b/src/utils/index.ts index ded258f..33e17c1 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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 { + 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') +}