polkadot-api 0.5.5 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +292 -68
- package/dist/index.d.ts +292 -68
- package/dist/index.js +620 -462
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +630 -457
- package/dist/index.mjs.map +1 -1
- package/dist/min/index.d.ts +292 -68
- package/dist/min/index.js +1 -1
- package/dist/min/index.js.map +1 -1
- package/package.json +12 -17
package/dist/index.mjs
CHANGED
|
@@ -1,113 +1,18 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
2
|
var __export = (target, all) => {
|
|
4
3
|
for (var name in all)
|
|
5
4
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
6
5
|
};
|
|
7
|
-
var __publicField = (obj, key, value) => {
|
|
8
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
9
|
-
return value;
|
|
10
|
-
};
|
|
11
6
|
|
|
12
7
|
// src/client.ts
|
|
8
|
+
import { getObservableClient } from "@polkadot-api/observable-client";
|
|
13
9
|
import {
|
|
14
10
|
createClient as createRawClient
|
|
15
11
|
} from "@polkadot-api/substrate-client";
|
|
16
|
-
import { firstValueFrom as
|
|
17
|
-
|
|
18
|
-
// src/constants.ts
|
|
19
|
-
import { firstValueFrom, map as map2 } from "rxjs";
|
|
20
|
-
|
|
21
|
-
// src/runtime.ts
|
|
22
|
-
import { combineLatest, filter, map } from "rxjs";
|
|
23
|
-
var Runtime = class {
|
|
24
|
-
constructor(ctx, checksums) {
|
|
25
|
-
__publicField(this, "_ctx");
|
|
26
|
-
__publicField(this, "_checksums");
|
|
27
|
-
this._ctx = ctx;
|
|
28
|
-
this._checksums = checksums;
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
function getRuntimeContext(runtime) {
|
|
32
|
-
return runtime._ctx;
|
|
33
|
-
}
|
|
34
|
-
function getImportedChecksum(runtime, idx) {
|
|
35
|
-
return runtime._checksums[idx];
|
|
36
|
-
}
|
|
37
|
-
var createRuntime = (ctx, checksums) => new Runtime(ctx, checksums);
|
|
38
|
-
var getRuntimeApi = (checksums, chainHead) => {
|
|
39
|
-
let latestRuntime;
|
|
40
|
-
let resolve = null;
|
|
41
|
-
latestRuntime = new Promise((res) => {
|
|
42
|
-
resolve = res;
|
|
43
|
-
});
|
|
44
|
-
const runtimeWithChecksums$ = combineLatest([chainHead.runtime$, checksums]);
|
|
45
|
-
runtimeWithChecksums$.subscribe(([x, checksums2]) => {
|
|
46
|
-
if (x) {
|
|
47
|
-
const runtime = createRuntime(x, checksums2);
|
|
48
|
-
if (resolve) {
|
|
49
|
-
resolve(runtime);
|
|
50
|
-
resolve = null;
|
|
51
|
-
} else {
|
|
52
|
-
latestRuntime = Promise.resolve(runtime);
|
|
53
|
-
}
|
|
54
|
-
} else if (!resolve) {
|
|
55
|
-
latestRuntime = new Promise((res) => {
|
|
56
|
-
resolve = res;
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
const result = runtimeWithChecksums$.pipe(
|
|
61
|
-
filter(([x]) => Boolean(x)),
|
|
62
|
-
map(([x, checksums2]) => createRuntime(x, checksums2))
|
|
63
|
-
);
|
|
64
|
-
result.latest = () => latestRuntime;
|
|
65
|
-
return result;
|
|
66
|
-
};
|
|
67
|
-
var compatibilityHelper = (runtimeApi, checksumIdx) => (getChecksum) => {
|
|
68
|
-
function isCompatibleSync(runtime) {
|
|
69
|
-
const ctx = getRuntimeContext(runtime);
|
|
70
|
-
const checksum = getImportedChecksum(runtime, checksumIdx);
|
|
71
|
-
return getChecksum(ctx) === checksum;
|
|
72
|
-
}
|
|
73
|
-
const isCompatible = (runtime) => {
|
|
74
|
-
if (runtime) {
|
|
75
|
-
return isCompatibleSync(runtime);
|
|
76
|
-
}
|
|
77
|
-
return runtimeApi.latest().then(isCompatibleSync);
|
|
78
|
-
};
|
|
79
|
-
const waitChecksums = async () => {
|
|
80
|
-
const runtime = await runtimeApi.latest();
|
|
81
|
-
return (ctx) => getChecksum(ctx) === getImportedChecksum(runtime, checksumIdx);
|
|
82
|
-
};
|
|
83
|
-
const compatibleRuntime$ = (chainHead, hash, error) => combineLatest([chainHead.getRuntimeContext$(hash), waitChecksums()]).pipe(
|
|
84
|
-
map(([ctx, isCompatible2]) => {
|
|
85
|
-
if (!isCompatible2(ctx)) {
|
|
86
|
-
throw error();
|
|
87
|
-
}
|
|
88
|
-
return ctx;
|
|
89
|
-
})
|
|
90
|
-
);
|
|
91
|
-
const withCompatibleRuntime = (chainHead, mapper, error) => (source$) => combineLatest([
|
|
92
|
-
source$.pipe(chainHead.withRuntime(mapper)),
|
|
93
|
-
waitChecksums()
|
|
94
|
-
]).pipe(
|
|
95
|
-
map(([[x, ctx], isCompatible2]) => {
|
|
96
|
-
if (!isCompatible2(ctx)) {
|
|
97
|
-
throw error();
|
|
98
|
-
}
|
|
99
|
-
return [x, ctx];
|
|
100
|
-
})
|
|
101
|
-
);
|
|
102
|
-
return {
|
|
103
|
-
isCompatible,
|
|
104
|
-
waitChecksums,
|
|
105
|
-
withCompatibleRuntime,
|
|
106
|
-
compatibleRuntime$
|
|
107
|
-
};
|
|
108
|
-
};
|
|
12
|
+
import { firstValueFrom as firstValueFrom5 } from "rxjs";
|
|
109
13
|
|
|
110
14
|
// src/constants.ts
|
|
15
|
+
import { firstValueFrom, map } from "rxjs";
|
|
111
16
|
var createConstantEntry = (palletName, name, chainHead, compatibilityHelper2) => {
|
|
112
17
|
const { isCompatible, compatibleRuntime$ } = compatibilityHelper2(
|
|
113
18
|
(ctx) => ctx.checksumBuilder.buildConstant(palletName, name)
|
|
@@ -128,11 +33,11 @@ var createConstantEntry = (palletName, name, chainHead, compatibilityHelper2) =>
|
|
|
128
33
|
if (runtime) {
|
|
129
34
|
if (!isCompatible(runtime))
|
|
130
35
|
throw checksumError();
|
|
131
|
-
return getValueWithContext(
|
|
36
|
+
return getValueWithContext(runtime._getCtx());
|
|
132
37
|
}
|
|
133
38
|
return firstValueFrom(
|
|
134
39
|
compatibleRuntime$(chainHead, null, checksumError).pipe(
|
|
135
|
-
|
|
40
|
+
map(getValueWithContext)
|
|
136
41
|
)
|
|
137
42
|
);
|
|
138
43
|
};
|
|
@@ -140,7 +45,7 @@ var createConstantEntry = (palletName, name, chainHead, compatibilityHelper2) =>
|
|
|
140
45
|
};
|
|
141
46
|
|
|
142
47
|
// src/event.ts
|
|
143
|
-
import { firstValueFrom as firstValueFrom2, map as
|
|
48
|
+
import { firstValueFrom as firstValueFrom2, map as map2, mergeMap } from "rxjs";
|
|
144
49
|
|
|
145
50
|
// src/utils/shareLatest.ts
|
|
146
51
|
import { ReplaySubject, share } from "rxjs";
|
|
@@ -183,8 +88,8 @@ function firstValueFromWithSignal(source, signal) {
|
|
|
183
88
|
}
|
|
184
89
|
|
|
185
90
|
// src/utils/concatMapEager.ts
|
|
186
|
-
import { defer, Observable as
|
|
187
|
-
var concatMapEager = (mapper, concurrent = Infinity) => (source$) => new
|
|
91
|
+
import { defer, Observable as Observable3 } from "rxjs";
|
|
92
|
+
var concatMapEager = (mapper, concurrent = Infinity) => (source$) => new Observable3((observer) => {
|
|
188
93
|
let topSubscription;
|
|
189
94
|
const queues = /* @__PURE__ */ new Map();
|
|
190
95
|
const innerSubscriptions = /* @__PURE__ */ new Map();
|
|
@@ -265,8 +170,8 @@ var concatMapEager = (mapper, concurrent = Infinity) => (source$) => new Observa
|
|
|
265
170
|
});
|
|
266
171
|
|
|
267
172
|
// src/utils/raceMap.ts
|
|
268
|
-
import { Observable as
|
|
269
|
-
var raceMap = (mapper, concurrent) => (source$) => new
|
|
173
|
+
import { Observable as Observable4, Subscription as Subscription3 } from "rxjs";
|
|
174
|
+
var raceMap = (mapper, concurrent) => (source$) => new Observable4((observer) => {
|
|
270
175
|
let innerSubscriptions = new Array();
|
|
271
176
|
let isOuterDone = false;
|
|
272
177
|
const createSubscription = (value) => {
|
|
@@ -314,6 +219,30 @@ var raceMap = (mapper, concurrent) => (source$) => new Observable5((observer) =>
|
|
|
314
219
|
};
|
|
315
220
|
});
|
|
316
221
|
|
|
222
|
+
// src/utils/continue-with.ts
|
|
223
|
+
import { Observable as Observable5 } from "rxjs";
|
|
224
|
+
var NOTIN = {};
|
|
225
|
+
var continueWith = (mapper) => (source) => new Observable5((observer) => {
|
|
226
|
+
let latestValue = NOTIN;
|
|
227
|
+
let subscription = source.subscribe({
|
|
228
|
+
next(v) {
|
|
229
|
+
observer.next(latestValue = v);
|
|
230
|
+
},
|
|
231
|
+
error(e) {
|
|
232
|
+
observer.error(e);
|
|
233
|
+
},
|
|
234
|
+
complete() {
|
|
235
|
+
if (latestValue === NOTIN)
|
|
236
|
+
observer.complete();
|
|
237
|
+
else
|
|
238
|
+
subscription = mapper(latestValue).subscribe(observer);
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
return () => {
|
|
242
|
+
subscription.unsubscribe();
|
|
243
|
+
};
|
|
244
|
+
});
|
|
245
|
+
|
|
317
246
|
// src/event.ts
|
|
318
247
|
var createEventEntry = (pallet, name, chainHead, compatibilityHelper2) => {
|
|
319
248
|
const { isCompatible, withCompatibleRuntime } = compatibilityHelper2(
|
|
@@ -324,7 +253,7 @@ var createEventEntry = (pallet, name, chainHead, compatibilityHelper2) => {
|
|
|
324
253
|
withCompatibleRuntime(chainHead, (x) => x.hash, checksumError),
|
|
325
254
|
concatMapEager(
|
|
326
255
|
([block]) => chainHead.eventsAt$(block.hash).pipe(
|
|
327
|
-
|
|
256
|
+
map2((events) => {
|
|
328
257
|
const winners = events.filter(
|
|
329
258
|
(e) => e.event.type === pallet && e.event.value.type === name
|
|
330
259
|
);
|
|
@@ -344,12 +273,108 @@ var createEventEntry = (pallet, name, chainHead, compatibilityHelper2) => {
|
|
|
344
273
|
);
|
|
345
274
|
const watch = (f) => shared$.pipe(mergeMap((x) => f ? x.filter((d) => f(d.payload)) : x));
|
|
346
275
|
const pull = () => firstValueFrom2(shared$);
|
|
347
|
-
const
|
|
348
|
-
return { watch, pull, filter:
|
|
276
|
+
const filter3 = (events) => events.filter((e) => e.type === pallet && e.value.type === name).map((x) => x.value.value);
|
|
277
|
+
return { watch, pull, filter: filter3, isCompatible };
|
|
349
278
|
};
|
|
350
279
|
|
|
351
|
-
// src/
|
|
352
|
-
import {
|
|
280
|
+
// src/runtime.ts
|
|
281
|
+
import {
|
|
282
|
+
ReplaySubject as ReplaySubject2,
|
|
283
|
+
combineLatest,
|
|
284
|
+
connectable,
|
|
285
|
+
filter,
|
|
286
|
+
firstValueFrom as firstValueFrom3,
|
|
287
|
+
map as map3
|
|
288
|
+
} from "rxjs";
|
|
289
|
+
var Runtime = class _Runtime {
|
|
290
|
+
constructor(_ctx, _checksums, _descriptors) {
|
|
291
|
+
this._ctx = _ctx;
|
|
292
|
+
this._checksums = _checksums;
|
|
293
|
+
this._descriptors = _descriptors;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* @access package - Internal implementation detail. Do not use.
|
|
297
|
+
*/
|
|
298
|
+
static _create(ctx, checksums, descriptors) {
|
|
299
|
+
return new _Runtime(ctx, checksums, descriptors);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* @access package - Internal implementation detail. Do not use.
|
|
303
|
+
*/
|
|
304
|
+
_getCtx() {
|
|
305
|
+
return this._ctx;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* @access package - Internal implementation detail. Do not use.
|
|
309
|
+
*/
|
|
310
|
+
_getPalletChecksum(opType, pallet, name) {
|
|
311
|
+
return this._checksums[this._descriptors.pallets[pallet][opType][name]];
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* @access package - Internal implementation detail. Do not use.
|
|
315
|
+
*/
|
|
316
|
+
_getApiChecksum(name, method) {
|
|
317
|
+
return this._checksums[this._descriptors.apis[name][method]];
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
var getRuntimeApi = (checksums, descriptors, chainHead) => {
|
|
321
|
+
const runtimeWithChecksums$ = connectable(
|
|
322
|
+
combineLatest([chainHead.runtime$, checksums, descriptors]).pipe(
|
|
323
|
+
map3(
|
|
324
|
+
([x, checksums2, descriptors2]) => x ? Runtime._create(x, checksums2, descriptors2) : null
|
|
325
|
+
)
|
|
326
|
+
),
|
|
327
|
+
{
|
|
328
|
+
connector: () => new ReplaySubject2(1)
|
|
329
|
+
}
|
|
330
|
+
);
|
|
331
|
+
runtimeWithChecksums$.connect();
|
|
332
|
+
const result = runtimeWithChecksums$.pipe(
|
|
333
|
+
filter((v) => Boolean(v))
|
|
334
|
+
);
|
|
335
|
+
result.latest = () => firstValueFrom3(result);
|
|
336
|
+
return result;
|
|
337
|
+
};
|
|
338
|
+
var compatibilityHelper = (runtimeApi, getDescriptorChecksum) => (getChecksum) => {
|
|
339
|
+
function isCompatibleSync(runtime) {
|
|
340
|
+
return getChecksum(runtime._getCtx()) === getDescriptorChecksum(runtime);
|
|
341
|
+
}
|
|
342
|
+
const isCompatible = (runtime) => {
|
|
343
|
+
if (runtime) {
|
|
344
|
+
return isCompatibleSync(runtime);
|
|
345
|
+
}
|
|
346
|
+
return runtimeApi.latest().then(isCompatibleSync);
|
|
347
|
+
};
|
|
348
|
+
const waitChecksums = async () => {
|
|
349
|
+
const runtime = await runtimeApi.latest();
|
|
350
|
+
return (ctx) => getChecksum(ctx) === getDescriptorChecksum(runtime);
|
|
351
|
+
};
|
|
352
|
+
const compatibleRuntime$ = (chainHead, hash, error) => combineLatest([chainHead.getRuntimeContext$(hash), waitChecksums()]).pipe(
|
|
353
|
+
map3(([ctx, isCompatible2]) => {
|
|
354
|
+
if (!isCompatible2(ctx)) {
|
|
355
|
+
throw error();
|
|
356
|
+
}
|
|
357
|
+
return ctx;
|
|
358
|
+
})
|
|
359
|
+
);
|
|
360
|
+
const withCompatibleRuntime = (chainHead, mapper, error) => (source$) => combineLatest([
|
|
361
|
+
source$.pipe(chainHead.withRuntime(mapper)),
|
|
362
|
+
waitChecksums()
|
|
363
|
+
]).pipe(
|
|
364
|
+
map3(([[x, ctx], isCompatible2]) => {
|
|
365
|
+
if (!isCompatible2(ctx)) {
|
|
366
|
+
throw error();
|
|
367
|
+
}
|
|
368
|
+
return [x, ctx];
|
|
369
|
+
})
|
|
370
|
+
);
|
|
371
|
+
return {
|
|
372
|
+
isCompatible,
|
|
373
|
+
waitChecksums,
|
|
374
|
+
withCompatibleRuntime,
|
|
375
|
+
compatibleRuntime$
|
|
376
|
+
};
|
|
377
|
+
};
|
|
353
378
|
|
|
354
379
|
// src/runtime-call.ts
|
|
355
380
|
import { toHex } from "@polkadot-api/utils";
|
|
@@ -385,6 +410,7 @@ var createRuntimeCallEntry = (api, method, chainHead, compatibilityHelper2) => {
|
|
|
385
410
|
|
|
386
411
|
// src/storage.ts
|
|
387
412
|
import { debounceTime, distinctUntilChanged, map as map5 } from "rxjs";
|
|
413
|
+
import { NotBestBlockError } from "@polkadot-api/observable-client";
|
|
388
414
|
var isOptionalArg2 = (lastArg) => {
|
|
389
415
|
if (typeof lastArg !== "object")
|
|
390
416
|
return false;
|
|
@@ -393,13 +419,19 @@ var isOptionalArg2 = (lastArg) => {
|
|
|
393
419
|
);
|
|
394
420
|
};
|
|
395
421
|
var createStorageEntry = (pallet, name, chainHead, compatibilityHelper2) => {
|
|
422
|
+
const isSystemNumber = pallet === "System" && name === "Number";
|
|
396
423
|
const { isCompatible, waitChecksums, withCompatibleRuntime } = compatibilityHelper2((ctx) => ctx.checksumBuilder.buildStorage(pallet, name));
|
|
397
424
|
const checksumError = () => new Error(`Incompatible runtime entry Storage(${pallet}.${name})`);
|
|
398
425
|
const invalidArgs = (args) => new Error(`Invalid Arguments calling ${pallet}.${name}(${args})`);
|
|
399
426
|
const watchValue = (...args) => {
|
|
400
|
-
const
|
|
401
|
-
const actualArgs =
|
|
402
|
-
|
|
427
|
+
const target = args[args.length - 1];
|
|
428
|
+
const actualArgs = target === "best" || target === "finalized" ? args.slice(0, -1) : args;
|
|
429
|
+
if (isSystemNumber)
|
|
430
|
+
return chainHead.bestBlocks$.pipe(
|
|
431
|
+
map5((blocks) => blocks.at(target === "best" ? 0 : -1).number),
|
|
432
|
+
distinctUntilChanged()
|
|
433
|
+
);
|
|
434
|
+
return chainHead[target === "best" ? "best$" : "finalized$"].pipe(
|
|
403
435
|
debounceTime(0),
|
|
404
436
|
withCompatibleRuntime(chainHead, (x) => x.hash, checksumError),
|
|
405
437
|
raceMap(([block, ctx]) => {
|
|
@@ -417,25 +449,44 @@ var createStorageEntry = (pallet, name, chainHead, compatibilityHelper2) => {
|
|
|
417
449
|
const isLastArgOptional = isOptionalArg2(lastArg);
|
|
418
450
|
const { signal, at: _at } = isLastArgOptional ? lastArg : {};
|
|
419
451
|
const at = _at ?? null;
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
452
|
+
let result$;
|
|
453
|
+
if (isSystemNumber) {
|
|
454
|
+
result$ = chainHead.bestBlocks$.pipe(
|
|
455
|
+
map5((blocks) => {
|
|
456
|
+
if (at === "finalized" || !at)
|
|
457
|
+
return blocks.at(-1);
|
|
458
|
+
if (at === "best")
|
|
459
|
+
return blocks.at(0);
|
|
460
|
+
return blocks.find((block) => block.hash === at);
|
|
461
|
+
}),
|
|
462
|
+
map5((block) => {
|
|
463
|
+
if (!block)
|
|
464
|
+
throw new NotBestBlockError();
|
|
465
|
+
return block.number;
|
|
466
|
+
}),
|
|
467
|
+
distinctUntilChanged()
|
|
468
|
+
);
|
|
469
|
+
} else {
|
|
470
|
+
const isCompatible2 = await waitChecksums();
|
|
471
|
+
result$ = chainHead.storage$(
|
|
472
|
+
at,
|
|
473
|
+
"value",
|
|
474
|
+
(ctx) => {
|
|
475
|
+
if (!isCompatible2(ctx))
|
|
476
|
+
throw checksumError();
|
|
477
|
+
const codecs = ctx.dynamicBuilder.buildStorage(pallet, name);
|
|
478
|
+
const actualArgs = args.length === codecs.len ? args : args.slice(0, -1);
|
|
479
|
+
if (args !== actualArgs && !isLastArgOptional)
|
|
480
|
+
throw invalidArgs(args);
|
|
481
|
+
return codecs.enc(...actualArgs);
|
|
482
|
+
},
|
|
483
|
+
null,
|
|
484
|
+
(data, ctx) => {
|
|
485
|
+
const codecs = ctx.dynamicBuilder.buildStorage(pallet, name);
|
|
486
|
+
return data === null ? codecs.fallback : codecs.dec(data);
|
|
487
|
+
}
|
|
488
|
+
);
|
|
489
|
+
}
|
|
439
490
|
return firstValueFromWithSignal(result$, signal);
|
|
440
491
|
};
|
|
441
492
|
const getEntries = async (...args) => {
|
|
@@ -475,182 +526,49 @@ var createStorageEntry = (pallet, name, chainHead, compatibilityHelper2) => {
|
|
|
475
526
|
return { isCompatible, getValue, getValues, getEntries, watchValue };
|
|
476
527
|
};
|
|
477
528
|
|
|
478
|
-
// src/tx.ts
|
|
529
|
+
// src/tx/tx.ts
|
|
479
530
|
import {
|
|
531
|
+
AccountId,
|
|
480
532
|
Binary,
|
|
481
533
|
Enum,
|
|
482
|
-
Option,
|
|
483
534
|
Tuple,
|
|
484
|
-
compact,
|
|
535
|
+
compact as compact3,
|
|
485
536
|
u128,
|
|
486
|
-
u32
|
|
537
|
+
u32 as u323,
|
|
538
|
+
u8 as u82
|
|
487
539
|
} from "@polkadot-api/substrate-bindings";
|
|
488
|
-
import { mergeUint8, toHex as
|
|
540
|
+
import { fromHex as fromHex5, mergeUint8, toHex as toHex4 } from "@polkadot-api/utils";
|
|
489
541
|
import {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
mergeMap as mergeMap3,
|
|
496
|
-
of,
|
|
497
|
-
startWith,
|
|
498
|
-
take,
|
|
499
|
-
withLatestFrom
|
|
542
|
+
firstValueFrom as firstValueFrom4,
|
|
543
|
+
map as map12,
|
|
544
|
+
mergeMap as mergeMap6,
|
|
545
|
+
take as take3,
|
|
546
|
+
throwError
|
|
500
547
|
} from "rxjs";
|
|
501
|
-
|
|
502
|
-
const events = systemEvents.filter((x) => x.phase.type === "ApplyExtrinsic" && x.phase.value === txIdx).map((x) => x.event);
|
|
503
|
-
const lastEvent = events[events.length - 1];
|
|
504
|
-
const ok = lastEvent.type === "System" && lastEvent.value.type === "ExtrinsicSuccess";
|
|
505
|
-
return { ok, events };
|
|
506
|
-
};
|
|
507
|
-
var getSubmitFns = (chainHead, client) => {
|
|
508
|
-
const tx$ = (tx) => concat(
|
|
509
|
-
chainHead.finalized$.pipe(
|
|
510
|
-
take(1),
|
|
511
|
-
mergeMap3((finalized) => chainHead.validateTx$(tx, finalized.hash)),
|
|
512
|
-
map6((isValid) => {
|
|
513
|
-
if (!isValid)
|
|
514
|
-
throw new Error("Invalid");
|
|
515
|
-
return { type: "broadcasted" };
|
|
516
|
-
})
|
|
517
|
-
),
|
|
518
|
-
new Observable8((observer) => {
|
|
519
|
-
const subscription = chainHead.trackTx$(tx).subscribe(observer);
|
|
520
|
-
subscription.add(
|
|
521
|
-
client.broadcastTx$(tx).subscribe({
|
|
522
|
-
error(e) {
|
|
523
|
-
observer.error(e);
|
|
524
|
-
}
|
|
525
|
-
})
|
|
526
|
-
);
|
|
527
|
-
return subscription;
|
|
528
|
-
})
|
|
529
|
-
);
|
|
530
|
-
const submit$ = (transaction) => tx$(transaction).pipe(
|
|
531
|
-
mergeMap3((result) => {
|
|
532
|
-
return result.type !== "finalized" ? of(result) : chainHead.eventsAt$(result.block.hash).pipe(
|
|
533
|
-
map6((events) => ({
|
|
534
|
-
...result,
|
|
535
|
-
...getTxSuccessFromSystemEvents(
|
|
536
|
-
events,
|
|
537
|
-
Number(result.block.index)
|
|
538
|
-
)
|
|
539
|
-
}))
|
|
540
|
-
);
|
|
541
|
-
})
|
|
542
|
-
);
|
|
543
|
-
const submit = async (transaction) => lastValueFrom(submit$(transaction)).then((x) => {
|
|
544
|
-
if (x.type !== "finalized")
|
|
545
|
-
throw null;
|
|
546
|
-
const result = { ...x };
|
|
547
|
-
delete result.type;
|
|
548
|
-
return result;
|
|
549
|
-
});
|
|
550
|
-
return { submit$, submit };
|
|
551
|
-
};
|
|
552
|
-
var feeDetailsDec = Option(Tuple(u128, u128, u128)).dec;
|
|
553
|
-
var createTxEntry = (pallet, name, assetChecksum, chainHead, submits, signer, compatibilityHelper2) => {
|
|
554
|
-
const { isCompatible, compatibleRuntime$ } = compatibilityHelper2(
|
|
555
|
-
(ctx) => ctx.checksumBuilder.buildCall(pallet, name)
|
|
556
|
-
);
|
|
557
|
-
const checksumError = () => new Error(`Incompatible runtime entry Tx(${pallet}.${name})`);
|
|
558
|
-
const fn = (arg) => {
|
|
559
|
-
const getCallDataWithContext = ({ dynamicBuilder, asset: [assetEnc, assetCheck] }, arg2, hinted = {}) => {
|
|
560
|
-
let returnHinted = hinted;
|
|
561
|
-
if (hinted.asset) {
|
|
562
|
-
if (assetChecksum !== assetCheck)
|
|
563
|
-
throw new Error(`Incompatible runtime asset`);
|
|
564
|
-
returnHinted = { ...hinted, asset: assetEnc(hinted.asset) };
|
|
565
|
-
}
|
|
566
|
-
const { location, codec } = dynamicBuilder.buildCall(pallet, name);
|
|
567
|
-
return {
|
|
568
|
-
callData: Binary.fromBytes(
|
|
569
|
-
mergeUint8(new Uint8Array(location), codec.enc(arg2))
|
|
570
|
-
),
|
|
571
|
-
hinted: returnHinted
|
|
572
|
-
};
|
|
573
|
-
};
|
|
574
|
-
const getCallData$ = (arg2, hinted = {}) => compatibleRuntime$(chainHead, null, checksumError).pipe(
|
|
575
|
-
map6((ctx) => getCallDataWithContext(ctx, arg2, hinted))
|
|
576
|
-
);
|
|
577
|
-
const getEncodedData = (runtime) => {
|
|
578
|
-
if (runtime) {
|
|
579
|
-
if (!isCompatible(runtime)) {
|
|
580
|
-
throw checksumError();
|
|
581
|
-
}
|
|
582
|
-
return getCallDataWithContext(getRuntimeContext(runtime), arg).callData;
|
|
583
|
-
}
|
|
584
|
-
return firstValueFrom3(getCallData$(arg).pipe(map6((x) => x.callData)));
|
|
585
|
-
};
|
|
586
|
-
const sign$ = (from, _hinted) => getCallData$(arg, _hinted).pipe(
|
|
587
|
-
withLatestFrom(chainHead.finalized$),
|
|
588
|
-
take(1),
|
|
589
|
-
mergeMap3(
|
|
590
|
-
([{ callData, hinted }, finalized]) => signer(from, callData.asBytes(), finalized, hinted)
|
|
591
|
-
)
|
|
592
|
-
);
|
|
593
|
-
const sign = (from, _hinted) => firstValueFrom3(sign$(from, _hinted)).then(toHex2);
|
|
594
|
-
const signAndSubmit = (from, _hinted) => sign(from, _hinted).then(submits.submit);
|
|
595
|
-
const signSubmitAndWatch = (from, _hinted) => sign$(from, _hinted).pipe(
|
|
596
|
-
mergeMap3((result) => {
|
|
597
|
-
const tx = toHex2(result);
|
|
598
|
-
return submits.submit$(tx).pipe(startWith({ type: "signed", tx }));
|
|
599
|
-
})
|
|
600
|
-
);
|
|
601
|
-
const getEstimatedFees = async () => {
|
|
602
|
-
const encoded = (await getEncodedData()).asBytes();
|
|
603
|
-
const preLen = encoded.length + 103;
|
|
604
|
-
const len = preLen + compact.enc(preLen).length;
|
|
605
|
-
const args = toHex2(mergeUint8(encoded, u32.enc(len)));
|
|
606
|
-
return firstValueFrom3(
|
|
607
|
-
chainHead.call$(null, "TransactionPaymentCallApi_query_call_fee_details", args).pipe(
|
|
608
|
-
map6((x) => {
|
|
609
|
-
const result = feeDetailsDec(x);
|
|
610
|
-
if (!result)
|
|
611
|
-
throw new Error("Unable to calculate tx fees");
|
|
612
|
-
return result.reduce((a, b) => a + b);
|
|
613
|
-
})
|
|
614
|
-
)
|
|
615
|
-
);
|
|
616
|
-
};
|
|
617
|
-
return {
|
|
618
|
-
getEstimatedFees,
|
|
619
|
-
decodedCall: {
|
|
620
|
-
type: pallet,
|
|
621
|
-
value: Enum(name, arg)
|
|
622
|
-
},
|
|
623
|
-
getEncodedData,
|
|
624
|
-
sign,
|
|
625
|
-
signSubmitAndWatch,
|
|
626
|
-
signAndSubmit
|
|
627
|
-
};
|
|
628
|
-
};
|
|
629
|
-
return Object.assign(fn, { isCompatible });
|
|
630
|
-
};
|
|
548
|
+
import { getPolkadotSigner } from "@polkadot-api/signer";
|
|
631
549
|
|
|
632
|
-
// src/
|
|
633
|
-
import { combineLatest as combineLatest2, mergeMap as mergeMap4, of as of6, take
|
|
550
|
+
// src/tx/create-tx.ts
|
|
551
|
+
import { combineLatest as combineLatest2, mergeMap as mergeMap4, of as of6, take } from "rxjs";
|
|
634
552
|
|
|
635
|
-
// src/signed-extensions/user/ChargeTransactionPayment.ts
|
|
553
|
+
// src/tx/signed-extensions/user/ChargeTransactionPayment.ts
|
|
636
554
|
import { compactBn } from "@polkadot-api/substrate-bindings";
|
|
637
|
-
import { of as
|
|
555
|
+
import { of as of2 } from "rxjs";
|
|
638
556
|
|
|
639
|
-
// src/signed-extensions/utils.ts
|
|
640
|
-
import { map as
|
|
557
|
+
// src/tx/signed-extensions/utils.ts
|
|
558
|
+
import { map as map6, noop as noop2, of } from "rxjs";
|
|
641
559
|
import {
|
|
642
560
|
Storage,
|
|
643
561
|
Twox64Concat,
|
|
644
|
-
u32
|
|
562
|
+
u32
|
|
645
563
|
} from "@polkadot-api/substrate-bindings";
|
|
646
564
|
import { getDynamicBuilder, getLookupFn } from "@polkadot-api/metadata-builders";
|
|
647
565
|
import { fromHex } from "@polkadot-api/utils";
|
|
648
566
|
var empty = new Uint8Array();
|
|
649
567
|
var genesisHashStorageKey = Storage("System")("BlockHash", noop2, [
|
|
650
|
-
|
|
568
|
+
u32,
|
|
651
569
|
Twox64Concat
|
|
652
570
|
]).enc(0);
|
|
653
|
-
var genesisHashFromCtx = (ctx) => ctx.chainHead.storage$(ctx.at, "value", () => genesisHashStorageKey, null).pipe(
|
|
571
|
+
var genesisHashFromCtx = (ctx) => ctx.chainHead.storage$(ctx.at, "value", () => genesisHashStorageKey, null).pipe(map6((result) => fromHex(result)));
|
|
654
572
|
var systemVersionProp$ = (propName, metadata) => {
|
|
655
573
|
const lookupFn = getLookupFn(metadata.lookup);
|
|
656
574
|
const dynamicBuilder = getDynamicBuilder(metadata);
|
|
@@ -662,17 +580,17 @@ var systemVersionProp$ = (propName, metadata) => {
|
|
|
662
580
|
const valueEnc = dynamicBuilder.buildDefinition(
|
|
663
581
|
systemVersion.value[propName].id
|
|
664
582
|
).enc;
|
|
665
|
-
return
|
|
583
|
+
return of(valueEnc(systemVersionDec(constant.value)[propName]));
|
|
666
584
|
};
|
|
667
585
|
|
|
668
|
-
// src/signed-extensions/user/ChargeTransactionPayment.ts
|
|
669
|
-
var ChargeTransactionPayment = (tip) =>
|
|
586
|
+
// src/tx/signed-extensions/user/ChargeTransactionPayment.ts
|
|
587
|
+
var ChargeTransactionPayment = (tip) => of2({
|
|
670
588
|
value: compactBn.enc(tip),
|
|
671
589
|
additionalSigned: empty
|
|
672
590
|
});
|
|
673
591
|
|
|
674
|
-
// src/signed-extensions/user/CheckMortality.ts
|
|
675
|
-
import { map as
|
|
592
|
+
// src/tx/signed-extensions/user/CheckMortality.ts
|
|
593
|
+
import { map as map7, of as of3 } from "rxjs";
|
|
676
594
|
import { Bytes, enhanceEncoder, u16 } from "@polkadot-api/substrate-bindings";
|
|
677
595
|
import { fromHex as fromHex2 } from "@polkadot-api/utils";
|
|
678
596
|
function trailingZeroes(n) {
|
|
@@ -696,13 +614,13 @@ var zero = new Uint8Array([0]);
|
|
|
696
614
|
var CheckMortality = (input, ctx) => {
|
|
697
615
|
if (!input)
|
|
698
616
|
return genesisHashFromCtx(ctx).pipe(
|
|
699
|
-
|
|
617
|
+
map7((additionalSigned) => ({
|
|
700
618
|
additionalSigned,
|
|
701
619
|
value: zero
|
|
702
620
|
}))
|
|
703
621
|
);
|
|
704
622
|
const { period, blockNumber } = input;
|
|
705
|
-
return
|
|
623
|
+
return of3({
|
|
706
624
|
additionalSigned: fromHex2(ctx.at),
|
|
707
625
|
value: mortal({
|
|
708
626
|
period,
|
|
@@ -711,19 +629,19 @@ var CheckMortality = (input, ctx) => {
|
|
|
711
629
|
});
|
|
712
630
|
};
|
|
713
631
|
|
|
714
|
-
// src/signed-extensions/user/ChargeAssetTxPayment.ts
|
|
715
|
-
import { of as
|
|
632
|
+
// src/tx/signed-extensions/user/ChargeAssetTxPayment.ts
|
|
633
|
+
import { of as of4 } from "rxjs";
|
|
716
634
|
import {
|
|
717
635
|
Bytes as Bytes2,
|
|
718
|
-
Option
|
|
636
|
+
Option,
|
|
719
637
|
Struct,
|
|
720
|
-
compact
|
|
638
|
+
compact
|
|
721
639
|
} from "@polkadot-api/substrate-bindings";
|
|
722
640
|
var encoder = Struct({
|
|
723
|
-
tip:
|
|
724
|
-
asset:
|
|
641
|
+
tip: compact,
|
|
642
|
+
asset: Option(Bytes2(Infinity))
|
|
725
643
|
}).enc;
|
|
726
|
-
var ChargeAssetTxPayment = (tip, asset) =>
|
|
644
|
+
var ChargeAssetTxPayment = (tip, asset) => of4({
|
|
727
645
|
value: encoder({
|
|
728
646
|
tip,
|
|
729
647
|
asset
|
|
@@ -731,231 +649,486 @@ var ChargeAssetTxPayment = (tip, asset) => of5({
|
|
|
731
649
|
additionalSigned: empty
|
|
732
650
|
});
|
|
733
651
|
|
|
734
|
-
// src/signed-extensions/chain/index.ts
|
|
652
|
+
// src/tx/signed-extensions/chain/index.ts
|
|
735
653
|
var chain_exports = {};
|
|
736
654
|
__export(chain_exports, {
|
|
737
655
|
CheckGenesis: () => CheckGenesis,
|
|
738
656
|
CheckNonce: () => CheckNonce,
|
|
739
657
|
CheckSpecVersion: () => CheckSpecVersion,
|
|
740
|
-
CheckTxVersion: () => CheckTxVersion
|
|
658
|
+
CheckTxVersion: () => CheckTxVersion,
|
|
659
|
+
getNonce: () => getNonce
|
|
741
660
|
});
|
|
742
661
|
|
|
743
|
-
// src/signed-extensions/chain/CheckGenesis.ts
|
|
744
|
-
import { map as
|
|
662
|
+
// src/tx/signed-extensions/chain/CheckGenesis.ts
|
|
663
|
+
import { map as map8 } from "rxjs";
|
|
745
664
|
var CheckGenesis = (ctx) => genesisHashFromCtx(ctx).pipe(
|
|
746
|
-
|
|
665
|
+
map8((additionalSigned) => ({ value: empty, additionalSigned }))
|
|
747
666
|
);
|
|
748
667
|
|
|
749
|
-
// src/signed-extensions/chain/CheckNonce.ts
|
|
750
|
-
import {
|
|
751
|
-
import { compact as
|
|
752
|
-
import { fromHex as fromHex3, toHex as
|
|
668
|
+
// src/tx/signed-extensions/chain/CheckNonce.ts
|
|
669
|
+
import { mergeMap as mergeMap3, of as of5 } from "rxjs";
|
|
670
|
+
import { compact as compact2, u16 as u162, u32 as u322, u64, u8 } from "@polkadot-api/substrate-bindings";
|
|
671
|
+
import { fromHex as fromHex3, toHex as toHex2 } from "@polkadot-api/utils";
|
|
672
|
+
var NONCE_RUNTIME_CALL = "AccountNonceApi_account_nonce";
|
|
753
673
|
var lenToDecoder = {
|
|
754
674
|
1: u8.dec,
|
|
755
675
|
2: u162.dec,
|
|
756
|
-
4:
|
|
676
|
+
4: u322.dec,
|
|
757
677
|
8: u64.dec
|
|
758
678
|
};
|
|
759
|
-
var
|
|
760
|
-
|
|
679
|
+
var getNonce = (input) => of5({ value: compact2.enc(input), additionalSigned: empty });
|
|
680
|
+
var CheckNonce = (ctx) => ctx.chainHead.call$(ctx.at, NONCE_RUNTIME_CALL, toHex2(ctx.from)).pipe(
|
|
681
|
+
mergeMap3((result) => {
|
|
761
682
|
const bytes = fromHex3(result);
|
|
762
683
|
const decoder = lenToDecoder[bytes.length];
|
|
763
684
|
if (!decoder)
|
|
764
|
-
throw new Error(
|
|
765
|
-
return
|
|
766
|
-
})
|
|
767
|
-
map10((value) => ({ value, additionalSigned: empty }))
|
|
685
|
+
throw new Error(`${NONCE_RUNTIME_CALL} retrieved wrong data`);
|
|
686
|
+
return getNonce(decoder(bytes));
|
|
687
|
+
})
|
|
768
688
|
);
|
|
769
689
|
|
|
770
|
-
// src/signed-extensions/chain/CheckSpecVersion.ts
|
|
771
|
-
import { map as
|
|
690
|
+
// src/tx/signed-extensions/chain/CheckSpecVersion.ts
|
|
691
|
+
import { map as map9 } from "rxjs";
|
|
772
692
|
var CheckSpecVersion = ({ metadata }) => systemVersionProp$("spec_version", metadata).pipe(
|
|
773
|
-
|
|
693
|
+
map9((additionalSigned) => ({ additionalSigned, value: empty }))
|
|
774
694
|
);
|
|
775
695
|
|
|
776
|
-
// src/signed-extensions/chain/CheckTxVersion.ts
|
|
777
|
-
import { map as
|
|
696
|
+
// src/tx/signed-extensions/chain/CheckTxVersion.ts
|
|
697
|
+
import { map as map10 } from "rxjs";
|
|
778
698
|
var CheckTxVersion = ({ metadata }) => systemVersionProp$("transaction_version", metadata).pipe(
|
|
779
|
-
|
|
699
|
+
map10((additionalSigned) => ({ additionalSigned, value: empty }))
|
|
780
700
|
);
|
|
781
701
|
|
|
782
|
-
// src/
|
|
702
|
+
// src/tx/create-tx.ts
|
|
783
703
|
import { _void } from "@polkadot-api/substrate-bindings";
|
|
784
|
-
var
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
704
|
+
var createTx = (chainHead, signer, callData, atBlock, hinted = {}) => chainHead.getRuntimeContext$(atBlock.hash).pipe(
|
|
705
|
+
take(1),
|
|
706
|
+
mergeMap4((ctx) => {
|
|
707
|
+
const signedExtensionsCtx = {
|
|
708
|
+
metadata: ctx.metadata,
|
|
709
|
+
chainHead,
|
|
710
|
+
callData,
|
|
711
|
+
at: atBlock.hash,
|
|
712
|
+
from: signer.publicKey
|
|
713
|
+
};
|
|
714
|
+
const mortality = !hinted.mortality ? { period: 64, blockNumber: atBlock.number } : hinted.mortality.mortal ? { period: hinted.mortality.period, blockNumber: atBlock.number } : void 0;
|
|
715
|
+
return combineLatest2(
|
|
716
|
+
ctx.metadata.extrinsic.signedExtensions.map(
|
|
717
|
+
({ identifier, type, additionalSigned }) => {
|
|
718
|
+
if (identifier === "CheckMortality")
|
|
719
|
+
return CheckMortality(mortality, signedExtensionsCtx);
|
|
720
|
+
if (identifier === "ChargeTransactionPayment")
|
|
721
|
+
return ChargeTransactionPayment(hinted.tip ?? 0n);
|
|
722
|
+
if (identifier === "ChargeAssetTxPayment")
|
|
723
|
+
return ChargeAssetTxPayment(hinted.tip ?? 0n, hinted.asset);
|
|
724
|
+
if (identifier === "CheckNonce" && "nonce" in hinted)
|
|
725
|
+
return getNonce(hinted.nonce);
|
|
726
|
+
const fn = chain_exports[identifier];
|
|
727
|
+
if (!fn) {
|
|
728
|
+
if (ctx.dynamicBuilder.buildDefinition(type) === _void && ctx.dynamicBuilder.buildDefinition(additionalSigned) === _void)
|
|
729
|
+
return of6({
|
|
730
|
+
value: empty,
|
|
731
|
+
additionalSigned: empty
|
|
732
|
+
});
|
|
733
|
+
throw new Error(`Unsupported signed-extension: ${identifier}`);
|
|
734
|
+
}
|
|
735
|
+
return fn(signedExtensionsCtx);
|
|
736
|
+
}
|
|
737
|
+
)
|
|
738
|
+
).pipe(
|
|
739
|
+
mergeMap4(
|
|
740
|
+
(signedExtensions) => signer.sign(
|
|
792
741
|
callData,
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
({ identifier, type, additionalSigned }) => {
|
|
800
|
-
if (identifier === "CheckMortality")
|
|
801
|
-
return CheckMortality(mortality, signedExtensionsCtx);
|
|
802
|
-
if (identifier === "ChargeTransactionPayment")
|
|
803
|
-
return ChargeTransactionPayment(hinted?.tip ?? 0n);
|
|
804
|
-
if (identifier === "ChargeAssetTxPayment")
|
|
805
|
-
return ChargeAssetTxPayment(hinted?.tip ?? 0n, hinted?.asset);
|
|
806
|
-
const fn = chain_exports[identifier];
|
|
807
|
-
if (!fn) {
|
|
808
|
-
if (ctx.dynamicBuilder.buildDefinition(type) === _void && ctx.dynamicBuilder.buildDefinition(additionalSigned) === _void)
|
|
809
|
-
return of6({
|
|
810
|
-
value: empty,
|
|
811
|
-
additionalSigned: empty
|
|
812
|
-
});
|
|
813
|
-
throw new Error(`Unsupported signed-extension: ${identifier}`);
|
|
814
|
-
}
|
|
815
|
-
return fn(signedExtensionsCtx);
|
|
816
|
-
}
|
|
817
|
-
)
|
|
818
|
-
).pipe(
|
|
819
|
-
mergeMap4(
|
|
820
|
-
(signedExtensions) => signer.sign(
|
|
821
|
-
callData,
|
|
822
|
-
Object.fromEntries(
|
|
823
|
-
ctx.metadata.extrinsic.signedExtensions.map(
|
|
824
|
-
({ identifier }, idx) => [
|
|
825
|
-
identifier,
|
|
826
|
-
{ identifier, ...signedExtensions[idx] }
|
|
827
|
-
]
|
|
828
|
-
)
|
|
829
|
-
),
|
|
830
|
-
ctx.metadataRaw,
|
|
831
|
-
atBlock.number
|
|
742
|
+
Object.fromEntries(
|
|
743
|
+
ctx.metadata.extrinsic.signedExtensions.map(
|
|
744
|
+
({ identifier }, idx) => [
|
|
745
|
+
identifier,
|
|
746
|
+
{ identifier, ...signedExtensions[idx] }
|
|
747
|
+
]
|
|
832
748
|
)
|
|
833
|
-
)
|
|
834
|
-
|
|
835
|
-
|
|
749
|
+
),
|
|
750
|
+
ctx.metadataRaw,
|
|
751
|
+
atBlock.number
|
|
752
|
+
)
|
|
753
|
+
)
|
|
836
754
|
);
|
|
837
|
-
}
|
|
838
|
-
|
|
755
|
+
})
|
|
756
|
+
);
|
|
839
757
|
|
|
840
|
-
// src/
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
758
|
+
// src/tx/submit-fns.ts
|
|
759
|
+
import { Blake2256 } from "@polkadot-api/substrate-bindings";
|
|
760
|
+
import {
|
|
761
|
+
EMPTY,
|
|
762
|
+
Observable as Observable10,
|
|
763
|
+
concat,
|
|
764
|
+
distinctUntilChanged as distinctUntilChanged2,
|
|
765
|
+
filter as filter2,
|
|
766
|
+
lastValueFrom,
|
|
767
|
+
map as map11,
|
|
768
|
+
mergeMap as mergeMap5,
|
|
769
|
+
of as of7,
|
|
770
|
+
take as take2
|
|
771
|
+
} from "rxjs";
|
|
772
|
+
import { fromHex as fromHex4, toHex as toHex3 } from "@polkadot-api/utils";
|
|
773
|
+
var hashFromTx = (tx) => toHex3(Blake2256(fromHex4(tx)));
|
|
774
|
+
var computeState = (analized$, blocks$) => new Observable10((observer) => {
|
|
775
|
+
const analyzedBlocks = /* @__PURE__ */ new Map();
|
|
776
|
+
let pinnedBlocks;
|
|
777
|
+
let latestState;
|
|
778
|
+
const computeNextState = () => {
|
|
779
|
+
let current = pinnedBlocks.best;
|
|
780
|
+
let analyzed = analyzedBlocks.get(current);
|
|
781
|
+
while (!analyzed) {
|
|
782
|
+
const block = pinnedBlocks.blocks.get(current);
|
|
783
|
+
if (!block)
|
|
784
|
+
break;
|
|
785
|
+
analyzed = analyzedBlocks.get(current = block.parent);
|
|
855
786
|
}
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
name,
|
|
865
|
-
descriptors.asset,
|
|
866
|
-
chainHead,
|
|
867
|
-
submitFns,
|
|
868
|
-
createTxFromSigner,
|
|
869
|
-
compatibilityHelper(runtimeApi, txEntries[name])
|
|
870
|
-
);
|
|
787
|
+
if (!analyzed)
|
|
788
|
+
return;
|
|
789
|
+
const isFinalized = pinnedBlocks.blocks.get(analyzed.hash).number <= pinnedBlocks.blocks.get(pinnedBlocks.finalized).number;
|
|
790
|
+
const found = analyzed.found.type;
|
|
791
|
+
if (found && typeof latestState === "object" && latestState.hash === analyzed.hash) {
|
|
792
|
+
if (isFinalized)
|
|
793
|
+
observer.complete();
|
|
794
|
+
return;
|
|
871
795
|
}
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
);
|
|
796
|
+
observer.next(
|
|
797
|
+
latestState = found ? {
|
|
798
|
+
hash: analyzed.hash,
|
|
799
|
+
...analyzed.found
|
|
800
|
+
} : analyzed.found.isValid
|
|
801
|
+
);
|
|
802
|
+
if (isFinalized) {
|
|
803
|
+
if (found)
|
|
804
|
+
observer.complete();
|
|
805
|
+
else if (!analyzed.found.isValid)
|
|
806
|
+
observer.error(new Error("Invalid"));
|
|
884
807
|
}
|
|
885
|
-
}
|
|
886
|
-
const
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
808
|
+
};
|
|
809
|
+
const subscription = blocks$.pipe(
|
|
810
|
+
distinctUntilChanged2(
|
|
811
|
+
(a, b) => a.finalized === b.finalized && a.best === b.best
|
|
812
|
+
)
|
|
813
|
+
).subscribe({
|
|
814
|
+
next: (pinned) => {
|
|
815
|
+
pinnedBlocks = pinned;
|
|
816
|
+
if (analyzedBlocks.size === 0)
|
|
817
|
+
return;
|
|
818
|
+
computeNextState();
|
|
819
|
+
},
|
|
820
|
+
error(e) {
|
|
821
|
+
observer.error(e);
|
|
897
822
|
}
|
|
898
|
-
}
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
823
|
+
});
|
|
824
|
+
subscription.add(
|
|
825
|
+
analized$.subscribe({
|
|
826
|
+
next: (block) => {
|
|
827
|
+
analyzedBlocks.set(block.hash, block);
|
|
828
|
+
computeNextState();
|
|
829
|
+
},
|
|
830
|
+
error(e) {
|
|
831
|
+
observer.error(e);
|
|
832
|
+
}
|
|
833
|
+
})
|
|
834
|
+
);
|
|
835
|
+
return subscription;
|
|
836
|
+
}).pipe(distinctUntilChanged2((a, b) => a === b));
|
|
837
|
+
var getTxSuccessFromSystemEvents = (systemEvents, txIdx) => {
|
|
838
|
+
const events = systemEvents.filter((x) => x.phase.type === "ApplyExtrinsic" && x.phase.value === txIdx).map((x) => x.event);
|
|
839
|
+
const lastEvent = events[events.length - 1];
|
|
840
|
+
const ok = lastEvent.type === "System" && lastEvent.value.type === "ExtrinsicSuccess";
|
|
841
|
+
return { ok, events };
|
|
842
|
+
};
|
|
843
|
+
var submit$ = (chainHead, broadcastTx$, tx, at, emitSign = false) => {
|
|
844
|
+
const txHash = hashFromTx(tx);
|
|
845
|
+
const getTxEvent = (type, rest) => ({
|
|
846
|
+
type,
|
|
847
|
+
txHash,
|
|
848
|
+
...rest
|
|
849
|
+
});
|
|
850
|
+
const at$ = chainHead.pinnedBlocks$.pipe(
|
|
851
|
+
take2(1),
|
|
852
|
+
map11((blocks) => blocks.blocks.get(at)?.hash ?? blocks.finalized)
|
|
853
|
+
);
|
|
854
|
+
const validate$ = at$.pipe(
|
|
855
|
+
mergeMap5(
|
|
856
|
+
(at2) => chainHead.validateTx$(at2, tx).pipe(
|
|
857
|
+
filter2((x) => !x),
|
|
858
|
+
map11(() => {
|
|
859
|
+
throw new Error("Invalid");
|
|
860
|
+
})
|
|
861
|
+
)
|
|
862
|
+
)
|
|
863
|
+
);
|
|
864
|
+
const track$ = new Observable10((observer) => {
|
|
865
|
+
const subscription = chainHead.trackTx$(tx).subscribe(observer);
|
|
866
|
+
subscription.add(
|
|
867
|
+
broadcastTx$(tx).subscribe({
|
|
868
|
+
error(e) {
|
|
869
|
+
observer.error(e);
|
|
870
|
+
}
|
|
871
|
+
})
|
|
872
|
+
);
|
|
873
|
+
return subscription;
|
|
874
|
+
});
|
|
875
|
+
const bestBlockState$ = computeState(track$, chainHead.pinnedBlocks$).pipe(
|
|
876
|
+
map11((x) => {
|
|
877
|
+
if (x === true || x === false)
|
|
878
|
+
return getTxEvent("txBestBlocksState", {
|
|
879
|
+
found: false,
|
|
880
|
+
isValid: x
|
|
881
|
+
});
|
|
882
|
+
return getTxEvent("txBestBlocksState", {
|
|
883
|
+
found: true,
|
|
884
|
+
block: {
|
|
885
|
+
index: x.index,
|
|
886
|
+
hash: x.hash
|
|
887
|
+
},
|
|
888
|
+
...getTxSuccessFromSystemEvents(x.events, x.index)
|
|
889
|
+
});
|
|
890
|
+
})
|
|
891
|
+
);
|
|
892
|
+
return concat(
|
|
893
|
+
emitSign ? of7(getTxEvent("signed", {})) : EMPTY,
|
|
894
|
+
validate$,
|
|
895
|
+
of7(getTxEvent("broadcasted", {})),
|
|
896
|
+
bestBlockState$.pipe(
|
|
897
|
+
continueWith(
|
|
898
|
+
({ found, type, ...rest }) => found ? of7(getTxEvent("finalized", rest)) : EMPTY
|
|
899
|
+
)
|
|
900
|
+
)
|
|
901
|
+
);
|
|
902
|
+
};
|
|
903
|
+
var submit = async (chainHead, broadcastTx$, transaction, at) => lastValueFrom(submit$(chainHead, broadcastTx$, transaction, at)).then((x) => {
|
|
904
|
+
if (x.type !== "finalized")
|
|
905
|
+
throw null;
|
|
906
|
+
const result = { ...x };
|
|
907
|
+
delete result.type;
|
|
908
|
+
return result;
|
|
909
|
+
});
|
|
910
|
+
|
|
911
|
+
// src/tx/tx.ts
|
|
912
|
+
var accountIdEnc = AccountId().enc;
|
|
913
|
+
var queryInfoRawDec = Tuple(compact3, compact3, u82, u128).dec;
|
|
914
|
+
var queryInfoDec = (input) => queryInfoRawDec(input)[3];
|
|
915
|
+
var fakeSignature = new Uint8Array(64);
|
|
916
|
+
var getFakeSignature = () => fakeSignature;
|
|
917
|
+
var createTxEntry = (pallet, name, assetChecksum, chainHead, broadcast, compatibilityHelper2) => {
|
|
918
|
+
const { isCompatible, compatibleRuntime$ } = compatibilityHelper2(
|
|
919
|
+
(ctx) => ctx.checksumBuilder.buildCall(pallet, name)
|
|
920
|
+
);
|
|
921
|
+
const checksumError = () => new Error(`Incompatible runtime entry Tx(${pallet}.${name})`);
|
|
922
|
+
const fn = (arg) => {
|
|
923
|
+
const getCallDataWithContext = ({ dynamicBuilder, asset: [assetEnc, assetCheck] }, arg2, txOptions = {}) => {
|
|
924
|
+
let returnOptions = txOptions;
|
|
925
|
+
if (txOptions.asset) {
|
|
926
|
+
if (assetChecksum !== assetCheck)
|
|
927
|
+
throw new Error(`Incompatible runtime asset`);
|
|
928
|
+
returnOptions = { ...txOptions, asset: assetEnc(txOptions.asset) };
|
|
929
|
+
}
|
|
930
|
+
const { location, codec } = dynamicBuilder.buildCall(pallet, name);
|
|
931
|
+
return {
|
|
932
|
+
callData: Binary.fromBytes(
|
|
933
|
+
mergeUint8(new Uint8Array(location), codec.enc(arg2))
|
|
934
|
+
),
|
|
935
|
+
options: returnOptions
|
|
936
|
+
};
|
|
937
|
+
};
|
|
938
|
+
const getCallData$ = (arg2, options = {}) => compatibleRuntime$(chainHead, null, checksumError).pipe(
|
|
939
|
+
map12((ctx) => getCallDataWithContext(ctx, arg2, options))
|
|
940
|
+
);
|
|
941
|
+
const getEncodedData = (runtime) => {
|
|
942
|
+
if (!runtime)
|
|
943
|
+
return firstValueFrom4(getCallData$(arg).pipe(map12((x) => x.callData)));
|
|
944
|
+
if (!isCompatible(runtime))
|
|
945
|
+
throw checksumError();
|
|
946
|
+
return getCallDataWithContext(runtime._getCtx(), arg).callData;
|
|
947
|
+
};
|
|
948
|
+
const sign$ = (from, { ..._options }, atBlock) => getCallData$(arg, _options).pipe(
|
|
949
|
+
mergeMap6(
|
|
950
|
+
({ callData, options }) => createTx(chainHead, from, callData.asBytes(), atBlock, options)
|
|
951
|
+
)
|
|
952
|
+
);
|
|
953
|
+
const _sign = (from, { at, ..._options } = {}) => {
|
|
954
|
+
return (!at || at === "finalized" ? chainHead.finalized$ : at === "best" ? chainHead.best$ : chainHead.bestBlocks$.pipe(
|
|
955
|
+
map12((x) => x.find((b) => b.hash === at))
|
|
956
|
+
)).pipe(
|
|
957
|
+
take3(1),
|
|
958
|
+
mergeMap6(
|
|
959
|
+
(atBlock) => atBlock ? sign$(from, _options, atBlock).pipe(
|
|
960
|
+
map12((signed) => ({
|
|
961
|
+
tx: toHex4(signed),
|
|
962
|
+
block: atBlock
|
|
963
|
+
}))
|
|
964
|
+
) : throwError(() => new Error(`Uknown block ${at}`))
|
|
965
|
+
)
|
|
966
|
+
);
|
|
967
|
+
};
|
|
968
|
+
const sign = (from, options) => firstValueFrom4(_sign(from, options)).then((x) => x.tx);
|
|
969
|
+
const signAndSubmit = (from, _options) => firstValueFrom4(_sign(from, _options)).then(
|
|
970
|
+
({ tx, block }) => submit(chainHead, broadcast, tx, block.hash)
|
|
971
|
+
);
|
|
972
|
+
const signSubmitAndWatch = (from, _options) => _sign(from, _options).pipe(
|
|
973
|
+
mergeMap6(
|
|
974
|
+
({ tx, block }) => submit$(chainHead, broadcast, tx, block.hash, true)
|
|
975
|
+
)
|
|
976
|
+
);
|
|
977
|
+
const getEstimatedFees = async (from, _options) => {
|
|
978
|
+
const fakeSigner = getPolkadotSigner(
|
|
979
|
+
from instanceof Uint8Array ? from : accountIdEnc(from),
|
|
980
|
+
"Sr25519",
|
|
981
|
+
getFakeSignature
|
|
909
982
|
);
|
|
983
|
+
const encoded = fromHex5(await sign(fakeSigner, _options));
|
|
984
|
+
const args = toHex4(mergeUint8(encoded, u323.enc(encoded.length)));
|
|
985
|
+
return firstValueFrom4(
|
|
986
|
+
chainHead.call$(null, "TransactionPaymentApi_query_info", args).pipe(map12(queryInfoDec))
|
|
987
|
+
);
|
|
988
|
+
};
|
|
989
|
+
return {
|
|
990
|
+
getEstimatedFees,
|
|
991
|
+
decodedCall: {
|
|
992
|
+
type: pallet,
|
|
993
|
+
value: Enum(name, arg)
|
|
994
|
+
},
|
|
995
|
+
getEncodedData,
|
|
996
|
+
sign,
|
|
997
|
+
signSubmitAndWatch,
|
|
998
|
+
signAndSubmit
|
|
999
|
+
};
|
|
1000
|
+
};
|
|
1001
|
+
return Object.assign(fn, { isCompatible });
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
// src/client.ts
|
|
1005
|
+
var createTypedApi = (chainDefinition, chainHead, broadcast$) => {
|
|
1006
|
+
const runtime = getRuntimeApi(
|
|
1007
|
+
chainDefinition.checksums,
|
|
1008
|
+
chainDefinition.descriptors,
|
|
1009
|
+
chainHead
|
|
1010
|
+
);
|
|
1011
|
+
const target = {};
|
|
1012
|
+
const createProxy = (propCall) => new Proxy(target, {
|
|
1013
|
+
get(_, prop) {
|
|
1014
|
+
return propCall(prop);
|
|
910
1015
|
}
|
|
911
|
-
}
|
|
1016
|
+
});
|
|
1017
|
+
const createProxyPath = (pathCall) => {
|
|
1018
|
+
const cache = {};
|
|
1019
|
+
return createProxy((a) => {
|
|
1020
|
+
if (!cache[a])
|
|
1021
|
+
cache[a] = {};
|
|
1022
|
+
return createProxy((b) => {
|
|
1023
|
+
if (!cache[a][b])
|
|
1024
|
+
cache[a][b] = pathCall(a, b);
|
|
1025
|
+
return cache[a][b];
|
|
1026
|
+
});
|
|
1027
|
+
});
|
|
1028
|
+
};
|
|
1029
|
+
const query = createProxyPath(
|
|
1030
|
+
(pallet, name) => createStorageEntry(
|
|
1031
|
+
pallet,
|
|
1032
|
+
name,
|
|
1033
|
+
chainHead,
|
|
1034
|
+
compatibilityHelper(
|
|
1035
|
+
runtime,
|
|
1036
|
+
(r) => r._getPalletChecksum(0 /* Storage */, pallet, name)
|
|
1037
|
+
)
|
|
1038
|
+
)
|
|
1039
|
+
);
|
|
1040
|
+
const tx = createProxyPath(
|
|
1041
|
+
(pallet, name) => createTxEntry(
|
|
1042
|
+
pallet,
|
|
1043
|
+
name,
|
|
1044
|
+
chainDefinition.asset,
|
|
1045
|
+
chainHead,
|
|
1046
|
+
broadcast$,
|
|
1047
|
+
compatibilityHelper(
|
|
1048
|
+
runtime,
|
|
1049
|
+
(r) => r._getPalletChecksum(1 /* Tx */, pallet, name)
|
|
1050
|
+
)
|
|
1051
|
+
)
|
|
1052
|
+
);
|
|
1053
|
+
const event = createProxyPath(
|
|
1054
|
+
(pallet, name) => createEventEntry(
|
|
1055
|
+
pallet,
|
|
1056
|
+
name,
|
|
1057
|
+
chainHead,
|
|
1058
|
+
compatibilityHelper(
|
|
1059
|
+
runtime,
|
|
1060
|
+
(r) => r._getPalletChecksum(2 /* Event */, pallet, name)
|
|
1061
|
+
)
|
|
1062
|
+
)
|
|
1063
|
+
);
|
|
1064
|
+
const constants = createProxyPath(
|
|
1065
|
+
(pallet, name) => createConstantEntry(
|
|
1066
|
+
pallet,
|
|
1067
|
+
name,
|
|
1068
|
+
chainHead,
|
|
1069
|
+
compatibilityHelper(
|
|
1070
|
+
runtime,
|
|
1071
|
+
(r) => r._getPalletChecksum(4 /* Const */, pallet, name)
|
|
1072
|
+
)
|
|
1073
|
+
)
|
|
1074
|
+
);
|
|
1075
|
+
const apis = createProxyPath(
|
|
1076
|
+
(api, method) => createRuntimeCallEntry(
|
|
1077
|
+
api,
|
|
1078
|
+
method,
|
|
1079
|
+
chainHead,
|
|
1080
|
+
compatibilityHelper(runtime, (r) => r._getApiChecksum(api, method))
|
|
1081
|
+
)
|
|
1082
|
+
);
|
|
912
1083
|
return {
|
|
913
1084
|
query,
|
|
914
1085
|
tx,
|
|
915
|
-
event
|
|
1086
|
+
event,
|
|
916
1087
|
apis,
|
|
917
1088
|
constants,
|
|
918
|
-
runtime
|
|
1089
|
+
runtime
|
|
919
1090
|
};
|
|
920
1091
|
};
|
|
921
1092
|
function createClient(provider) {
|
|
922
1093
|
const rawClient = createRawClient(provider);
|
|
923
1094
|
const client = getObservableClient(rawClient);
|
|
924
1095
|
const chainHead = client.chainHead$();
|
|
925
|
-
const createTxFromSigner = getCreateTx(chainHead);
|
|
926
|
-
const submitFns = getSubmitFns(chainHead, client);
|
|
927
|
-
const { submit, submit$: submitAndWatch } = submitFns;
|
|
928
1096
|
const { getChainSpecData } = rawClient;
|
|
1097
|
+
const _request = rawClient.request;
|
|
1098
|
+
const { broadcastTx$ } = client;
|
|
929
1099
|
return {
|
|
930
1100
|
getChainSpecData,
|
|
931
1101
|
finalizedBlock$: chainHead.finalized$,
|
|
932
|
-
getFinalizedBlock: () =>
|
|
1102
|
+
getFinalizedBlock: () => firstValueFrom5(chainHead.finalized$),
|
|
933
1103
|
bestBlocks$: chainHead.bestBlocks$,
|
|
934
|
-
getBestBlocks: () =>
|
|
935
|
-
|
|
936
|
-
getBlockBody: (hash) =>
|
|
937
|
-
getBlockHeader: (hash) =>
|
|
938
|
-
submit,
|
|
939
|
-
submitAndWatch,
|
|
940
|
-
getTypedApi: (
|
|
1104
|
+
getBestBlocks: () => firstValueFrom5(chainHead.bestBlocks$),
|
|
1105
|
+
watchBlockBody: chainHead.body$,
|
|
1106
|
+
getBlockBody: (hash) => firstValueFrom5(chainHead.body$(hash)),
|
|
1107
|
+
getBlockHeader: (hash) => firstValueFrom5(chainHead.header$(hash ?? null)),
|
|
1108
|
+
submit: (...args) => submit(chainHead, broadcastTx$, ...args),
|
|
1109
|
+
submitAndWatch: (...args) => submit$(chainHead, broadcastTx$, ...args),
|
|
1110
|
+
getTypedApi: (chainDefinition) => createTypedApi(chainDefinition, chainHead, broadcastTx$),
|
|
941
1111
|
destroy: () => {
|
|
942
1112
|
chainHead.unfollow();
|
|
943
1113
|
client.destroy();
|
|
944
|
-
}
|
|
1114
|
+
},
|
|
1115
|
+
_request
|
|
945
1116
|
};
|
|
946
1117
|
}
|
|
947
1118
|
|
|
948
1119
|
// src/re-exports.ts
|
|
949
1120
|
import {
|
|
950
|
-
AccountId,
|
|
1121
|
+
AccountId as AccountId2,
|
|
951
1122
|
Binary as Binary2,
|
|
1123
|
+
FixedSizeBinary,
|
|
952
1124
|
Enum as Enum2,
|
|
953
1125
|
_Enum
|
|
954
1126
|
} from "@polkadot-api/substrate-bindings";
|
|
955
1127
|
export {
|
|
956
|
-
AccountId,
|
|
1128
|
+
AccountId2 as AccountId,
|
|
957
1129
|
Binary2 as Binary,
|
|
958
1130
|
Enum2 as Enum,
|
|
1131
|
+
FixedSizeBinary,
|
|
959
1132
|
_Enum,
|
|
960
1133
|
createClient
|
|
961
1134
|
};
|