@fuel-ts/account 0.96.0 → 0.96.1

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
@@ -904,6 +904,17 @@ var IsUserAccountDocument = gql`
904
904
  }
905
905
  }
906
906
  `;
907
+ var GetConsensusParametersVersionDocument = gql`
908
+ query getConsensusParametersVersion {
909
+ chain {
910
+ latestBlock {
911
+ header {
912
+ consensusParametersVersion
913
+ }
914
+ }
915
+ }
916
+ }
917
+ `;
907
918
  var SubmitAndAwaitDocument = gql`
908
919
  subscription submitAndAwait($encodedTransaction: HexString!) {
909
920
  submitAndAwait(tx: $encodedTransaction) {
@@ -1023,6 +1034,9 @@ function getSdk(requester) {
1023
1034
  isUserAccount(variables, options) {
1024
1035
  return requester(IsUserAccountDocument, variables, options);
1025
1036
  },
1037
+ getConsensusParametersVersion(variables, options) {
1038
+ return requester(GetConsensusParametersVersionDocument, variables, options);
1039
+ },
1026
1040
  submitAndAwait(variables, options) {
1027
1041
  return requester(SubmitAndAwaitDocument, variables, options);
1028
1042
  },
@@ -4369,6 +4383,7 @@ var RESOURCES_PAGE_SIZE_LIMIT = 512;
4369
4383
  var TRANSACTIONS_PAGE_SIZE_LIMIT = 60;
4370
4384
  var BLOCKS_PAGE_SIZE_LIMIT = 5;
4371
4385
  var DEFAULT_RESOURCE_CACHE_TTL = 2e4;
4386
+ var GAS_USED_MODIFIER = 1.2;
4372
4387
  var processGqlChain = (chain) => {
4373
4388
  const { name, daHeight, consensusParameters } = chain;
4374
4389
  const {
@@ -4444,6 +4459,8 @@ var _Provider = class {
4444
4459
  __publicField(this, "url");
4445
4460
  /** @hidden */
4446
4461
  __publicField(this, "urlWithoutAuth");
4462
+ /** @hidden */
4463
+ __publicField(this, "consensusParametersTimestamp");
4447
4464
  __publicField(this, "options", {
4448
4465
  timeout: void 0,
4449
4466
  resourceCacheTTL: void 0,
@@ -4596,16 +4613,19 @@ var _Provider = class {
4596
4613
  }
4597
4614
  /**
4598
4615
  * Return the chain and node information.
4599
- *
4616
+ * @param ignoreCache - If true, ignores the cache and re-fetch configs.
4600
4617
  * @returns A promise that resolves to the Chain and NodeInfo.
4601
4618
  */
4602
- async fetchChainAndNodeInfo() {
4619
+ async fetchChainAndNodeInfo(ignoreCache = false) {
4603
4620
  let nodeInfo;
4604
4621
  let chain;
4605
4622
  try {
4623
+ if (ignoreCache) {
4624
+ throw new Error(`Jumps to the catch block andre-fetch`);
4625
+ }
4606
4626
  nodeInfo = this.getNode();
4607
4627
  chain = this.getChain();
4608
- } catch (error) {
4628
+ } catch (_err) {
4609
4629
  const data = await this.operations.getChainAndNodeInfo();
4610
4630
  nodeInfo = {
4611
4631
  maxDepth: bn17(data.nodeInfo.maxDepth),
@@ -4618,6 +4638,7 @@ var _Provider = class {
4618
4638
  chain = processGqlChain(data.chain);
4619
4639
  _Provider.chainInfoCache[this.urlWithoutAuth] = chain;
4620
4640
  _Provider.nodeInfoCache[this.urlWithoutAuth] = nodeInfo;
4641
+ this.consensusParametersTimestamp = Date.now();
4621
4642
  }
4622
4643
  return {
4623
4644
  chain,
@@ -4767,18 +4788,25 @@ Supported fuel-core version: ${supportedVersion}.`
4767
4788
  } = this.getChain();
4768
4789
  return baseAssetId;
4769
4790
  }
4770
- validateTransaction(tx, consensusParameters) {
4771
- const { maxOutputs, maxInputs } = consensusParameters.txParameters;
4791
+ /**
4792
+ * @hidden
4793
+ */
4794
+ validateTransaction(tx) {
4795
+ const {
4796
+ consensusParameters: {
4797
+ txParameters: { maxInputs, maxOutputs }
4798
+ }
4799
+ } = this.getChain();
4772
4800
  if (bn17(tx.inputs.length).gt(maxInputs)) {
4773
4801
  throw new FuelError16(
4774
4802
  ErrorCode14.MAX_INPUTS_EXCEEDED,
4775
- "The transaction exceeds the maximum allowed number of inputs."
4803
+ `The transaction exceeds the maximum allowed number of inputs. Tx inputs: ${tx.inputs.length}, max inputs: ${maxInputs}`
4776
4804
  );
4777
4805
  }
4778
4806
  if (bn17(tx.outputs.length).gt(maxOutputs)) {
4779
4807
  throw new FuelError16(
4780
4808
  ErrorCode14.MAX_OUTPUTS_EXCEEDED,
4781
- "The transaction exceeds the maximum allowed number of outputs."
4809
+ `The transaction exceeds the maximum allowed number of outputs. Tx outputs: ${tx.outputs.length}, max outputs: ${maxOutputs}`
4782
4810
  );
4783
4811
  }
4784
4812
  }
@@ -4797,8 +4825,7 @@ Supported fuel-core version: ${supportedVersion}.`
4797
4825
  if (estimateTxDependencies) {
4798
4826
  await this.estimateTxDependencies(transactionRequest);
4799
4827
  }
4800
- const { consensusParameters } = this.getChain();
4801
- this.validateTransaction(transactionRequest, consensusParameters);
4828
+ this.validateTransaction(transactionRequest);
4802
4829
  const encodedTransaction = hexlify15(transactionRequest.toTransactionBytes());
4803
4830
  let abis;
4804
4831
  if (isTransactionTypeScript(transactionRequest)) {
@@ -4886,6 +4913,7 @@ Supported fuel-core version: ${supportedVersion}.`
4886
4913
  const missingContractIds = [];
4887
4914
  let outputVariables = 0;
4888
4915
  let dryRunStatus;
4916
+ this.validateTransaction(transactionRequest);
4889
4917
  for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
4890
4918
  const {
4891
4919
  dryRun: [{ receipts: rawReceipts, status }]
@@ -5008,6 +5036,27 @@ Supported fuel-core version: ${supportedVersion}.`
5008
5036
  });
5009
5037
  return results;
5010
5038
  }
5039
+ async autoRefetchConfigs() {
5040
+ const now = Date.now();
5041
+ const diff = now - (this.consensusParametersTimestamp ?? 0);
5042
+ if (diff < 6e4) {
5043
+ return;
5044
+ }
5045
+ const chainInfo = this.getChain();
5046
+ const {
5047
+ consensusParameters: { version: previous }
5048
+ } = chainInfo;
5049
+ const {
5050
+ chain: {
5051
+ latestBlock: {
5052
+ header: { consensusParametersVersion: current }
5053
+ }
5054
+ }
5055
+ } = await this.operations.getConsensusParametersVersion();
5056
+ if (previous !== current) {
5057
+ await this.fetchChainAndNodeInfo(true);
5058
+ }
5059
+ }
5011
5060
  /**
5012
5061
  * Estimates the transaction gas and fee based on the provided transaction request.
5013
5062
  * @param transactionRequest - The transaction request object.
@@ -5016,6 +5065,7 @@ Supported fuel-core version: ${supportedVersion}.`
5016
5065
  async estimateTxGasAndFee(params) {
5017
5066
  const { transactionRequest } = params;
5018
5067
  let { gasPrice } = params;
5068
+ await this.autoRefetchConfigs();
5019
5069
  const chainInfo = this.getChain();
5020
5070
  const { gasPriceFactor, maxGasPerTx } = this.getGasConfig();
5021
5071
  const minGas = transactionRequest.calculateMinGas(chainInfo);
@@ -5129,7 +5179,9 @@ Supported fuel-core version: ${supportedVersion}.`
5129
5179
  if (dryRunStatus && "reason" in dryRunStatus) {
5130
5180
  throw this.extractDryRunError(txRequestClone, receipts, dryRunStatus);
5131
5181
  }
5132
- gasUsed = getGasUsedFromReceipts(receipts);
5182
+ const { maxGasPerTx } = this.getGasConfig();
5183
+ const pristineGasUsed = getGasUsedFromReceipts(receipts);
5184
+ gasUsed = bn17(pristineGasUsed.muln(GAS_USED_MODIFIER)).max(maxGasPerTx.sub(minGas));
5133
5185
  txRequestClone.gasLimit = gasUsed;
5134
5186
  ({ maxFee, maxGas, minFee, minGas, gasPrice } = await this.estimateTxGasAndFee({
5135
5187
  transactionRequest: txRequestClone,
@@ -6853,6 +6905,7 @@ var Account = class extends AbstractAccount {
6853
6905
  `The account ${this.address} does not have enough base asset funds to cover the transaction execution.`
6854
6906
  );
6855
6907
  }
6908
+ this.provider.validateTransaction(request);
6856
6909
  request.updatePredicateGasUsed(estimatedPredicates);
6857
6910
  const requestToReestimate = clone9(request);
6858
6911
  if (addedSignatures) {
@@ -11273,8 +11326,9 @@ var _Fuel = class extends FuelConnector {
11273
11326
  }
11274
11327
  async initialize() {
11275
11328
  try {
11276
- await this.setDefaultConnector();
11329
+ const connectResponse = this.setDefaultConnector();
11277
11330
  this._targetUnsubscribe = this.setupConnectorListener();
11331
+ await connectResponse;
11278
11332
  } catch (error) {
11279
11333
  throw new FuelError30(ErrorCode26.INVALID_PROVIDER, "Error initializing Fuel Connector");
11280
11334
  }
@@ -11611,6 +11665,7 @@ export {
11611
11665
  FuelConnectorEventType,
11612
11666
  FuelConnectorEventTypes,
11613
11667
  FuelConnectorMethods,
11668
+ GAS_USED_MODIFIER,
11614
11669
  hdwallet_default as HDWallet,
11615
11670
  Language,
11616
11671
  LocalStorage,