-
Notifications
You must be signed in to change notification settings - Fork 7
/
bundle-polkadot-api-contract.js
1172 lines (1139 loc) · 49.4 KB
/
bundle-polkadot-api-contract.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@polkadot/types'), require('@polkadot/util'), require('@polkadot/api'), require('@polkadot/util-crypto')) :
typeof define === 'function' && define.amd ? define(['exports', '@polkadot/types', '@polkadot/util', '@polkadot/api', '@polkadot/util-crypto'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.polkadotApiContract = {}, global.polkadotTypes, global.polkadotUtil, global.polkadotApi, global.polkadotUtilCrypto));
})(this, (function (exports, types, util, api, utilCrypto) { 'use strict';
const global = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : window;
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
var TypeDefInfo;
(function (TypeDefInfo) {
TypeDefInfo[TypeDefInfo["BTreeMap"] = 0] = "BTreeMap";
TypeDefInfo[TypeDefInfo["BTreeSet"] = 1] = "BTreeSet";
TypeDefInfo[TypeDefInfo["Compact"] = 2] = "Compact";
TypeDefInfo[TypeDefInfo["DoNotConstruct"] = 3] = "DoNotConstruct";
TypeDefInfo[TypeDefInfo["Enum"] = 4] = "Enum";
TypeDefInfo[TypeDefInfo["HashMap"] = 5] = "HashMap";
TypeDefInfo[TypeDefInfo["Int"] = 6] = "Int";
TypeDefInfo[TypeDefInfo["Linkage"] = 7] = "Linkage";
TypeDefInfo[TypeDefInfo["Null"] = 8] = "Null";
TypeDefInfo[TypeDefInfo["Option"] = 9] = "Option";
TypeDefInfo[TypeDefInfo["Plain"] = 10] = "Plain";
TypeDefInfo[TypeDefInfo["Range"] = 11] = "Range";
TypeDefInfo[TypeDefInfo["RangeInclusive"] = 12] = "RangeInclusive";
TypeDefInfo[TypeDefInfo["Result"] = 13] = "Result";
TypeDefInfo[TypeDefInfo["Set"] = 14] = "Set";
TypeDefInfo[TypeDefInfo["Si"] = 15] = "Si";
TypeDefInfo[TypeDefInfo["Struct"] = 16] = "Struct";
TypeDefInfo[TypeDefInfo["Tuple"] = 17] = "Tuple";
TypeDefInfo[TypeDefInfo["UInt"] = 18] = "UInt";
TypeDefInfo[TypeDefInfo["Vec"] = 19] = "Vec";
TypeDefInfo[TypeDefInfo["VecFixed"] = 20] = "VecFixed";
TypeDefInfo[TypeDefInfo["WrapperKeepOpaque"] = 21] = "WrapperKeepOpaque";
TypeDefInfo[TypeDefInfo["WrapperOpaque"] = 22] = "WrapperOpaque";
})(TypeDefInfo || (TypeDefInfo = {}));
function v0ToV1Names(all) {
return all.map((e) => util.objectSpread({}, e, {
name: Array.isArray(e.name)
? e.name
: [e.name]
}));
}
function v0ToV1(registry, v0) {
if (!v0.metadataVersion.length) {
throw new Error('Invalid format for V0 (detected) contract metadata');
}
return registry.createType('ContractMetadataV1', util.objectSpread({}, v0, {
spec: util.objectSpread({}, v0.spec, {
constructors: v0ToV1Names(v0.spec.constructors),
messages: v0ToV1Names(v0.spec.messages)
}),
types: types.convertSiV0toV1(registry, v0.types)
}));
}
const ARG_TYPES = {
ContractConstructorSpec: 'ContractMessageParamSpecV2',
ContractEventSpec: 'ContractEventParamSpecV2',
ContractMessageSpec: 'ContractMessageParamSpecV2'
};
function v1ToV2Label(entry) {
return util.objectSpread({}, entry, {
label: Array.isArray(entry.name)
? entry.name.join('::')
: entry.name
});
}
function v1ToV2Labels(registry, outType, all) {
return all.map((e) => registry.createType(`${outType}V2`, util.objectSpread(v1ToV2Label(e), {
args: e.args.map((a) => registry.createType(ARG_TYPES[outType], v1ToV2Label(a)))
})));
}
function v1ToV2(registry, v1) {
return registry.createType('ContractMetadataV2', util.objectSpread({}, v1, {
spec: util.objectSpread({}, v1.spec, {
constructors: v1ToV2Labels(registry, 'ContractConstructorSpec', v1.spec.constructors),
events: v1ToV2Labels(registry, 'ContractEventSpec', v1.spec.events),
messages: v1ToV2Labels(registry, 'ContractMessageSpec', v1.spec.messages)
})
}));
}
function v2ToV3(registry, v2) {
return registry.createType('ContractMetadataV3', util.objectSpread({}, v2, {
spec: util.objectSpread({}, v2.spec, {
constructors: v2.spec.constructors.map((c) =>
registry.createType('ContractConstructorSpecV3', util.objectSpread({}, c, { payable: true })))
})
}));
}
function v3ToV4(registry, v3) {
return registry.createType('ContractMetadataV4', util.objectSpread({}, v3, {
spec: util.objectSpread({}, v3.spec, {
constructors: v3.spec.constructors.map((c) => registry.createType('ContractConstructorSpecV4', util.objectSpread({}, c))),
messages: v3.spec.messages.map((m) => registry.createType('ContractMessageSpecV3', util.objectSpread({}, m)))
}),
version: registry.createType('Text', '4')
}));
}
const enumVersions = ['V5', 'V4', 'V3', 'V2', 'V1'];
function createConverter(next, step) {
return (registry, input) => next(registry, step(registry, input));
}
function v5ToLatestCompatible(_registry, v5) {
return v5;
}
function v4ToLatestCompatible(_registry, v4) {
return v4;
}
const v3ToLatestCompatible = createConverter(v4ToLatestCompatible, v3ToV4);
const v2ToLatestCompatible = createConverter(v3ToLatestCompatible, v2ToV3);
const v1ToLatestCompatible = createConverter(v2ToLatestCompatible, v1ToV2);
const v0ToLatestCompatible = createConverter(v1ToLatestCompatible, v0ToV1);
const convertVersions = [
['V5', v5ToLatestCompatible],
['V4', v4ToLatestCompatible],
['V3', v3ToLatestCompatible],
['V2', v2ToLatestCompatible],
['V1', v1ToLatestCompatible],
['V0', v0ToLatestCompatible]
];
const l$1 = util.logger('Abi');
const PRIMITIVE_ALWAYS = ['AccountId', 'AccountIndex', 'Address', 'Balance'];
function findMessage(list, messageOrId) {
const message = util.isNumber(messageOrId)
? list[messageOrId]
: util.isString(messageOrId)
? list.find(({ identifier }) => [identifier, util.stringCamelCase(identifier)].includes(messageOrId.toString()))
: messageOrId;
return util.assertReturn(message, () => `Attempted to call an invalid contract interface, ${util.stringify(messageOrId)}`);
}
function getMetadata(registry, json) {
const vx = enumVersions.find((v) => util.isObject(json[v]));
const jsonVersion = json.version;
if (!vx && jsonVersion && !enumVersions.find((v) => v === `V${jsonVersion}`)) {
throw new Error(`Unable to handle version ${jsonVersion}`);
}
const metadata = registry.createType('ContractMetadata', vx
? { [vx]: json[vx] }
: jsonVersion
? { [`V${jsonVersion}`]: json }
: { V0: json });
const converter = convertVersions.find(([v]) => metadata[`is${v}`]);
if (!converter) {
throw new Error(`Unable to convert ABI with version ${metadata.type} to a supported version`);
}
const upgradedMetadata = converter[1](registry, metadata[`as${converter[0]}`]);
return upgradedMetadata;
}
function parseJson(json, chainProperties) {
const registry = new types.TypeRegistry();
const info = registry.createType('ContractProjectInfo', json);
const metadata = getMetadata(registry, json);
const lookup = registry.createType('PortableRegistry', { types: metadata.types }, true);
registry.setLookup(lookup);
if (chainProperties) {
registry.setChainProperties(chainProperties);
}
lookup.types.forEach(({ id }) => lookup.getTypeDef(id));
return [json, registry, metadata, info];
}
function isTypeSpec(value) {
return !!value && value instanceof Map && !util.isUndefined(value.type) && !util.isUndefined(value.displayName);
}
function isOption(value) {
return !!value && value instanceof types.Option;
}
class Abi {
events;
constructors;
info;
json;
messages;
metadata;
registry;
environment = new Map();
constructor(abiJson, chainProperties) {
[this.json, this.registry, this.metadata, this.info] = parseJson(util.isString(abiJson)
? JSON.parse(abiJson)
: abiJson, chainProperties);
this.constructors = this.metadata.spec.constructors.map((spec, index) => this.__internal__createMessage(spec, index, {
isConstructor: true,
isDefault: spec.default.isTrue,
isPayable: spec.payable.isTrue,
returnType: spec.returnType.isSome
? this.registry.lookup.getTypeDef(spec.returnType.unwrap().type)
: null
}));
this.events = this.metadata.spec.events.map((_, index) => this.__internal__createEvent(index));
this.messages = this.metadata.spec.messages.map((spec, index) => this.__internal__createMessage(spec, index, {
isDefault: spec.default.isTrue,
isMutating: spec.mutates.isTrue,
isPayable: spec.payable.isTrue,
returnType: spec.returnType.isSome
? this.registry.lookup.getTypeDef(spec.returnType.unwrap().type)
: null
}));
for (const [key, opt] of this.metadata.spec.environment.entries()) {
if (isOption(opt)) {
if (opt.isSome) {
const value = opt.unwrap();
if (util.isBn(value)) {
this.environment.set(key, value);
}
else if (isTypeSpec(value)) {
this.environment.set(key, this.registry.lookup.getTypeDef(value.type));
}
else {
throw new Error(`Invalid environment definition for ${key}:: Expected either Number or ContractTypeSpec`);
}
}
}
else {
throw new Error(`Expected Option<*> definition for ${key} in ContractEnvironment`);
}
}
}
decodeEvent(record) {
switch (this.metadata.version.toString()) {
case '4':
return this.__internal__decodeEventV4(record);
default:
return this.__internal__decodeEventV5(record);
}
}
__internal__decodeEventV5 = (record) => {
const signatureTopic = record.topics[0];
const data = record.event.data[1];
if (signatureTopic) {
const event = this.events.find((e) => e.signatureTopic !== undefined && e.signatureTopic !== null && e.signatureTopic === signatureTopic.toHex());
if (event) {
return event.fromU8a(data);
}
}
const amountOfTopics = record.topics.length;
const potentialEvents = this.events.filter((e) => {
if (e.signatureTopic !== null && e.signatureTopic !== undefined) {
return false;
}
const amountIndexed = e.args.filter((a) => a.indexed).length;
if (amountIndexed !== amountOfTopics) {
return false;
}
return true;
});
if (potentialEvents.length === 1) {
return potentialEvents[0].fromU8a(data);
}
throw new Error('Unable to determine event');
};
__internal__decodeEventV4 = (record) => {
const data = record.event.data[1];
const index = data[0];
const event = this.events[index];
if (!event) {
throw new Error(`Unable to find event with index ${index}`);
}
return event.fromU8a(data.subarray(1));
};
decodeConstructor(data) {
return this.__internal__decodeMessage('message', this.constructors, data);
}
decodeMessage(data) {
return this.__internal__decodeMessage('message', this.messages, data);
}
findConstructor(constructorOrId) {
return findMessage(this.constructors, constructorOrId);
}
findMessage(messageOrId) {
return findMessage(this.messages, messageOrId);
}
__internal__createArgs = (args, spec) => {
return args.map(({ label, type }, index) => {
try {
if (!util.isObject(type)) {
throw new Error('Invalid type definition found');
}
const displayName = type.displayName.length
? type.displayName[type.displayName.length - 1].toString()
: undefined;
const camelName = util.stringCamelCase(label);
if (displayName && PRIMITIVE_ALWAYS.includes(displayName)) {
return {
name: camelName,
type: {
info: TypeDefInfo.Plain,
type: displayName
}
};
}
const typeDef = this.registry.lookup.getTypeDef(type.type);
return {
name: camelName,
type: displayName && !typeDef.type.startsWith(displayName)
? { displayName, ...typeDef }
: typeDef
};
}
catch (error) {
l$1.error(`Error expanding argument ${index} in ${util.stringify(spec)}`);
throw error;
}
});
};
__internal__createMessageParams = (args, spec) => {
return this.__internal__createArgs(args, spec);
};
__internal__createEventParams = (args, spec) => {
const params = this.__internal__createArgs(args, spec);
return params.map((p, index) => ({ ...p, indexed: args[index].indexed.toPrimitive() }));
};
__internal__createEvent = (index) => {
switch (this.metadata.version.toString()) {
case '4':
return this.__internal__createEventV4(this.metadata.spec.events[index], index);
default:
return this.__internal__createEventV5(this.metadata.spec.events[index], index);
}
};
__internal__createEventV5 = (spec, index) => {
const args = this.__internal__createEventParams(spec.args, spec);
const event = {
args,
docs: spec.docs.map((d) => d.toString()),
fromU8a: (data) => ({
args: this.__internal__decodeArgs(args, data),
event
}),
identifier: [spec.module_path, spec.label].join('::'),
index,
signatureTopic: spec.signature_topic.isSome ? spec.signature_topic.unwrap().toHex() : null
};
return event;
};
__internal__createEventV4 = (spec, index) => {
const args = this.__internal__createEventParams(spec.args, spec);
const event = {
args,
docs: spec.docs.map((d) => d.toString()),
fromU8a: (data) => ({
args: this.__internal__decodeArgs(args, data),
event
}),
identifier: spec.label.toString(),
index
};
return event;
};
__internal__createMessage = (spec, index, add = {}) => {
const args = this.__internal__createMessageParams(spec.args, spec);
const identifier = spec.label.toString();
const message = {
...add,
args,
docs: spec.docs.map((d) => d.toString()),
fromU8a: (data) => ({
args: this.__internal__decodeArgs(args, data),
message
}),
identifier,
index,
isDefault: spec.default.isTrue,
method: util.stringCamelCase(identifier),
path: identifier.split('::').map((s) => util.stringCamelCase(s)),
selector: spec.selector,
toU8a: (params) => this.__internal__encodeMessageArgs(spec, args, params)
};
return message;
};
__internal__decodeArgs = (args, data) => {
let offset = 0;
return args.map(({ type: { lookupName, type } }) => {
const value = this.registry.createType(lookupName || type, data.subarray(offset));
offset += value.encodedLength;
return value;
});
};
__internal__decodeMessage = (type, list, data) => {
const [, trimmed] = util.compactStripLength(data);
const selector = trimmed.subarray(0, 4);
const message = list.find((m) => m.selector.eq(selector));
if (!message) {
throw new Error(`Unable to find ${type} with selector ${util.u8aToHex(selector)}`);
}
return message.fromU8a(trimmed.subarray(4));
};
__internal__encodeMessageArgs = ({ label, selector }, args, data) => {
if (data.length !== args.length) {
throw new Error(`Expected ${args.length} arguments to contract message '${label.toString()}', found ${data.length}`);
}
return util.compactAddLength(util.u8aConcat(this.registry.createType('ContractSelector', selector).toU8a(), ...args.map(({ type: { lookupName, type } }, index) => this.registry.createType(lookupName || type, data[index]).toU8a())));
};
}
const packageInfo = { name: '@polkadot/api-contract', path: (({ url: (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-api-contract.js', document.baseURI).href)) }) && (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-api-contract.js', document.baseURI).href))) ? new URL((typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-api-contract.js', document.baseURI).href))).pathname.substring(0, new URL((typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle-polkadot-api-contract.js', document.baseURI).href))).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '14.3.1' };
function applyOnEvent(result, types, fn) {
if (result.isInBlock || result.isFinalized) {
const records = result.filterRecords('contracts', types);
if (records.length) {
return fn(records);
}
}
return undefined;
}
class Base {
abi;
api;
_decorateMethod;
_isWeightV1;
constructor(api, abi, decorateMethod) {
if (!api || !api.isConnected || !api.tx) {
throw new Error('Your API has not been initialized correctly and is not connected to a chain');
}
else if (!api.tx.contracts || !util.isFunction(api.tx.contracts.instantiateWithCode) || api.tx.contracts.instantiateWithCode.meta.args.length !== 6) {
throw new Error('The runtime does not expose api.tx.contracts.instantiateWithCode with storageDepositLimit');
}
else if (!api.call.contractsApi || !util.isFunction(api.call.contractsApi.call)) {
throw new Error('Your runtime does not expose the api.call.contractsApi.call runtime interfaces');
}
this.abi = abi instanceof Abi
? abi
: new Abi(abi, api.registry.getChainProperties());
this.api = api;
this._decorateMethod = decorateMethod;
this._isWeightV1 = !api.registry.createType('Weight').proofSize;
}
get registry() {
return this.api.registry;
}
}
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
function __values(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
}
function __read(o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
}
function __spreadArray(to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
function isFunction(value) {
return typeof value === 'function';
}
function createErrorClass(createImpl) {
var _super = function (instance) {
Error.call(instance);
instance.stack = new Error().stack;
};
var ctorFunc = createImpl(_super);
ctorFunc.prototype = Object.create(Error.prototype);
ctorFunc.prototype.constructor = ctorFunc;
return ctorFunc;
}
var UnsubscriptionError = createErrorClass(function (_super) {
return function UnsubscriptionErrorImpl(errors) {
_super(this);
this.message = errors
? errors.length + " errors occurred during unsubscription:\n" + errors.map(function (err, i) { return i + 1 + ") " + err.toString(); }).join('\n ')
: '';
this.name = 'UnsubscriptionError';
this.errors = errors;
};
});
function arrRemove(arr, item) {
if (arr) {
var index = arr.indexOf(item);
0 <= index && arr.splice(index, 1);
}
}
var Subscription = (function () {
function Subscription(initialTeardown) {
this.initialTeardown = initialTeardown;
this.closed = false;
this._parentage = null;
this._finalizers = null;
}
Subscription.prototype.unsubscribe = function () {
var e_1, _a, e_2, _b;
var errors;
if (!this.closed) {
this.closed = true;
var _parentage = this._parentage;
if (_parentage) {
this._parentage = null;
if (Array.isArray(_parentage)) {
try {
for (var _parentage_1 = __values(_parentage), _parentage_1_1 = _parentage_1.next(); !_parentage_1_1.done; _parentage_1_1 = _parentage_1.next()) {
var parent_1 = _parentage_1_1.value;
parent_1.remove(this);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_parentage_1_1 && !_parentage_1_1.done && (_a = _parentage_1.return)) _a.call(_parentage_1);
}
finally { if (e_1) throw e_1.error; }
}
}
else {
_parentage.remove(this);
}
}
var initialFinalizer = this.initialTeardown;
if (isFunction(initialFinalizer)) {
try {
initialFinalizer();
}
catch (e) {
errors = e instanceof UnsubscriptionError ? e.errors : [e];
}
}
var _finalizers = this._finalizers;
if (_finalizers) {
this._finalizers = null;
try {
for (var _finalizers_1 = __values(_finalizers), _finalizers_1_1 = _finalizers_1.next(); !_finalizers_1_1.done; _finalizers_1_1 = _finalizers_1.next()) {
var finalizer = _finalizers_1_1.value;
try {
execFinalizer(finalizer);
}
catch (err) {
errors = errors !== null && errors !== void 0 ? errors : [];
if (err instanceof UnsubscriptionError) {
errors = __spreadArray(__spreadArray([], __read(errors)), __read(err.errors));
}
else {
errors.push(err);
}
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_finalizers_1_1 && !_finalizers_1_1.done && (_b = _finalizers_1.return)) _b.call(_finalizers_1);
}
finally { if (e_2) throw e_2.error; }
}
}
if (errors) {
throw new UnsubscriptionError(errors);
}
}
};
Subscription.prototype.add = function (teardown) {
var _a;
if (teardown && teardown !== this) {
if (this.closed) {
execFinalizer(teardown);
}
else {
if (teardown instanceof Subscription) {
if (teardown.closed || teardown._hasParent(this)) {
return;
}
teardown._addParent(this);
}
(this._finalizers = (_a = this._finalizers) !== null && _a !== void 0 ? _a : []).push(teardown);
}
}
};
Subscription.prototype._hasParent = function (parent) {
var _parentage = this._parentage;
return _parentage === parent || (Array.isArray(_parentage) && _parentage.includes(parent));
};
Subscription.prototype._addParent = function (parent) {
var _parentage = this._parentage;
this._parentage = Array.isArray(_parentage) ? (_parentage.push(parent), _parentage) : _parentage ? [_parentage, parent] : parent;
};
Subscription.prototype._removeParent = function (parent) {
var _parentage = this._parentage;
if (_parentage === parent) {
this._parentage = null;
}
else if (Array.isArray(_parentage)) {
arrRemove(_parentage, parent);
}
};
Subscription.prototype.remove = function (teardown) {
var _finalizers = this._finalizers;
_finalizers && arrRemove(_finalizers, teardown);
if (teardown instanceof Subscription) {
teardown._removeParent(this);
}
};
Subscription.EMPTY = (function () {
var empty = new Subscription();
empty.closed = true;
return empty;
})();
return Subscription;
}());
Subscription.EMPTY;
function isSubscription(value) {
return (value instanceof Subscription ||
(value && 'closed' in value && isFunction(value.remove) && isFunction(value.add) && isFunction(value.unsubscribe)));
}
function execFinalizer(finalizer) {
if (isFunction(finalizer)) {
finalizer();
}
else {
finalizer.unsubscribe();
}
}
var config = {
onUnhandledError: null,
onStoppedNotification: null,
Promise: undefined,
useDeprecatedSynchronousErrorHandling: false,
useDeprecatedNextContext: false,
};
var timeoutProvider = {
setTimeout: function (handler, timeout) {
var args = [];
for (var _i = 2; _i < arguments.length; _i++) {
args[_i - 2] = arguments[_i];
}
return setTimeout.apply(void 0, __spreadArray([handler, timeout], __read(args)));
},
clearTimeout: function (handle) {
var delegate = timeoutProvider.delegate;
return ((delegate === null || delegate === void 0 ? void 0 : delegate.clearTimeout) || clearTimeout)(handle);
},
delegate: undefined,
};
function reportUnhandledError(err) {
timeoutProvider.setTimeout(function () {
{
throw err;
}
});
}
function noop() { }
var Subscriber = (function (_super) {
__extends(Subscriber, _super);
function Subscriber(destination) {
var _this = _super.call(this) || this;
_this.isStopped = false;
if (destination) {
_this.destination = destination;
if (isSubscription(destination)) {
destination.add(_this);
}
}
else {
_this.destination = EMPTY_OBSERVER;
}
return _this;
}
Subscriber.create = function (next, error, complete) {
return new SafeSubscriber(next, error, complete);
};
Subscriber.prototype.next = function (value) {
if (this.isStopped) ;
else {
this._next(value);
}
};
Subscriber.prototype.error = function (err) {
if (this.isStopped) ;
else {
this.isStopped = true;
this._error(err);
}
};
Subscriber.prototype.complete = function () {
if (this.isStopped) ;
else {
this.isStopped = true;
this._complete();
}
};
Subscriber.prototype.unsubscribe = function () {
if (!this.closed) {
this.isStopped = true;
_super.prototype.unsubscribe.call(this);
this.destination = null;
}
};
Subscriber.prototype._next = function (value) {
this.destination.next(value);
};
Subscriber.prototype._error = function (err) {
try {
this.destination.error(err);
}
finally {
this.unsubscribe();
}
};
Subscriber.prototype._complete = function () {
try {
this.destination.complete();
}
finally {
this.unsubscribe();
}
};
return Subscriber;
}(Subscription));
var _bind = Function.prototype.bind;
function bind(fn, thisArg) {
return _bind.call(fn, thisArg);
}
var ConsumerObserver = (function () {
function ConsumerObserver(partialObserver) {
this.partialObserver = partialObserver;
}
ConsumerObserver.prototype.next = function (value) {
var partialObserver = this.partialObserver;
if (partialObserver.next) {
try {
partialObserver.next(value);
}
catch (error) {
handleUnhandledError(error);
}
}
};
ConsumerObserver.prototype.error = function (err) {
var partialObserver = this.partialObserver;
if (partialObserver.error) {
try {
partialObserver.error(err);
}
catch (error) {
handleUnhandledError(error);
}
}
else {
handleUnhandledError(err);
}
};
ConsumerObserver.prototype.complete = function () {
var partialObserver = this.partialObserver;
if (partialObserver.complete) {
try {
partialObserver.complete();
}
catch (error) {
handleUnhandledError(error);
}
}
};
return ConsumerObserver;
}());
var SafeSubscriber = (function (_super) {
__extends(SafeSubscriber, _super);
function SafeSubscriber(observerOrNext, error, complete) {
var _this = _super.call(this) || this;
var partialObserver;
if (isFunction(observerOrNext) || !observerOrNext) {
partialObserver = {
next: (observerOrNext !== null && observerOrNext !== void 0 ? observerOrNext : undefined),
error: error !== null && error !== void 0 ? error : undefined,
complete: complete !== null && complete !== void 0 ? complete : undefined,
};
}
else {
var context_1;
if (_this && config.useDeprecatedNextContext) {
context_1 = Object.create(observerOrNext);
context_1.unsubscribe = function () { return _this.unsubscribe(); };
partialObserver = {
next: observerOrNext.next && bind(observerOrNext.next, context_1),
error: observerOrNext.error && bind(observerOrNext.error, context_1),
complete: observerOrNext.complete && bind(observerOrNext.complete, context_1),
};
}
else {
partialObserver = observerOrNext;
}
}
_this.destination = new ConsumerObserver(partialObserver);
return _this;
}
return SafeSubscriber;
}(Subscriber));
function handleUnhandledError(error) {
{
reportUnhandledError(error);
}
}
function defaultErrorHandler(err) {
throw err;
}
var EMPTY_OBSERVER = {
closed: true,
next: noop,
error: defaultErrorHandler,
complete: noop,
};
function hasLift(source) {
return isFunction(source === null || source === void 0 ? void 0 : source.lift);
}
function operate(init) {
return function (source) {
if (hasLift(source)) {
return source.lift(function (liftedSource) {
try {
return init(liftedSource, this);
}
catch (err) {
this.error(err);
}
});
}
throw new TypeError('Unable to lift unknown Observable type');
};
}
function createOperatorSubscriber(destination, onNext, onComplete, onError, onFinalize) {
return new OperatorSubscriber(destination, onNext, onComplete, onError, onFinalize);
}
var OperatorSubscriber = (function (_super) {
__extends(OperatorSubscriber, _super);
function OperatorSubscriber(destination, onNext, onComplete, onError, onFinalize, shouldUnsubscribe) {
var _this = _super.call(this, destination) || this;
_this.onFinalize = onFinalize;
_this.shouldUnsubscribe = shouldUnsubscribe;
_this._next = onNext
? function (value) {
try {
onNext(value);
}
catch (err) {
destination.error(err);
}
}
: _super.prototype._next;
_this._error = onError
? function (err) {
try {
onError(err);
}
catch (err) {
destination.error(err);
}
finally {
this.unsubscribe();
}
}
: _super.prototype._error;
_this._complete = onComplete
? function () {
try {
onComplete();
}
catch (err) {
destination.error(err);
}
finally {
this.unsubscribe();
}
}
: _super.prototype._complete;
return _this;
}
OperatorSubscriber.prototype.unsubscribe = function () {
var _a;
if (!this.shouldUnsubscribe || this.shouldUnsubscribe()) {
var closed_1 = this.closed;
_super.prototype.unsubscribe.call(this);
!closed_1 && ((_a = this.onFinalize) === null || _a === void 0 ? void 0 : _a.call(this));
}
};
return OperatorSubscriber;
}(Subscriber));
function map(project, thisArg) {
return operate(function (source, subscriber) {
var index = 0;
source.subscribe(createOperatorSubscriber(subscriber, function (value) {
subscriber.next(project.call(thisArg, value, index++));
}));
});
}
const EMPTY_SALT = new Uint8Array();
function withMeta(meta, creator) {
creator.meta = meta;
return creator;
}
function createBluePrintTx(meta, fn) {
return withMeta(meta, (options, ...params) => fn(options, params));
}
function encodeSalt(salt = utilCrypto.randomAsU8a()) {
return salt instanceof types.Bytes
? salt
: salt?.length
? util.compactAddLength(util.u8aToU8a(salt))
: EMPTY_SALT;
}
function convertWeight(weight) {
const [refTime, proofSize] = isWeightV2(weight)
? [weight.refTime.toBn(), weight.proofSize.toBn()]
: [util.bnToBn(weight), undefined];
return {
v1Weight: refTime,
v2Weight: { proofSize, refTime }
};
}
function isWeightV2(weight) {
return !!weight.proofSize;
}
const MAX_CALL_GAS = new util.BN(5_000_000_000_000).isub(util.BN_ONE);
const l = util.logger('Contract');
function createQuery(meta, fn) {
return withMeta(meta, (origin, options, ...params) => fn(origin, options, params));
}
function createTx(meta, fn) {
return withMeta(meta, (options, ...params) => fn(options, params));
}
class ContractSubmittableResult extends api.SubmittableResult {
contractEvents;
constructor(result, contractEvents) {
super(result);
this.contractEvents = contractEvents;
}
}
class Contract extends Base {
address;
__internal__query = {};
__internal__tx = {};
constructor(api, abi, address, decorateMethod) {
super(api, abi, decorateMethod);
this.address = this.registry.createType('AccountId', address);
this.abi.messages.forEach((m) => {
if (util.isUndefined(this.__internal__tx[m.method])) {
this.__internal__tx[m.method] = createTx(m, (o, p) => this.__internal__exec(m, o, p));
}
if (util.isUndefined(this.__internal__query[m.method])) {
this.__internal__query[m.method] = createQuery(m, (f, o, p) => this.__internal__read(m, o, p).send(f));
}
});
}