-
Notifications
You must be signed in to change notification settings - Fork 150
/
BlocksService.ts
839 lines (741 loc) · 24.5 KB
/
BlocksService.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
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
// Copyright 2017-2024 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
import type { ApiPromise } from '@polkadot/api';
import type { ApiDecoration } from '@polkadot/api/types';
import { extractAuthor } from '@polkadot/api-derive/type/util';
import { Compact, GenericCall, Option, Struct, Text, u32, Vec } from '@polkadot/types';
import type { GenericExtrinsic } from '@polkadot/types/extrinsic';
import type {
AccountId32,
Block,
BlockHash,
BlockNumber,
DispatchInfo,
EventRecord,
Header,
InclusionFee,
RuntimeDispatchInfo,
RuntimeDispatchInfoV1,
Weight,
WeightV1,
} from '@polkadot/types/interfaces';
import type { AnyJson, Codec, Registry } from '@polkadot/types/types';
import { u8aToHex } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
import { calc_partial_fee } from '@substrate/calc';
import BN from 'bn.js';
import { BadRequest, InternalServerError } from 'http-errors';
import type { LRUCache } from 'lru-cache';
import { QueryFeeDetailsCache } from '../../chains-config/cache';
import {
IBlock,
IBlockRaw,
IExtrinsic,
IExtrinsicIndex,
ISanitizedCall,
ISanitizedEvent,
isFrameMethod,
} from '../../types/responses';
import type { IOption } from '../../types/util';
import { isPaysFee } from '../../types/util';
import { PromiseQueue } from '../../util/PromiseQueue';
import { AbstractService } from '../AbstractService';
import { XcmDecoder } from './XCMDecoder';
/**
* Types for fetchBlock's options
* @field eventDocs
* @field extrinsicDocs
* @field checkFinalized Option to reduce rpc calls. Equals true when blockId is a hash.
* @field queryFinalizedHead Option to reduce rpc calls. Equals true when finalized head has not been queried.
* @field omitFinalizedTag Option to omit the finalized tag, and return it as undefined.
*/
interface FetchBlockOptions {
eventDocs: boolean;
extrinsicDocs: boolean;
checkFinalized: boolean;
queryFinalizedHead: boolean;
omitFinalizedTag: boolean;
noFees: boolean;
checkDecodedXcm: boolean;
paraId?: number;
}
interface ExtrinsicSuccessOrFailedOverride {
weight: Weight;
class: DispatchInfo;
}
/**
* Event methods that we check for.
*/
enum Event {
success = 'ExtrinsicSuccess',
failure = 'ExtrinsicFailed',
transactionPaidFee = 'TransactionFeePaid',
}
export class BlocksService extends AbstractService {
constructor(
api: ApiPromise,
private minCalcFeeRuntime: IOption<number>,
private blockStore: LRUCache<string, IBlock>,
private hasQueryFeeApi: QueryFeeDetailsCache,
) {
super(api);
}
/**
* Fetch a block augmented with derived values.
*
* @param hash `BlockHash` of the block to fetch.
* @param FetchBlockOptions options for additonal information.
*/
async fetchBlock(
hash: BlockHash,
historicApi: ApiDecoration<'promise'>,
{
eventDocs,
extrinsicDocs,
checkFinalized,
queryFinalizedHead,
omitFinalizedTag,
noFees,
checkDecodedXcm,
paraId,
}: FetchBlockOptions,
): Promise<IBlock> {
const { api } = this;
// Create a key for the cache that is a concatenation of the block hash and all the query params included in the request
const cacheKey =
hash.toString() +
Number(eventDocs) +
Number(extrinsicDocs) +
Number(checkFinalized) +
Number(noFees) +
Number(checkDecodedXcm) +
Number(paraId);
// Before making any api calls check the cache if the queried block exists
const isBlockCached = this.blockStore.get(cacheKey);
if (isBlockCached && isBlockCached.finalized !== false) {
return isBlockCached;
}
const [{ block }, { specName, specVersion }, validators, events, finalizedHead] = await Promise.all([
api.rpc.chain.getBlock(hash),
api.rpc.state.getRuntimeVersion(hash),
this.fetchValidators(historicApi),
this.fetchEvents(historicApi),
queryFinalizedHead ? api.rpc.chain.getFinalizedHead() : Promise.resolve(hash),
]);
if (block === undefined) {
throw new InternalServerError('Error querying for block');
}
const { parentHash, number, stateRoot, extrinsicsRoot, digest } = block.header;
const authorId = extractAuthor(digest, validators);
const logs = digest.logs.map(({ type, index, value }) => {
return { type, index, value };
});
const nonSanitizedExtrinsics = this.extractExtrinsics(block, events, historicApi.registry, extrinsicDocs);
const { extrinsics, onInitialize, onFinalize } = this.sanitizeEvents(
events,
nonSanitizedExtrinsics,
hash,
eventDocs,
);
let finalized = undefined;
if (!omitFinalizedTag) {
// Check if the requested block is finalized
finalized = await this.isFinalizedBlock(api, number, hash, finalizedHead, checkFinalized);
}
// The genesis block is a special case with little information associated with it.
if (parentHash.every((byte) => !byte)) {
return {
number,
hash,
parentHash,
stateRoot,
extrinsicsRoot,
authorId,
logs,
onInitialize,
extrinsics,
onFinalize,
finalized,
};
}
const previousBlockHash = await this.fetchPreviousBlockHash(number);
const prevBlockHistoricApi = await api.at(previousBlockHash);
/**
* Fee calculation logic. This runs the extrinsics concurrently.
*/
const pQueue = new PromiseQueue(4);
const feeTasks = [];
for (let idx = 0; idx < block.extrinsics.length; ++idx) {
feeTasks.push(
pQueue.run(async () => {
await this.resolveExtFees(
extrinsics,
block,
idx,
noFees,
previousBlockHash,
specVersion,
specName,
// Inject historic api here or undefined if not available, should save at least two calls per extrinsic
prevBlockHistoricApi,
);
}),
);
}
const decodedMsgs = checkDecodedXcm ? new XcmDecoder(api, specName.toString(), extrinsics, paraId) : undefined;
const decodedXcmMsgs = decodedMsgs?.messages;
await Promise.all(feeTasks);
const response = {
number,
hash,
parentHash,
stateRoot,
extrinsicsRoot,
authorId,
logs,
onInitialize,
extrinsics,
onFinalize,
finalized,
decodedXcmMsgs,
};
// Store the block in the cache
this.blockStore.set(cacheKey, response);
return response;
}
private async resolveExtFees(
extrinsics: IExtrinsic[],
block: Block,
idx: number,
noFees: boolean,
previousBlockHash: BlockHash,
specVersion: u32,
specName: Text,
historicApi?: ApiDecoration<'promise'>,
) {
const { api } = this;
if (noFees) {
extrinsics[idx].info = {};
return;
}
if (!extrinsics[idx].paysFee || !block.extrinsics[idx].isSigned) {
return;
}
if (this.minCalcFeeRuntime === null) {
extrinsics[idx].info = {
error: `Fee calculation not supported for this network`,
};
return;
}
if (this.minCalcFeeRuntime > specVersion.toNumber()) {
extrinsics[idx].info = {
error: `Fee calculation not supported for ${specVersion.toString()}#${specName.toString()}`,
};
return;
}
const xtEvents = extrinsics[idx].events;
const completedEvent = xtEvents.find(
({ method }) => isFrameMethod(method) && (method.method === Event.success || method.method === Event.failure),
);
if (!completedEvent) {
extrinsics[idx].info = {
error: 'Unable to find success or failure event for extrinsic',
};
return;
}
const completedData = completedEvent.data;
if (!completedData) {
extrinsics[idx].info = {
error: 'Success or failure event for extrinsic does not contain expected data',
};
return;
}
// Both ExtrinsicSuccess and ExtrinsicFailed events have DispatchInfo
// types as their final arg
const weightInfo = completedData[completedData.length - 1] as DispatchInfo;
if (!weightInfo.weight) {
extrinsics[idx].info = {
error: 'Success or failure event for extrinsic does not specify weight',
};
return;
}
if (!api.rpc.payment?.queryInfo && !api.call.transactionPaymentApi?.queryInfo) {
extrinsics[idx].info = {
error: 'Rpc method payment::queryInfo is not available',
};
return;
}
const transactionPaidFeeEvent = xtEvents.find(
({ method }) => isFrameMethod(method) && method.method === Event.transactionPaidFee,
);
const extrinsicSuccess = xtEvents.find(({ method }) => isFrameMethod(method) && method.method === Event.success);
const extrinsicFailed = xtEvents.find(({ method }) => isFrameMethod(method) && method.method === Event.failure);
const eventFailureOrSuccess = extrinsicSuccess || extrinsicFailed;
if (transactionPaidFeeEvent && eventFailureOrSuccess) {
let availableData: ExtrinsicSuccessOrFailedOverride;
if (extrinsicSuccess) {
availableData = eventFailureOrSuccess.data[0] as unknown as ExtrinsicSuccessOrFailedOverride;
} else {
availableData = eventFailureOrSuccess.data[1] as unknown as ExtrinsicSuccessOrFailedOverride;
}
extrinsics[idx].info = {
weight: availableData.weight,
class: availableData.class,
partialFee: transactionPaidFeeEvent.data[1].toString(),
kind: 'fromEvent',
};
return;
}
/**
* Grab the initial partialFee, and information required for calculating a partialFee
* if queryFeeDetails is available in the runtime.
*/
const {
class: dispatchClass,
partialFee,
weight,
} = await this.fetchQueryInfo(block.extrinsics[idx], previousBlockHash, historicApi);
const versionedWeight = (weight as Weight).refTime ? (weight as Weight).refTime.unwrap() : (weight as WeightV1);
let finalPartialFee = partialFee.toString(),
dispatchFeeType = 'preDispatch';
if (transactionPaidFeeEvent) {
finalPartialFee = transactionPaidFeeEvent.data[1].toString();
dispatchFeeType = 'fromEvent';
} else {
/**
* Call queryFeeDetails. It may not be available in the runtime and will
* error automatically when we try to call it. We cache the runtimes it will error so we
* don't try to call it again given a specVersion.
*/
const doesQueryFeeDetailsExist = this.hasQueryFeeApi.hasQueryFeeDetails(specVersion.toNumber());
if (doesQueryFeeDetailsExist === 'available') {
finalPartialFee = await this.fetchQueryFeeDetails(
block.extrinsics[idx],
previousBlockHash,
weightInfo.weight,
versionedWeight.toString(),
historicApi,
);
dispatchFeeType = 'postDispatch';
} else if (doesQueryFeeDetailsExist === 'unknown') {
try {
finalPartialFee = await this.fetchQueryFeeDetails(
block.extrinsics[idx],
previousBlockHash,
weightInfo.weight,
versionedWeight.toString(),
historicApi,
);
dispatchFeeType = 'postDispatch';
this.hasQueryFeeApi.setRegisterWithCall(specVersion.toNumber());
} catch {
this.hasQueryFeeApi.setRegisterWithoutCall(specVersion.toNumber());
console.warn('The error above is automatically emitted from polkadot-js, and can be ignored.');
}
}
}
extrinsics[idx].info = {
weight: weightInfo.weight,
class: dispatchClass,
partialFee: api.registry.createType('Balance', finalPartialFee),
kind: dispatchFeeType,
};
}
/**
* Fetch `payment_queryFeeDetails`.
*
* @param ext
* @param previousBlockHash
* @param extrinsicSuccessWeight
* @param estWeight
*/
private async fetchQueryFeeDetails(
ext: GenericExtrinsic,
previousBlockHash: BlockHash,
extrinsicSuccessWeight: Weight,
estWeight: string,
historicApi?: ApiDecoration<'promise'>,
): Promise<string> {
const { api } = this;
// Get injected historicApi for previousBlockHash or create a new one
const apiAt = historicApi || (await api.at(previousBlockHash));
let inclusionFee;
if (apiAt.call.transactionPaymentApi.queryFeeDetails) {
const u8a = ext.toU8a();
const result = await apiAt.call.transactionPaymentApi.queryFeeDetails(u8a, u8a.length);
inclusionFee = result.inclusionFee;
} else {
const result = await api.rpc.payment.queryFeeDetails(ext.toHex(), previousBlockHash);
inclusionFee = result.inclusionFee;
}
const finalPartialFee = this.calcPartialFee(extrinsicSuccessWeight, estWeight, inclusionFee);
return finalPartialFee;
}
/**
* Fetch `payment_queryInfo`.
*
* @param ext
* @param previousBlockHash
*/
private async fetchQueryInfo(
ext: GenericExtrinsic,
previousBlockHash: BlockHash,
historicApi?: ApiDecoration<'promise'>,
): Promise<RuntimeDispatchInfo | RuntimeDispatchInfoV1> {
const { api } = this;
// Get injected historicApi for previousBlockHash or create a new one
const apiAt = historicApi || (await api.at(previousBlockHash));
if (apiAt.call.transactionPaymentApi.queryInfo) {
const u8a = ext.toU8a();
return apiAt.call.transactionPaymentApi.queryInfo(u8a, u8a.length);
} else {
// fallback to rpc call
return api.rpc.payment.queryInfo(ext.toHex(), previousBlockHash);
}
}
/**
* Retrieve the blockHash for the previous block to the one getting queried.
* If the block is the geneisis hash it will return the same blockHash.
*
* @param blockNumber The blockId being queried
*/
private async fetchPreviousBlockHash(blockNumber: Compact<BlockNumber>): Promise<BlockHash> {
const { api } = this;
const num = blockNumber.toBn();
return num.isZero() ? await api.rpc.chain.getBlockHash(num) : await api.rpc.chain.getBlockHash(num.sub(new BN(1)));
}
/**
* Calculate the partialFee for an extrinsic. This uses `calc_partial_fee` from `@substrate/calc`.
* Please reference the rust code in `@substrate/calc` too see docs on the algorithm.
*
* @param extrinsicSuccessWeight
* @param estWeight
* @param inclusionFee
*/
private calcPartialFee(
extrinsicSuccessWeight: Weight,
estWeight: string,
inclusionFee: Option<InclusionFee>,
): string {
if (inclusionFee.isSome) {
const { baseFee, lenFee, adjustedWeightFee } = inclusionFee.unwrap();
return calc_partial_fee(
baseFee.toString(),
lenFee.toString(),
adjustedWeightFee.toString(),
estWeight.toString(),
extrinsicSuccessWeight.toString(),
);
} else {
// When the inclusion fee isNone we are dealing with a unsigned extrinsic.
return '0';
}
}
/**
* Return the header of a block
*
* @param hash When no hash is inputted the header of the chain will be queried.
*/
async fetchBlockHeader(hash?: BlockHash): Promise<Header> {
const { api } = this;
const header = hash ? await api.rpc.chain.getHeader(hash) : await api.rpc.chain.getHeader();
return header;
}
/**
*
* @param block Takes in a block which is the result of `BlocksService.fetchBlock`
* @param extrinsicIndex Parameter passed into the request
*/
fetchExtrinsicByIndex(block: IBlock, extrinsicIndex: number): IExtrinsicIndex {
if (extrinsicIndex > block.extrinsics.length - 1) {
throw new BadRequest('Requested `extrinsicIndex` does not exist');
}
const { hash, number } = block;
const height = number.unwrap().toString(10);
return {
at: {
height,
hash,
},
extrinsics: block.extrinsics[extrinsicIndex],
};
}
/**
* Extract extrinsics from a block.
*
* @param block Block
* @param events events fetched by `fetchEvents`
* @param regsitry The corresponding blocks runtime registry
* @param extrinsicDocs To include the extrinsic docs or not
*/
private extractExtrinsics(
block: Block,
events: Vec<EventRecord> | string,
registry: Registry,
extrinsicDocs: boolean,
): IExtrinsic[] {
const defaultSuccess = typeof events === 'string' ? events : false;
return block.extrinsics.map((extrinsic) => {
const { method, nonce, signature, signer, isSigned, tip, era } = extrinsic;
const hash = u8aToHex(blake2AsU8a(extrinsic.toU8a(), 256));
const call = registry.createType('Call', method);
return {
method: {
pallet: method.section,
method: method.method,
},
signature: isSigned ? { signature, signer } : null,
nonce: isSigned ? nonce : null,
args: this.parseGenericCall(call, registry).args,
tip: isSigned ? tip : null,
hash,
info: {},
era,
events: [] as ISanitizedEvent[],
success: defaultSuccess,
// paysFee overrides to bool if `system.ExtrinsicSuccess|ExtrinsicFailed` event is present
// we set to false if !isSigned because unsigned never pays a fee
paysFee: isSigned ? null : false,
docs: extrinsicDocs ? this.sanitizeDocs(extrinsic.meta.docs) : undefined,
};
});
}
/**
* Sanitize events and attribute them to an extrinsic, onInitialize, or
* onFinalize.
*
* @param events events from `fetchEvents`
* @param extrinsics extrinsics from
* @param hash hash of the block the events are from
*/
private sanitizeEvents(
events: EventRecord[] | string,
extrinsics: IExtrinsic[],
hash: BlockHash,
eventDocs: boolean,
) {
const onInitialize = { events: [] as ISanitizedEvent[] };
const onFinalize = { events: [] as ISanitizedEvent[] };
if (Array.isArray(events)) {
for (const record of events) {
const { event, phase } = record;
const sanitizedEvent = {
method: {
pallet: event.section,
method: event.method,
},
data: event.data,
docs: eventDocs ? this.sanitizeDocs(event.data.meta.docs) : undefined,
};
if (phase.isApplyExtrinsic) {
const extrinsicIdx = phase.asApplyExtrinsic.toNumber();
const extrinsic = extrinsics[extrinsicIdx];
if (!extrinsic) {
throw new Error(`Missing extrinsic ${extrinsicIdx} in block ${hash.toString()}`);
}
if (event.method === Event.success) {
extrinsic.success = true;
}
if (event.method === Event.success || event.method === Event.failure) {
const sanitizedData = event.data.toJSON() as AnyJson[];
for (const data of sanitizedData) {
if (extrinsic.signature && isPaysFee(data)) {
extrinsic.paysFee = data.paysFee === true || data.paysFee === 'Yes';
break;
}
}
}
extrinsic.events.push(sanitizedEvent);
} else if (phase.isFinalization) {
onFinalize.events.push(sanitizedEvent);
} else if (phase.isInitialization) {
onInitialize.events.push(sanitizedEvent);
}
}
}
return {
extrinsics,
onInitialize,
onFinalize,
};
}
/**
* Fetch events for the specified block.
*
* @param historicApi ApiDecoration to use for the query
*/
private async fetchEvents(historicApi: ApiDecoration<'promise'>): Promise<Vec<EventRecord> | string> {
try {
return await historicApi.query.system.events();
} catch {
return 'Unable to fetch Events, cannot confirm extrinsic status. Check pruning settings on the node.';
}
}
/**
* Checks to see if the current chain has the session module, then retrieve all
* validators.
*
* @param historicApi ApiDecoration to use for the query
*/
private async fetchValidators(historicApi: ApiDecoration<'promise'>): Promise<Vec<AccountId32>> {
return historicApi.query.session
? await historicApi.query.session.validators()
: ([] as unknown as Vec<AccountId32>);
}
/**
* Helper function for `parseGenericCall`.
*
* @param argsArray array of `Codec` values
* @param registry type registry of the block the call belongs to
*/
private parseArrayGenericCalls(argsArray: Codec[], registry: Registry): (Codec | ISanitizedCall)[] {
return argsArray.map((argument) => {
if (argument instanceof GenericCall) {
return this.parseGenericCall(argument as GenericCall, registry);
}
return argument;
});
}
/**
* Recursively parse a `GenericCall` in order to label its arguments with
* their param names and give a human friendly method name (opposed to just a
* call index). Parses `GenericCall`s that are nested as arguments.
*
* @param genericCall `GenericCall`
* @param registry type registry of the block the call belongs to
*/
private parseGenericCall(genericCall: GenericCall, registry: Registry): ISanitizedCall {
const newArgs: Record<string, unknown> = {};
// Pull out the struct of arguments to this call
const callArgs = genericCall.get('args') as Struct;
// Make sure callArgs exists and we can access its keys
if (callArgs && callArgs.defKeys) {
// paramName is a string
for (const paramName of callArgs.defKeys) {
const argument = callArgs.get(paramName);
if (Array.isArray(argument)) {
newArgs[paramName] = this.parseArrayGenericCalls(argument, registry);
} else if (argument instanceof GenericCall) {
newArgs[paramName] = this.parseGenericCall(argument as GenericCall, registry);
} else if (
argument &&
paramName === 'call' &&
['Bytes', 'WrapperKeepOpaque<Call>', 'WrapperOpaque<Call>'].includes(argument?.toRawType())
) {
// multiSig.asMulti.args.call is either an OpaqueCall (Vec<u8>),
// WrapperKeepOpaque<Call>, or WrapperOpaque<Call> that we
// serialize to a polkadot-js Call and parse so it is not a hex blob.
try {
const call = registry.createType('Call', argument.toHex());
newArgs[paramName] = this.parseGenericCall(call, registry);
} catch {
newArgs[paramName] = argument;
}
} else {
newArgs[paramName] = argument;
}
}
}
return {
method: {
pallet: genericCall.section,
method: genericCall.method,
},
args: newArgs,
};
}
/**
* When querying a block this will immediately inform the request whether
* or not the queried block is considered finalized at the time of querying.
*
* @param api ApiPromise to use for query
* @param blockNumber Queried block number
* @param queriedHash Hash of user queried block
* @param finalizedHead Finalized head for our chain
* @param checkFinalized If the passed in blockId is a hash
*/
private async isFinalizedBlock(
api: ApiPromise,
blockNumber: Compact<BlockNumber>,
queriedHash: BlockHash,
finalizedHead: BlockHash,
checkFinalized: boolean,
): Promise<boolean> {
if (checkFinalized) {
// The blockId url param is a hash
const [finalizedHeadBlock, canonHash] = await Promise.all([
// Returns the header of the most recently finalized block
api.rpc.chain.getHeader(finalizedHead),
// Fetch the hash of the block with equal height on the canon chain.
// N.B. We assume when we query by number <= finalized head height,
// we will always get a block on the finalized, canonical chain.
api.rpc.chain.getBlockHash(blockNumber.unwrap()),
]);
// If queried by hash this is the original request param
const hash = queriedHash.toHex();
// If this conditional is satisfied, the queried hash is on a fork,
// and is not on the canonical chain and therefore not finalized
if (canonHash.toHex() !== hash) {
return false;
}
// Retreive the finalized head blockNumber
const finalizedHeadBlockNumber = finalizedHeadBlock?.number;
// If the finalized head blockNumber is undefined return false
if (!finalizedHeadBlockNumber) {
return false;
}
// Check if the user's block is less than or equal to the finalized head.
// If so, the user's block is finalized.
return blockNumber.unwrap().lte(finalizedHeadBlockNumber.unwrap());
} else {
// The blockId url param is an integer
// Returns the header of the most recently finalized block
const finalizedHeadBlock = await api.rpc.chain.getHeader(finalizedHead);
// Retreive the finalized head blockNumber
const finalizedHeadBlockNumber = finalizedHeadBlock?.number;
// If the finalized head blockNumber is undefined return false
if (!finalizedHeadBlockNumber) {
return false;
}
// Check if the user's block is less than or equal to the finalized head.
// If so, the user's block is finalized.
return blockNumber.unwrap().lte(finalizedHeadBlockNumber.unwrap());
}
}
/**
* Fetch a block with raw extrinics values.
*
* @param hash `BlockHash` of the block to fetch.
*/
async fetchBlockRaw(hash: BlockHash): Promise<IBlockRaw> {
const { api } = this;
const { block } = await api.rpc.chain.getBlock(hash);
const { parentHash, number, stateRoot, extrinsicsRoot, digest } = block.header;
const { extrinsics } = block;
const logs = digest.logs.map(({ type, index, value }) => {
return { type, index, value };
});
return {
parentHash: parentHash,
number: number.toHex(),
stateRoot: stateRoot,
extrinsicRoot: extrinsicsRoot,
digest: { logs },
extrinsics: extrinsics,
};
}
}