/
rpc-cache.ts
1150 lines (1089 loc) · 40 KB
/
rpc-cache.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
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
import { RPCMethodName } from './../rpc-client-interface';
import BigNumber from 'bignumber.js';
import { defaultRPCOptions, RpcClientInterface, RPCOptions } from '../rpc-client-interface';
import {
BakingRightsQueryArguments,
BakingRightsResponse,
BalanceResponse,
BallotListResponse,
BallotsResponse,
BigMapGetResponse,
BigMapKey,
BigMapResponse,
BlockHeaderResponse,
BlockMetadata,
BlockResponse,
ConstantsResponse,
ContractResponse,
CurrentProposalResponse,
CurrentQuorumResponse,
DelegateResponse,
DelegatesResponse,
VotingInfoResponse,
AttestationRightsQueryArguments,
AttestationRightsResponse,
EndorsingRightsQueryArguments,
EndorsingRightsResponse,
EntrypointsResponse,
ForgeOperationsParams,
ManagerKeyResponse,
OperationHash,
PackDataParams,
PreapplyParams,
PreapplyResponse,
ProposalsResponse,
ProtocolsResponse,
RPCRunCodeParam,
RPCRunOperationParam,
RPCRunScriptViewParam,
RPCRunViewParam,
RunCodeResult,
RunScriptViewResult,
RunViewResult,
SaplingDiffResponse,
ScriptResponse,
StorageResponse,
UnparsingMode,
VotesListingsResponse,
VotingPeriodBlockResult,
TicketTokenParams,
AllTicketBalances,
PendingOperationsQueryArguments,
PendingOperationsV1,
PendingOperationsV2,
RPCSimulateOperationParam,
} from '../types';
import { InvalidAddressError, InvalidContractAddressError } from '@taquito/core';
import {
validateContractAddress,
validateAddress,
ValidationResult,
invalidDetail,
} from '@taquito/utils';
interface CachedDataInterface {
[key: string]: {
handle: () => void;
response: Promise<any>;
};
}
type RpcMethodParam =
| string
| UnparsingMode
| BigMapKey
| BakingRightsQueryArguments
| PendingOperationsQueryArguments
| AttestationRightsQueryArguments
| EndorsingRightsQueryArguments;
const defaultTtl = 1000;
/***
* @description RpcClientCache acts as a decorator over the RpcClient instance by caching responses for the period defined by the ttl.
*/
export class RpcClientCache implements RpcClientInterface {
private _cache: CachedDataInterface = {};
/**
*
* @param rpcClient rpcClient responsible of the interaction with Tezos network through an rpc node
* @param ttl number representing the time to live (default 1000 milliseconds)
*
* @example new RpcClientCache(new RpcClient('https://mainnet.ecadinfra.com/'))
*/
constructor(
private rpcClient: RpcClientInterface,
private ttl = defaultTtl
) {}
getAllCachedData() {
return this._cache;
}
/**
* @description Remove all the data in the cache.
*
*/
deleteAllCachedData() {
for (const key in this._cache) {
delete this._cache[key];
}
}
private formatCacheKey(
rpcUrl: string,
rpcMethodName: RPCMethodName,
rpcMethodParams: RpcMethodParam[],
rpcMethodData?: object
) {
let paramsToString = '';
rpcMethodParams.forEach((param) => {
paramsToString =
typeof param === 'object'
? paramsToString + JSON.stringify(param) + '/'
: paramsToString + param + '/';
});
return rpcMethodData
? `${rpcUrl}/${rpcMethodName}/${paramsToString}${JSON.stringify(rpcMethodData)}/`
: `${rpcUrl}/${rpcMethodName}/${paramsToString}`;
}
private has(key: string) {
return key in this._cache;
}
private get(key: string) {
return this._cache[key].response;
}
private put<T>(key: string, response: Promise<T>) {
const handle = setTimeout(() => {
return this.remove(key);
}, this.ttl);
Object.assign(this._cache, { [key]: { handle, response } });
}
private remove(key: string) {
if (key in this._cache) {
delete this._cache[key];
}
}
private validateAddress(address: string) {
const addressValidation = validateAddress(address);
if (addressValidation !== ValidationResult.VALID) {
throw new InvalidAddressError(address, invalidDetail(addressValidation));
}
}
private validateContract(address: string) {
const addressValidation = validateContractAddress(address);
if (addressValidation !== ValidationResult.VALID) {
throw new InvalidContractAddressError(address, invalidDetail(addressValidation));
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Get the block's hash, its unique identifier.
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-hash
*/
async getBlockHash({ block }: RPCOptions = defaultRPCOptions): Promise<string> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_BLOCK_HASH, [
block,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getBlockHash({ block });
this.put(key, response);
return response;
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description List the ancestors of the given block which, if referred to as the branch in an operation header, are recent enough for that operation to be included in the current block.
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-live-blocks
*/
async getLiveBlocks({ block }: RPCOptions = defaultRPCOptions): Promise<string[]> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_LIVE_BLOCKS, [
block,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getLiveBlocks({ block });
this.put(key, response);
return response;
}
}
/**
* @param address address from which we want to retrieve the balance
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the spendable balance of a contract, excluding frozen bonds
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-context-contracts-contract-id-balance
*/
async getBalance(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_BALANCE, [
block,
address,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getBalance(address, { block });
this.put(key, response);
return response;
}
}
/**
* @param address contract address from which we want to retrieve the storage
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the data of the contract.
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-context-contracts-contract-id-storage
*/
async getStorage(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<StorageResponse> {
this.validateContract(address);
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_STORAGE, [
block,
address,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getStorage(address, { block });
this.put(key, response);
return response;
}
}
/**
* @param address contract address from which we want to retrieve the script
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the code and data of the contract.
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-context-contracts-contract-id-script
*/
async getScript(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<ScriptResponse> {
this.validateContract(address);
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_SCRIPT, [
block,
address,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getScript(address, { block });
this.put(key, response);
return response;
}
}
/**
* @param address contract address from which we want to retrieve the script
* @param unparsingMode default is { unparsing_mode: "Readable" }
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the script of the contract and normalize it using the requested unparsing mode.
*/
async getNormalizedScript(
address: string,
unparsingMode: UnparsingMode = { unparsing_mode: 'Readable' },
{ block }: { block: string } = defaultRPCOptions
): Promise<ScriptResponse> {
this.validateContract(address);
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_NORMALIZED_SCRIPT,
[block, address],
unparsingMode
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getNormalizedScript(address, unparsingMode, { block });
this.put(key, response);
return response;
}
}
/**
* @param address contract address from which we want to retrieve
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the complete status of a contract.
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-context-contracts-contract-id
*/
async getContract(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<ContractResponse> {
this.validateAddress(address);
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_CONTRACT, [
block,
address,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getContract(address, { block });
this.put(key, response);
return response;
}
}
/**
* @param address contract address from which we want to retrieve the manager
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the manager of an implicit contract
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-context-contracts-contract-id-manager-key
*/
async getManagerKey(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<ManagerKeyResponse> {
this.validateAddress(address);
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_MANAGER_KEY, [
block,
address,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getManagerKey(address, { block });
this.put(key, response);
return response;
}
}
/**
* @param address contract address from which we want to retrieve the delegate (baker)
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the delegate of a contract, if any
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-context-contracts-contract-id-delegate
*/
async getDelegate(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<DelegateResponse> {
this.validateAddress(address);
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_DELEGATE, [
block,
address,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getDelegate(address, { block });
this.put(key, response);
return response;
}
}
/**
* @deprecated Deprecated in favor of getBigMapKeyByID
* @param address contract address from which we want to retrieve the big map key
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the value associated with a key in the big map storage of the contract.
* @see https://tezos.gitlab.io/api/rpc.html#post-block-id-context-contracts-contract-id-big-map-get
*/
async getBigMapKey(
address: string,
key: BigMapKey,
{ block }: { block: string } = defaultRPCOptions
): Promise<BigMapGetResponse> {
this.validateAddress(address);
const keyUrl = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_BIG_MAP_KEY,
[block, address],
key
);
if (this.has(keyUrl)) {
return this.get(keyUrl);
} else {
const response = this.rpcClient.getBigMapKey(address, key, { block });
this.put(keyUrl, response);
return response;
}
}
/**
* @param id Big Map ID
* @param expr Expression hash to query (A b58check encoded Blake2b hash of the expression (The expression can be packed using the pack_data method))
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the value associated with a key in a big map.
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-context-big-maps-big-map-id-script-expr
*/
async getBigMapExpr(
id: string,
expr: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<BigMapResponse> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_BIG_MAP_EXPR, [
block,
id,
expr,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getBigMapExpr(id, expr, { block });
this.put(key, response);
return response;
}
}
/**
* @param address delegate address which we want to retrieve
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Everything about a delegate
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-context-delegates-pkh
*/
async getDelegates(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<DelegatesResponse> {
this.validateAddress(address);
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_DELEGATES, [
block,
address,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getDelegates(address, { block });
this.put(key, response);
return response;
}
}
/**
* @param address delegate address which we want to retrieve
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Returns the delegate info (e.g. voting power) found in the listings of the current voting period
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-delegates-pkh-voting-info
*/
async getVotingInfo(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<VotingInfoResponse> {
this.validateAddress(address);
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_VOTING_INFO, [
block,
address,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getVotingInfo(address, { block });
this.put(key, response);
return response;
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description All constants
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-context-constants
*/
async getConstants({ block }: RPCOptions = defaultRPCOptions): Promise<ConstantsResponse> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_CONSTANTS, [
block,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getConstants({ block });
this.put(key, response);
return response;
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head) and version.
* @description All the information about a block
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id
* @example getBlock() will default to `/main/chains/block/head?version=0` which shows { kind: endorsement }
* @example getBlock({ block: 'head~2', version: 1 }) will return an offset of 2 from head blocks and shows { kind: attestation }
* @example getBlock({ block: 'BL8fTiWcSxWCjiMVnDkbh6EuhqVPZzgWheJ2dqwrxYRm9AephXh~2' }) will return an offset of 2 blocks from given block hash..
*/
async getBlock({ block }: RPCOptions = defaultRPCOptions): Promise<BlockResponse> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_BLOCK, [block]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getBlock({ block });
this.put(key, response);
return response;
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description The whole block header
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-header
*/
async getBlockHeader({ block }: RPCOptions = defaultRPCOptions): Promise<BlockHeaderResponse> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_BLOCK_HEADER, [
block,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getBlockHeader({ block });
this.put(key, response);
return response;
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head) and version
* @description All the metadata associated to the block
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-metadata
*/
async getBlockMetadata({ block }: RPCOptions = defaultRPCOptions): Promise<BlockMetadata> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_BLOCK_METADATA, [
block,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getBlockMetadata({ block });
this.put(key, response);
return response;
}
}
/**
* @param args contains optional query arguments (level, cycle, delegate, consensus_key, and max_round)
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Retrieves the list of delegates allowed to bake a block.
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-helpers-baking-rights
*/
async getBakingRights(
args: BakingRightsQueryArguments = {},
{ block }: RPCOptions = defaultRPCOptions
): Promise<BakingRightsResponse> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_BAKING_RIGHTS, [
block,
args,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getBakingRights(args, { block });
this.put(key, response);
return response;
}
}
/**
* @param args contains optional query arguments (level, cycle, delegate, and consensus_key)
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Retrieves the delegates allowed to attest a block
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-helpers-endorsing-rights
*/
async getAttestationRights(
args: AttestationRightsQueryArguments = {},
{ block }: RPCOptions = defaultRPCOptions
): Promise<AttestationRightsResponse> {
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_ATTESTATION_RIGHTS,
[block, args]
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getAttestationRights(args, { block });
this.put(key, response);
return response;
}
}
/**
* @deprecated Deprecated in favor of getAttestationRights
* @param args contains optional query arguments (level, cycle, delegate, and consensus_key)
* @param options contains generic configuration for rpc calls
* @description Retrieves the delegates allowed to endorse a block
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-helpers-endorsing-rights
*/
async getEndorsingRights(
args: EndorsingRightsQueryArguments = {},
{ block }: RPCOptions = defaultRPCOptions
): Promise<EndorsingRightsResponse> {
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_ENDORSING_RIGHTS,
[block, args]
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getEndorsingRights(args, { block });
this.put(key, response);
return response;
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Ballots casted so far during a voting period
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-votes-ballot-list
*/
async getBallotList({ block }: RPCOptions = defaultRPCOptions): Promise<BallotListResponse> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_BALLOT_LIST, [
block,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getBallotList({ block });
this.put(key, response);
return response;
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Sum of ballots casted so far during a voting period
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-votes-ballots
*/
async getBallots({ block }: RPCOptions = defaultRPCOptions): Promise<BallotsResponse> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_BALLOTS, [block]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getBallots({ block });
this.put(key, response);
return response;
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Current proposal under evaluation.
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-votes-current-proposal
*/
async getCurrentProposal({
block,
}: RPCOptions = defaultRPCOptions): Promise<CurrentProposalResponse> {
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_CURRENT_PROPOSAL,
[block]
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getCurrentProposal({ block });
this.put(key, response);
return response;
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Current expected quorum.
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-votes-current-quorum
*/
async getCurrentQuorum({
block,
}: RPCOptions = defaultRPCOptions): Promise<CurrentQuorumResponse> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_CURRENT_QUORUM, [
block,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getCurrentQuorum({ block });
this.put(key, response);
return response;
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description List of delegates with their voting power
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-votes-listings
*/
async getVotesListings({
block,
}: RPCOptions = defaultRPCOptions): Promise<VotesListingsResponse> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_VOTES_LISTINGS, [
block,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getVotesListings({ block });
this.put(key, response);
return response;
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description List of proposals with number of supporters
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-votes-proposals
*/
async getProposals({ block }: RPCOptions = defaultRPCOptions): Promise<ProposalsResponse> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_PROPOSALS, [
block,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getProposals({ block });
this.put(key, response);
return response;
}
}
/**
* @param data operation contents to forge
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Forge an operation returning the unsigned bytes
* @see https://tezos.gitlab.io/api/rpc.html#post-block-id-helpers-forge-operations
*/
async forgeOperations(
data: ForgeOperationsParams,
{ block }: RPCOptions = defaultRPCOptions
): Promise<string> {
return this.rpcClient.forgeOperations(data, { block });
}
/**
* @param signedOpBytes signed bytes to inject
* @description Inject an operation in node and broadcast it and return the ID of the operation
* @see https://tezos.gitlab.io/api/rpc.html#post-injection-operation
*/
async injectOperation(signedOpBytes: string): Promise<OperationHash> {
return this.rpcClient.injectOperation(signedOpBytes);
}
/**
* @param ops Operations to apply
* @param options contains generic configuration for rpc calls to specified block and version
* @description Simulate the application of the operations with the context of the given block and return the result of each operation application
* @see https://tezos.gitlab.io/api/rpc.html#post-block-id-helpers-preapply-operations
*/
async preapplyOperations(
ops: PreapplyParams,
{ block }: RPCOptions = defaultRPCOptions
): Promise<PreapplyResponse[]> {
return this.rpcClient.preapplyOperations(ops, { block });
}
/**
* @param contract address of the contract we want to get the entrypoints of
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Return the list of entrypoints of the contract
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-context-contracts-contract-id-entrypoints
* @version 005_PsBABY5H
*/
async getEntrypoints(
contract: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<EntrypointsResponse> {
this.validateContract(contract);
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_ENTRYPOINTS, [
block,
contract,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getEntrypoints(contract, { block });
this.put(key, response);
return response;
}
}
/**
* @deprecated Deprecated in favor of simulateOperation
* @param op Operation to run
* @param options contains generic configuration for rpc calls to specified block and version
* @description Run an operation with the context of the given block and without signature checks and return the operation application result, including the consumed gas.
* @see https://tezos.gitlab.io/api/rpc.html#post-block-id-helpers-scripts-run-operation
*/
async runOperation(
op: RPCRunOperationParam,
{ block }: RPCOptions = defaultRPCOptions
): Promise<PreapplyResponse> {
return this.rpcClient.runOperation(op, { block });
}
/**
* @param op Operation to simulate
* @param options contains generic configuration for rpc calls to specified block and version
* @description Simulate running an operation at some future moment (based on the number of blocks given in the `latency` argument), and return the operation application result.
* @see https://gitlab.com/tezos/tezos/-/blob/master/docs/api/oxford-openapi.json
*/
async simulateOperation(
op: RPCSimulateOperationParam,
{ block }: RPCOptions = defaultRPCOptions
): Promise<PreapplyResponse> {
return this.rpcClient.simulateOperation(op, { block });
}
/**
* @param code Code to run
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Run a Michelson script in the current context
* @see https://tezos.gitlab.io/api/rpc.html#post-block-id-helpers-scripts-run-code
*/
async runCode(
code: RPCRunCodeParam,
{ block }: RPCOptions = defaultRPCOptions
): Promise<RunCodeResult> {
return this.rpcClient.runCode(code, { block });
}
/**
* @param viewScriptParams Parameters of the script view to run
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Simulate a call to a michelson view
*/
async runScriptView(
{ unparsing_mode = 'Readable', ...rest }: RPCRunScriptViewParam,
{ block }: RPCOptions = defaultRPCOptions
): Promise<RunScriptViewResult> {
return this.rpcClient.runScriptView(
{
unparsing_mode,
...rest,
},
{ block }
);
}
/**
* @param viewParams Parameters of the view to run
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Simulate a call to a view following the TZIP-4 standard. See https://gitlab.com/tzip/tzip/-/blob/master/proposals/tzip-4/tzip-4.md#view-entrypoints.
*/
async runView(
{ unparsing_mode = 'Readable', ...rest }: RPCRunViewParam,
{ block }: RPCOptions = defaultRPCOptions
): Promise<RunViewResult> {
return this.rpcClient.runView(
{
unparsing_mode,
...rest,
},
{ block }
);
}
async getChainId() {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_CHAIN_ID, []);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getChainId();
this.put(key, response);
return response;
}
}
/**
* @param data Data to pack
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Computes the serialized version of a data expression using the same algorithm as script instruction PACK
* Note: You should always verify the packed bytes before signing or requesting that they be signed when using the the RPC to pack.
* This precaution helps protect you and your applications users from RPC nodes that have been compromised.
* A node that is operated by a bad actor, or compromised by a bad actor could return a fully formed operation that does not correspond to the input provided to the RPC endpoint.
* A safer solution to pack and sign data would be to use the `packDataBytes` function available in the `@taquito/michel-codec` package.
* @example packData({ data: { string: "test" }, type: { prim: "string" } })
* @see https://tezos.gitlab.io/api/rpc.html#post-block-id-helpers-scripts-pack-data
*/
async packData(
data: PackDataParams,
{ block }: RPCOptions = defaultRPCOptions
): Promise<{ packed: string; gas: BigNumber | 'unaccounted' | undefined }> {
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.PACK_DATA,
[block],
data
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.packData(data, { block });
this.put(key, response);
return response;
}
}
/**
*
* @description Return rpc root url
*/
getRpcUrl() {
return this.rpcClient.getRpcUrl();
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Returns the voting period (index, kind, starting position) and related information (position, remaining) of the interrogated block
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-votes-current-period
*/
async getCurrentPeriod({
block,
}: RPCOptions = defaultRPCOptions): Promise<VotingPeriodBlockResult> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_CURRENT_PERIOD, [
block,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getCurrentPeriod({ block });
this.put(key, response);
return response;
}
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Returns the voting period (index, kind, starting position) and related information (position, remaining) of the next block.Useful to craft operations that will be valid in the next block
* @example getSuccessorPeriod() will default to successor voting period for /main/chains/block/head.
* @see https://tezos.gitlab.io/api/rpc.html#get-block-id-votes-successor-period
*/
async getSuccessorPeriod({
block,
}: RPCOptions = defaultRPCOptions): Promise<VotingPeriodBlockResult> {
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_SUCCESSOR_PERIOD,
[block]
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getSuccessorPeriod({ block });
this.put(key, response);
return response;
}
}
/**
* @param id Sapling state ID
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Returns the root and a diff of a state starting from an optional offset which is zero by default
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-sapling-sapling-state-id-get-diff
*/
async getSaplingDiffById(
id: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<SaplingDiffResponse> {
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_SAPLING_DIFF_BY_ID,
[block, id]
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getSaplingDiffById(id, { block });
this.put(key, response);
return response;
}
}
/**
* @param contract address of the contract we want to get the sapling diff
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Returns the root and a diff of a state starting from an optional offset which is zero by default
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-contracts-contract-id-single-sapling-get-diff
*/
async getSaplingDiffByContract(
contract: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<SaplingDiffResponse> {
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_SAPLING_DIFF_BY_CONTRACT,
[block, contract]
);