Skip to content

Commit 3396c31

Browse files
author
Gergely Békési
committed
feat: add function for getting a specific global batch
1 parent 17354e4 commit 3396c31

6 files changed

Lines changed: 66 additions & 4 deletions

File tree

package-lock.json

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
"debug": "^4.4.1",
7070
"isomorphic-ws": "^4.0.1",
7171
"semver": "^7.3.5",
72-
"ws": "^8.20.1"
72+
"ws": "^8.20.1",
73+
"zod": "^4.4.3"
7374
},
7475
"devDependencies": {
7576
"@babel/cli": "^7.19.3",

src/bee.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,9 +2076,9 @@ export class Bee {
20762076
* to buying storage for a certain size and duration on the Swarm network.
20772077
*
20782078
* Use {@link getStorageCost} to calculate the cost of creating a postage batch.
2079-
*
2079+
*
20802080
* For the low level API, use {@link createPostageBatch}.
2081-
*
2081+
*
20822082
* @example const batchId = await bee.buyStorage(Size.fromGigabytes(8), Duration.fromDays(31))
20832083
20842084
* @param size
@@ -2492,6 +2492,21 @@ export class Bee {
24922492
)
24932493
}
24942494

2495+
/**
2496+
* Returns details for specific globally available postage batch.
2497+
*
2498+
* @param postageBatchId Batch ID
2499+
* @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
2500+
*/
2501+
async getGlobalPostageBatch(
2502+
postageBatchId: BatchId | Uint8Array | string,
2503+
requestOptions?: BeeRequestOptions,
2504+
): Promise<GlobalPostageBatch> {
2505+
postageBatchId = new BatchId(postageBatchId)
2506+
2507+
return stamps.getGlobalPostageBatch(this.getRequestOptionsForCall(requestOptions), postageBatchId)
2508+
}
2509+
24952510
/**
24962511
* Return detailed information related to buckets for specific postage batch.
24972512
*

src/modules/debug/stamps.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
PostageBatchOptions,
99
RedundancyLevel,
1010
} from '../../types'
11+
import { GetGlobalPostageBatchResponse } from '../../types/schema'
1112
import { http } from '../../utils/http'
1213
import { mapPostageBatch, RawPostageBatch } from '../../utils/stamps'
1314
import { asNumberString } from '../../utils/type'
@@ -38,6 +39,19 @@ export async function getGlobalPostageBatches(requestOptions: BeeRequestOptions)
3839
}))
3940
}
4041

42+
export async function getGlobalPostageBatch(
43+
requestOptions: BeeRequestOptions,
44+
postageBatchId: BatchId,
45+
): Promise<GlobalPostageBatch> {
46+
const response = await http<unknown>(requestOptions, {
47+
method: 'get',
48+
url: `${BATCHES_ENDPOINT}/${postageBatchId}`,
49+
responseType: 'json',
50+
})
51+
52+
return GetGlobalPostageBatchResponse.parse(response.data)
53+
}
54+
4155
export async function getAllPostageBatches(requestOptions: BeeRequestOptions): Promise<PostageBatch[]> {
4256
const response = await http<unknown>(requestOptions, {
4357
method: 'get',

src/types/schema.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { z } from 'zod'
2+
import { asNumberString } from '../utils/type'
3+
import { BatchId, EthAddress } from '../utils/typed-bytes'
4+
5+
export const GetGlobalPostageBatchResponse = z.object({
6+
batchID: z.string().transform(s => new BatchId(s)),
7+
batchTTL: z.number(),
8+
bucketDepth: z.number(),
9+
depth: z.number(),
10+
immutable: z.boolean(),
11+
owner: z.string().transform(s => new EthAddress(s)),
12+
start: z.number(),
13+
value: z.string().transform(s => asNumberString(s, { name: 'value' })),
14+
})

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)