-
Notifications
You must be signed in to change notification settings - Fork 8
/
utility.ts
55 lines (49 loc) · 1.99 KB
/
utility.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { TFClient } from "../clients/tf-grid/client";
import { GridClientConfig } from "../config";
import { expose } from "../helpers/expose";
import { BatchModel } from "./models";
import { checkBalance } from "./utils";
interface Extrinsic {
extrinsic: [];
}
class Utility {
client: TFClient;
/**
* Represents a `Utility class` that provides `utility methods` for interacting with the `TFClient`.
* This class includes methods for `batching operations` and `checking balance` before applying extrinsics.
*
* @class Utility
* @param {GridClientConfig} config - The configuration object for initializing the client.
*/
constructor(public config: GridClientConfig) {
this.client = config.tfclient;
}
/**
* Executes a batch operation using the provided options.
* This method first checks the balance to ensure it is sufficient before applying the extrinsics.
*
* @param {BatchModel<T>} options - The options for the batch operation, including the extrinsics to be executed.
* @returns {Promise<T[]>} A promise that resolves with the result of the batch operation.
*/
@expose
@checkBalance
async batch<T>(options: BatchModel<T>): Promise<T[]> {
return await this.client.utility.batch(options.extrinsics);
}
/**
* Executes a batch operation for all provided extrinsics.
* This method first checks the balance to ensure it is sufficient before applying the extrinsics.
*
* if one of the extrinsics fails in the case of using batchAll, all the extrinsics will be rolled back. However,
* the extrinsics won't roll back in the case of using batch.
*
* @param {BatchModel<T>} options - The options for the batch operation, including the extrinsics to be executed.
* @returns {Promise<T[]>} A promise that resolves with the result of the batch operation.
*/
@expose
@checkBalance
async batchAll<T>(options: BatchModel<T>): Promise<T[]> {
return await this.client.utility.batchAll(options.extrinsics);
}
}
export { Utility as utility };