Skip to content

Commit 9404784

Browse files
slapec93Gergely Békési
andauthored
feat: add function for getting a specific global batch (#1196)
* feat: add function for getting a specific global batch * fix: merge issue --------- Co-authored-by: Gergely Békési <gergely.bekesi@ethswarm.org>
1 parent a557c60 commit 9404784

5 files changed

Lines changed: 50 additions & 22 deletions

File tree

src/bee.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,21 @@ export class Bee {
25222522
)
25232523
}
25242524

2525+
/**
2526+
* Returns details for specific globally available postage batch.
2527+
*
2528+
* @param postageBatchId Batch ID
2529+
* @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
2530+
*/
2531+
async getGlobalPostageBatch(
2532+
postageBatchId: BatchId | Uint8Array | string,
2533+
requestOptions?: BeeRequestOptions,
2534+
): Promise<GlobalPostageBatch> {
2535+
postageBatchId = new BatchId(postageBatchId)
2536+
2537+
return stamps.getGlobalPostageBatch(this.getRequestOptionsForCall(requestOptions), postageBatchId)
2538+
}
2539+
25252540
/**
25262541
* Return detailed information related to buckets for specific postage batch.
25272542
*

src/modules/debug/stamps.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import {
1111
BatchIdResponse,
1212
GetAllPostageBatchesResponse,
1313
GetGlobalPostageBatchesResponse,
14+
GetGlobalPostageBatchResponse,
1415
GetPostageBatchBucketsResponse,
1516
GetPostageBatchResponse,
1617
} from '../../types/schema/stamps'
18+
1719
import { http } from '../../utils/http'
1820
import { mapPostageBatch } from '../../utils/stamps'
1921
import { BatchId } from '../../utils/typed-bytes'
@@ -31,6 +33,19 @@ export async function getGlobalPostageBatches(requestOptions: BeeRequestOptions)
3133
return GetGlobalPostageBatchesResponse.parse(response.data).batches
3234
}
3335

36+
export async function getGlobalPostageBatch(
37+
requestOptions: BeeRequestOptions,
38+
postageBatchId: BatchId,
39+
): Promise<GlobalPostageBatch> {
40+
const response = await http<unknown>(requestOptions, {
41+
method: 'get',
42+
url: `${BATCHES_ENDPOINT}/${postageBatchId}`,
43+
responseType: 'json',
44+
})
45+
46+
return GetGlobalPostageBatchResponse.parse(response.data)
47+
}
48+
3449
export async function getAllPostageBatches(requestOptions: BeeRequestOptions): Promise<PostageBatch[]> {
3550
const response = await http<unknown>(requestOptions, {
3651
method: 'get',

src/types/schema.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/types/schema/stamps.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ export const RawPostageBatchSchema = z.object({
1515
batchTTL: z.number(),
1616
})
1717

18+
export const GetGlobalPostageBatchResponse = z.object({
19+
batchID: z.string().transform(s => new BatchId(s)),
20+
batchTTL: z.number(),
21+
bucketDepth: z.number(),
22+
depth: z.number(),
23+
immutable: z.boolean(),
24+
owner: z.string().transform(s => new EthAddress(s)),
25+
start: z.number(),
26+
value: z.string().transform(s => asNumberString(s, { name: 'value' })),
27+
})
28+
1829
export const GetGlobalPostageBatchesResponse = z.object({
19-
batches: z.array(
20-
z.object({
21-
batchID: z.string().transform(s => new BatchId(s)),
22-
batchTTL: z.number(),
23-
bucketDepth: z.number(),
24-
depth: z.number(),
25-
immutable: z.boolean(),
26-
owner: z.string().transform(s => new EthAddress(s)),
27-
start: z.number(),
28-
value: z.string().transform(s => asNumberString(s, { name: 'value' })),
29-
}),
30-
),
30+
batches: z.array(GetGlobalPostageBatchResponse),
3131
})
3232

3333
export const GetAllPostageBatchesResponse = z.object({

test/integration/stamp.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ test('GET batches', async () => {
2020
expect(batches.length).toBeGreaterThan(0)
2121
})
2222

23+
test('GET batch', async () => {
24+
const batches = await bee.getGlobalPostageBatches()
25+
expect(batches.length).toBeGreaterThan(0)
26+
27+
const batch = await bee.getGlobalPostageBatch(batches[0].batchID)
28+
expect(Objects.deepEquals(batch, batches[0])).toBeTruthy()
29+
})
30+
2331
test('POST stamps', async () => {
2432
const response = await bee.createPostageBatch('1098006401', 17, { waitForUsable: true })
2533
expect(response.toHex()).toHaveLength(64)

0 commit comments

Comments
 (0)