starknet 4.4.2 → 4.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +3 -1
  3. package/__tests__/defaultProvider.test.ts +7 -9
  4. package/__tests__/rpcProvider.test.ts +107 -12
  5. package/__tests__/utils/utils.test.ts +17 -0
  6. package/account/default.js +13 -7
  7. package/dist/account/default.js +13 -7
  8. package/dist/provider/default.d.ts +1 -0
  9. package/dist/provider/default.js +7 -0
  10. package/dist/provider/interface.d.ts +6 -0
  11. package/dist/provider/rpc.d.ts +12 -2
  12. package/dist/provider/rpc.js +126 -70
  13. package/dist/provider/sequencer.d.ts +1 -0
  14. package/dist/provider/sequencer.js +9 -1
  15. package/dist/provider/utils.d.ts +11 -35
  16. package/dist/provider/utils.js +52 -63
  17. package/dist/types/api/openrpc.d.ts +392 -32
  18. package/dist/types/api/openrpc.js +21 -3
  19. package/dist/types/api/rpc.d.ts +74 -107
  20. package/dist/utils/responseParser/rpc.d.ts +7 -4
  21. package/dist/utils/responseParser/rpc.js +1 -1
  22. package/package.json +4 -1
  23. package/provider/default.d.ts +1 -0
  24. package/provider/default.js +7 -0
  25. package/provider/interface.d.ts +6 -0
  26. package/provider/rpc.d.ts +12 -2
  27. package/provider/rpc.js +126 -70
  28. package/provider/sequencer.d.ts +1 -0
  29. package/provider/sequencer.js +9 -1
  30. package/provider/utils.d.ts +11 -35
  31. package/provider/utils.js +52 -63
  32. package/src/account/default.ts +4 -2
  33. package/src/provider/default.ts +4 -0
  34. package/src/provider/interface.ts +7 -0
  35. package/src/provider/rpc.ts +90 -54
  36. package/src/provider/sequencer.ts +7 -2
  37. package/src/provider/utils.ts +43 -56
  38. package/src/types/api/openrpc.ts +371 -41
  39. package/src/types/api/rpc.ts +71 -125
  40. package/src/utils/responseParser/rpc.ts +9 -5
  41. package/types/api/openrpc.d.ts +392 -32
  42. package/types/api/openrpc.js +21 -3
  43. package/types/api/rpc.d.ts +74 -107
  44. package/utils/responseParser/rpc.d.ts +7 -4
  45. package/utils/responseParser/rpc.js +1 -1
@@ -58,54 +58,45 @@ var RpcProvider = /** @class */ (function () {
58
58
  _this.chainId = chainId;
59
59
  });
60
60
  }
61
+ RpcProvider.prototype.fetch = function (method, params) {
62
+ return (0, fetchPonyfill_1.default)(this.nodeUrl, {
63
+ method: 'POST',
64
+ body: (0, json_1.stringify)({ method: method, jsonrpc: '2.0', params: params, id: 0 }),
65
+ headers: { 'Content-Type': 'application/json' },
66
+ });
67
+ };
68
+ RpcProvider.prototype.errorHandler = function (error) {
69
+ if (error) {
70
+ var code = error.code, message = error.message;
71
+ throw new Error("".concat(code, ": ").concat(message));
72
+ }
73
+ };
61
74
  RpcProvider.prototype.fetchEndpoint = function (method, request) {
62
75
  var _a;
63
76
  return __awaiter(this, void 0, void 0, function () {
64
- var requestData, rawResult, _b, error, result, code, message, error_1, data;
77
+ var rawResult, _b, error, result, error_1;
65
78
  return __generator(this, function (_c) {
66
79
  switch (_c.label) {
67
80
  case 0:
68
- requestData = {
69
- method: method,
70
- jsonrpc: '2.0',
71
- params: request,
72
- id: 0,
73
- };
74
- _c.label = 1;
81
+ _c.trys.push([0, 3, , 4]);
82
+ return [4 /*yield*/, this.fetch(method, request)];
75
83
  case 1:
76
- _c.trys.push([1, 4, , 5]);
77
- return [4 /*yield*/, (0, fetchPonyfill_1.default)(this.nodeUrl, {
78
- method: 'POST',
79
- body: (0, json_1.stringify)(requestData),
80
- headers: {
81
- 'Content-Type': 'application/json',
82
- },
83
- })];
84
- case 2:
85
84
  rawResult = _c.sent();
86
85
  return [4 /*yield*/, rawResult.json()];
87
- case 3:
86
+ case 2:
88
87
  _b = _c.sent(), error = _b.error, result = _b.result;
89
- if (error) {
90
- code = error.code, message = error.message;
91
- throw new Error("".concat(code, ": ").concat(message));
92
- }
93
- else {
94
- return [2 /*return*/, result];
95
- }
96
- return [3 /*break*/, 5];
97
- case 4:
88
+ this.errorHandler(error);
89
+ return [2 /*return*/, result];
90
+ case 3:
98
91
  error_1 = _c.sent();
99
- data = (_a = error_1 === null || error_1 === void 0 ? void 0 : error_1.response) === null || _a === void 0 ? void 0 : _a.data;
100
- if (data === null || data === void 0 ? void 0 : data.message) {
101
- throw new Error("".concat(data.code, ": ").concat(data.message));
102
- }
92
+ this.errorHandler((_a = error_1 === null || error_1 === void 0 ? void 0 : error_1.response) === null || _a === void 0 ? void 0 : _a.data);
103
93
  throw error_1;
104
- case 5: return [2 /*return*/];
94
+ case 4: return [2 /*return*/];
105
95
  }
106
96
  });
107
97
  });
108
98
  };
99
+ // Methods from Interface
109
100
  RpcProvider.prototype.getChainId = function () {
110
101
  return __awaiter(this, void 0, void 0, function () {
111
102
  return __generator(this, function (_a) {
@@ -113,7 +104,7 @@ var RpcProvider = /** @class */ (function () {
113
104
  });
114
105
  });
115
106
  };
116
- // Common Interface
107
+ // Methods from Interface
117
108
  RpcProvider.prototype.getBlock = function (blockIdentifier) {
118
109
  if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
119
110
  return __awaiter(this, void 0, void 0, function () {
@@ -122,25 +113,39 @@ var RpcProvider = /** @class */ (function () {
122
113
  });
123
114
  });
124
115
  };
116
+ RpcProvider.prototype.getBlockHashAndNumber = function () {
117
+ return __awaiter(this, void 0, void 0, function () {
118
+ return __generator(this, function (_a) {
119
+ return [2 /*return*/, this.fetchEndpoint('starknet_blockHashAndNumber')];
120
+ });
121
+ });
122
+ };
125
123
  RpcProvider.prototype.getBlockWithTxHashes = function (blockIdentifier) {
126
124
  if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
127
125
  return __awaiter(this, void 0, void 0, function () {
128
- var blockIdentifierGetter;
126
+ var block;
129
127
  return __generator(this, function (_a) {
130
- blockIdentifierGetter = new utils_1.BlockIdentifierClass(blockIdentifier);
131
- return [2 /*return*/, this.fetchEndpoint('starknet_getBlockWithTxHashes', [
132
- blockIdentifierGetter.getIdentifier(),
133
- ])];
128
+ block = new utils_1.Block(blockIdentifier);
129
+ return [2 /*return*/, this.fetchEndpoint('starknet_getBlockWithTxHashes', [block.identifier])];
134
130
  });
135
131
  });
136
132
  };
137
133
  RpcProvider.prototype.getBlockWithTxs = function (blockIdentifier) {
138
134
  if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
139
135
  return __awaiter(this, void 0, void 0, function () {
140
- var blockIdentifierGetter;
136
+ var block;
137
+ return __generator(this, function (_a) {
138
+ block = new utils_1.Block(blockIdentifier);
139
+ return [2 /*return*/, this.fetchEndpoint('starknet_getBlockWithTxs', [block.identifier])];
140
+ });
141
+ });
142
+ };
143
+ RpcProvider.prototype.getClassHashAt = function (blockIdentifier, contractAddress) {
144
+ return __awaiter(this, void 0, void 0, function () {
145
+ var block;
141
146
  return __generator(this, function (_a) {
142
- blockIdentifierGetter = new utils_1.BlockIdentifierClass(blockIdentifier);
143
- return [2 /*return*/, this.fetchEndpoint('starknet_getBlockWithTxs', [blockIdentifierGetter.getIdentifier()])];
147
+ block = new utils_1.Block(blockIdentifier);
148
+ return [2 /*return*/, this.fetchEndpoint('starknet_getClassHashAt', [block.identifier, contractAddress])];
144
149
  });
145
150
  });
146
151
  };
@@ -151,22 +156,45 @@ var RpcProvider = /** @class */ (function () {
151
156
  });
152
157
  });
153
158
  };
159
+ RpcProvider.prototype.getPendingTransactions = function () {
160
+ return __awaiter(this, void 0, void 0, function () {
161
+ return __generator(this, function (_a) {
162
+ return [2 /*return*/, this.fetchEndpoint('starknet_pendingTransactions')];
163
+ });
164
+ });
165
+ };
166
+ RpcProvider.prototype.getProtocolVersion = function () {
167
+ return __awaiter(this, void 0, void 0, function () {
168
+ return __generator(this, function (_a) {
169
+ throw new Error('Pathfinder does not implement this rpc 0.1.0 method');
170
+ });
171
+ });
172
+ };
173
+ RpcProvider.prototype.getStateUpdate = function (blockIdentifier) {
174
+ return __awaiter(this, void 0, void 0, function () {
175
+ var block;
176
+ return __generator(this, function (_a) {
177
+ block = new utils_1.Block(blockIdentifier);
178
+ return [2 /*return*/, this.fetchEndpoint('starknet_getStateUpdate', [block.identifier])];
179
+ });
180
+ });
181
+ };
154
182
  RpcProvider.prototype.getStorageAt = function (contractAddress, key, blockIdentifier) {
155
183
  if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
156
184
  return __awaiter(this, void 0, void 0, function () {
157
- var parsedKey, blockIdentifierGetter;
185
+ var parsedKey, block;
158
186
  return __generator(this, function (_a) {
159
187
  parsedKey = (0, number_1.toHex)((0, number_1.toBN)(key));
160
- blockIdentifierGetter = new utils_1.BlockIdentifierClass(blockIdentifier);
188
+ block = new utils_1.Block(blockIdentifier);
161
189
  return [2 /*return*/, this.fetchEndpoint('starknet_getStorageAt', [
162
190
  contractAddress,
163
191
  parsedKey,
164
- blockIdentifierGetter.getIdentifier(),
192
+ block.identifier,
165
193
  ])];
166
194
  });
167
195
  });
168
196
  };
169
- // common interface
197
+ // Methods from Interface
170
198
  RpcProvider.prototype.getTransaction = function (txHash) {
171
199
  return __awaiter(this, void 0, void 0, function () {
172
200
  return __generator(this, function (_a) {
@@ -183,8 +211,13 @@ var RpcProvider = /** @class */ (function () {
183
211
  };
184
212
  RpcProvider.prototype.getTransactionByBlockIdAndIndex = function (blockIdentifier, index) {
185
213
  return __awaiter(this, void 0, void 0, function () {
214
+ var block;
186
215
  return __generator(this, function (_a) {
187
- return [2 /*return*/, this.fetchEndpoint('starknet_getTransactionByHash', [blockIdentifier, index])];
216
+ block = new utils_1.Block(blockIdentifier);
217
+ return [2 /*return*/, this.fetchEndpoint('starknet_getTransactionByBlockIdAndIndex', [
218
+ block.identifier,
219
+ index,
220
+ ])];
188
221
  });
189
222
  });
190
223
  };
@@ -195,15 +228,26 @@ var RpcProvider = /** @class */ (function () {
195
228
  });
196
229
  });
197
230
  };
231
+ RpcProvider.prototype.getClass = function (classHash) {
232
+ return __awaiter(this, void 0, void 0, function () {
233
+ return __generator(this, function (_a) {
234
+ return [2 /*return*/, this.fetchEndpoint('starknet_getClass', [classHash])];
235
+ });
236
+ });
237
+ };
198
238
  RpcProvider.prototype.getClassAt = function (contractAddress, blockIdentifier) {
199
239
  return __awaiter(this, void 0, void 0, function () {
200
- var blockIdentifierGetter;
240
+ var block;
201
241
  return __generator(this, function (_a) {
202
- blockIdentifierGetter = new utils_1.BlockIdentifierClass(blockIdentifier);
203
- return [2 /*return*/, this.fetchEndpoint('starknet_getClassAt', [
204
- blockIdentifierGetter.getIdentifier(),
205
- contractAddress,
206
- ])];
242
+ block = new utils_1.Block(blockIdentifier);
243
+ return [2 /*return*/, this.fetchEndpoint('starknet_getClassAt', [block.identifier, contractAddress])];
244
+ });
245
+ });
246
+ };
247
+ RpcProvider.prototype.getCode = function (_contractAddress, _blockIdentifier) {
248
+ return __awaiter(this, void 0, void 0, function () {
249
+ return __generator(this, function (_a) {
250
+ throw new Error('RPC 0.1.0 does not implement getCode function');
207
251
  });
208
252
  });
209
253
  };
@@ -211,7 +255,9 @@ var RpcProvider = /** @class */ (function () {
211
255
  if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
212
256
  if (invocationDetails === void 0) { invocationDetails = {}; }
213
257
  return __awaiter(this, void 0, void 0, function () {
258
+ var block_id;
214
259
  return __generator(this, function (_a) {
260
+ block_id = new utils_1.Block(blockIdentifier).identifier;
215
261
  return [2 /*return*/, this.fetchEndpoint('starknet_estimateFee', [
216
262
  {
217
263
  contract_address: invocation.contractAddress,
@@ -220,7 +266,7 @@ var RpcProvider = /** @class */ (function () {
220
266
  signature: (0, number_1.bigNumberishArrayToHexadecimalStringArray)(invocation.signature || []),
221
267
  version: (0, number_1.toHex)((0, number_1.toBN)((invocationDetails === null || invocationDetails === void 0 ? void 0 : invocationDetails.version) || 0)),
222
268
  },
223
- blockIdentifier,
269
+ block_id,
224
270
  ]).then(this.responseParser.parseFeeEstimateResponse)];
225
271
  });
226
272
  });
@@ -235,6 +281,7 @@ var RpcProvider = /** @class */ (function () {
235
281
  {
236
282
  program: contractDefinition.program,
237
283
  entry_points_by_type: contractDefinition.entry_points_by_type,
284
+ abi: contractDefinition.abi, // rpc 2.0
238
285
  },
239
286
  (0, number_1.toHex)((0, number_1.toBN)(version || 0)),
240
287
  ]).then(this.responseParser.parseDeclareContractResponse)];
@@ -253,6 +300,7 @@ var RpcProvider = /** @class */ (function () {
253
300
  {
254
301
  program: contractDefinition.program,
255
302
  entry_points_by_type: contractDefinition.entry_points_by_type,
303
+ abi: contractDefinition.abi, // rpc 2.0
256
304
  },
257
305
  ]).then(this.responseParser.parseDeployContractResponse)];
258
306
  });
@@ -274,20 +322,23 @@ var RpcProvider = /** @class */ (function () {
274
322
  });
275
323
  });
276
324
  };
325
+ // Methods from Interface
277
326
  RpcProvider.prototype.callContract = function (call, blockIdentifier) {
278
327
  if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
279
328
  return __awaiter(this, void 0, void 0, function () {
280
- var result;
329
+ var block_id, result;
281
330
  return __generator(this, function (_a) {
282
331
  switch (_a.label) {
283
- case 0: return [4 /*yield*/, this.fetchEndpoint('starknet_call', [
284
- {
285
- contract_address: call.contractAddress,
286
- entry_point_selector: (0, hash_1.getSelectorFromName)(call.entrypoint),
287
- calldata: (0, provider_1.parseCalldata)(call.calldata),
288
- },
289
- blockIdentifier,
290
- ])];
332
+ case 0:
333
+ block_id = new utils_1.Block(blockIdentifier).identifier;
334
+ return [4 /*yield*/, this.fetchEndpoint('starknet_call', [
335
+ {
336
+ contract_address: call.contractAddress,
337
+ entry_point_selector: (0, hash_1.getSelectorFromName)(call.entrypoint),
338
+ calldata: (0, provider_1.parseCalldata)(call.calldata),
339
+ },
340
+ block_id,
341
+ ])];
291
342
  case 1:
292
343
  result = _a.sent();
293
344
  return [2 /*return*/, this.responseParser.parseCallContractResponse(result)];
@@ -295,10 +346,17 @@ var RpcProvider = /** @class */ (function () {
295
346
  });
296
347
  });
297
348
  };
298
- RpcProvider.prototype.getCode = function (_contractAddress, _blockIdentifier) {
349
+ RpcProvider.prototype.traceTransaction = function (transactionHash) {
299
350
  return __awaiter(this, void 0, void 0, function () {
300
351
  return __generator(this, function (_a) {
301
- throw new Error('RPC 0.1.0 does not implement getCode function');
352
+ return [2 /*return*/, this.fetchEndpoint('starknet_traceTransaction', [transactionHash])];
353
+ });
354
+ });
355
+ };
356
+ RpcProvider.prototype.traceBlockTransactions = function (blockHash) {
357
+ return __awaiter(this, void 0, void 0, function () {
358
+ return __generator(this, function (_a) {
359
+ return [2 /*return*/, this.fetchEndpoint('starknet_traceBlockTransactions', [blockHash])];
302
360
  });
303
361
  });
304
362
  };
@@ -310,7 +368,7 @@ var RpcProvider = /** @class */ (function () {
310
368
  switch (_a.label) {
311
369
  case 0:
312
370
  onchain = false;
313
- retries = 100;
371
+ retries = 200;
314
372
  _a.label = 1;
315
373
  case 1:
316
374
  if (!!onchain) return [3 /*break*/, 7];
@@ -366,12 +424,10 @@ var RpcProvider = /** @class */ (function () {
366
424
  */
367
425
  RpcProvider.prototype.getTransactionCount = function (blockIdentifier) {
368
426
  return __awaiter(this, void 0, void 0, function () {
369
- var blockIdentifierGetter;
427
+ var block;
370
428
  return __generator(this, function (_a) {
371
- blockIdentifierGetter = new utils_1.BlockIdentifierClass(blockIdentifier);
372
- return [2 /*return*/, this.fetchEndpoint('starknet_getBlockTransactionCount', [
373
- blockIdentifierGetter.getIdentifier(),
374
- ])];
429
+ block = new utils_1.Block(blockIdentifier);
430
+ return [2 /*return*/, this.fetchEndpoint('starknet_getBlockTransactionCount', [block.identifier])];
375
431
  });
376
432
  });
377
433
  };
@@ -27,6 +27,7 @@ export declare class SequencerProvider implements ProviderInterface {
27
27
  private getQueryString;
28
28
  private getHeaders;
29
29
  protected fetchEndpoint<T extends keyof Sequencer.Endpoints>(endpoint: T, ...[query, request]: Sequencer.Endpoints[T]['QUERY'] extends never ? Sequencer.Endpoints[T]['REQUEST'] extends never ? [] : [undefined, Sequencer.Endpoints[T]['REQUEST']] : Sequencer.Endpoints[T]['REQUEST'] extends never ? [Sequencer.Endpoints[T]['QUERY']] : [Sequencer.Endpoints[T]['QUERY'], Sequencer.Endpoints[T]['REQUEST']]): Promise<Sequencer.Endpoints[T]['RESPONSE']>;
30
+ getChainId(): Promise<StarknetChainId>;
30
31
  callContract({ contractAddress, entrypoint: entryPointSelector, calldata }: Call, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
31
32
  getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
32
33
  getStorageAt(contractAddress: string, key: BigNumberish, blockIdentifier?: BlockIdentifier): Promise<BigNumberish>;
@@ -134,7 +134,8 @@ var SequencerProvider = /** @class */ (function () {
134
134
  .map(function (_a) {
135
135
  var _b = __read(_a, 2), key = _b[0], value = _b[1];
136
136
  if (key === 'blockIdentifier') {
137
- return "".concat((0, utils_1.getFormattedBlockIdentifier)(value));
137
+ var block = new utils_1.Block(value);
138
+ return "".concat(block.queryIdentifier);
138
139
  }
139
140
  return "".concat(key, "=").concat(value);
140
141
  })
@@ -227,6 +228,13 @@ var SequencerProvider = /** @class */ (function () {
227
228
  });
228
229
  });
229
230
  };
231
+ SequencerProvider.prototype.getChainId = function () {
232
+ return __awaiter(this, void 0, void 0, function () {
233
+ return __generator(this, function (_a) {
234
+ return [2 /*return*/, Promise.resolve(this.chainId)];
235
+ });
236
+ });
237
+ };
230
238
  SequencerProvider.prototype.callContract = function (_a, blockIdentifier) {
231
239
  var contractAddress = _a.contractAddress, entryPointSelector = _a.entrypoint, _b = _a.calldata, calldata = _b === void 0 ? [] : _b;
232
240
  if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
@@ -1,4 +1,3 @@
1
- /// <reference types="bn.js" />
2
1
  import type { BlockNumber } from '../types';
3
2
  import { BigNumberish } from '../utils/number';
4
3
  /**
@@ -17,38 +16,15 @@ export declare function formatHash(hashValue: BigNumberish): string;
17
16
  */
18
17
  export declare function txIdentifier(txHash?: BigNumberish, txId?: BigNumberish): string;
19
18
  export declare type BlockIdentifier = BlockNumber | BigNumberish;
20
- declare type BlockIdentifierObject = {
21
- type: 'BLOCK_NUMBER';
22
- data: BlockNumber;
23
- } | {
24
- type: 'BLOCK_HASH';
25
- data: BigNumberish;
26
- };
27
- export declare class BlockIdentifierClass {
28
- blockIdentifier: BlockIdentifier;
29
- constructor(blockIdentifier: BlockIdentifier);
30
- getIdentifier(): string | import("bn.js") | {
31
- block_hash: string;
32
- block_number?: undefined;
33
- } | {
34
- block_number: number;
35
- block_hash?: undefined;
36
- } | null;
19
+ export declare class Block {
20
+ hash: BlockIdentifier;
21
+ number: BlockIdentifier;
22
+ tag: BlockIdentifier;
23
+ private setIdentifier;
24
+ constructor(_identifier: BlockIdentifier);
25
+ get queryIdentifier(): any;
26
+ get identifier(): any;
27
+ set identifier(_identifier: BlockIdentifier);
28
+ valueOf: () => BlockIdentifier;
29
+ toString: () => BlockIdentifier;
37
30
  }
38
- /**
39
- * Identifies the block to be queried.
40
- *
41
- * @param blockIdentifier - block identifier
42
- * @returns block identifier object
43
- */
44
- export declare function getBlockIdentifier(blockIdentifier: BlockIdentifier): BlockIdentifierObject;
45
- /**
46
- * Gets the block identifier for API request
47
- *
48
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L164-L173)
49
- *
50
- * @param blockIdentifier
51
- * @returns block identifier for API request
52
- */
53
- export declare function getFormattedBlockIdentifier(blockIdentifier?: BlockIdentifier): string;
54
- export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFormattedBlockIdentifier = exports.getBlockIdentifier = exports.BlockIdentifierClass = exports.txIdentifier = exports.formatHash = void 0;
3
+ exports.Block = exports.txIdentifier = exports.formatHash = void 0;
4
4
  var number_1 = require("../utils/number");
5
5
  /**
6
6
  *
@@ -29,67 +29,56 @@ function txIdentifier(txHash, txId) {
29
29
  return "transactionHash=".concat(hashString);
30
30
  }
31
31
  exports.txIdentifier = txIdentifier;
32
- var BlockIdentifierClass = /** @class */ (function () {
33
- function BlockIdentifierClass(blockIdentifier) {
34
- this.blockIdentifier = blockIdentifier;
32
+ var Block = /** @class */ (function () {
33
+ function Block(_identifier) {
34
+ var _this = this;
35
+ this.hash = null;
36
+ this.number = null;
37
+ this.tag = null;
38
+ this.valueOf = function () { return _this.number; };
39
+ this.toString = function () { return _this.hash; };
40
+ this.setIdentifier = function (__identifier) {
41
+ if (typeof __identifier === 'string' && (0, number_1.isHex)(__identifier)) {
42
+ this.hash = __identifier;
43
+ }
44
+ else if (typeof __identifier === 'number') {
45
+ this.number = __identifier;
46
+ }
47
+ else {
48
+ this.tag = __identifier;
49
+ }
50
+ };
51
+ this.setIdentifier(_identifier);
35
52
  }
36
- BlockIdentifierClass.prototype.getIdentifier = function () {
37
- if (typeof this.blockIdentifier === 'string' && (0, number_1.isHex)(this.blockIdentifier)) {
38
- return { block_hash: this.blockIdentifier };
39
- }
40
- if (typeof this.blockIdentifier === 'number') {
41
- return { block_number: this.blockIdentifier };
42
- }
43
- return this.blockIdentifier;
44
- };
45
- return BlockIdentifierClass;
53
+ Object.defineProperty(Block.prototype, "queryIdentifier", {
54
+ get: function () {
55
+ if (this.number !== null) {
56
+ return "blockNumber=".concat(this.number);
57
+ }
58
+ if (this.hash !== null) {
59
+ return "blockHash=".concat(this.hash);
60
+ }
61
+ return "blockNumber=".concat(this.tag);
62
+ },
63
+ enumerable: false,
64
+ configurable: true
65
+ });
66
+ Object.defineProperty(Block.prototype, "identifier", {
67
+ get: function () {
68
+ if (this.number !== null) {
69
+ return { block_number: this.number };
70
+ }
71
+ if (this.hash !== null) {
72
+ return { block_hash: this.hash };
73
+ }
74
+ return this.tag;
75
+ },
76
+ set: function (_identifier) {
77
+ this.setIdentifier(_identifier);
78
+ },
79
+ enumerable: false,
80
+ configurable: true
81
+ });
82
+ return Block;
46
83
  }());
47
- exports.BlockIdentifierClass = BlockIdentifierClass;
48
- /**
49
- * Identifies the block to be queried.
50
- *
51
- * @param blockIdentifier - block identifier
52
- * @returns block identifier object
53
- */
54
- function getBlockIdentifier(blockIdentifier) {
55
- if (blockIdentifier === null || blockIdentifier === 'latest') {
56
- return { type: 'BLOCK_NUMBER', data: 'latest' }; // default to latest block
57
- }
58
- if (blockIdentifier === 'pending') {
59
- return { type: 'BLOCK_NUMBER', data: 'pending' };
60
- }
61
- if (typeof blockIdentifier === 'number' || typeof blockIdentifier === 'bigint') {
62
- return { type: 'BLOCK_NUMBER', data: blockIdentifier };
63
- }
64
- if (typeof blockIdentifier === 'string' && blockIdentifier.startsWith('0x')) {
65
- return { type: 'BLOCK_HASH', data: blockIdentifier };
66
- }
67
- if (typeof blockIdentifier === 'string' && !Number.isNaN(parseInt(blockIdentifier, 10))) {
68
- return { type: 'BLOCK_NUMBER', data: parseInt(blockIdentifier, 10) };
69
- }
70
- if (typeof blockIdentifier === 'string') {
71
- throw new Error("Invalid block identifier: ".concat(blockIdentifier));
72
- }
73
- return { type: 'BLOCK_HASH', data: blockIdentifier };
74
- }
75
- exports.getBlockIdentifier = getBlockIdentifier;
76
- /**
77
- * Gets the block identifier for API request
78
- *
79
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L164-L173)
80
- *
81
- * @param blockIdentifier
82
- * @returns block identifier for API request
83
- */
84
- function getFormattedBlockIdentifier(blockIdentifier) {
85
- if (blockIdentifier === void 0) { blockIdentifier = null; }
86
- var blockIdentifierObject = getBlockIdentifier(blockIdentifier);
87
- if (blockIdentifierObject.type === 'BLOCK_NUMBER' && blockIdentifierObject.data === null) {
88
- return '';
89
- }
90
- if (blockIdentifierObject.type === 'BLOCK_NUMBER') {
91
- return "blockNumber=".concat(blockIdentifierObject.data);
92
- }
93
- return "blockHash=".concat((0, number_1.toHex)((0, number_1.toBN)(blockIdentifierObject.data)));
94
- }
95
- exports.getFormattedBlockIdentifier = getFormattedBlockIdentifier;
84
+ exports.Block = Block;