Skip to content

Commit 00d21a6

Browse files
slapec93Gergely Békési
andauthored
feat: add postage batch label update endpoint (#1193)
* feat: wip add stamp label update * test: finish spec * test: add specs for alias method * test: fix test cases --------- Co-authored-by: Gergely Békési <gergely.bekesi@ethswarm.org>
1 parent 1e1c9db commit 00d21a6

3 files changed

Lines changed: 65 additions & 2 deletions

File tree

src/bee.ts

Lines changed: 34 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
@@ -2374,6 +2374,38 @@ export class Bee {
23742374
return getStampCost(batch.depth, amount)
23752375
}
23762376

2377+
/**
2378+
* Updates the label of a certain postage batch.
2379+
*
2380+
* @param postageBatchId Batch ID of the postage batch to update.
2381+
* @param label New label for the postage batch.
2382+
* @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
2383+
*/
2384+
async updatePostageBatchLabel(
2385+
postageBatchId: BatchId | Uint8Array | string,
2386+
label: string,
2387+
requestOptions?: BeeRequestOptions,
2388+
): Promise<void> {
2389+
postageBatchId = new BatchId(postageBatchId)
2390+
2391+
return stamps.updatePostageBatchLabel(this.getRequestOptionsForCall(requestOptions), postageBatchId, label)
2392+
}
2393+
2394+
/**
2395+
* Renames a storage. This is a convenience method that calls {@link updatePostageBatchLabel}.
2396+
*
2397+
* @param postageBatchId Batch ID of the postage batch to update.
2398+
* @param newName New name for the storage.
2399+
* @param requestOptions Options for making requests, such as timeouts, custom HTTP agents, headers, etc.
2400+
*/
2401+
async renameStorage(
2402+
postageBatchId: BatchId | Uint8Array | string,
2403+
newLabel: string,
2404+
requestOptions?: BeeRequestOptions,
2405+
): Promise<void> {
2406+
return this.updatePostageBatchLabel(postageBatchId, newLabel, requestOptions)
2407+
}
2408+
23772409
/**
23782410
* Calculates the `amount` and expected duration extension for topping up a postage batch with a given BZZ value.
23792411
*

src/modules/debug/stamps.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ export async function createPostageBatch(
122122
return new BatchId(Types.asString(body.batchID, { name: 'batchID' }))
123123
}
124124

125+
export async function updatePostageBatchLabel(
126+
requestOptions: BeeRequestOptions,
127+
id: BatchId,
128+
label: string,
129+
): Promise<void> {
130+
await http<unknown>(requestOptions, {
131+
method: 'patch',
132+
url: `${STAMPS_ENDPOINT}/${id}`,
133+
responseType: 'json',
134+
data: { label },
135+
})
136+
}
137+
125138
export async function topUpBatch(
126139
requestOptions: BeeRequestOptions,
127140
id: BatchId,

test/integration/stamp.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ test('POST stamps rejections', async () => {
5757
await expect(bee.createPostageBatch('500000000', 256)).rejects.toThrow()
5858
})
5959

60+
test('PATCH stamp label (updatePostageBatchLabel)', async () => {
61+
const response = await bee.createPostageBatch('1098006401', 17, { waitForUsable: true })
62+
expect(response.toHex()).toHaveLength(64)
63+
64+
const label = 'test-label'
65+
await expect(bee.updatePostageBatchLabel(response, label)).resolves.not.toThrow()
66+
await expect(bee.getPostageBatch(response)).resolves.toMatchObject({ label })
67+
})
68+
69+
test('PATCH stamp label (renameStorage)', async () => {
70+
const response = await bee.createPostageBatch('1098006401', 17, { waitForUsable: true })
71+
expect(response.toHex()).toHaveLength(64)
72+
73+
const label = 'test-label'
74+
await expect(bee.renameStorage(response, label)).resolves.not.toThrow()
75+
await expect(bee.getPostageBatch(response)).resolves.toMatchObject({ label })
76+
})
77+
6078
test('POST envelope', async () => {
6179
const data = Strings.randomAlphanumeric(40)
6280
const cac = makeContentAddressedChunk(data)

0 commit comments

Comments
 (0)