From eb51dbb090a22d398c13e355de26c229c79d4a6f Mon Sep 17 00:00:00 2001 From: Cafe137 <77121044+Cafe137@users.noreply.github.com> Date: Mon, 4 Jul 2022 15:28:43 +0200 Subject: [PATCH] fix: remove expired stamps (#463) * fix: increase waitUntilStampUsable timeout * fix: wait for stamp to exist after buying --- src/pages/stamps/PostageStampCreation.tsx | 11 +++++++++-- src/providers/Stamps.tsx | 2 +- src/utils/index.ts | 23 ++++++++++++++++------- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/pages/stamps/PostageStampCreation.tsx b/src/pages/stamps/PostageStampCreation.tsx index dc8210d..b29075e 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, + waitUntilStampExists, +} from '../../utils' import { getHumanReadableFileSize } from '../../utils/file' interface FormValues { @@ -97,7 +103,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 batchId = await beeDebugApi.createPostageBatch(amount.toString(), depth, options) + await waitUntilStampExists(batchId, beeDebugApi) actions.resetForm() await refresh() onFinished() diff --git a/src/providers/Stamps.tsx b/src/providers/Stamps.tsx index 0117f22..92d6d88 100644 --- a/src/providers/Stamps.tsx +++ b/src/providers/Stamps.tsx @@ -65,7 +65,7 @@ export function Provider({ children }: Props): ReactElement { setIsLoading(true) const stamps = await beeDebugApi.getAllPostageBatch() - setStamps(stamps.map(enrichStamp)) + setStamps(stamps.filter(x => x.exists).map(enrichStamp)) setLastUpdate(Date.now()) } catch (e) { setError(e as Error) diff --git a/src/utils/index.ts b/src/utils/index.ts index 4660ec8..41e1183 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,8 +1,8 @@ -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' +import { decodeCid } from '@ethersphere/swarm-cid' +import { BigNumber } from 'bignumber.js' +import { BZZ_LINK_DOMAIN } from '../constants' +import { Token } from '../models/Token' /** * Test if value is an integer @@ -229,16 +229,25 @@ export function shortenText(text: string, length = 20, separator = '[…]'): str } const DEFAULT_POLLING_FREQUENCY = 1_000 -const DEFAULT_STAMP_USABLE_TIMEOUT = 120_000 +const DEFAULT_STAMP_USABLE_TIMEOUT = 240_000 interface Options { pollingFrequency?: number timeout?: number } -export async function waitUntilStampUsable( +export function waitUntilStampUsable(batchId: BatchId, beeDebug: BeeDebug, options?: Options): Promise { + return waitForStamp(batchId, beeDebug, 'usable', options) +} + +export function waitUntilStampExists(batchId: BatchId, beeDebug: BeeDebug, options?: Options): Promise { + return waitForStamp(batchId, beeDebug, 'exists', options) +} + +async function waitForStamp( batchId: BatchId, beeDebug: BeeDebug, + field: 'exists' | 'usable', options?: Options, ): Promise { const timeout = options?.timeout || DEFAULT_STAMP_USABLE_TIMEOUT @@ -247,7 +256,7 @@ export async function waitUntilStampUsable( for (let i = 0; i < timeout; i += pollingFrequency) { const stamp = await beeDebug.getPostageBatch(batchId) - if (stamp.usable) return stamp + if (stamp[field]) return stamp await sleepMs(pollingFrequency) }