-
Notifications
You must be signed in to change notification settings - Fork 115
/
taquito-rpc.ts
732 lines (675 loc) · 22.3 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
import { HttpBackend } from '@taquito/http-utils';
import BigNumber from 'bignumber.js';
import {
BakingRightsQueryArguments,
BakingRightsResponse,
BalanceResponse,
BallotListResponse,
BallotsResponse,
BigMapGetResponse,
BigMapKey,
BigMapResponse,
BlockHeaderResponse,
BlockMetadata,
BlockResponse,
ConstantsResponse,
ContractResponse,
CurrentProposalResponse,
CurrentQuorumResponse,
DelegateResponse,
DelegatesResponse,
EndorsingRightsQueryArguments,
EndorsingRightsResponse,
EntrypointsResponse,
ForgeOperationsParams,
ManagerKeyResponse,
ManagerResponse,
OperationHash,
PackDataParams,
PackDataResponse,
PeriodKindResponse,
PreapplyParams,
PreapplyResponse,
ProposalsResponse,
RawBlockHeaderResponse,
RPCRunOperationParam,
ScriptResponse,
StorageResponse,
VotesListingsResponse,
} from './types';
import { castToBigNumber } from './utils/utils';
export * from './types';
export * from './types.common';
const defaultRPC = 'https://mainnet.tezrpc.me';
const defaultChain = 'main';
interface RPCOptions {
block: string;
}
const defaultRPCOptions: RPCOptions = { block: 'head' };
/***
* @description RpcClient allows interaction with Tezos network through an rpc node
*/
export class RpcClient {
/**
*
* @param url rpc root url (default https://mainnet.tezrpc.me)
* @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.tezrpc.me', 'main') this will use https://mainnet.tezrpc.me/chains/main
*/
constructor(
private url: string = defaultRPC,
private chain: string = defaultChain,
private httpBackend: HttpBackend = new HttpBackend()
) {}
private createURL(path: string) {
// Trim trailing slashes because it is assumed to be included in path
return `${this.url.replace(/\/+$/g, '')}${path}`;
}
/**
*
* @param options contains generic configuration for rpc calls
*
* @description Get the block's hash, its unique identifier.
*
* @see http://tezos.gitlab.io/mainnet/api/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 address address from which we want to retrieve the balance
* @param options contains generic configuration for rpc calls
*
* @description Access the balance of a contract.
*
* @see http://tezos.gitlab.io/master/api/rpc.html#get-block-id-context-contracts-contract-id-balance
*/
async getBalance(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
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 contract address from which we want to retrieve the storage
* @param options contains generic configuration for rpc calls
*
* @description Access the data of the contract.
*
* @see http://tezos.gitlab.io/master/api/rpc.html#get-block-id-context-contracts-contract-id-storage
*/
async getStorage(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<StorageResponse> {
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
*
* @description Access the code and data of the contract.
*
* @see http://tezos.gitlab.io/master/api/rpc.html#get-block-id-context-contracts-contract-id-script
*/
async getScript(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<ScriptResponse> {
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
* @param options contains generic configuration for rpc calls
*
* @description Access the complete status of a contract.
*
* @see http://tezos.gitlab.io/master/api/rpc.html#get-block-id-context-contracts-contract-id
*/
async getContract(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<ContractResponse> {
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
*
* @deprecated Remove in 005
*
* @description Access the manager of a contract.
*
* @see http://tezos.gitlab.io/master/api/rpc.html#get-block-id-context-contracts-contract-id-manager
*/
async getManager(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<ManagerResponse> {
return this.httpBackend.createRequest<ManagerResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/manager`
),
method: 'GET',
});
}
/**
*
* @param address contract address from which we want to retrieve the manager
* @param options contains generic configuration for rpc calls
*
* @description Access the manager key of a contract.
*
* @see http://tezos.gitlab.io/master/api/rpc.html#get-block-id-context-contracts-contract-id-manager-key
*/
async getManagerKey(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<ManagerKeyResponse> {
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
*
* @description Access the delegate of a contract, if any.
*
* @see http://tezos.gitlab.io/master/api/rpc.html#get-block-id-context-contracts-contract-id-delegate
*/
async getDelegate(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<DelegateResponse> {
return this.httpBackend.createRequest<DelegateResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/delegate`
),
method: 'GET',
});
}
/**
*
* @param address contract address from which we want to retrieve the big map key
* @param options contains generic configuration for rpc calls
*
* @description Access the value associated with a key in the big map storage of the contract.
*
* @see http://tezos.gitlab.io/master/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> {
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
*
* @description Access the value associated with a key in a big map.
*
* @see https://tezos.gitlab.io/mainnet/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> {
return this.httpBackend.createRequest<BigMapResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/context/big_maps/${id}/${expr}`),
method: 'GET',
});
}
/**
*
* @param address delegate address which we want to retrieve
* @param options contains generic configuration for rpc calls
*
* @description Fetches information about a delegate from RPC.
*
* @see http://tezos.gitlab.io/mainnet/api/rpc.html#get-block-id-context-delegates-pkh
*/
async getDelegates(
address: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<DelegatesResponse> {
const response = await this.httpBackend.createRequest<DelegatesResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/context/delegates/${address}`),
method: 'GET',
});
return {
deactivated: response.deactivated,
balance: new BigNumber(response.balance),
frozen_balance: new BigNumber(response.frozen_balance),
frozen_balance_by_cycle: response.frozen_balance_by_cycle.map(
({ deposit, fees, rewards, ...rest }) => ({
...rest,
deposit: new BigNumber(deposit),
fees: new BigNumber(fees),
rewards: new BigNumber(rewards),
})
),
staking_balance: new BigNumber(response.staking_balance),
delegated_contracts: response.delegated_contracts,
delegated_balance: new BigNumber(response.delegated_balance),
grace_period: response.grace_period,
};
}
/**
*
* @param options contains generic configuration for rpc calls
*
* @description All constants
*
* @see http://tezos.gitlab.io/master/api/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',
'block_security_deposit',
'endorsement_security_deposit',
'block_reward',
'endorsement_reward',
'cost_per_byte',
'hard_storage_limit_per_operation',
]);
return {
...response,
...(castedResponse as ConstantsResponse),
};
}
/**
*
* @param options contains generic configuration for rpc calls
*
* @description All the information about a block
*
* @see http://tezos.gitlab.io/master/api/rpc.html#get-block-id
*/
async getBlock({ block }: RPCOptions = defaultRPCOptions): Promise<BlockResponse> {
const response = await this.httpBackend.createRequest<BlockResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}`),
method: 'GET',
});
return response;
}
/**
*
* @param options contains generic configuration for rpc calls
*
* @description The whole block header
*
* @see https://tezos.gitlab.io/tezos/api/rpc.html#get-block-id-header
*/
async getBlockHeader({ block }: RPCOptions = defaultRPCOptions): Promise<BlockHeaderResponse> {
const response = await this.httpBackend.createRequest<RawBlockHeaderResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/header`),
method: 'GET',
});
return response;
}
/**
*
* @param options contains generic configuration for rpc calls
*
* @description All the metadata associated to the block
*
* @see https://tezos.gitlab.io/tezos/api/rpc.html#get-block-id-metadata
*/
async getBlockMetadata({ block }: RPCOptions = defaultRPCOptions): Promise<BlockMetadata> {
const response = await this.httpBackend.createRequest<BlockMetadata>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/metadata`),
method: 'GET',
});
return response;
}
/**
*
* @param args contains optional query arguments
* @param options contains generic configuration for rpc calls
*
* @description Retrieves the list of delegates allowed to bake a block.
*
* @see https://tezos.gitlab.io/mainnet/api/rpc.html#get-block-id-helpers-baking-rights
*/
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
* @param options contains generic configuration for rpc calls
*
* @description Retrieves the list of delegates allowed to bake a block.
*
* @see https://tezos.gitlab.io/mainnet/api/rpc.html#get-block-id-helpers-endorsing-rights
*/
async getEndorsingRights(
args: EndorsingRightsQueryArguments = {},
{ block }: RPCOptions = defaultRPCOptions
): Promise<EndorsingRightsResponse> {
const response = await this.httpBackend.createRequest<EndorsingRightsResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/helpers/endorsing_rights`),
method: 'GET',
query: args,
});
return response;
}
/**
* @param options contains generic configuration for rpc calls
*
* @description Ballots casted so far during a voting period
*
* @see https://tezos.gitlab.io/mainnet/api/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
*
* @description Sum of ballots casted so far during a voting period.
*
* @see https://tezos.gitlab.io/mainnet/api/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',
});
return response;
}
/**
*
* @param options contains generic configuration for rpc calls
*
* @description Current period kind.
*
* @see https://tezos.gitlab.io/mainnet/api/rpc.html#get-block-id-votes-current-period-kind
*/
async getCurrentPeriodKind({ block }: RPCOptions = defaultRPCOptions): Promise<
PeriodKindResponse
> {
const response = await this.httpBackend.createRequest<PeriodKindResponse>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/votes/current_period_kind`),
method: 'GET',
});
return response;
}
/**
*
* @param options contains generic configuration for rpc calls
*
* @description Current proposal under evaluation.
*
* @see https://tezos.gitlab.io/mainnet/api/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
*
* @description Current expected quorum.
*
* @see https://tezos.gitlab.io/mainnet/api/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
*
* @description List of delegates with their voting weight, in number of rolls.
*
* @see https://tezos.gitlab.io/mainnet/api/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',
});
return response;
}
/**
*
* @param options contains generic configuration for rpc calls
*
* @description List of proposals with number of supporters.
*
* @see https://tezos.gitlab.io/mainnet/api/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',
});
return response;
}
/**
*
* @param data operation contents to forge
* @param options contains generic configuration for rpc calls
*
* @description Forge an operation returning the unsigned bytes
*
* @see https://tezos.gitlab.io/tezos/api/rpc.html#post-block-id-helpers-forge-operations
*/
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. Returns the ID of the operation. The `signedOperationContents` should be constructed using a contextual RPCs from the latest block and signed by the client. By default, the RPC will wait for the operation to be (pre-)validated before answering. See RPCs under /blocks/prevalidation for more details on the prevalidation context.
*
* @see https://tezos.gitlab.io/tezos/api/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
*
* @description Simulate the validation of an operation
*
* @see https://tezos.gitlab.io/tezos/api/rpc.html#post-block-id-helpers-preapply-operations
*/
async preapplyOperations(
ops: PreapplyParams,
{ block }: RPCOptions = defaultRPCOptions
): Promise<PreapplyResponse[]> {
const response = await this.httpBackend.createRequest<PreapplyResponse[]>(
{
url: this.createURL(`/chains/${this.chain}/blocks/${block}/helpers/preapply/operations`),
method: 'POST',
},
ops
);
return response;
}
/**
*
* @param contract address of the contract we want to get the entrypoints of
*
* @description Return the list of entrypoints of the contract
*
* @see http://tezos.gitlab.io/zeronet/api/rpc.html#get-block-id-context-contracts-contract-id-entrypoints
*
* @version 005_PsBABY5H
*/
async getEntrypoints(
contract: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<EntrypointsResponse> {
const contractResponse = await this.httpBackend.createRequest<{
entrypoints: { [key: string]: Object };
}>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${contract}/entrypoints`
),
method: 'GET',
});
return contractResponse;
}
/**
* @param op Operation to run
* @param options contains generic configuration for rpc calls
*
* @description Run an operation without signature checks
*
* @see https://tezos.gitlab.io/mainnet/api/rpc.html#post-block-id-helpers-scripts-run-operation
*/
async runOperation(
op: RPCRunOperationParam,
{ block }: RPCOptions = defaultRPCOptions
): Promise<PreapplyResponse> {
const response = await this.httpBackend.createRequest<any>(
{
url: this.createURL(`/chains/${this.chain}/blocks/${block}/helpers/scripts/run_operation`),
method: 'POST',
},
op
);
return response;
}
async getChainId() {
return this.httpBackend.createRequest<string>({
url: this.createURL(`/chains/${this.chain}/chain_id`),
method: 'GET',
});
}
/**
*
* @param data Data to pack
* @param options contains generic configuration for rpc calls
*
* @description Computes the serialized version of a data expression using the same algorithm as script instruction PACK
*
* @example packData({ data: { string: "test" }, type: { prim: "string" } })
*
* @see http://tezos.gitlab.io/mainnet/api/rpc.html#post-block-id-helpers-scripts-pack-data
*/
async packData(data: PackDataParams, { block }: RPCOptions = defaultRPCOptions) {
const { gas, ...rest } = await this.httpBackend.createRequest<PackDataResponse>(
{
url: this.createURL(`/chains/${this.chain}/blocks/${block}/helpers/scripts/pack_data`),
method: 'POST',
},
data
);
let formattedGas = gas;
const tryBigNumber = new BigNumber(gas || '');
if (!tryBigNumber.isNaN()) {
formattedGas = tryBigNumber;
}
return { gas: formattedGas, ...rest };
}
}