bee-js icon indicating copy to clipboard operation
bee-js copied to clipboard

Built-in method to find a usable stamp

Open Cafe137 opened this issue 9 months ago • 1 comments

Cafe137 avatar Mar 24 '25 09:03 Cafe137

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?

NoahMaizels avatar Mar 24 '25 09:03 NoahMaizels