starknet 4.18.0 → 4.19.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.mjs CHANGED
@@ -2821,6 +2821,9 @@ var RpcProvider = class {
2821
2821
  block_id
2822
2822
  }).then(this.responseParser.parseFeeEstimateResponse);
2823
2823
  }
2824
+ async getEstimateFeeBulk(_invocations, _blockIdentifier = this.blockIdentifier) {
2825
+ throw new Error("RPC does not implement getInvokeEstimateFeeBulk function");
2826
+ }
2824
2827
  async declareContract({ contractDefinition, signature, senderAddress }, details) {
2825
2828
  return this.fetchEndpoint("starknet_addDeclareTransaction", {
2826
2829
  declare_transaction: {
@@ -3002,6 +3005,27 @@ var SequencerAPIResponseParser = class extends ResponseParser {
3002
3005
  overall_fee: toBN(res.amount)
3003
3006
  };
3004
3007
  }
3008
+ parseFeeEstimateBulkResponse(res) {
3009
+ return [].concat(res).map((item) => {
3010
+ if ("overall_fee" in item) {
3011
+ let gasInfo = {};
3012
+ try {
3013
+ gasInfo = {
3014
+ gas_consumed: toBN(item.gas_usage),
3015
+ gas_price: toBN(item.gas_price)
3016
+ };
3017
+ } catch {
3018
+ }
3019
+ return {
3020
+ overall_fee: toBN(item.overall_fee),
3021
+ ...gasInfo
3022
+ };
3023
+ }
3024
+ return {
3025
+ overall_fee: toBN(item.amount)
3026
+ };
3027
+ });
3028
+ }
3005
3029
  parseCallContractResponse(res) {
3006
3030
  return {
3007
3031
  result: res.result
@@ -3134,6 +3158,7 @@ var SequencerProvider = class {
3134
3158
  "call_contract",
3135
3159
  "estimate_fee",
3136
3160
  "estimate_message_fee",
3161
+ "estimate_fee_bulk",
3137
3162
  "simulate_transaction"
3138
3163
  ];
3139
3164
  return postMethodEndpoints.includes(endpoint) ? "POST" : "GET";
@@ -3336,6 +3361,42 @@ var SequencerProvider = class {
3336
3361
  }
3337
3362
  ).then(this.responseParser.parseFeeEstimateResponse);
3338
3363
  }
3364
+ async getEstimateFeeBulk(invocations, blockIdentifier = this.blockIdentifier) {
3365
+ const params = invocations.map((invocation) => {
3366
+ let res;
3367
+ if (invocation.type === "INVOKE_FUNCTION") {
3368
+ res = {
3369
+ type: invocation.type,
3370
+ contract_address: invocation.contractAddress,
3371
+ calldata: invocation.calldata ?? []
3372
+ };
3373
+ } else if (invocation.type === "DECLARE") {
3374
+ res = {
3375
+ type: invocation.type,
3376
+ sender_address: invocation.senderAddress,
3377
+ contract_class: invocation.contractDefinition
3378
+ };
3379
+ } else {
3380
+ res = {
3381
+ type: invocation.type,
3382
+ class_hash: toHex(toBN(invocation.classHash)),
3383
+ constructor_calldata: bigNumberishArrayToDecimalStringArray(
3384
+ invocation.constructorCalldata || []
3385
+ ),
3386
+ contract_address_salt: toHex(toBN(invocation.addressSalt || 0))
3387
+ };
3388
+ }
3389
+ return {
3390
+ ...res,
3391
+ signature: bigNumberishArrayToDecimalStringArray(invocation.signature || []),
3392
+ version: toHex(toBN((invocation == null ? void 0 : invocation.version) || 1)),
3393
+ nonce: toHex(toBN(invocation.nonce))
3394
+ };
3395
+ });
3396
+ return this.fetchEndpoint("estimate_fee_bulk", { blockIdentifier }, params).then(
3397
+ this.responseParser.parseFeeEstimateBulkResponse
3398
+ );
3399
+ }
3339
3400
  async getCode(contractAddress, blockIdentifier = this.blockIdentifier) {
3340
3401
  return this.fetchEndpoint("get_code", { contractAddress, blockIdentifier });
3341
3402
  }
@@ -3438,6 +3499,9 @@ var Provider = class {
3438
3499
  blockIdentifier
3439
3500
  );
3440
3501
  }
3502
+ async getEstimateFeeBulk(invocations, blockIdentifier) {
3503
+ return this.provider.getEstimateFeeBulk(invocations, blockIdentifier);
3504
+ }
3441
3505
  async getNonceForAddress(contractAddress, blockIdentifier) {
3442
3506
  return this.provider.getNonceForAddress(contractAddress, blockIdentifier);
3443
3507
  }
@@ -4492,10 +4556,9 @@ var Account = class extends Provider {
4492
4556
  version,
4493
4557
  chainId
4494
4558
  };
4495
- const signature = await this.signer.signTransaction(transactions, signerDetails);
4496
- const calldata = fromCallsToExecuteCalldata(transactions);
4559
+ const invocation = await this.buildInvocation(transactions, signerDetails);
4497
4560
  const response = await super.getInvokeEstimateFee(
4498
- { contractAddress: this.address, calldata, signature },
4561
+ { ...invocation },
4499
4562
  { version, nonce },
4500
4563
  blockIdentifier
4501
4564
  );
@@ -4509,17 +4572,12 @@ var Account = class extends Provider {
4509
4572
  const nonce = toBN(providedNonce ?? await this.getNonce());
4510
4573
  const version = toBN(feeTransactionVersion);
4511
4574
  const chainId = await this.getChainId();
4512
- const contractDefinition = parseContract(contract);
4513
- const signature = await this.signer.signDeclareTransaction({
4514
- classHash,
4515
- senderAddress: this.address,
4516
- chainId,
4517
- maxFee: ZERO,
4518
- version,
4519
- nonce
4520
- });
4575
+ const payload = await this.buildDeclarePayload(
4576
+ { classHash, contract },
4577
+ { nonce, chainId, version, walletAddress: this.address, maxFee: ZERO }
4578
+ );
4521
4579
  const response = await super.getDeclareEstimateFee(
4522
- { senderAddress: this.address, signature, contractDefinition },
4580
+ { ...payload },
4523
4581
  { version, nonce },
4524
4582
  blockIdentifier
4525
4583
  );
@@ -4538,19 +4596,12 @@ var Account = class extends Provider {
4538
4596
  const nonce = "0x0";
4539
4597
  const version = toBN(feeTransactionVersion);
4540
4598
  const chainId = await this.getChainId();
4541
- const contractAddress = providedContractAddress ?? calculateContractAddressFromHash(addressSalt, classHash, constructorCalldata, 0);
4542
- const signature = await this.signer.signDeployAccountTransaction({
4543
- classHash,
4544
- contractAddress,
4545
- chainId,
4546
- maxFee: ZERO,
4547
- version,
4548
- nonce,
4549
- addressSalt,
4550
- constructorCalldata
4551
- });
4599
+ const payload = await this.buildAccountDeployPayload(
4600
+ { classHash, addressSalt, constructorCalldata, contractAddress: providedContractAddress },
4601
+ { nonce, chainId, version, walletAddress: this.address, maxFee: ZERO }
4602
+ );
4552
4603
  const response = await super.getDeployAccountEstimateFee(
4553
- { classHash, addressSalt, constructorCalldata, signature },
4604
+ { ...payload },
4554
4605
  { version, nonce },
4555
4606
  blockIdentifier
4556
4607
  );
@@ -4561,27 +4612,85 @@ var Account = class extends Provider {
4561
4612
  };
4562
4613
  }
4563
4614
  async estimateDeployFee(payload, transactionsDetail) {
4564
- const calls = [].concat(payload).map((it) => {
4565
- const {
4566
- classHash,
4567
- salt = "0",
4568
- unique = true,
4569
- constructorCalldata = []
4570
- } = it;
4571
- const compiledConstructorCallData = compileCalldata(constructorCalldata);
4615
+ const calls = this.buildUDCContractPayload(payload);
4616
+ return this.estimateInvokeFee(calls, transactionsDetail);
4617
+ }
4618
+ async estimateFeeBulk(transactions, { nonce: providedNonce, blockIdentifier } = {}) {
4619
+ const nonce = toBN(providedNonce ?? await this.getNonce());
4620
+ const version = toBN(feeTransactionVersion);
4621
+ const chainId = await this.getChainId();
4622
+ const params = await Promise.all(
4623
+ [].concat(transactions).map(async (transaction, index) => {
4624
+ const signerDetails = {
4625
+ walletAddress: this.address,
4626
+ nonce: toBN(Number(nonce) + index),
4627
+ maxFee: ZERO,
4628
+ version,
4629
+ chainId
4630
+ };
4631
+ const txPayload = transaction.payload;
4632
+ let res;
4633
+ if (typeof transaction === "object" && transaction.type === "INVOKE_FUNCTION") {
4634
+ const invocation = await this.buildInvocation(
4635
+ Array.isArray(txPayload) ? txPayload : [txPayload],
4636
+ signerDetails
4637
+ );
4638
+ res = {
4639
+ type: "INVOKE_FUNCTION",
4640
+ ...invocation,
4641
+ version,
4642
+ nonce: toBN(Number(nonce) + index),
4643
+ blockIdentifier
4644
+ };
4645
+ } else if (typeof transaction === "object" && transaction.type === "DECLARE") {
4646
+ const declareContractPayload = await this.buildDeclarePayload(txPayload, signerDetails);
4647
+ res = {
4648
+ type: "DECLARE",
4649
+ ...declareContractPayload,
4650
+ version,
4651
+ nonce: toBN(Number(nonce) + index),
4652
+ blockIdentifier
4653
+ };
4654
+ } else if (typeof transaction === "object" && transaction.type === "DEPLOY_ACCOUNT") {
4655
+ const payload = await this.buildAccountDeployPayload(txPayload, signerDetails);
4656
+ res = {
4657
+ type: "DEPLOY_ACCOUNT",
4658
+ ...payload,
4659
+ version,
4660
+ nonce,
4661
+ blockIdentifier
4662
+ };
4663
+ } else if (typeof transaction === "object" && transaction.type === "DEPLOY") {
4664
+ const calls = this.buildUDCContractPayload(txPayload);
4665
+ const invocation = await this.buildInvocation(calls, signerDetails);
4666
+ res = {
4667
+ type: "INVOKE_FUNCTION",
4668
+ ...invocation,
4669
+ version,
4670
+ nonce: toBN(Number(nonce) + index),
4671
+ blockIdentifier
4672
+ };
4673
+ }
4674
+ return res;
4675
+ })
4676
+ );
4677
+ const response = await super.getEstimateFeeBulk(params, blockIdentifier);
4678
+ return [].concat(response).map((elem) => {
4679
+ const suggestedMaxFee = estimatedFeeToMaxFee(elem.overall_fee);
4572
4680
  return {
4573
- contractAddress: UDC.ADDRESS,
4574
- entrypoint: UDC.ENTRYPOINT,
4575
- calldata: [
4576
- classHash,
4577
- salt,
4578
- toCairoBool(unique),
4579
- compiledConstructorCallData.length,
4580
- ...compiledConstructorCallData
4581
- ]
4681
+ ...elem,
4682
+ suggestedMaxFee
4582
4683
  };
4583
4684
  });
4584
- return this.estimateInvokeFee(calls, transactionsDetail);
4685
+ }
4686
+ async buildInvocation(call, signerDetails) {
4687
+ const calldata = fromCallsToExecuteCalldata(call);
4688
+ const signature = await this.signer.signTransaction(call, signerDetails);
4689
+ return {
4690
+ contractAddress: this.address,
4691
+ calldata,
4692
+ signature
4693
+ };
4585
4694
  }
4586
4695
  async execute(calls, abis = void 0, transactionsDetail = {}) {
4587
4696
  const transactions = Array.isArray(calls) ? calls : [calls];
@@ -4770,6 +4879,69 @@ var Account = class extends Provider {
4770
4879
  }
4771
4880
  return feeEstimate.suggestedMaxFee.toString();
4772
4881
  }
4882
+ async buildDeclarePayload({ classHash, contract }, { nonce, chainId, version, walletAddress, maxFee }) {
4883
+ const contractDefinition = parseContract(contract);
4884
+ const signature = await this.signer.signDeclareTransaction({
4885
+ classHash,
4886
+ senderAddress: walletAddress,
4887
+ chainId,
4888
+ maxFee,
4889
+ version,
4890
+ nonce
4891
+ });
4892
+ return {
4893
+ senderAddress: walletAddress,
4894
+ signature,
4895
+ contractDefinition
4896
+ };
4897
+ }
4898
+ async buildAccountDeployPayload({
4899
+ classHash,
4900
+ addressSalt = 0,
4901
+ constructorCalldata = [],
4902
+ contractAddress: providedContractAddress
4903
+ }, { nonce, chainId, version, maxFee }) {
4904
+ const contractAddress = providedContractAddress ?? calculateContractAddressFromHash(addressSalt, classHash, constructorCalldata, 0);
4905
+ const signature = await this.signer.signDeployAccountTransaction({
4906
+ classHash,
4907
+ contractAddress,
4908
+ chainId,
4909
+ maxFee,
4910
+ version,
4911
+ nonce,
4912
+ addressSalt,
4913
+ constructorCalldata
4914
+ });
4915
+ return {
4916
+ classHash,
4917
+ addressSalt,
4918
+ constructorCalldata,
4919
+ signature
4920
+ };
4921
+ }
4922
+ buildUDCContractPayload(payload) {
4923
+ const calls = [].concat(payload).map((it) => {
4924
+ const {
4925
+ classHash,
4926
+ salt = "0",
4927
+ unique = true,
4928
+ constructorCalldata = []
4929
+ } = it;
4930
+ const compiledConstructorCallData = compileCalldata(constructorCalldata);
4931
+ return {
4932
+ contractAddress: UDC.ADDRESS,
4933
+ entrypoint: UDC.ENTRYPOINT,
4934
+ calldata: [
4935
+ classHash,
4936
+ salt,
4937
+ toCairoBool(unique),
4938
+ compiledConstructorCallData.length,
4939
+ ...compiledConstructorCallData
4940
+ ]
4941
+ };
4942
+ });
4943
+ return calls;
4944
+ }
4773
4945
  };
4774
4946
 
4775
4947
  // src/account/interface.ts