bee-js
bee-js copied to clipboard
Built-in method to find a usable stamp
Maybe the method could take a criteria callback function which returns the first batch id that matches the criteria, like this example:
import { Bee } from '@ethersphere/bee-js'
const bee = new Bee('http://localhost:1633')
export async function getBatch(bee, criteriaFn) {
const batches = await bee.getAllPostageBatch()
const matchingBatch = batches.find(criteriaFn)
if (!matchingBatch) {
throw new Error('No batch matching criteria found.')
}
return matchingBatch.batchID.toString()
}
function isUsableHasSpace(batch) {
return batch.usable && batch.remainingSize.bytes > 0
}
const batchId = await getBatch(bee, isUsableHasSpace)
console.log(batchId)
And perhaps it could also default to return the first usable stamp with remaining space and TTL?