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.js CHANGED
@@ -2878,6 +2878,9 @@ var RpcProvider = class {
2878
2878
  block_id
2879
2879
  }).then(this.responseParser.parseFeeEstimateResponse);
2880
2880
  }
2881
+ async getEstimateFeeBulk(_invocations, _blockIdentifier = this.blockIdentifier) {
2882
+ throw new Error("RPC does not implement getInvokeEstimateFeeBulk function");
2883
+ }
2881
2884
  async declareContract({ contractDefinition, signature, senderAddress }, details) {
2882
2885
  return this.fetchEndpoint("starknet_addDeclareTransaction", {
2883
2886
  declare_transaction: {
@@ -3059,6 +3062,27 @@ var SequencerAPIResponseParser = class extends ResponseParser {
3059
3062
  overall_fee: toBN(res.amount)
3060
3063
  };
3061
3064
  }
3065
+ parseFeeEstimateBulkResponse(res) {
3066
+ return [].concat(res).map((item) => {
3067
+ if ("overall_fee" in item) {
3068
+ let gasInfo = {};
3069
+ try {
3070
+ gasInfo = {
3071
+ gas_consumed: toBN(item.gas_usage),
3072
+ gas_price: toBN(item.gas_price)
3073
+ };
3074
+ } catch {
3075
+ }
3076
+ return {
3077
+ overall_fee: toBN(item.overall_fee),
3078
+ ...gasInfo
3079
+ };
3080
+ }
3081
+ return {
3082
+ overall_fee: toBN(item.amount)
3083
+ };
3084
+ });
3085
+ }
3062
3086
  parseCallContractResponse(res) {
3063
3087
  return {
3064
3088
  result: res.result
@@ -3191,6 +3215,7 @@ var SequencerProvider = class {
3191
3215
  "call_contract",
3192
3216
  "estimate_fee",
3193
3217
  "estimate_message_fee",
3218
+ "estimate_fee_bulk",
3194
3219
  "simulate_transaction"
3195
3220
  ];
3196
3221
  return postMethodEndpoints.includes(endpoint) ? "POST" : "GET";
@@ -3393,6 +3418,42 @@ var SequencerProvider = class {
3393
3418
  }
3394
3419
  ).then(this.responseParser.parseFeeEstimateResponse);
3395
3420
  }
3421
+ async getEstimateFeeBulk(invocations, blockIdentifier = this.blockIdentifier) {
3422
+ const params = invocations.map((invocation) => {
3423
+ let res;
3424
+ if (invocation.type === "INVOKE_FUNCTION") {
3425
+ res = {
3426
+ type: invocation.type,
3427
+ contract_address: invocation.contractAddress,
3428
+ calldata: invocation.calldata ?? []
3429
+ };
3430
+ } else if (invocation.type === "DECLARE") {
3431
+ res = {
3432
+ type: invocation.type,
3433
+ sender_address: invocation.senderAddress,
3434
+ contract_class: invocation.contractDefinition
3435
+ };
3436
+ } else {
3437
+ res = {
3438
+ type: invocation.type,
3439
+ class_hash: toHex(toBN(invocation.classHash)),
3440
+ constructor_calldata: bigNumberishArrayToDecimalStringArray(
3441
+ invocation.constructorCalldata || []
3442
+ ),
3443
+ contract_address_salt: toHex(toBN(invocation.addressSalt || 0))
3444
+ };
3445
+ }
3446
+ return {
3447
+ ...res,
3448
+ signature: bigNumberishArrayToDecimalStringArray(invocation.signature || []),
3449
+ version: toHex(toBN((invocation == null ? void 0 : invocation.version) || 1)),
3450
+ nonce: toHex(toBN(invocation.nonce))
3451
+ };
3452
+ });
3453
+ return this.fetchEndpoint("estimate_fee_bulk", { blockIdentifier }, params).then(
3454
+ this.responseParser.parseFeeEstimateBulkResponse
3455
+ );
3456
+ }
3396
3457
  async getCode(contractAddress, blockIdentifier = this.blockIdentifier) {
3397
3458
  return this.fetchEndpoint("get_code", { contractAddress, blockIdentifier });
3398
3459
  }
@@ -3495,6 +3556,9 @@ var Provider = class {
3495
3556
  blockIdentifier
3496
3557
  );
3497
3558
  }
3559
+ async getEstimateFeeBulk(invocations, blockIdentifier) {
3560
+ return this.provider.getEstimateFeeBulk(invocations, blockIdentifier);
3561
+ }
3498
3562
  async getNonceForAddress(contractAddress, blockIdentifier) {
3499
3563
  return this.provider.getNonceForAddress(contractAddress, blockIdentifier);
3500
3564
  }
@@ -4549,10 +4613,9 @@ var Account = class extends Provider {
4549
4613
  version,
4550
4614
  chainId
4551
4615
  };
4552
- const signature = await this.signer.signTransaction(transactions, signerDetails);
4553
- const calldata = fromCallsToExecuteCalldata(transactions);
4616
+ const invocation = await this.buildInvocation(transactions, signerDetails);
4554
4617
  const response = await super.getInvokeEstimateFee(
4555
- { contractAddress: this.address, calldata, signature },
4618
+ { ...invocation },
4556
4619
  { version, nonce },
4557
4620
  blockIdentifier
4558
4621
  );
@@ -4566,17 +4629,12 @@ var Account = class extends Provider {
4566
4629
  const nonce = toBN(providedNonce ?? await this.getNonce());
4567
4630
  const version = toBN(feeTransactionVersion);
4568
4631
  const chainId = await this.getChainId();
4569
- const contractDefinition = parseContract(contract);
4570
- const signature = await this.signer.signDeclareTransaction({
4571
- classHash,
4572
- senderAddress: this.address,
4573
- chainId,
4574
- maxFee: ZERO,
4575
- version,
4576
- nonce
4577
- });
4632
+ const payload = await this.buildDeclarePayload(
4633
+ { classHash, contract },
4634
+ { nonce, chainId, version, walletAddress: this.address, maxFee: ZERO }
4635
+ );
4578
4636
  const response = await super.getDeclareEstimateFee(
4579
- { senderAddress: this.address, signature, contractDefinition },
4637
+ { ...payload },
4580
4638
  { version, nonce },
4581
4639
  blockIdentifier
4582
4640
  );
@@ -4595,19 +4653,12 @@ var Account = class extends Provider {
4595
4653
  const nonce = "0x0";
4596
4654
  const version = toBN(feeTransactionVersion);
4597
4655
  const chainId = await this.getChainId();
4598
- const contractAddress = providedContractAddress ?? calculateContractAddressFromHash(addressSalt, classHash, constructorCalldata, 0);
4599
- const signature = await this.signer.signDeployAccountTransaction({
4600
- classHash,
4601
- contractAddress,
4602
- chainId,
4603
- maxFee: ZERO,
4604
- version,
4605
- nonce,
4606
- addressSalt,
4607
- constructorCalldata
4608
- });
4656
+ const payload = await this.buildAccountDeployPayload(
4657
+ { classHash, addressSalt, constructorCalldata, contractAddress: providedContractAddress },
4658
+ { nonce, chainId, version, walletAddress: this.address, maxFee: ZERO }
4659
+ );
4609
4660
  const response = await super.getDeployAccountEstimateFee(
4610
- { classHash, addressSalt, constructorCalldata, signature },
4661
+ { ...payload },
4611
4662
  { version, nonce },
4612
4663
  blockIdentifier
4613
4664
  );
@@ -4618,27 +4669,85 @@ var Account = class extends Provider {
4618
4669
  };
4619
4670
  }
4620
4671
  async estimateDeployFee(payload, transactionsDetail) {
4621
- const calls = [].concat(payload).map((it) => {
4622
- const {
4623
- classHash,
4624
- salt = "0",
4625
- unique = true,
4626
- constructorCalldata = []
4627
- } = it;
4628
- const compiledConstructorCallData = compileCalldata(constructorCalldata);
4672
+ const calls = this.buildUDCContractPayload(payload);
4673
+ return this.estimateInvokeFee(calls, transactionsDetail);
4674
+ }
4675
+ async estimateFeeBulk(transactions, { nonce: providedNonce, blockIdentifier } = {}) {
4676
+ const nonce = toBN(providedNonce ?? await this.getNonce());
4677
+ const version = toBN(feeTransactionVersion);
4678
+ const chainId = await this.getChainId();
4679
+ const params = await Promise.all(
4680
+ [].concat(transactions).map(async (transaction, index) => {
4681
+ const signerDetails = {
4682
+ walletAddress: this.address,
4683
+ nonce: toBN(Number(nonce) + index),
4684
+ maxFee: ZERO,
4685
+ version,
4686
+ chainId
4687
+ };
4688
+ const txPayload = transaction.payload;
4689
+ let res;
4690
+ if (typeof transaction === "object" && transaction.type === "INVOKE_FUNCTION") {
4691
+ const invocation = await this.buildInvocation(
4692
+ Array.isArray(txPayload) ? txPayload : [txPayload],
4693
+ signerDetails
4694
+ );
4695
+ res = {
4696
+ type: "INVOKE_FUNCTION",
4697
+ ...invocation,
4698
+ version,
4699
+ nonce: toBN(Number(nonce) + index),
4700
+ blockIdentifier
4701
+ };
4702
+ } else if (typeof transaction === "object" && transaction.type === "DECLARE") {
4703
+ const declareContractPayload = await this.buildDeclarePayload(txPayload, signerDetails);
4704
+ res = {
4705
+ type: "DECLARE",
4706
+ ...declareContractPayload,
4707
+ version,
4708
+ nonce: toBN(Number(nonce) + index),
4709
+ blockIdentifier
4710
+ };
4711
+ } else if (typeof transaction === "object" && transaction.type === "DEPLOY_ACCOUNT") {
4712
+ const payload = await this.buildAccountDeployPayload(txPayload, signerDetails);
4713
+ res = {
4714
+ type: "DEPLOY_ACCOUNT",
4715
+ ...payload,
4716
+ version,
4717
+ nonce,
4718
+ blockIdentifier
4719
+ };
4720
+ } else if (typeof transaction === "object" && transaction.type === "DEPLOY") {
4721
+ const calls = this.buildUDCContractPayload(txPayload);
4722
+ const invocation = await this.buildInvocation(calls, signerDetails);
4723
+ res = {
4724
+ type: "INVOKE_FUNCTION",
4725
+ ...invocation,
4726
+ version,
4727
+ nonce: toBN(Number(nonce) + index),
4728
+ blockIdentifier
4729
+ };
4730
+ }
4731
+ return res;
4732
+ })
4733
+ );
4734
+ const response = await super.getEstimateFeeBulk(params, blockIdentifier);
4735
+ return [].concat(response).map((elem) => {
4736
+ const suggestedMaxFee = estimatedFeeToMaxFee(elem.overall_fee);
4629
4737
  return {
4630
- contractAddress: UDC.ADDRESS,
4631
- entrypoint: UDC.ENTRYPOINT,
4632
- calldata: [
4633
- classHash,
4634
- salt,
4635
- toCairoBool(unique),
4636
- compiledConstructorCallData.length,
4637
- ...compiledConstructorCallData
4638
- ]
4738
+ ...elem,
4739
+ suggestedMaxFee
4639
4740
  };
4640
4741
  });
4641
- return this.estimateInvokeFee(calls, transactionsDetail);
4742
+ }
4743
+ async buildInvocation(call, signerDetails) {
4744
+ const calldata = fromCallsToExecuteCalldata(call);
4745
+ const signature = await this.signer.signTransaction(call, signerDetails);
4746
+ return {
4747
+ contractAddress: this.address,
4748
+ calldata,
4749
+ signature
4750
+ };
4642
4751
  }
4643
4752
  async execute(calls, abis = void 0, transactionsDetail = {}) {
4644
4753
  const transactions = Array.isArray(calls) ? calls : [calls];
@@ -4827,6 +4936,69 @@ var Account = class extends Provider {
4827
4936
  }
4828
4937
  return feeEstimate.suggestedMaxFee.toString();
4829
4938
  }
4939
+ async buildDeclarePayload({ classHash, contract }, { nonce, chainId, version, walletAddress, maxFee }) {
4940
+ const contractDefinition = parseContract(contract);
4941
+ const signature = await this.signer.signDeclareTransaction({
4942
+ classHash,
4943
+ senderAddress: walletAddress,
4944
+ chainId,
4945
+ maxFee,
4946
+ version,
4947
+ nonce
4948
+ });
4949
+ return {
4950
+ senderAddress: walletAddress,
4951
+ signature,
4952
+ contractDefinition
4953
+ };
4954
+ }
4955
+ async buildAccountDeployPayload({
4956
+ classHash,
4957
+ addressSalt = 0,
4958
+ constructorCalldata = [],
4959
+ contractAddress: providedContractAddress
4960
+ }, { nonce, chainId, version, maxFee }) {
4961
+ const contractAddress = providedContractAddress ?? calculateContractAddressFromHash(addressSalt, classHash, constructorCalldata, 0);
4962
+ const signature = await this.signer.signDeployAccountTransaction({
4963
+ classHash,
4964
+ contractAddress,
4965
+ chainId,
4966
+ maxFee,
4967
+ version,
4968
+ nonce,
4969
+ addressSalt,
4970
+ constructorCalldata
4971
+ });
4972
+ return {
4973
+ classHash,
4974
+ addressSalt,
4975
+ constructorCalldata,
4976
+ signature
4977
+ };
4978
+ }
4979
+ buildUDCContractPayload(payload) {
4980
+ const calls = [].concat(payload).map((it) => {
4981
+ const {
4982
+ classHash,
4983
+ salt = "0",
4984
+ unique = true,
4985
+ constructorCalldata = []
4986
+ } = it;
4987
+ const compiledConstructorCallData = compileCalldata(constructorCalldata);
4988
+ return {
4989
+ contractAddress: UDC.ADDRESS,
4990
+ entrypoint: UDC.ENTRYPOINT,
4991
+ calldata: [
4992
+ classHash,
4993
+ salt,
4994
+ toCairoBool(unique),
4995
+ compiledConstructorCallData.length,
4996
+ ...compiledConstructorCallData
4997
+ ]
4998
+ };
4999
+ });
5000
+ return calls;
5001
+ }
4830
5002
  };
4831
5003
 
4832
5004
  // src/account/interface.ts