Skip to content

Commit 5e2eec3

Browse files
committed
Fix batch fetch util for marketplace-v3 (#5904)
--- title: "[SDK/Dashboard/Portal] Feature/Fix: Concise title for the changes" --- CORE-680 ## Notes for the reviewer Anything important to call out? Be sure to also clarify these in your comments. ## How to test Unit tests, playground, etc. <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on fixing the batch fetching utility in the `marketplace-v3` and adding tests to ensure its functionality. ### Detailed summary - Fixed the calculation of the end index in the batch fetching logic in `packages/thirdweb/src/extensions/marketplace/utils.ts`. - Added tests for `getAllInBatches` in `packages/thirdweb/src/extensions/marketplace/getAllInBatches.test.ts` to validate handling of multiple and single batches. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 0fbce06 commit 5e2eec3

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

.changeset/pink-deers-fetch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fix batch fetch util for marketplace-v3
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { describe, expect, it } from "vitest";
2+
import { getAllInBatches } from "./utils.js";
3+
4+
describe("getAllInBatches", () => {
5+
it("should handle range with multiple batches", async () => {
6+
const mockFnCalls: { start: bigint; end: bigint }[] = [];
7+
const mockFn = async (start: bigint, end: bigint) => {
8+
mockFnCalls.push({ start, end });
9+
return { start, end };
10+
};
11+
12+
const options = {
13+
start: 1n,
14+
end: 20n,
15+
maxSize: 5n,
16+
};
17+
18+
const result = await getAllInBatches(mockFn, options);
19+
20+
expect(mockFnCalls.length).toEqual(4);
21+
expect(mockFnCalls[0]).toEqual({ start: 1n, end: 5n });
22+
expect(mockFnCalls[1]).toEqual({ start: 6n, end: 10n });
23+
expect(mockFnCalls[2]).toEqual({ start: 11n, end: 15n });
24+
expect(mockFnCalls[3]).toEqual({ start: 16n, end: 19n });
25+
26+
expect(result).toEqual([
27+
{ start: 1n, end: 5n },
28+
{ start: 6n, end: 10n },
29+
{ start: 11n, end: 15n },
30+
{ start: 16n, end: 19n },
31+
]);
32+
});
33+
34+
it("should handle single batch", async () => {
35+
const mockFnCalls: { start: bigint; end: bigint }[] = [];
36+
const mockFn = async (start: bigint, end: bigint) => {
37+
mockFnCalls.push({ start, end });
38+
return { start, end };
39+
};
40+
41+
const options = {
42+
start: 1n,
43+
end: 4n,
44+
maxSize: 10n,
45+
};
46+
47+
const result = await getAllInBatches(mockFn, options);
48+
49+
expect(mockFnCalls.length).toEqual(1);
50+
expect(mockFnCalls[0]).toEqual({ start: 1n, end: 3n });
51+
52+
expect(result).toEqual([{ start: 1n, end: 3n }]);
53+
});
54+
});

packages/thirdweb/src/extensions/marketplace/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export async function getAllInBatches<const T>(
8787
let start = options.start;
8888
const batches: Promise<T>[] = [];
8989
while (options.end - start > options.maxSize) {
90-
batches.push(fn(start, options.end + options.maxSize - 1n));
90+
batches.push(fn(start, start + options.maxSize - 1n));
9191
start += options.maxSize;
9292
}
9393
batches.push(fn(start, options.end - 1n));

0 commit comments

Comments
 (0)