Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions packages/sdk/src/__tests__/batchDistribution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import type { DistributorClient } from "../DistributorClient";
import {
createBatches,
prepareBatchEqualDistribution,
prepareBatchWeightedDistribution,
} from "../utils/batchDistribution";

const SENDER = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF";
const TOKEN = "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM";
const RECIPIENT_A = "GBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
const RECIPIENT_B = "GCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC";
const RECIPIENT_C = "GDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";

function createMockClient(): DistributorClient {
return {
distributeEqual: vi.fn().mockResolvedValue({ signAndSend: vi.fn() }),
distributeWeighted: vi.fn().mockResolvedValue({ signAndSend: vi.fn() }),
} as unknown as DistributorClient;
}

describe("batchDistribution", () => {
let client: DistributorClient;

beforeEach(() => {
client = createMockClient();
});

describe("createBatches", () => {
it("splits arrays into fixed-size chunks", () => {
expect(createBatches([1, 2, 3], 2)).toEqual([[1, 2], [3]]);
});

it("rejects zero batch size", () => {
expect(() => createBatches([1, 2, 3], 0)).toThrow(
"batchSize must be a positive integer"
);
});

it("rejects negative batch size", () => {
expect(() => createBatches([1, 2, 3], -1)).toThrow(
"batchSize must be a positive integer"
);
});

it("rejects fractional batch size", () => {
expect(() => createBatches([1, 2, 3], 1.5)).toThrow(
"batchSize must be a positive integer"
);
});
});

describe("prepareBatchEqualDistribution", () => {
it.each([0, -1, 1.5])(
"rejects maxRecipientsPerBatch=%s before RPC calls",
async (maxRecipientsPerBatch) => {
await expect(
prepareBatchEqualDistribution(client, {
sender: SENDER,
token: TOKEN,
total_amount: 300n,
recipients: [RECIPIENT_A, RECIPIENT_B],
config: { maxRecipientsPerBatch },
})
).rejects.toThrow(
"config.maxRecipientsPerBatch must be a positive integer"
);

expect(client.distributeEqual).not.toHaveBeenCalled();
}
);
});

describe("prepareBatchWeightedDistribution", () => {
it.each([0, -1, 1.5])(
"rejects maxRecipientsPerBatch=%s before RPC calls",
async (maxRecipientsPerBatch) => {
await expect(
prepareBatchWeightedDistribution(client, {
sender: SENDER,
token: TOKEN,
recipients: [RECIPIENT_A, RECIPIENT_B, RECIPIENT_C],
amounts: [100n, 200n, 300n],
config: { maxRecipientsPerBatch },
})
).rejects.toThrow(
"config.maxRecipientsPerBatch must be a positive integer"
);

expect(client.distributeWeighted).not.toHaveBeenCalled();
}
);
});
});
16 changes: 16 additions & 0 deletions packages/sdk/src/utils/batchDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ export interface BatchDistributionResult {
amountBatches?: bigint[][];
}

function assertPositiveInteger(value: number, name: string): void {
if (!Number.isInteger(value) || value <= 0) {
throw new Error(`${name} must be a positive integer (got ${value})`);
}
}

/**
* Splits recipients into batches and creates assembled transactions for equal distribution.
*
Expand Down Expand Up @@ -197,6 +203,10 @@ export async function prepareBatchEqualDistribution(
): Promise<BatchDistributionResult> {
const { sender, token, total_amount, recipients, config = {} } = params;
const maxRecipientsPerBatch = config.maxRecipientsPerBatch ?? 100;
assertPositiveInteger(
maxRecipientsPerBatch,
'config.maxRecipientsPerBatch'
);

if (recipients.length === 0) {
throw new Error('Recipients array cannot be empty');
Expand Down Expand Up @@ -280,6 +290,10 @@ export async function prepareBatchWeightedDistribution(
): Promise<BatchDistributionResult> {
const { sender, token, recipients, amounts, config = {} } = params;
const maxRecipientsPerBatch = config.maxRecipientsPerBatch ?? 100;
assertPositiveInteger(
maxRecipientsPerBatch,
'config.maxRecipientsPerBatch'
);

if (recipients.length === 0) {
throw new Error('Recipients array cannot be empty');
Expand Down Expand Up @@ -341,6 +355,8 @@ export async function prepareBatchWeightedDistribution(
* \`\`\`
*/
export function createBatches<T>(array: T[], batchSize: number): T[][] {
assertPositiveInteger(batchSize, 'batchSize');

const batches: T[][] = [];

for (let i = 0; i < array.length; i += batchSize) {
Expand Down