-
Notifications
You must be signed in to change notification settings - Fork 1
/
suite.ts
181 lines (146 loc) · 4.74 KB
/
suite.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { WsProvider, ApiPromise, Keyring } from "@polkadot/api";
import { chunk } from "lodash";
import { of, range } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import { options } from "@acala-network/api";
import { KeyringPair } from "@polkadot/keyring/types";
import { cryptoWaitReady } from "@polkadot/util-crypto";
import { SubmittableExtrinsic } from "@polkadot/api/types";
import { ITuple } from "@polkadot/types/types";
import { DispatchError } from "@polkadot/types/interfaces";
import { Deferred } from "./utils/deferred";
type KeyringPairType = "mnemonic" | "seed" | "uri";
export class Suite {
public api!: ApiPromise;
public sudo!: KeyringPair;
private maxBatchTxNum = 100;
constructor() {
this.sudoWarpper = this.sudoWarpper.bind(this);
this.batchWrapper = this.batchWrapper.bind(this);
this.send = this.send.bind(this);
}
// config
setMaxBatchTxNum(value: number) {
this.maxBatchTxNum = value;
}
async connect(endpoint: string = 'ws://localhost:9944') {
await cryptoWaitReady();
// use alice for the default sudo account
this.importSudo("uri", "//Alice");
const provider = new WsProvider(endpoint);
this.api = await ApiPromise.create(options({ provider }));
const [chain, nodeName, nodeVersion] = await Promise.all([
this.api.rpc.system.chain(),
this.api.rpc.system.name(),
this.api.rpc.system.version()
]);
this.log(`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`);
this.log("connect to acala");
}
async disconnect() {
await this.api.disconnect()
}
async isReady() {
return this.api.isReady;
}
async waitConncetedWrapper(fn: any) {
await this.api.connect;
return fn;
}
async importSudo(type: KeyringPairType, data: string) {
const fnMap: Record<KeyringPairType, string> = {
mnemonic: "addFromMnemonic",
seed: "addFromSeed",
uri: "addFromUri",
};
if (!Object.keys(fnMap).includes(type)) {
console.error(`importSudo error: con't found ${type}`);
}
const keyring = new Keyring({ type: "sr25519" });
this.sudo = (keyring as any)[fnMap[type]](data);
}
sudoWarpper(
tx: SubmittableExtrinsic<"promise">
): SubmittableExtrinsic<"promise"> {
return this.api.tx.sudo.sudo(tx as SubmittableExtrinsic<"promise">);
}
batchWrapper(
txs: SubmittableExtrinsic<"promise">[]
): SubmittableExtrinsic<"promise">[] {
if (txs.length <= this.maxBatchTxNum) {
return [this.api.tx.utility.batch(txs)];
}
return chunk(txs, this.maxBatchTxNum).map(
(data) => this.batchWrapper(data)[0] as SubmittableExtrinsic<"promise">
);
}
send(
account: KeyringPair,
tx: SubmittableExtrinsic<"promise">[]
): Promise<boolean>;
send(
account: KeyringPair,
tx: SubmittableExtrinsic<"promise">
): Promise<boolean>;
send(
account: KeyringPair,
tx: SubmittableExtrinsic<"promise"> | SubmittableExtrinsic<"promise">[]
): Promise<boolean> {
if (Array.isArray(tx)) {
return (async () => {
for (const i of tx) {
const temp = this.singleSend(account, i);
await temp;
}
return true;
})();
}
return this.singleSend(account, tx as SubmittableExtrinsic<"promise">);
}
async batchSend (params: [KeyringPair, SubmittableExtrinsic<"promise">][], concurrent: number = 10) {
return range(0, params.length).pipe(
mergeMap((index) => {
return of(() => this.send.apply(this, params[index]));
}, concurrent)
).toPromise();
}
singleSend(
account: KeyringPair,
tx: SubmittableExtrinsic<"promise">
): Promise<boolean> {
const isFinalize = new Deferred<boolean>();
tx.signAndSend(account, (result) => {
if (result.isInBlock) {
isFinalize.resolve(true);
}
if (result.isError) {
const errorEvent = result.events
.filter((event) => !event)
.filter(({ event: { data, section, method } }) => {
return section === "system" && method === "ExtrinsicFailed";
});
let errorMsg = "";
if (errorEvent.length) {
const [dispatchError] = (errorEvent[0].event
.data as unknown) as ITuple<[DispatchError]>;
if (dispatchError.isModule) {
try {
const mod = dispatchError.asModule;
const error = dispatchError.registry.findMetaError(mod)
errorMsg = `${error.section}.${error.name}`;
} catch (error) {
errorMsg = "unkonwn error";
}
} else {
errorMsg = "unkonwn error";
}
}
isFinalize.reject(errorMsg);
}
});
return isFinalize.promise;
}
log(content: string) {
console.log(content);
}
}