Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a batchSize and applying extrinsics per batch #2728

Merged
merged 9 commits into from
May 27, 2024
27 changes: 21 additions & 6 deletions packages/tfchain_client/src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Client } from "./client";
import { ExtrinsicResult } from "./types";
import { checkConnection } from "./utils";

const BATCH_SIZE = 400;
class Utility {
client: Client;

Expand All @@ -13,9 +14,16 @@ class Utility {
async batch<T>(extrinsics: ExtrinsicResult<T>[]): Promise<T[]> {
extrinsics = extrinsics.filter(Boolean);
if (extrinsics.length > 0) {
const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(extrinsics);
const batchExtrinsic = await this.client.api.tx.utility.batch(extrinsics);
return this.client.applyExtrinsic<T[]>(batchExtrinsic, resultSections, resultEvents);
let result: T[] = [];
for (let i = 0; i < extrinsics.length; i += BATCH_SIZE) {
const batch = extrinsics.slice(i, i + BATCH_SIZE);
const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(batch);
const batchExtrinsic = await this.client.api.tx.utility.batch(batch);
const res = await this.client.applyExtrinsic<T[]>(batchExtrinsic, resultSections, resultEvents);
result = result.concat(res);
}

return result;
}
return [];
}
Expand All @@ -24,9 +32,16 @@ class Utility {
async batchAll<T>(extrinsics: ExtrinsicResult<T>[]): Promise<T[]> {
extrinsics = extrinsics.filter(Boolean);
if (extrinsics.length > 0) {
const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(extrinsics);
const batchAllExtrinsic = await this.client.api.tx.utility.batchAll(extrinsics);
return this.client.applyExtrinsic<T[]>(batchAllExtrinsic, resultSections, resultEvents);
let result: T[] = [];
for (let i = 0; i < extrinsics.length; i += BATCH_SIZE) {
const batch = extrinsics.slice(i, i + BATCH_SIZE);
const { resultSections, resultEvents } = this.extractResultSectionsAndEvents(batch);
const batchAllExtrinsic = await this.client.api.tx.utility.batchAll(batch);
const res = await this.client.applyExtrinsic<T[]>(batchAllExtrinsic, resultSections, resultEvents);
result = result.concat(res);
}

return result;
}
return [];
}
Expand Down
Loading