starknet 5.22.0 → 5.23.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.
package/dist/index.js CHANGED
@@ -78,6 +78,7 @@ __export(src_exports, {
78
78
  fixStack: () => fixStack,
79
79
  getCalldata: () => getCalldata,
80
80
  getChecksumAddress: () => getChecksumAddress,
81
+ getDefaultNodeUrl: () => getDefaultNodeUrl,
81
82
  hash: () => hash_exports,
82
83
  isSierra: () => isSierra,
83
84
  isUrl: () => isUrl,
@@ -116,6 +117,8 @@ __export(constants_exports, {
116
117
  MASK_250: () => MASK_250,
117
118
  MASK_251: () => MASK_251,
118
119
  NetworkName: () => NetworkName,
120
+ RPC_GOERLI_NODES: () => RPC_GOERLI_NODES,
121
+ RPC_MAINNET_NODES: () => RPC_MAINNET_NODES,
119
122
  StarknetChainId: () => StarknetChainId,
120
123
  TEXT_TO_FELT_MAX_LEN: () => TEXT_TO_FELT_MAX_LEN,
121
124
  TransactionHashPrefix: () => TransactionHashPrefix,
@@ -141,6 +144,7 @@ __export(encode_exports, {
141
144
  stringToArrayBuffer: () => stringToArrayBuffer,
142
145
  utf8ToArray: () => utf8ToArray
143
146
  });
147
+ var import_base = require("@scure/base");
144
148
  var IS_BROWSER = typeof window !== "undefined";
145
149
  var STRING_ZERO = "0";
146
150
  function arrayBufferToString(array) {
@@ -153,10 +157,10 @@ function stringToArrayBuffer(str) {
153
157
  return utf8ToArray(str);
154
158
  }
155
159
  function atobUniversal(a) {
156
- return IS_BROWSER ? utf8ToArray(atob(a)) : Buffer.from(a, "base64");
160
+ return import_base.base64.decode(a);
157
161
  }
158
162
  function btoaUniversal(b) {
159
- return IS_BROWSER ? btoa(arrayBufferToString(b)) : Buffer.from(b).toString("base64");
163
+ return import_base.base64.encode(new Uint8Array(b));
160
164
  }
161
165
  function buf2hex(buffer) {
162
166
  return buffer.reduce((r, x) => r + x.toString(16).padStart(2, "0"), "");
@@ -236,6 +240,14 @@ var UDC = {
236
240
  ADDRESS: "0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf",
237
241
  ENTRYPOINT: "deployContract"
238
242
  };
243
+ var RPC_GOERLI_NODES = [
244
+ "https://starknet-testnet.public.blastapi.io/rpc/v0.5",
245
+ "https://limited-rpc.nethermind.io/goerli-juno/v0_5"
246
+ ];
247
+ var RPC_MAINNET_NODES = [
248
+ "https://starknet-mainnet.public.blastapi.io/rpc/v0.5",
249
+ "https://limited-rpc.nethermind.io/mainnet-juno/v0_5"
250
+ ];
239
251
 
240
252
  // src/types/index.ts
241
253
  var types_exports = {};
@@ -256,10 +268,10 @@ __export(types_exports, {
256
268
  });
257
269
 
258
270
  // src/types/account.ts
259
- var SIMULATION_FLAG = /* @__PURE__ */ ((SIMULATION_FLAG3) => {
260
- SIMULATION_FLAG3["SKIP_VALIDATE"] = "SKIP_VALIDATE";
261
- SIMULATION_FLAG3["SKIP_EXECUTE"] = "SKIP_EXECUTE";
262
- return SIMULATION_FLAG3;
271
+ var SIMULATION_FLAG = /* @__PURE__ */ ((SIMULATION_FLAG2) => {
272
+ SIMULATION_FLAG2["SKIP_VALIDATE"] = "SKIP_VALIDATE";
273
+ SIMULATION_FLAG2["SKIP_EXECUTE"] = "SKIP_EXECUTE";
274
+ return SIMULATION_FLAG2;
263
275
  })(SIMULATION_FLAG || {});
264
276
 
265
277
  // src/types/calldata.ts
@@ -293,12 +305,12 @@ var EntryPointType = /* @__PURE__ */ ((EntryPointType2) => {
293
305
  })(EntryPointType || {});
294
306
 
295
307
  // src/types/lib/index.ts
296
- var TransactionType = /* @__PURE__ */ ((TransactionType3) => {
297
- TransactionType3["DECLARE"] = "DECLARE";
298
- TransactionType3["DEPLOY"] = "DEPLOY";
299
- TransactionType3["DEPLOY_ACCOUNT"] = "DEPLOY_ACCOUNT";
300
- TransactionType3["INVOKE"] = "INVOKE_FUNCTION";
301
- return TransactionType3;
308
+ var TransactionType = /* @__PURE__ */ ((TransactionType2) => {
309
+ TransactionType2["DECLARE"] = "DECLARE";
310
+ TransactionType2["DEPLOY"] = "DEPLOY";
311
+ TransactionType2["DEPLOY_ACCOUNT"] = "DEPLOY_ACCOUNT";
312
+ TransactionType2["INVOKE"] = "INVOKE_FUNCTION";
313
+ return TransactionType2;
302
314
  })(TransactionType || {});
303
315
  var TransactionStatus = /* @__PURE__ */ ((TransactionStatus2) => {
304
316
  TransactionStatus2["NOT_RECEIVED"] = "NOT_RECEIVED";
@@ -309,18 +321,18 @@ var TransactionStatus = /* @__PURE__ */ ((TransactionStatus2) => {
309
321
  TransactionStatus2["REVERTED"] = "REVERTED";
310
322
  return TransactionStatus2;
311
323
  })(TransactionStatus || {});
312
- var TransactionFinalityStatus = /* @__PURE__ */ ((TransactionFinalityStatus3) => {
313
- TransactionFinalityStatus3["NOT_RECEIVED"] = "NOT_RECEIVED";
314
- TransactionFinalityStatus3["RECEIVED"] = "RECEIVED";
315
- TransactionFinalityStatus3["ACCEPTED_ON_L2"] = "ACCEPTED_ON_L2";
316
- TransactionFinalityStatus3["ACCEPTED_ON_L1"] = "ACCEPTED_ON_L1";
317
- return TransactionFinalityStatus3;
324
+ var TransactionFinalityStatus = /* @__PURE__ */ ((TransactionFinalityStatus2) => {
325
+ TransactionFinalityStatus2["NOT_RECEIVED"] = "NOT_RECEIVED";
326
+ TransactionFinalityStatus2["RECEIVED"] = "RECEIVED";
327
+ TransactionFinalityStatus2["ACCEPTED_ON_L2"] = "ACCEPTED_ON_L2";
328
+ TransactionFinalityStatus2["ACCEPTED_ON_L1"] = "ACCEPTED_ON_L1";
329
+ return TransactionFinalityStatus2;
318
330
  })(TransactionFinalityStatus || {});
319
- var TransactionExecutionStatus = /* @__PURE__ */ ((TransactionExecutionStatus3) => {
320
- TransactionExecutionStatus3["REJECTED"] = "REJECTED";
321
- TransactionExecutionStatus3["REVERTED"] = "REVERTED";
322
- TransactionExecutionStatus3["SUCCEEDED"] = "SUCCEEDED";
323
- return TransactionExecutionStatus3;
331
+ var TransactionExecutionStatus = /* @__PURE__ */ ((TransactionExecutionStatus2) => {
332
+ TransactionExecutionStatus2["REJECTED"] = "REJECTED";
333
+ TransactionExecutionStatus2["REVERTED"] = "REVERTED";
334
+ TransactionExecutionStatus2["SUCCEEDED"] = "SUCCEEDED";
335
+ return TransactionExecutionStatus2;
324
336
  })(TransactionExecutionStatus || {});
325
337
  var BlockStatus = /* @__PURE__ */ ((BlockStatus2) => {
326
338
  BlockStatus2["PENDING"] = "PENDING";
@@ -338,42 +350,68 @@ var BlockTag = /* @__PURE__ */ ((BlockTag2) => {
338
350
  // src/types/api/rpc.ts
339
351
  var rpc_exports = {};
340
352
  __export(rpc_exports, {
341
- SimulationFlag: () => SimulationFlag,
342
- TransactionExecutionStatus: () => TransactionExecutionStatus2,
343
- TransactionFinalityStatus: () => TransactionFinalityStatus2,
344
- TransactionType: () => TransactionType2
353
+ EBlockTag: () => EBlockTag,
354
+ EDataAvailabilityMode: () => EDataAvailabilityMode,
355
+ ESimulationFlag: () => ESimulationFlag,
356
+ ETransactionExecutionStatus: () => ETransactionExecutionStatus,
357
+ ETransactionFinalityStatus: () => ETransactionFinalityStatus,
358
+ ETransactionStatus: () => ETransactionStatus,
359
+ ETransactionType: () => ETransactionType,
360
+ Errors: () => errors_exports,
361
+ JRPC: () => jsonrpc_exports,
362
+ SPEC: () => components_exports
345
363
  });
346
364
 
347
- // src/types/api/openrpc.ts
348
- var TXN_TYPE = /* @__PURE__ */ ((TXN_TYPE2) => {
349
- TXN_TYPE2["DECLARE"] = "DECLARE";
350
- TXN_TYPE2["DEPLOY"] = "DEPLOY";
351
- TXN_TYPE2["DEPLOY_ACCOUNT"] = "DEPLOY_ACCOUNT";
352
- TXN_TYPE2["INVOKE"] = "INVOKE";
353
- TXN_TYPE2["L1_HANDLER"] = "L1_HANDLER";
354
- return TXN_TYPE2;
355
- })(TXN_TYPE || {});
356
- var TXN_FINALITY_STATUS = /* @__PURE__ */ ((TXN_FINALITY_STATUS2) => {
357
- TXN_FINALITY_STATUS2["ACCEPTED_ON_L2"] = "ACCEPTED_ON_L2";
358
- TXN_FINALITY_STATUS2["ACCEPTED_ON_L1"] = "ACCEPTED_ON_L1";
359
- return TXN_FINALITY_STATUS2;
360
- })(TXN_FINALITY_STATUS || {});
361
- var TXN_EXECUTION_STATUS = /* @__PURE__ */ ((TXN_EXECUTION_STATUS2) => {
362
- TXN_EXECUTION_STATUS2["SUCCEEDED"] = "SUCCEEDED";
363
- TXN_EXECUTION_STATUS2["REVERTED"] = "REVERTED";
364
- return TXN_EXECUTION_STATUS2;
365
- })(TXN_EXECUTION_STATUS || {});
366
- var SIMULATION_FLAG2 = /* @__PURE__ */ ((SIMULATION_FLAG3) => {
367
- SIMULATION_FLAG3["SKIP_VALIDATE"] = "SKIP_VALIDATE";
368
- SIMULATION_FLAG3["SKIP_FEE_CHARGE"] = "SKIP_FEE_CHARGE";
369
- return SIMULATION_FLAG3;
370
- })(SIMULATION_FLAG2 || {});
365
+ // src/types/api/jsonrpc/index.ts
366
+ var jsonrpc_exports = {};
371
367
 
372
- // src/types/api/rpc.ts
373
- var TransactionType2 = TXN_TYPE;
374
- var SimulationFlag = SIMULATION_FLAG2;
375
- var TransactionFinalityStatus2 = TXN_FINALITY_STATUS;
376
- var TransactionExecutionStatus2 = TXN_EXECUTION_STATUS;
368
+ // src/types/api/rpcspec/errors.ts
369
+ var errors_exports = {};
370
+
371
+ // src/types/api/rpcspec/components.ts
372
+ var components_exports = {};
373
+
374
+ // src/types/api/rpcspec/nonspec.ts
375
+ var ETransactionType = /* @__PURE__ */ ((ETransactionType2) => {
376
+ ETransactionType2["DECLARE"] = "DECLARE";
377
+ ETransactionType2["DEPLOY"] = "DEPLOY";
378
+ ETransactionType2["DEPLOY_ACCOUNT"] = "DEPLOY_ACCOUNT";
379
+ ETransactionType2["INVOKE"] = "INVOKE";
380
+ ETransactionType2["L1_HANDLER"] = "L1_HANDLER";
381
+ return ETransactionType2;
382
+ })(ETransactionType || {});
383
+ var ESimulationFlag = /* @__PURE__ */ ((ESimulationFlag2) => {
384
+ ESimulationFlag2["SKIP_VALIDATE"] = "SKIP_VALIDATE";
385
+ ESimulationFlag2["SKIP_FEE_CHARGE"] = "SKIP_FEE_CHARGE";
386
+ return ESimulationFlag2;
387
+ })(ESimulationFlag || {});
388
+ var ETransactionStatus = /* @__PURE__ */ ((ETransactionStatus2) => {
389
+ ETransactionStatus2["RECEIVED"] = "RECEIVED";
390
+ ETransactionStatus2["REJECTED"] = "REJECTED";
391
+ ETransactionStatus2["ACCEPTED_ON_L2"] = "ACCEPTED_ON_L2";
392
+ ETransactionStatus2["ACCEPTED_ON_L1"] = "ACCEPTED_ON_L1";
393
+ return ETransactionStatus2;
394
+ })(ETransactionStatus || {});
395
+ var ETransactionFinalityStatus = /* @__PURE__ */ ((ETransactionFinalityStatus2) => {
396
+ ETransactionFinalityStatus2["ACCEPTED_ON_L2"] = "ACCEPTED_ON_L2";
397
+ ETransactionFinalityStatus2["ACCEPTED_ON_L1"] = "ACCEPTED_ON_L1";
398
+ return ETransactionFinalityStatus2;
399
+ })(ETransactionFinalityStatus || {});
400
+ var ETransactionExecutionStatus = /* @__PURE__ */ ((ETransactionExecutionStatus2) => {
401
+ ETransactionExecutionStatus2["SUCCEEDED"] = "SUCCEEDED";
402
+ ETransactionExecutionStatus2["REVERTED"] = "REVERTED";
403
+ return ETransactionExecutionStatus2;
404
+ })(ETransactionExecutionStatus || {});
405
+ var EBlockTag = /* @__PURE__ */ ((EBlockTag2) => {
406
+ EBlockTag2["LATEST"] = "latest";
407
+ EBlockTag2["PENDING"] = "pending";
408
+ return EBlockTag2;
409
+ })(EBlockTag || {});
410
+ var EDataAvailabilityMode = /* @__PURE__ */ ((EDataAvailabilityMode2) => {
411
+ EDataAvailabilityMode2["L1"] = "L1";
412
+ EDataAvailabilityMode2["L2"] = "L2";
413
+ return EDataAvailabilityMode2;
414
+ })(EDataAvailabilityMode || {});
377
415
 
378
416
  // src/types/api/sequencer.ts
379
417
  var sequencer_exports = {};
@@ -2241,10 +2279,10 @@ function compressProgram(jsonProgram) {
2241
2279
  const compressedProgram = (0, import_pako.gzip)(stringified);
2242
2280
  return btoaUniversal(compressedProgram);
2243
2281
  }
2244
- function decompressProgram(base64) {
2245
- if (Array.isArray(base64))
2246
- return base64;
2247
- const decompressed = arrayBufferToString((0, import_pako.ungzip)(atobUniversal(base64)));
2282
+ function decompressProgram(base642) {
2283
+ if (Array.isArray(base642))
2284
+ return base642;
2285
+ const decompressed = arrayBufferToString((0, import_pako.ungzip)(atobUniversal(base642)));
2248
2286
  return parse2(decompressed);
2249
2287
  }
2250
2288
  function randomAddress() {
@@ -2349,22 +2387,22 @@ var RPCResponseParser = class {
2349
2387
  parseGetBlockResponse(res) {
2350
2388
  return {
2351
2389
  timestamp: res.timestamp,
2352
- block_hash: res.block_hash,
2353
- block_number: res.block_number,
2354
- new_root: res.new_root,
2390
+ block_hash: "block_hash" in res ? res.block_hash : "",
2391
+ block_number: "block_number" in res ? res.block_number : -1,
2392
+ new_root: "new_root" in res ? res.new_root : "",
2355
2393
  parent_hash: res.parent_hash,
2356
- status: res.status,
2394
+ status: "status" in res ? res.status : "PENDING" /* PENDING */,
2357
2395
  transactions: res.transactions
2358
2396
  };
2359
2397
  }
2360
2398
  parseGetTransactionResponse(res) {
2361
2399
  return {
2362
- calldata: res.calldata || [],
2363
- contract_address: res.contract_address,
2364
- sender_address: res.contract_address,
2365
- max_fee: res.max_fee,
2366
- nonce: res.nonce,
2367
- signature: res.signature || [],
2400
+ calldata: "calldata" in res ? res.calldata : [],
2401
+ contract_address: "contract_address" in res ? res.contract_address : "",
2402
+ sender_address: "sender_address" in res ? res.sender_address : "",
2403
+ max_fee: "max_fee" in res ? res.max_fee : "",
2404
+ nonce: "nonce" in res ? res.nonce : "",
2405
+ signature: "signature" in res ? res.signature : [],
2368
2406
  transaction_hash: res.transaction_hash,
2369
2407
  version: res.version
2370
2408
  };
@@ -2640,6 +2678,13 @@ var Block = class {
2640
2678
  };
2641
2679
 
2642
2680
  // src/provider/rpc.ts
2681
+ var getDefaultNodeUrl = (networkName, mute = false) => {
2682
+ if (!mute)
2683
+ console.warn("Using default public node url, please provide nodeUrl in provider options!");
2684
+ const nodes = networkName === "SN_MAIN" /* SN_MAIN */ ? RPC_MAINNET_NODES : RPC_GOERLI_NODES;
2685
+ const randIdx = Math.floor(Math.random() * nodes.length);
2686
+ return nodes[randIdx];
2687
+ };
2643
2688
  var defaultOptions = {
2644
2689
  headers: { "Content-Type": "application/json" },
2645
2690
  blockIdentifier: "pending" /* pending */,
@@ -2648,52 +2693,127 @@ var defaultOptions = {
2648
2693
  var RpcProvider = class {
2649
2694
  constructor(optionsOrProvider) {
2650
2695
  this.responseParser = new RPCResponseParser();
2651
- const { nodeUrl, retries, headers, blockIdentifier, chainId } = optionsOrProvider;
2652
- this.nodeUrl = nodeUrl;
2696
+ /**
2697
+ * @deprecated renamed to getBlockLatestAccepted(); (will be removed in next minor version)
2698
+ */
2699
+ this.getBlockHashAndNumber = this.getBlockLatestAccepted;
2700
+ /**
2701
+ * @deprecated renamed to getBlockStateUpdate();
2702
+ */
2703
+ this.getStateUpdate = this.getBlockStateUpdate;
2704
+ /**
2705
+ * Returns the execution traces of all transactions included in the given block
2706
+ * @deprecated renamed to getBlockTransactionsTraces()
2707
+ */
2708
+ this.traceBlockTransactions = this.getBlockTransactionsTraces;
2709
+ /**
2710
+ * Get the number of transactions in a block given a block id
2711
+ * @deprecated renamed to getBlockTransactionCount()
2712
+ * @returns Number of transactions
2713
+ */
2714
+ this.getTransactionCount = this.getBlockTransactionCount;
2715
+ /**
2716
+ * @deprecated renamed to getTransactionTrace();
2717
+ * For a given executed transaction, return the trace of its execution, including internal calls
2718
+ */
2719
+ this.traceTransaction = this.getTransactionTrace;
2720
+ /**
2721
+ * @deprecated renamed to simulateTransaction();
2722
+ */
2723
+ this.getSimulateTransaction = this.simulateTransaction;
2724
+ const { nodeUrl, retries, headers, blockIdentifier, chainId } = optionsOrProvider || {};
2725
+ if (Object.values(NetworkName).includes(nodeUrl)) {
2726
+ this.nodeUrl = getDefaultNodeUrl(nodeUrl, optionsOrProvider?.default);
2727
+ } else if (nodeUrl) {
2728
+ this.nodeUrl = nodeUrl;
2729
+ } else {
2730
+ this.nodeUrl = getDefaultNodeUrl(void 0, optionsOrProvider?.default);
2731
+ }
2653
2732
  this.retries = retries || defaultOptions.retries;
2654
2733
  this.headers = { ...defaultOptions.headers, ...headers };
2655
2734
  this.blockIdentifier = blockIdentifier || defaultOptions.blockIdentifier;
2656
2735
  this.chainId = chainId;
2657
2736
  this.getChainId();
2658
2737
  }
2659
- fetch(method, params) {
2660
- const body = stringify2({ method, jsonrpc: "2.0", params, id: 0 });
2738
+ fetch(method, params, id = 0) {
2739
+ const rpcRequestBody = {
2740
+ id,
2741
+ jsonrpc: "2.0",
2742
+ method,
2743
+ ...params && { params }
2744
+ };
2661
2745
  return fetchPonyfill_default(this.nodeUrl, {
2662
2746
  method: "POST",
2663
- body,
2747
+ body: stringify2(rpcRequestBody),
2664
2748
  headers: this.headers
2665
2749
  });
2666
2750
  }
2667
- errorHandler(error) {
2668
- if (error) {
2669
- const { code, message } = error;
2670
- throw new LibraryError(`${code}: ${message}`);
2751
+ errorHandler(method, params, rpcError, otherError) {
2752
+ if (rpcError) {
2753
+ const { code, message, data } = rpcError;
2754
+ throw new LibraryError(
2755
+ `RPC: ${method} with params ${JSON.stringify(params)}
2756
+ ${code}: ${message}: ${data}`
2757
+ );
2758
+ }
2759
+ if (otherError instanceof LibraryError) {
2760
+ throw otherError;
2761
+ }
2762
+ if (otherError) {
2763
+ throw Error(otherError.message);
2671
2764
  }
2672
2765
  }
2673
2766
  async fetchEndpoint(method, params) {
2674
2767
  try {
2675
2768
  const rawResult = await this.fetch(method, params);
2676
2769
  const { error, result } = await rawResult.json();
2677
- this.errorHandler(error);
2770
+ this.errorHandler(method, params, error);
2678
2771
  return result;
2679
2772
  } catch (error) {
2680
- this.errorHandler(error?.response?.data);
2773
+ this.errorHandler(method, params, error?.response?.data, error);
2681
2774
  throw error;
2682
2775
  }
2683
2776
  }
2684
- // Methods from Interface
2685
2777
  async getChainId() {
2686
2778
  this.chainId ?? (this.chainId = await this.fetchEndpoint("starknet_chainId"));
2687
2779
  return this.chainId;
2688
2780
  }
2781
+ /**
2782
+ * NEW: Returns the version of the Starknet JSON-RPC specification being used
2783
+ */
2784
+ async getSpecVersion() {
2785
+ return this.fetchEndpoint("starknet_specVersion");
2786
+ }
2787
+ async getNonceForAddress(contractAddress, blockIdentifier = this.blockIdentifier) {
2788
+ const contract_address = toHex(contractAddress);
2789
+ const block_id = new Block(blockIdentifier).identifier;
2790
+ return this.fetchEndpoint("starknet_getNonce", {
2791
+ contract_address,
2792
+ block_id
2793
+ });
2794
+ }
2795
+ /**
2796
+ * @deprecated use getBlockWithTxHashes or getBlockWithTxs (will be removed on sequencer deprecation)
2797
+ */
2689
2798
  async getBlock(blockIdentifier = this.blockIdentifier) {
2690
2799
  return this.getBlockWithTxHashes(blockIdentifier).then(
2691
2800
  this.responseParser.parseGetBlockResponse
2692
2801
  );
2693
2802
  }
2694
- async getBlockHashAndNumber() {
2803
+ /**
2804
+ * Get the most recent accepted block hash and number
2805
+ */
2806
+ async getBlockLatestAccepted() {
2695
2807
  return this.fetchEndpoint("starknet_blockHashAndNumber");
2696
2808
  }
2809
+ /**
2810
+ * @deprecated redundant use getBlockLatestAccepted();
2811
+ * Get the most recent accepted block number
2812
+ * @returns Number of the latest block
2813
+ */
2814
+ async getBlockNumber() {
2815
+ return this.fetchEndpoint("starknet_blockNumber");
2816
+ }
2697
2817
  async getBlockWithTxHashes(blockIdentifier = this.blockIdentifier) {
2698
2818
  const block_id = new Block(blockIdentifier).identifier;
2699
2819
  return this.fetchEndpoint("starknet_getBlockWithTxHashes", { block_id });
@@ -2702,68 +2822,176 @@ var RpcProvider = class {
2702
2822
  const block_id = new Block(blockIdentifier).identifier;
2703
2823
  return this.fetchEndpoint("starknet_getBlockWithTxs", { block_id });
2704
2824
  }
2705
- async getClassHashAt(contractAddress, blockIdentifier = this.blockIdentifier) {
2825
+ async getBlockStateUpdate(blockIdentifier = this.blockIdentifier) {
2706
2826
  const block_id = new Block(blockIdentifier).identifier;
2707
- return this.fetchEndpoint("starknet_getClassHashAt", {
2708
- block_id,
2709
- contract_address: contractAddress
2710
- });
2827
+ return this.fetchEndpoint("starknet_getStateUpdate", { block_id });
2711
2828
  }
2712
- async getNonceForAddress(contractAddress, blockIdentifier = this.blockIdentifier) {
2829
+ async getBlockTransactionsTraces(blockIdentifier = this.blockIdentifier) {
2713
2830
  const block_id = new Block(blockIdentifier).identifier;
2714
- return this.fetchEndpoint("starknet_getNonce", {
2715
- contract_address: contractAddress,
2716
- block_id
2717
- });
2831
+ return this.fetchEndpoint("starknet_traceBlockTransactions", { block_id });
2718
2832
  }
2833
+ async getBlockTransactionCount(blockIdentifier = this.blockIdentifier) {
2834
+ const block_id = new Block(blockIdentifier).identifier;
2835
+ return this.fetchEndpoint("starknet_getBlockTransactionCount", { block_id });
2836
+ }
2837
+ /**
2838
+ * Return transactions from pending block
2839
+ * @deprecated Instead use getBlock(BlockTag.pending); (will be removed in next minor version)
2840
+ */
2719
2841
  async getPendingTransactions() {
2720
- return this.fetchEndpoint("starknet_pendingTransactions");
2842
+ const { transactions } = await this.getBlock("pending" /* pending */);
2843
+ return Promise.all(transactions.map((it) => this.getTransactionByHash(it)));
2721
2844
  }
2722
- async getProtocolVersion() {
2723
- throw new Error("Pathfinder does not implement this rpc 0.1.0 method");
2845
+ /**
2846
+ * @deprecated use getTransactionByHash or getTransactionByBlockIdAndIndex (will be removed on sequencer deprecation)
2847
+ */
2848
+ async getTransaction(txHash) {
2849
+ return this.getTransactionByHash(txHash).then(this.responseParser.parseGetTransactionResponse);
2724
2850
  }
2725
- async getStateUpdate(blockIdentifier = this.blockIdentifier) {
2851
+ async getTransactionByHash(txHash) {
2852
+ const transaction_hash = toHex(txHash);
2853
+ return this.fetchEndpoint("starknet_getTransactionByHash", {
2854
+ transaction_hash
2855
+ });
2856
+ }
2857
+ async getTransactionByBlockIdAndIndex(blockIdentifier, index) {
2726
2858
  const block_id = new Block(blockIdentifier).identifier;
2727
- return this.fetchEndpoint("starknet_getStateUpdate", { block_id });
2859
+ return this.fetchEndpoint("starknet_getTransactionByBlockIdAndIndex", { block_id, index });
2860
+ }
2861
+ async getTransactionReceipt(txHash) {
2862
+ const transaction_hash = toHex(txHash);
2863
+ return this.fetchEndpoint("starknet_getTransactionReceipt", { transaction_hash });
2864
+ }
2865
+ async getTransactionTrace(txHash) {
2866
+ const transaction_hash = toHex(txHash);
2867
+ return this.fetchEndpoint("starknet_traceTransaction", { transaction_hash });
2868
+ }
2869
+ // TODO: implement in waitforTransaction, add add tests, when RPC 0.5 become standard /
2870
+ /**
2871
+ * NEW: Get the status of a transaction
2872
+ */
2873
+ async getTransactionStatus(transactionHash) {
2874
+ const transaction_hash = toHex(transactionHash);
2875
+ return this.fetchEndpoint("starknet_getTransactionStatus", { transaction_hash });
2876
+ }
2877
+ /**
2878
+ * @param invocations AccountInvocations
2879
+ * @param simulateTransactionOptions blockIdentifier and flags to skip validation and fee charge<br/>
2880
+ * - blockIdentifier<br/>
2881
+ * - skipValidate (default false)<br/>
2882
+ * - skipFeeCharge (default true)<br/>
2883
+ */
2884
+ async simulateTransaction(invocations, {
2885
+ blockIdentifier = this.blockIdentifier,
2886
+ skipValidate = false,
2887
+ skipFeeCharge = true
2888
+ }) {
2889
+ const block_id = new Block(blockIdentifier).identifier;
2890
+ const simulationFlags = [];
2891
+ if (skipValidate)
2892
+ simulationFlags.push(rpc_exports.ESimulationFlag.SKIP_VALIDATE);
2893
+ if (skipFeeCharge)
2894
+ simulationFlags.push(rpc_exports.ESimulationFlag.SKIP_FEE_CHARGE);
2895
+ return this.fetchEndpoint("starknet_simulateTransactions", {
2896
+ block_id,
2897
+ transactions: invocations.map((it) => this.buildTransaction(it)),
2898
+ simulation_flags: simulationFlags
2899
+ }).then(this.responseParser.parseSimulateTransactionResponse);
2900
+ }
2901
+ async waitForTransaction(txHash, options) {
2902
+ const transactionHash = toHex(txHash);
2903
+ let { retries } = this;
2904
+ let onchain = false;
2905
+ let isErrorState = false;
2906
+ const retryInterval = options?.retryInterval ?? 5e3;
2907
+ const errorStates = options?.errorStates ?? [
2908
+ rpc_exports.ETransactionStatus.REJECTED,
2909
+ rpc_exports.ETransactionExecutionStatus.REVERTED
2910
+ ];
2911
+ const successStates = options?.successStates ?? [
2912
+ rpc_exports.ETransactionExecutionStatus.SUCCEEDED,
2913
+ rpc_exports.ETransactionStatus.ACCEPTED_ON_L2,
2914
+ rpc_exports.ETransactionStatus.ACCEPTED_ON_L1
2915
+ ];
2916
+ let txStatus;
2917
+ while (!onchain) {
2918
+ await wait(retryInterval);
2919
+ try {
2920
+ txStatus = await this.getTransactionStatus(transactionHash);
2921
+ const executionStatus = txStatus.execution_status;
2922
+ const finalityStatus = txStatus.finality_status;
2923
+ if (!finalityStatus) {
2924
+ const error = new Error("waiting for transaction status");
2925
+ throw error;
2926
+ }
2927
+ if (successStates.includes(executionStatus) || successStates.includes(finalityStatus)) {
2928
+ onchain = true;
2929
+ } else if (errorStates.includes(executionStatus) || errorStates.includes(finalityStatus)) {
2930
+ const message = `${executionStatus}: ${finalityStatus}`;
2931
+ const error = new Error(message);
2932
+ error.response = txStatus;
2933
+ isErrorState = true;
2934
+ throw error;
2935
+ }
2936
+ } catch (error) {
2937
+ if (error instanceof Error && isErrorState) {
2938
+ throw error;
2939
+ }
2940
+ if (retries <= 0) {
2941
+ throw new Error(`waitForTransaction timed-out with retries ${this.retries}`);
2942
+ }
2943
+ }
2944
+ retries -= 1;
2945
+ }
2946
+ let txReceipt = null;
2947
+ while (txReceipt === null) {
2948
+ try {
2949
+ txReceipt = await this.getTransactionReceipt(transactionHash);
2950
+ } catch (error) {
2951
+ if (retries <= 0) {
2952
+ throw new Error(`waitForTransaction timed-out with retries ${this.retries}`);
2953
+ }
2954
+ }
2955
+ retries -= 1;
2956
+ await wait(retryInterval);
2957
+ }
2958
+ return txReceipt;
2728
2959
  }
2729
2960
  async getStorageAt(contractAddress, key, blockIdentifier = this.blockIdentifier) {
2961
+ const contract_address = toHex(contractAddress);
2730
2962
  const parsedKey = toStorageKey(key);
2731
2963
  const block_id = new Block(blockIdentifier).identifier;
2732
2964
  return this.fetchEndpoint("starknet_getStorageAt", {
2733
- contract_address: contractAddress,
2965
+ contract_address,
2734
2966
  key: parsedKey,
2735
2967
  block_id
2736
2968
  });
2737
2969
  }
2738
- // Methods from Interface
2739
- async getTransaction(txHash) {
2740
- return this.getTransactionByHash(txHash).then(this.responseParser.parseGetTransactionResponse);
2741
- }
2742
- async getTransactionByHash(txHash) {
2743
- return this.fetchEndpoint("starknet_getTransactionByHash", { transaction_hash: txHash });
2744
- }
2745
- async getTransactionByBlockIdAndIndex(blockIdentifier, index) {
2970
+ async getClassHashAt(contractAddress, blockIdentifier = this.blockIdentifier) {
2971
+ const contract_address = toHex(contractAddress);
2746
2972
  const block_id = new Block(blockIdentifier).identifier;
2747
- return this.fetchEndpoint("starknet_getTransactionByBlockIdAndIndex", { block_id, index });
2748
- }
2749
- async getTransactionReceipt(txHash) {
2750
- return this.fetchEndpoint("starknet_getTransactionReceipt", { transaction_hash: txHash });
2973
+ return this.fetchEndpoint("starknet_getClassHashAt", {
2974
+ block_id,
2975
+ contract_address
2976
+ });
2751
2977
  }
2752
2978
  async getClassByHash(classHash) {
2753
2979
  return this.getClass(classHash);
2754
2980
  }
2755
2981
  async getClass(classHash, blockIdentifier = this.blockIdentifier) {
2982
+ const class_hash = toHex(classHash);
2756
2983
  const block_id = new Block(blockIdentifier).identifier;
2757
2984
  return this.fetchEndpoint("starknet_getClass", {
2758
- class_hash: classHash,
2985
+ class_hash,
2759
2986
  block_id
2760
2987
  }).then(this.responseParser.parseContractClassResponse);
2761
2988
  }
2762
2989
  async getClassAt(contractAddress, blockIdentifier = this.blockIdentifier) {
2990
+ const contract_address = toHex(contractAddress);
2763
2991
  const block_id = new Block(blockIdentifier).identifier;
2764
2992
  return this.fetchEndpoint("starknet_getClassAt", {
2765
2993
  block_id,
2766
- contract_address: contractAddress
2994
+ contract_address
2767
2995
  }).then(this.responseParser.parseContractClassResponse);
2768
2996
  }
2769
2997
  async getCode(_contractAddress, _blockIdentifier) {
@@ -2787,6 +3015,9 @@ var RpcProvider = class {
2787
3015
  }
2788
3016
  return { cairo: "0", compiler: "0" };
2789
3017
  }
3018
+ /**
3019
+ * @deprecated use get*type*EstimateFee (will be refactored based on type after sequencer deprecation)
3020
+ */
2790
3021
  async getEstimateFee(invocation, invocationDetails, blockIdentifier = this.blockIdentifier) {
2791
3022
  return this.getInvokeEstimateFee(invocation, invocationDetails, blockIdentifier);
2792
3023
  }
@@ -2845,11 +3076,24 @@ var RpcProvider = class {
2845
3076
  block_id
2846
3077
  }).then(this.responseParser.parseFeeEstimateBulkResponse);
2847
3078
  }
3079
+ async invokeFunction(functionInvocation, details) {
3080
+ return this.fetchEndpoint("starknet_addInvokeTransaction", {
3081
+ invoke_transaction: {
3082
+ sender_address: functionInvocation.contractAddress,
3083
+ calldata: CallData.toHex(functionInvocation.calldata),
3084
+ type: rpc_exports.ETransactionType.INVOKE,
3085
+ max_fee: toHex(details.maxFee || 0),
3086
+ version: "0x1",
3087
+ signature: signatureToHexArray(functionInvocation.signature),
3088
+ nonce: toHex(details.nonce)
3089
+ }
3090
+ });
3091
+ }
2848
3092
  async declareContract({ contract, signature, senderAddress, compiledClassHash }, details) {
2849
3093
  if (!isSierra(contract)) {
2850
3094
  return this.fetchEndpoint("starknet_addDeclareTransaction", {
2851
3095
  declare_transaction: {
2852
- type: rpc_exports.TransactionType.DECLARE,
3096
+ type: rpc_exports.ETransactionType.DECLARE,
2853
3097
  contract_class: {
2854
3098
  program: contract.program,
2855
3099
  entry_points_by_type: contract.entry_points_by_type,
@@ -2865,7 +3109,7 @@ var RpcProvider = class {
2865
3109
  }
2866
3110
  return this.fetchEndpoint("starknet_addDeclareTransaction", {
2867
3111
  declare_transaction: {
2868
- type: rpc_exports.TransactionType.DECLARE,
3112
+ type: rpc_exports.ETransactionType.DECLARE,
2869
3113
  contract_class: {
2870
3114
  sierra_program: decompressProgram(contract.sierra_program),
2871
3115
  contract_class_version: contract.contract_class_version,
@@ -2887,7 +3131,7 @@ var RpcProvider = class {
2887
3131
  constructor_calldata: CallData.toHex(constructorCalldata || []),
2888
3132
  class_hash: toHex(classHash),
2889
3133
  contract_address_salt: toHex(addressSalt || 0),
2890
- type: rpc_exports.TransactionType.DEPLOY_ACCOUNT,
3134
+ type: rpc_exports.ETransactionType.DEPLOY_ACCOUNT,
2891
3135
  max_fee: toHex(details.maxFee || 0),
2892
3136
  version: toHex(details.version || 0),
2893
3137
  signature: signatureToHexArray(signature),
@@ -2895,20 +3139,6 @@ var RpcProvider = class {
2895
3139
  }
2896
3140
  });
2897
3141
  }
2898
- async invokeFunction(functionInvocation, details) {
2899
- return this.fetchEndpoint("starknet_addInvokeTransaction", {
2900
- invoke_transaction: {
2901
- sender_address: functionInvocation.contractAddress,
2902
- calldata: CallData.toHex(functionInvocation.calldata),
2903
- type: rpc_exports.TransactionType.INVOKE,
2904
- max_fee: toHex(details.maxFee || 0),
2905
- version: "0x1",
2906
- signature: signatureToHexArray(functionInvocation.signature),
2907
- nonce: toHex(details.nonce)
2908
- }
2909
- });
2910
- }
2911
- // Methods from Interface
2912
3142
  async callContract(call, blockIdentifier = this.blockIdentifier) {
2913
3143
  const block_id = new Block(blockIdentifier).identifier;
2914
3144
  const result = await this.fetchEndpoint("starknet_call", {
@@ -2921,117 +3151,47 @@ var RpcProvider = class {
2921
3151
  });
2922
3152
  return this.responseParser.parseCallContractResponse(result);
2923
3153
  }
2924
- async traceTransaction(transactionHash) {
2925
- return this.fetchEndpoint("starknet_traceTransaction", { transaction_hash: transactionHash });
2926
- }
2927
- async traceBlockTransactions(blockHash) {
2928
- return this.fetchEndpoint("starknet_traceBlockTransactions", { block_hash: blockHash });
2929
- }
2930
- async waitForTransaction(txHash, options) {
2931
- let { retries } = this;
2932
- let onchain = false;
2933
- let isErrorState = false;
2934
- let txReceipt = {};
2935
- const retryInterval = options?.retryInterval ?? 5e3;
2936
- const errorStates = options?.errorStates ?? [TransactionExecutionStatus2.REVERTED];
2937
- const successStates = options?.successStates ?? [
2938
- TransactionExecutionStatus2.SUCCEEDED,
2939
- TransactionFinalityStatus2.ACCEPTED_ON_L1,
2940
- TransactionFinalityStatus2.ACCEPTED_ON_L2
2941
- ];
2942
- while (!onchain) {
2943
- await wait(retryInterval);
2944
- try {
2945
- txReceipt = await this.getTransactionReceipt(txHash);
2946
- const executionStatus = pascalToSnake(txReceipt.execution_status);
2947
- const finalityStatus = pascalToSnake(txReceipt.finality_status);
2948
- if (!executionStatus || !finalityStatus) {
2949
- const error = new Error("waiting for transaction status");
2950
- throw error;
2951
- }
2952
- if (successStates.includes(executionStatus) || successStates.includes(finalityStatus)) {
2953
- onchain = true;
2954
- } else if (errorStates.includes(executionStatus) || errorStates.includes(finalityStatus)) {
2955
- const message = `${executionStatus}: ${finalityStatus}: ${txReceipt.revert_reason}`;
2956
- const error = new Error(message);
2957
- error.response = txReceipt;
2958
- isErrorState = true;
2959
- throw error;
2960
- }
2961
- } catch (error) {
2962
- if (error instanceof Error && isErrorState) {
2963
- throw error;
2964
- }
2965
- if (retries === 0) {
2966
- throw new Error(`waitForTransaction timed-out with retries ${this.retries}`);
2967
- }
2968
- }
2969
- retries -= 1;
2970
- }
2971
- await wait(retryInterval);
2972
- return txReceipt;
2973
- }
2974
3154
  /**
2975
- * Gets the transaction count from a block.
2976
- *
2977
- *
2978
- * @param blockIdentifier
2979
- * @returns Number of transactions
3155
+ * NEW: Estimate the fee for a message from L1
3156
+ * @param message Message From L1
2980
3157
  */
2981
- async getTransactionCount(blockIdentifier = this.blockIdentifier) {
3158
+ async estimateMessageFee(message, blockIdentifier = this.blockIdentifier) {
3159
+ const { from_address, to_address, entry_point_selector, payload } = message;
3160
+ const formattedMessage = {
3161
+ from_address: toHex(from_address),
3162
+ to_address: toHex(to_address),
3163
+ entry_point_selector: getSelector(entry_point_selector),
3164
+ payload: getHexStringArray(payload)
3165
+ };
2982
3166
  const block_id = new Block(blockIdentifier).identifier;
2983
- return this.fetchEndpoint("starknet_getBlockTransactionCount", { block_id });
2984
- }
2985
- /**
2986
- * Gets the latest block number
2987
- *
2988
- *
2989
- * @returns Number of the latest block
2990
- */
2991
- async getBlockNumber() {
2992
- return this.fetchEndpoint("starknet_blockNumber");
3167
+ return this.fetchEndpoint("starknet_estimateMessageFee", {
3168
+ message: formattedMessage,
3169
+ block_id
3170
+ });
2993
3171
  }
2994
3172
  /**
2995
- * Gets syncing status of the node
2996
- *
2997
- *
3173
+ * Returns an object about the sync status, or false if the node is not synching
2998
3174
  * @returns Object with the stats data
2999
3175
  */
3000
3176
  async getSyncingStats() {
3001
3177
  return this.fetchEndpoint("starknet_syncing");
3002
3178
  }
3003
3179
  /**
3004
- * Gets all the events filtered
3005
- *
3006
- *
3180
+ * Returns all events matching the given filter
3007
3181
  * @returns events and the pagination of the events
3008
3182
  */
3009
3183
  async getEvents(eventFilter) {
3010
3184
  return this.fetchEndpoint("starknet_getEvents", { filter: eventFilter });
3011
3185
  }
3012
- async getSimulateTransaction(invocations, {
3013
- blockIdentifier = this.blockIdentifier,
3014
- skipValidate = false,
3015
- skipExecute = false,
3016
- // @deprecated
3017
- skipFeeCharge = true
3018
- // Pathfinder currently does not support `starknet_simulateTransactions` without `SKIP_FEE_CHARGE` simulation flag being set. This will become supported in a future release
3019
- }) {
3020
- const block_id = new Block(blockIdentifier).identifier;
3021
- const simulationFlags = [];
3022
- if (skipValidate)
3023
- simulationFlags.push(SimulationFlag.SKIP_VALIDATE);
3024
- if (skipExecute || skipFeeCharge)
3025
- simulationFlags.push(SimulationFlag.SKIP_FEE_CHARGE);
3026
- return this.fetchEndpoint("starknet_simulateTransactions", {
3027
- block_id,
3028
- transactions: invocations.map((it) => this.buildTransaction(it)),
3029
- simulation_flags: simulationFlags
3030
- }).then(this.responseParser.parseSimulateTransactionResponse);
3031
- }
3186
+ /**
3187
+ * StarknetId Endpoint (get name from address)
3188
+ */
3032
3189
  async getStarkName(address, StarknetIdContract2) {
3033
3190
  return getStarkName(this, address, StarknetIdContract2);
3034
3191
  }
3192
+ /**
3193
+ * StarknetId Endpoint (get address from name)
3194
+ */
3035
3195
  async getAddressFromStarkName(name, StarknetIdContract2) {
3036
3196
  return getAddressFromStarkName(this, name, StarknetIdContract2);
3037
3197
  }
@@ -3044,12 +3204,11 @@ var RpcProvider = class {
3044
3204
  };
3045
3205
  if (invocation.type === "INVOKE_FUNCTION" /* INVOKE */) {
3046
3206
  return {
3047
- type: rpc_exports.TransactionType.INVOKE,
3207
+ type: rpc_exports.ETransactionType.INVOKE,
3048
3208
  // Diff between sequencer and rpc invoke type
3049
3209
  sender_address: invocation.contractAddress,
3050
3210
  calldata: CallData.toHex(invocation.calldata),
3051
3211
  version: toHex(invocation.version || defaultVersions.v1),
3052
- // HEX_STR_TRANSACTION_VERSION_1, // as any HOTFIX TODO: Resolve spec version
3053
3212
  ...details
3054
3213
  };
3055
3214
  }
@@ -3060,7 +3219,6 @@ var RpcProvider = class {
3060
3219
  contract_class: invocation.contract,
3061
3220
  sender_address: invocation.senderAddress,
3062
3221
  version: toHex(invocation.version || defaultVersions.v1),
3063
- // HEX_STR_TRANSACTION_VERSION_1, // as any HOTFIX TODO: Resolve spec version
3064
3222
  ...details
3065
3223
  };
3066
3224
  }
@@ -3074,7 +3232,6 @@ var RpcProvider = class {
3074
3232
  compiled_class_hash: invocation.compiledClassHash || "",
3075
3233
  sender_address: invocation.senderAddress,
3076
3234
  version: toHex(invocation.version || defaultVersions.v2),
3077
- // HEX_STR_TRANSACTION_VERSION_2, // as any HOTFIX TODO: Resolve spec version
3078
3235
  ...details
3079
3236
  };
3080
3237
  }
@@ -3758,7 +3915,7 @@ var Provider = class {
3758
3915
  } else if (providerOrOptions && "sequencer" in providerOrOptions) {
3759
3916
  this.provider = new SequencerProvider(providerOrOptions.sequencer);
3760
3917
  } else {
3761
- this.provider = new SequencerProvider();
3918
+ this.provider = new RpcProvider();
3762
3919
  }
3763
3920
  }
3764
3921
  async getChainId() {
@@ -4208,7 +4365,7 @@ var Signer = class {
4208
4365
  // src/utils/events.ts
4209
4366
  function parseUDCEvent(txReceipt) {
4210
4367
  if (!txReceipt.events) {
4211
- throw new Error("UDC emited event is empty");
4368
+ throw new Error("UDC emitted event is empty");
4212
4369
  }
4213
4370
  const event = txReceipt.events.find(
4214
4371
  (it) => cleanHex(it.from_address) === cleanHex(UDC.ADDRESS)
@@ -4747,7 +4904,7 @@ var ProviderInterface = class {
4747
4904
  };
4748
4905
 
4749
4906
  // src/provider/index.ts
4750
- var defaultProvider = new Provider();
4907
+ var defaultProvider = new Provider({ rpc: { default: true } });
4751
4908
 
4752
4909
  // src/account/interface.ts
4753
4910
  var AccountInterface = class extends ProviderInterface {
@@ -5205,6 +5362,7 @@ var number = num_exports;
5205
5362
  fixStack,
5206
5363
  getCalldata,
5207
5364
  getChecksumAddress,
5365
+ getDefaultNodeUrl,
5208
5366
  hash,
5209
5367
  isSierra,
5210
5368
  isUrl,