Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wait for postage stamp to be usable when bying it #352

Merged
merged 2 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/pages/stamps/PostageStampCreation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
27 changes: 27 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
}