@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.js CHANGED
@@ -73,6 +73,7 @@ __export(src_exports, {
73
73
  FuelConnectorEventType: () => FuelConnectorEventType,
74
74
  FuelConnectorEventTypes: () => FuelConnectorEventTypes,
75
75
  FuelConnectorMethods: () => FuelConnectorMethods,
76
+ GAS_USED_MODIFIER: () => GAS_USED_MODIFIER,
76
77
  HDWallet: () => hdwallet_default,
77
78
  Language: () => Language,
78
79
  LocalStorage: () => LocalStorage,
@@ -1092,6 +1093,17 @@ var IsUserAccountDocument = import_graphql_tag.default`
1092
1093
  }
1093
1094
  }
1094
1095
  `;
1096
+ var GetConsensusParametersVersionDocument = import_graphql_tag.default`
1097
+ query getConsensusParametersVersion {
1098
+ chain {
1099
+ latestBlock {
1100
+ header {
1101
+ consensusParametersVersion
1102
+ }
1103
+ }
1104
+ }
1105
+ }
1106
+ `;
1095
1107
  var SubmitAndAwaitDocument = import_graphql_tag.default`
1096
1108
  subscription submitAndAwait($encodedTransaction: HexString!) {
1097
1109
  submitAndAwait(tx: $encodedTransaction) {
@@ -1211,6 +1223,9 @@ function getSdk(requester) {
1211
1223
  isUserAccount(variables, options) {
1212
1224
  return requester(IsUserAccountDocument, variables, options);
1213
1225
  },
1226
+ getConsensusParametersVersion(variables, options) {
1227
+ return requester(GetConsensusParametersVersionDocument, variables, options);
1228
+ },
1214
1229
  submitAndAwait(variables, options) {
1215
1230
  return requester(SubmitAndAwaitDocument, variables, options);
1216
1231
  },
@@ -4540,6 +4555,7 @@ var RESOURCES_PAGE_SIZE_LIMIT = 512;
4540
4555
  var TRANSACTIONS_PAGE_SIZE_LIMIT = 60;
4541
4556
  var BLOCKS_PAGE_SIZE_LIMIT = 5;
4542
4557
  var DEFAULT_RESOURCE_CACHE_TTL = 2e4;
4558
+ var GAS_USED_MODIFIER = 1.2;
4543
4559
  var processGqlChain = (chain) => {
4544
4560
  const { name, daHeight, consensusParameters } = chain;
4545
4561
  const {
@@ -4615,6 +4631,8 @@ var _Provider = class {
4615
4631
  __publicField(this, "url");
4616
4632
  /** @hidden */
4617
4633
  __publicField(this, "urlWithoutAuth");
4634
+ /** @hidden */
4635
+ __publicField(this, "consensusParametersTimestamp");
4618
4636
  __publicField(this, "options", {
4619
4637
  timeout: void 0,
4620
4638
  resourceCacheTTL: void 0,
@@ -4767,16 +4785,19 @@ var _Provider = class {
4767
4785
  }
4768
4786
  /**
4769
4787
  * Return the chain and node information.
4770
- *
4788
+ * @param ignoreCache - If true, ignores the cache and re-fetch configs.
4771
4789
  * @returns A promise that resolves to the Chain and NodeInfo.
4772
4790
  */
4773
- async fetchChainAndNodeInfo() {
4791
+ async fetchChainAndNodeInfo(ignoreCache = false) {
4774
4792
  let nodeInfo;
4775
4793
  let chain;
4776
4794
  try {
4795
+ if (ignoreCache) {
4796
+ throw new Error(`Jumps to the catch block andre-fetch`);
4797
+ }
4777
4798
  nodeInfo = this.getNode();
4778
4799
  chain = this.getChain();
4779
- } catch (error) {
4800
+ } catch (_err) {
4780
4801
  const data = await this.operations.getChainAndNodeInfo();
4781
4802
  nodeInfo = {
4782
4803
  maxDepth: (0, import_math18.bn)(data.nodeInfo.maxDepth),
@@ -4789,6 +4810,7 @@ var _Provider = class {
4789
4810
  chain = processGqlChain(data.chain);
4790
4811
  _Provider.chainInfoCache[this.urlWithoutAuth] = chain;
4791
4812
  _Provider.nodeInfoCache[this.urlWithoutAuth] = nodeInfo;
4813
+ this.consensusParametersTimestamp = Date.now();
4792
4814
  }
4793
4815
  return {
4794
4816
  chain,
@@ -4938,18 +4960,25 @@ Supported fuel-core version: ${supportedVersion}.`
4938
4960
  } = this.getChain();
4939
4961
  return baseAssetId;
4940
4962
  }
4941
- validateTransaction(tx, consensusParameters) {
4942
- const { maxOutputs, maxInputs } = consensusParameters.txParameters;
4963
+ /**
4964
+ * @hidden
4965
+ */
4966
+ validateTransaction(tx) {
4967
+ const {
4968
+ consensusParameters: {
4969
+ txParameters: { maxInputs, maxOutputs }
4970
+ }
4971
+ } = this.getChain();
4943
4972
  if ((0, import_math18.bn)(tx.inputs.length).gt(maxInputs)) {
4944
4973
  throw new import_errors17.FuelError(
4945
4974
  import_errors17.ErrorCode.MAX_INPUTS_EXCEEDED,
4946
- "The transaction exceeds the maximum allowed number of inputs."
4975
+ `The transaction exceeds the maximum allowed number of inputs. Tx inputs: ${tx.inputs.length}, max inputs: ${maxInputs}`
4947
4976
  );
4948
4977
  }
4949
4978
  if ((0, import_math18.bn)(tx.outputs.length).gt(maxOutputs)) {
4950
4979
  throw new import_errors17.FuelError(
4951
4980
  import_errors17.ErrorCode.MAX_OUTPUTS_EXCEEDED,
4952
- "The transaction exceeds the maximum allowed number of outputs."
4981
+ `The transaction exceeds the maximum allowed number of outputs. Tx outputs: ${tx.outputs.length}, max outputs: ${maxOutputs}`
4953
4982
  );
4954
4983
  }
4955
4984
  }
@@ -4968,8 +4997,7 @@ Supported fuel-core version: ${supportedVersion}.`
4968
4997
  if (estimateTxDependencies) {
4969
4998
  await this.estimateTxDependencies(transactionRequest);
4970
4999
  }
4971
- const { consensusParameters } = this.getChain();
4972
- this.validateTransaction(transactionRequest, consensusParameters);
5000
+ this.validateTransaction(transactionRequest);
4973
5001
  const encodedTransaction = (0, import_utils28.hexlify)(transactionRequest.toTransactionBytes());
4974
5002
  let abis;
4975
5003
  if (isTransactionTypeScript(transactionRequest)) {
@@ -5057,6 +5085,7 @@ Supported fuel-core version: ${supportedVersion}.`
5057
5085
  const missingContractIds = [];
5058
5086
  let outputVariables = 0;
5059
5087
  let dryRunStatus;
5088
+ this.validateTransaction(transactionRequest);
5060
5089
  for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
5061
5090
  const {
5062
5091
  dryRun: [{ receipts: rawReceipts, status }]
@@ -5179,6 +5208,27 @@ Supported fuel-core version: ${supportedVersion}.`
5179
5208
  });
5180
5209
  return results;
5181
5210
  }
5211
+ async autoRefetchConfigs() {
5212
+ const now = Date.now();
5213
+ const diff = now - (this.consensusParametersTimestamp ?? 0);
5214
+ if (diff < 6e4) {
5215
+ return;
5216
+ }
5217
+ const chainInfo = this.getChain();
5218
+ const {
5219
+ consensusParameters: { version: previous }
5220
+ } = chainInfo;
5221
+ const {
5222
+ chain: {
5223
+ latestBlock: {
5224
+ header: { consensusParametersVersion: current }
5225
+ }
5226
+ }
5227
+ } = await this.operations.getConsensusParametersVersion();
5228
+ if (previous !== current) {
5229
+ await this.fetchChainAndNodeInfo(true);
5230
+ }
5231
+ }
5182
5232
  /**
5183
5233
  * Estimates the transaction gas and fee based on the provided transaction request.
5184
5234
  * @param transactionRequest - The transaction request object.
@@ -5187,6 +5237,7 @@ Supported fuel-core version: ${supportedVersion}.`
5187
5237
  async estimateTxGasAndFee(params) {
5188
5238
  const { transactionRequest } = params;
5189
5239
  let { gasPrice } = params;
5240
+ await this.autoRefetchConfigs();
5190
5241
  const chainInfo = this.getChain();
5191
5242
  const { gasPriceFactor, maxGasPerTx } = this.getGasConfig();
5192
5243
  const minGas = transactionRequest.calculateMinGas(chainInfo);
@@ -5300,7 +5351,9 @@ Supported fuel-core version: ${supportedVersion}.`
5300
5351
  if (dryRunStatus && "reason" in dryRunStatus) {
5301
5352
  throw this.extractDryRunError(txRequestClone, receipts, dryRunStatus);
5302
5353
  }
5303
- gasUsed = getGasUsedFromReceipts(receipts);
5354
+ const { maxGasPerTx } = this.getGasConfig();
5355
+ const pristineGasUsed = getGasUsedFromReceipts(receipts);
5356
+ gasUsed = (0, import_math18.bn)(pristineGasUsed.muln(GAS_USED_MODIFIER)).max(maxGasPerTx.sub(minGas));
5304
5357
  txRequestClone.gasLimit = gasUsed;
5305
5358
  ({ maxFee, maxGas, minFee, minGas, gasPrice } = await this.estimateTxGasAndFee({
5306
5359
  transactionRequest: txRequestClone,
@@ -7024,6 +7077,7 @@ var Account = class extends import_interfaces.AbstractAccount {
7024
7077
  `The account ${this.address} does not have enough base asset funds to cover the transaction execution.`
7025
7078
  );
7026
7079
  }
7080
+ this.provider.validateTransaction(request);
7027
7081
  request.updatePredicateGasUsed(estimatedPredicates);
7028
7082
  const requestToReestimate = (0, import_ramda9.clone)(request);
7029
7083
  if (addedSignatures) {
@@ -11435,8 +11489,9 @@ var _Fuel = class extends FuelConnector {
11435
11489
  }
11436
11490
  async initialize() {
11437
11491
  try {
11438
- await this.setDefaultConnector();
11492
+ const connectResponse = this.setDefaultConnector();
11439
11493
  this._targetUnsubscribe = this.setupConnectorListener();
11494
+ await connectResponse;
11440
11495
  } catch (error) {
11441
11496
  throw new import_errors31.FuelError(import_errors31.ErrorCode.INVALID_PROVIDER, "Error initializing Fuel Connector");
11442
11497
  }
@@ -11774,6 +11829,7 @@ __publicField(Fuel, "defaultConfig", {});
11774
11829
  FuelConnectorEventType,
11775
11830
  FuelConnectorEventTypes,
11776
11831
  FuelConnectorMethods,
11832
+ GAS_USED_MODIFIER,
11777
11833
  HDWallet,
11778
11834
  Language,
11779
11835
  LocalStorage,