Skip to content

Commit fda0bd2

Browse files
authored
Add force batch, refactor batch methods (#4486)
* Add force batch, refactor batch methods * Remove any type
1 parent de285e6 commit fda0bd2

3 files changed

Lines changed: 35 additions & 16 deletions

File tree

packages/grid_client/src/modules/utility.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ class Utility {
4646
async batchAll<T>(options: BatchModel<T>): Promise<T[]> {
4747
return await this.client.utility.batchAll(options.extrinsics);
4848
}
49+
50+
/**
51+
* Executes a force batch operation for all provided extrinsics.
52+
* This uses Substrate's `utility.force_batch`, which attempts to
53+
* execute all calls and only reverts the ones that fail.
54+
*
55+
* @param {BatchModel<T>} options - The options for the force batch operation, including the extrinsics to be executed.
56+
* @returns {Promise<T[]>} A promise that resolves with the result of the force batch operation.
57+
*/
58+
@expose
59+
@checkBalance
60+
async forceBatch<T>(options: BatchModel<T>): Promise<T[]> {
61+
// Cast to any until @threefold/tfchain_client Utility type exposes forceBatch in its typings
62+
return await this.client.utility.forceBatch(options.extrinsics);
63+
}
4964
}
5065

5166
export { Utility as utility };

packages/tfchain_client/src/client.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,15 @@ class Client extends QueryClient {
407407
}
408408
return result;
409409
}
410+
410411
async applyAllExtrinsics<T>(extrinsics: ExtrinsicResult<T>[]) {
411412
return this.utility.batchAll<T>(extrinsics);
412413
}
413414

415+
async applyForceBatchExtrinsics<T>(extrinsics: ExtrinsicResult<T>[]) {
416+
return this.utility.forceBatch<T>(extrinsics);
417+
}
418+
414419
patchExtrinsic<R>(extrinsic: Extrinsic, options: PatchExtrinsicOptions<R> = {}): ExtrinsicResult<R> {
415420
const self = this;
416421
(<any>extrinsic).apply = async () => {

packages/tfchain_client/src/utility.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,31 @@ class Utility {
1212

1313
@checkConnection
1414
async batch<T>(extrinsics: ExtrinsicResult<T>[]): Promise<T[]> {
15-
extrinsics = extrinsics.filter(Boolean);
16-
if (extrinsics.length > 0) {
17-
let result: T[] = [];
18-
for (let i = 0; i < extrinsics.length; i += BATCH_SIZE) {
19-
const batch = extrinsics.slice(i, i + BATCH_SIZE);
20-
const { resultSections, resultEvents, map } = this.extractResultSectionsAndEvents(batch);
21-
const batchExtrinsic = await this.client.api.tx.utility.batch(batch);
22-
const res = await this.client.applyExtrinsic<T>(batchExtrinsic, resultSections, resultEvents, map);
23-
result = result.concat(res);
24-
}
25-
26-
return result;
27-
}
28-
return [];
15+
return this.applyBatch(extrinsics, batch => this.client.api.tx.utility.batch(batch));
2916
}
3017

3118
@checkConnection
3219
async batchAll<T>(extrinsics: ExtrinsicResult<T>[]): Promise<T[]> {
20+
return this.applyBatch(extrinsics, batch => this.client.api.tx.utility.batchAll(batch));
21+
}
22+
23+
@checkConnection
24+
async forceBatch<T>(extrinsics: ExtrinsicResult<T>[]): Promise<T[]> {
25+
return this.applyBatch(extrinsics, batch => this.client.api.tx.utility.forceBatch(batch));
26+
}
27+
28+
private async applyBatch<T>(
29+
extrinsics: ExtrinsicResult<T>[],
30+
buildExtrinsic: (batch: ExtrinsicResult<T>[]) => any,
31+
): Promise<T[]> {
3332
extrinsics = extrinsics.filter(Boolean);
3433
if (extrinsics.length > 0) {
3534
let result: T[] = [];
3635
for (let i = 0; i < extrinsics.length; i += BATCH_SIZE) {
3736
const batch = extrinsics.slice(i, i + BATCH_SIZE);
3837
const { resultSections, resultEvents, map } = this.extractResultSectionsAndEvents(batch);
39-
const batchAllExtrinsic = await this.client.api.tx.utility.batchAll(batch);
40-
const res = await this.client.applyExtrinsic<T>(batchAllExtrinsic, resultSections, resultEvents, map);
38+
const extrinsic = await buildExtrinsic(batch);
39+
const res = await this.client.applyExtrinsic<T>(extrinsic, resultSections, resultEvents, map);
4140
result = result.concat(res);
4241
}
4342

0 commit comments

Comments
 (0)