diff --git a/.changeset/pink-deers-fetch.md b/.changeset/pink-deers-fetch.md new file mode 100644 index 00000000000..bdf09c9acc8 --- /dev/null +++ b/.changeset/pink-deers-fetch.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Fix batch fetch util for marketplace-v3 diff --git a/packages/thirdweb/src/extensions/marketplace/getAllInBatches.test.ts b/packages/thirdweb/src/extensions/marketplace/getAllInBatches.test.ts new file mode 100644 index 00000000000..7aad59f4401 --- /dev/null +++ b/packages/thirdweb/src/extensions/marketplace/getAllInBatches.test.ts @@ -0,0 +1,54 @@ +import { describe, expect, it } from "vitest"; +import { getAllInBatches } from "./utils.js"; + +describe("getAllInBatches", () => { + it("should handle range with multiple batches", async () => { + const mockFnCalls: { start: bigint; end: bigint }[] = []; + const mockFn = async (start: bigint, end: bigint) => { + mockFnCalls.push({ start, end }); + return { start, end }; + }; + + const options = { + start: 1n, + end: 20n, + maxSize: 5n, + }; + + const result = await getAllInBatches(mockFn, options); + + expect(mockFnCalls.length).toEqual(4); + expect(mockFnCalls[0]).toEqual({ start: 1n, end: 5n }); + expect(mockFnCalls[1]).toEqual({ start: 6n, end: 10n }); + expect(mockFnCalls[2]).toEqual({ start: 11n, end: 15n }); + expect(mockFnCalls[3]).toEqual({ start: 16n, end: 19n }); + + expect(result).toEqual([ + { start: 1n, end: 5n }, + { start: 6n, end: 10n }, + { start: 11n, end: 15n }, + { start: 16n, end: 19n }, + ]); + }); + + it("should handle single batch", async () => { + const mockFnCalls: { start: bigint; end: bigint }[] = []; + const mockFn = async (start: bigint, end: bigint) => { + mockFnCalls.push({ start, end }); + return { start, end }; + }; + + const options = { + start: 1n, + end: 4n, + maxSize: 10n, + }; + + const result = await getAllInBatches(mockFn, options); + + expect(mockFnCalls.length).toEqual(1); + expect(mockFnCalls[0]).toEqual({ start: 1n, end: 3n }); + + expect(result).toEqual([{ start: 1n, end: 3n }]); + }); +}); diff --git a/packages/thirdweb/src/extensions/marketplace/utils.ts b/packages/thirdweb/src/extensions/marketplace/utils.ts index c7a1efa50c6..309d3fe1ae6 100644 --- a/packages/thirdweb/src/extensions/marketplace/utils.ts +++ b/packages/thirdweb/src/extensions/marketplace/utils.ts @@ -87,7 +87,7 @@ export async function getAllInBatches( let start = options.start; const batches: Promise[] = []; while (options.end - start > options.maxSize) { - batches.push(fn(start, options.end + options.maxSize - 1n)); + batches.push(fn(start, start + options.maxSize - 1n)); start += options.maxSize; } batches.push(fn(start, options.end - 1n));