Skip to content

Commit

Permalink
Fix batch fetch util for marketplace-v3 (#5904)
Browse files Browse the repository at this point in the history
---
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 -->
  • Loading branch information
kumaryash90 committed Jan 7, 2025
1 parent 0fbce06 commit 5e2eec3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/pink-deers-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fix batch fetch util for marketplace-v3
Original file line number Diff line number Diff line change
@@ -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 }]);
});
});
2 changes: 1 addition & 1 deletion packages/thirdweb/src/extensions/marketplace/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export async function getAllInBatches<const T>(
let start = options.start;
const batches: Promise<T>[] = [];
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));
Expand Down

0 comments on commit 5e2eec3

Please sign in to comment.