-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathtaquito-rpc.ts
1290 lines (1200 loc) · 49.1 KB
/
taquito-rpc.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
/**
* @packageDocumentation
* @module @taquito/rpc
*/
import {
HttpBackend,
HttpRequestOptions,
HttpResponseError,
STATUS_CODE,
} from '@taquito/http-utils';
import BigNumber from 'bignumber.js';
import {
defaultChain,
defaultRPCOptions,
RpcClientInterface,
RPCOptions,
} from './rpc-client-interface';
import {
BakingRightsQueryArguments,
BakingRightsResponse,
BalanceResponse,
UnstakeRequestsResponse,
BallotListResponse,
BallotsResponse,
BigMapGetResponse,
BigMapKey,
BigMapResponse,
BlockHeaderResponse,
BlockMetadata,
BlockResponse,
ConstantsResponse,
ContractResponse,
CurrentProposalResponse,
CurrentQuorumResponse,
DelegateResponse,
DelegatesResponse,
VotingInfoResponse,
AttestationRightsQueryArguments,
AttestationRightsResponse,
EntrypointsResponse,
ForgeOperationsParams,
ManagerKeyResponse,
MichelsonV1ExpressionExtended,
OperationHash,
PackDataParams,
PackDataResponse,
PreapplyParams,
PreapplyResponse,
ProposalsResponse,
ProtocolsResponse,
RPCRunCodeParam,
RPCRunOperationParam,
RPCRunViewParam,
RPCRunScriptViewParam,
RunCodeResult,
RunViewResult,
RunScriptViewResult,
SaplingDiffResponse,
ScriptResponse,
StorageResponse,
UnparsingMode,
VotesListingsResponse,
VotingPeriodBlockResult,
TicketTokenParams,
AllTicketBalances,
PendingOperationsQueryArguments,
PendingOperationsV1,
PendingOperationsV2,
RPCSimulateOperationParam,
AILaunchCycleResponse,
AllDelegatesQueryArguments,
} from './types';
import { castToBigNumber } from './utils/utils';
import {
validateAddress,
validateContractAddress,
ValidationResult,
invalidDetail,
} from '@taquito/utils';
import { InvalidAddressError, InvalidContractAddressError } from '@taquito/core';
export { castToBigNumber } from './utils/utils';
export {
RPCOptions,
defaultChain,
defaultRPCOptions,
RpcClientInterface,
} from './rpc-client-interface';
export { RpcClientCache } from './rpc-client-modules/rpc-cache';
export * from './types';
export { OpKind } from './opkind';
export { VERSION } from './version';
/***
* @description RpcClient allows interaction with Tezos network through an rpc node
*/
export class RpcClient implements RpcClientInterface {
/**
*
* @param url rpc root url
* @param chain chain (default main)
* @param httpBackend Http backend that issue http request.
* You can override it by providing your own if you which to hook in the request/response
*
* @example new RpcClient('https://mainnet.tezos.ecadinfra.com/', 'main') this will use https://mainnet.tezos.ecadinfra.com//chains/main
*/
constructor(
protected url: string,
protected chain: string = defaultChain,
protected httpBackend: HttpBackend = new HttpBackend()
) {}
protected createURL(path: string) {
// Trim trailing slashes because it is assumed to be included in path
// the regex solution is prone to ReDoS. Please see: https://stackoverflow.com/questions/6680825/return-string-without-trailing-slash#comment124306698_6680877
// We also got a CodeQL error for the regex based solution
let rootUrl = this.url;
while (rootUrl.endsWith('/')) {
rootUrl = rootUrl.slice(0, -1);
}
return `${rootUrl}${path}`;
}
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/active/rpc.html#get-block-id-hash
*/
async getBlockHash({ block }: RPCOptions = defaultRPCOptions): Promise<string> {
const hash = await this.httpBackend.createRequest<string>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/hash`),
method: 'GET',
});
return hash;
}
/**
* @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/active/rpc.html#get-block-id-live-blocks
*/
async getLiveBlocks({ block }: RPCOptions = defaultRPCOptions): Promise<string[]> {
const blocks = await this.httpBackend.createRequest<string[]>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/live_blocks`),
method: 'GET',
});
return blocks;
}
/**
* @param address address from which we want to retrieve the spendable balance
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description The spendable balance of a contract (in mutez), also known as liquid balance. Corresponds to tez owned by the contract that are neither staked, nor in unstaked requests, nor in frozen bonds. Identical to the 'spendable' RPC.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-contracts-contract-id-balance
*/
async getBalance(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const balance = await this.httpBackend.createRequest<BalanceResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/balance`
),
method: 'GET',
});
return new BigNumber(balance);
}
/**
* @param address address from which we want to retrieve the spendable balance
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description The spendable balance of a contract (in mutez), also known as liquid balance. Corresponds to tez owned by the contract that are neither staked, nor in unstaked requests, nor in frozen bonds. Identical to the 'balance' RPC.
*/
async getSpendable(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const balance = await this.httpBackend.createRequest<BalanceResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/spendable`
),
method: 'GET',
});
return new BigNumber(balance);
}
/**
* @param address address from which we want to retrieve balance and frozen bonds
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description The sum (in mutez) of the spendable balance and frozen bonds of a contract. Corresponds to the contract's full balance from which staked funds and unstake requests have been excluded. Identical to the 'spendable_and_frozen_bonds' RPC.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-contracts-contract-id-full-balance
*/
async getBalanceAndFrozenBonds(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const balance = await this.httpBackend.createRequest<BalanceResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/balance_and_frozen_bonds`
),
method: 'GET',
});
return new BigNumber(balance);
}
/**
* @param address address from which we want to retrieve spendable and frozen bonds
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description The sum (in mutez) of the spendable balance and frozen bonds of a contract. Corresponds to the contract's full balance from which staked funds and unstake requests have been excluded. Identical to the 'balance_and_frozen_bonds' RPC.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-contracts-contract-id-full-balance
*/
async getSpendableAndFrozenBonds(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const balance = await this.httpBackend.createRequest<BalanceResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/spendable_and_frozen_bonds`
),
method: 'GET',
});
return new BigNumber(balance);
}
/**
* @param address address from which we want to retrieve the full balance
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the full balance of a contract, including frozen bonds and stake.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-contracts-contract-id-full-balance
*/
async getFullBalance(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const balance = await this.httpBackend.createRequest<BalanceResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/full_balance`
),
method: 'GET',
});
return new BigNumber(balance);
}
/**
* @param address address from which we want to retrieve the staked balance
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the staked balance of a contract. Returns None if the contract is originated, or neither delegated nor a delegate.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-contracts-contract-id-staked-balance
*/
async getStakedBalance(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const balance = await this.httpBackend.createRequest<BalanceResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/staked_balance`
),
method: 'GET',
});
return new BigNumber(balance);
}
/**
* @param address address from which we want to retrieve the unstaked finalizable balance
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the balance of a contract that was requested for an unstake operation, and is no longer frozen, which means it will appear in the spendable balance of the contract after any stake/unstake/finalize_unstake operation. Returns None if the contract is originated.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-contracts-contract-id-unstaked-finalizable-balance
*/
async getUnstakedFinalizableBalance(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const balance = await this.httpBackend.createRequest<BalanceResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/unstaked_finalizable_balance`
),
method: 'GET',
});
return new BigNumber(balance);
}
/**
* @param address address from which we want to retrieve the unstaked frozen balance
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the balance of a contract that was requested for an unstake operation, but is still frozen for the duration of the slashing period. Returns None if the contract is originated.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-contracts-contract-id-unstaked-frozen-balance
*/
async getUnstakedFrozenBalance(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const balance = await this.httpBackend.createRequest<BalanceResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/unstaked_frozen_balance`
),
method: 'GET',
});
return new BigNumber(balance);
}
/**
* @param address address from which we want to retrieve the unstaked requests
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Access the unstake requests of the contract. The requests that appear in the finalizable field can be finalized, which means that the contract can transfer these (no longer frozen) funds to their spendable balance with a [finalize_unstake] operation call. Returns null if there is no unstake request pending.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-contracts-contract-id-unstake-requests
*/
async getUnstakeRequests(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<UnstakeRequestsResponse> {
this.validateAddress(address);
const response = await this.httpBackend.createRequest<UnstakeRequestsResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/unstake_requests`
),
method: 'GET',
});
return response === null
? null
: {
finalizable: response.finalizable.map(({ amount, ...rest }) => {
const castedToBigNumber: any = castToBigNumber({ amount }, ['amount']);
return {
...rest,
amount: castedToBigNumber.amount,
};
}),
unfinalizable: {
delegate: response.unfinalizable.delegate,
requests: response.unfinalizable.requests.map(({ amount, cycle }) => {
const castedToBigNumber: any = castToBigNumber({ amount }, ['amount']);
return {
cycle,
amount: castedToBigNumber.amount,
};
}),
},
};
}
/**
* @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/active/rpc.html#get-block-id-context-contracts-contract-id-storage
*/
async getStorage(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<StorageResponse> {
this.validateContract(address);
return this.httpBackend.createRequest<StorageResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/storage`
),
method: 'GET',
});
}
/**
* @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/active/rpc.html#get-block-id-context-contracts-contract-id-script
*/
async getScript(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<ScriptResponse> {
this.validateContract(address);
return this.httpBackend.createRequest<ScriptResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/script`
),
method: 'GET',
});
}
/**
* @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);
return this.httpBackend.createRequest<ScriptResponse>(
{
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/script/normalized`
),
method: 'POST',
},
unparsingMode
);
}
/**
* @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/active/rpc.html#get-block-id-context-contracts-contract-id
*/
async getContract(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<ContractResponse> {
this.validateAddress(address);
const contractResponse = await this.httpBackend.createRequest<ContractResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/context/contracts/${address}`),
method: 'GET',
});
return {
...contractResponse,
balance: new BigNumber(contractResponse.balance),
};
}
/**
* @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/active/rpc.html#get-block-id-context-contracts-contract-id-manager-key
*/
async getManagerKey(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<ManagerKeyResponse> {
this.validateAddress(address);
return this.httpBackend.createRequest<ManagerKeyResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/manager_key`
),
method: 'GET',
});
}
/**
* @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/active/rpc.html#get-block-id-context-contracts-contract-id-delegate
*/
async getDelegate(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<DelegateResponse> {
this.validateAddress(address);
let delegate: DelegateResponse;
try {
delegate = await this.httpBackend.createRequest<DelegateResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/delegate`
),
method: 'GET',
});
} catch (ex) {
if (ex instanceof HttpResponseError && ex.status === STATUS_CODE.NOT_FOUND) {
delegate = null;
} else {
throw ex;
}
}
return delegate;
}
/**
* @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/active/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);
return this.httpBackend.createRequest<BigMapGetResponse>(
{
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/big_map_get`
),
method: 'POST',
},
key
);
}
/**
* @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/active/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> {
return this.httpBackend.createRequest<BigMapResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/context/big_maps/${id}/${expr}`),
method: 'GET',
});
}
/**
* @param args contains optional query arguments (active, inactive, with_minimal_stake, without_minimal_stake)
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Lists all registered delegates by default with query arguments to filter unneeded values.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-delegates-pkh
*/
async getAllDelegates(
args: AllDelegatesQueryArguments = {},
{ block }: { block: string } = defaultRPCOptions
): Promise<string[]> {
return await this.httpBackend.createRequest<string[]>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/context/delegates`),
method: 'GET',
query: args,
});
}
/**
* @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/active/rpc.html#get-block-id-context-delegates-pkh
*/
async getDelegates(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<DelegatesResponse> {
this.validateAddress(address);
const response = await this.httpBackend.createRequest<DelegatesResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/context/delegates/${address}`),
method: 'GET',
});
const castedResponse: any = castToBigNumber(response, [
'balance',
'full_balance',
'current_frozen_deposits',
'frozen_deposits',
'frozen_balance',
'frozen_deposits_limit',
'staking_balance',
'delegated_balance',
'voting_power',
'total_delegated_stake',
'staking_denominator',
]);
return {
...response,
...castedResponse,
frozen_balance_by_cycle: response.frozen_balance_by_cycle
? response.frozen_balance_by_cycle.map(({ deposit, deposits, fees, rewards, ...rest }) => {
const castedToBigNumber: any = castToBigNumber({ deposit, deposits, fees, rewards }, [
'deposit',
'deposits',
'fees',
'rewards',
]);
return {
...rest,
deposit: castedToBigNumber.deposit,
deposits: castedToBigNumber.deposits,
fees: castedToBigNumber.fees,
rewards: castedToBigNumber.rewards,
};
})
: undefined,
};
}
/**
* @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);
return await this.httpBackend.createRequest<VotingInfoResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/delegates/${address}/voting_info`
),
method: 'GET',
});
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description All constants
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-constants
*/
async getConstants({ block }: RPCOptions = defaultRPCOptions): Promise<ConstantsResponse> {
const response = await this.httpBackend.createRequest<ConstantsResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/context/constants`),
method: 'GET',
});
const castedResponse: any = castToBigNumber(response, [
'time_between_blocks',
'hard_gas_limit_per_operation',
'hard_gas_limit_per_block',
'proof_of_work_threshold',
'tokens_per_roll',
'seed_nonce_revelation_tip',
'block_security_deposit',
'endorsement_security_deposit',
'block_reward',
'endorsement_reward',
'cost_per_byte',
'hard_storage_limit_per_operation',
'test_chain_duration',
'baking_reward_per_endorsement',
'delay_per_missing_endorsement',
'minimal_block_delay',
'liquidity_baking_subsidy',
'cache_layout',
'baking_reward_fixed_portion',
'baking_reward_bonus_per_slot',
'endorsing_reward_per_slot',
'double_baking_punishment',
'delay_increment_per_round',
'tx_rollup_commitment_bond',
'vdf_difficulty',
'sc_rollup_stake_amount',
'minimal_stake',
]);
return {
...response,
...(castedResponse as ConstantsResponse),
};
}
/**
* @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/active/rpc.html#get-block-id
* @example getBlock() will default to `/main/chains/block/head?version=1`
* @example getBlock({ block: 'head~2' }) will return an offset of 2 from head blocks
* @example getBlock({ block: 'BL8fTiWcSxWCjiMVnDkbh6EuhqVPZzgWheJ2dqwrxYRm9AephXh~2' }) will return an offset of 2 blocks from given block hash..
*/
async getBlock({ block, version }: RPCOptions = defaultRPCOptions): Promise<BlockResponse> {
const requestOptions: HttpRequestOptions = {
url: this.createURL(`/chains/${this.chain}/blocks/${block}`),
method: 'GET',
};
if (version !== undefined) {
requestOptions.query = { version };
}
return await this.httpBackend.createRequest<BlockResponse>(requestOptions);
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description The whole block header
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-header
*/
async getBlockHeader({ block }: RPCOptions = defaultRPCOptions): Promise<BlockHeaderResponse> {
const response = await this.httpBackend.createRequest<BlockHeaderResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/header`),
method: 'GET',
});
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/active/rpc.html#get-block-id-metadata
*/
async getBlockMetadata({
block,
version,
}: RPCOptions = defaultRPCOptions): Promise<BlockMetadata> {
const requestOptions: HttpRequestOptions = {
url: this.createURL(`/chains/${this.chain}/blocks/${block}/metadata`),
method: 'GET',
};
if (version !== undefined) {
requestOptions.query = { version };
}
return await this.httpBackend.createRequest<BlockMetadata>(requestOptions);
}
/**
* @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://gitlab.com/tezos/tezos/-/blob/master/docs/api/alpha-openapi.json
*/
async getBakingRights(
args: BakingRightsQueryArguments = {},
{ block }: RPCOptions = defaultRPCOptions
): Promise<BakingRightsResponse> {
const response = await this.httpBackend.createRequest<BakingRightsResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/helpers/baking_rights`),
method: 'GET',
query: args,
});
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://gitlab.com/tezos/tezos/-/blob/master/docs/api/alpha-openapi.json
*/
async getAttestationRights(
args: AttestationRightsQueryArguments = {},
{ block }: RPCOptions = defaultRPCOptions
): Promise<AttestationRightsResponse> {
const response = await this.httpBackend.createRequest<AttestationRightsResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/helpers/attestation_rights`),
method: 'GET',
query: args,
});
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/active/rpc.html#get-block-id-votes-ballot-list
*/
async getBallotList({ block }: RPCOptions = defaultRPCOptions): Promise<BallotListResponse> {
const response = await this.httpBackend.createRequest<BallotListResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/votes/ballot_list`),
method: 'GET',
});
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/active/rpc.html#get-block-id-votes-ballots
*/
async getBallots({ block }: RPCOptions = defaultRPCOptions): Promise<BallotsResponse> {
const response = await this.httpBackend.createRequest<BallotsResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/votes/ballots`),
method: 'GET',
});
const casted: any = castToBigNumber(response, ['yay', 'nay', 'pass']);
return casted;
}
/**
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Current proposal under evaluation.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-votes-current-proposal
*/
async getCurrentProposal({
block,
}: RPCOptions = defaultRPCOptions): Promise<CurrentProposalResponse> {
const response = await this.httpBackend.createRequest<CurrentProposalResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/votes/current_proposal`),
method: 'GET',
});
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/active/rpc.html#get-block-id-votes-current-quorum
*/
async getCurrentQuorum({
block,
}: RPCOptions = defaultRPCOptions): Promise<CurrentQuorumResponse> {
const response = await this.httpBackend.createRequest<CurrentQuorumResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/votes/current_quorum`),
method: 'GET',
});
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/active/rpc.html#get-block-id-votes-listings
*/
async getVotesListings({
block,
}: RPCOptions = defaultRPCOptions): Promise<VotesListingsResponse> {
const response = await this.httpBackend.createRequest<VotesListingsResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/votes/listings`),
method: 'GET',
});
response.map((item) => {
if (item.voting_power) {
item.voting_power = new BigNumber(item.voting_power);
}
return item;
});
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/active/rpc.html#get-block-id-votes-proposals
*/
async getProposals({ block }: RPCOptions = defaultRPCOptions): Promise<ProposalsResponse> {
const response = await this.httpBackend.createRequest<ProposalsResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/votes/proposals`),
method: 'GET',
});
response.map((item) => {
return (item[1] = new BigNumber(item[1]));
});
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://gitlab.com/tezos/tezos/-/blob/master/docs/api/alpha-openapi.json
*/
async forgeOperations(
data: ForgeOperationsParams,
{ block }: RPCOptions = defaultRPCOptions
): Promise<string> {
return this.httpBackend.createRequest<string>(
{
url: this.createURL(`/chains/${this.chain}/blocks/${block}/helpers/forge/operations`),
method: 'POST',
},
data
);
}
/**
* @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/shell/rpc.html#post-injection-operation
*/
async injectOperation(signedOpBytes: string): Promise<OperationHash> {
return this.httpBackend.createRequest<any>(
{
url: this.createURL(`/injection/operation`),
method: 'POST',
},
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/active/rpc.html#post-block-id-helpers-preapply-operations
*/
async preapplyOperations(
ops: PreapplyParams,
{ block, version }: RPCOptions = defaultRPCOptions
): Promise<PreapplyResponse[]> {
const requestOptions: HttpRequestOptions = {
url: this.createURL(`/chains/${this.chain}/blocks/${block}/helpers/preapply/operations`),
method: 'POST',
};
if (version !== undefined) {
requestOptions.query = { version };
}
return await this.httpBackend.createRequest<PreapplyResponse[]>(requestOptions, ops);
}
/**
* @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/active/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 contractResponse = await this.httpBackend.createRequest<{
entrypoints: { [key: string]: MichelsonV1ExpressionExtended };
}>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${contract}/entrypoints`
),
method: 'GET',
});
return contractResponse;
}
/**
* @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://gitlab.com/tezos/tezos/-/blob/master/docs/api/alpha-openapi.json
*/
async runOperation(
op: RPCRunOperationParam,
{ block, version }: RPCOptions = defaultRPCOptions
): Promise<PreapplyResponse> {
const requestOptions: HttpRequestOptions = {
url: this.createURL(`/chains/${this.chain}/blocks/${block}/helpers/scripts/run_operation`),
method: 'POST',
};
if (version !== undefined) {
requestOptions.query = { version };
}
return await this.httpBackend.createRequest<any>(requestOptions, op);
}
/**
* @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/alpha-openapi.json
*/
async simulateOperation(
op: RPCSimulateOperationParam,
{ block, version }: RPCOptions = defaultRPCOptions
): Promise<PreapplyResponse> {
const requestOptions: HttpRequestOptions = {
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/helpers/scripts/simulate_operation`
),
method: 'POST',
};
if (version !== undefined) {
requestOptions.query = { version };
}
return await this.httpBackend.createRequest<any>(requestOptions, op);
}
/**
* @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://gitlab.com/tezos/tezos/-/blob/master/docs/api/alpha-openapi.json
*/
async runCode(
code: RPCRunCodeParam,
{ block }: RPCOptions = defaultRPCOptions
): Promise<RunCodeResult> {
const response = await this.httpBackend.createRequest<any>(