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:
Vojtech Simetka
2022-04-29 16:42:29 +05:00
committed by GitHub
parent b6b9914548
commit 1e2face10e
2 changed files with 36 additions and 2 deletions
+27
View File
@@ -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')
}