@fuel-ts/account 0.95.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.
@@ -1237,6 +1237,17 @@ var IsUserAccountDocument = import_graphql_tag.default`
1237
1237
  }
1238
1238
  }
1239
1239
  `;
1240
+ var GetConsensusParametersVersionDocument = import_graphql_tag.default`
1241
+ query getConsensusParametersVersion {
1242
+ chain {
1243
+ latestBlock {
1244
+ header {
1245
+ consensusParametersVersion
1246
+ }
1247
+ }
1248
+ }
1249
+ }
1250
+ `;
1240
1251
  var SubmitAndAwaitDocument = import_graphql_tag.default`
1241
1252
  subscription submitAndAwait($encodedTransaction: HexString!) {
1242
1253
  submitAndAwait(tx: $encodedTransaction) {
@@ -1356,6 +1367,9 @@ function getSdk(requester) {
1356
1367
  isUserAccount(variables, options) {
1357
1368
  return requester(IsUserAccountDocument, variables, options);
1358
1369
  },
1370
+ getConsensusParametersVersion(variables, options) {
1371
+ return requester(GetConsensusParametersVersionDocument, variables, options);
1372
+ },
1359
1373
  submitAndAwait(variables, options) {
1360
1374
  return requester(SubmitAndAwaitDocument, variables, options);
1361
1375
  },
@@ -4544,6 +4558,7 @@ var RESOURCES_PAGE_SIZE_LIMIT = 512;
4544
4558
  var TRANSACTIONS_PAGE_SIZE_LIMIT = 60;
4545
4559
  var BLOCKS_PAGE_SIZE_LIMIT = 5;
4546
4560
  var DEFAULT_RESOURCE_CACHE_TTL = 2e4;
4561
+ var GAS_USED_MODIFIER = 1.2;
4547
4562
  var processGqlChain = (chain) => {
4548
4563
  const { name, daHeight, consensusParameters } = chain;
4549
4564
  const {
@@ -4619,6 +4634,8 @@ var _Provider = class {
4619
4634
  __publicField(this, "url");
4620
4635
  /** @hidden */
4621
4636
  __publicField(this, "urlWithoutAuth");
4637
+ /** @hidden */
4638
+ __publicField(this, "consensusParametersTimestamp");
4622
4639
  __publicField(this, "options", {
4623
4640
  timeout: void 0,
4624
4641
  resourceCacheTTL: void 0,
@@ -4771,16 +4788,19 @@ var _Provider = class {
4771
4788
  }
4772
4789
  /**
4773
4790
  * Return the chain and node information.
4774
- *
4791
+ * @param ignoreCache - If true, ignores the cache and re-fetch configs.
4775
4792
  * @returns A promise that resolves to the Chain and NodeInfo.
4776
4793
  */
4777
- async fetchChainAndNodeInfo() {
4794
+ async fetchChainAndNodeInfo(ignoreCache = false) {
4778
4795
  let nodeInfo;
4779
4796
  let chain;
4780
4797
  try {
4798
+ if (ignoreCache) {
4799
+ throw new Error(`Jumps to the catch block andre-fetch`);
4800
+ }
4781
4801
  nodeInfo = this.getNode();
4782
4802
  chain = this.getChain();
4783
- } catch (error) {
4803
+ } catch (_err) {
4784
4804
  const data = await this.operations.getChainAndNodeInfo();
4785
4805
  nodeInfo = {
4786
4806
  maxDepth: (0, import_math19.bn)(data.nodeInfo.maxDepth),
@@ -4793,6 +4813,7 @@ var _Provider = class {
4793
4813
  chain = processGqlChain(data.chain);
4794
4814
  _Provider.chainInfoCache[this.urlWithoutAuth] = chain;
4795
4815
  _Provider.nodeInfoCache[this.urlWithoutAuth] = nodeInfo;
4816
+ this.consensusParametersTimestamp = Date.now();
4796
4817
  }
4797
4818
  return {
4798
4819
  chain,
@@ -4942,18 +4963,25 @@ Supported fuel-core version: ${supportedVersion}.`
4942
4963
  } = this.getChain();
4943
4964
  return baseAssetId;
4944
4965
  }
4945
- validateTransaction(tx, consensusParameters) {
4946
- const { maxOutputs, maxInputs } = consensusParameters.txParameters;
4966
+ /**
4967
+ * @hidden
4968
+ */
4969
+ validateTransaction(tx) {
4970
+ const {
4971
+ consensusParameters: {
4972
+ txParameters: { maxInputs, maxOutputs }
4973
+ }
4974
+ } = this.getChain();
4947
4975
  if ((0, import_math19.bn)(tx.inputs.length).gt(maxInputs)) {
4948
4976
  throw new import_errors18.FuelError(
4949
4977
  import_errors18.ErrorCode.MAX_INPUTS_EXCEEDED,
4950
- "The transaction exceeds the maximum allowed number of inputs."
4978
+ `The transaction exceeds the maximum allowed number of inputs. Tx inputs: ${tx.inputs.length}, max inputs: ${maxInputs}`
4951
4979
  );
4952
4980
  }
4953
4981
  if ((0, import_math19.bn)(tx.outputs.length).gt(maxOutputs)) {
4954
4982
  throw new import_errors18.FuelError(
4955
4983
  import_errors18.ErrorCode.MAX_OUTPUTS_EXCEEDED,
4956
- "The transaction exceeds the maximum allowed number of outputs."
4984
+ `The transaction exceeds the maximum allowed number of outputs. Tx outputs: ${tx.outputs.length}, max outputs: ${maxOutputs}`
4957
4985
  );
4958
4986
  }
4959
4987
  }
@@ -4972,8 +5000,7 @@ Supported fuel-core version: ${supportedVersion}.`
4972
5000
  if (estimateTxDependencies) {
4973
5001
  await this.estimateTxDependencies(transactionRequest);
4974
5002
  }
4975
- const { consensusParameters } = this.getChain();
4976
- this.validateTransaction(transactionRequest, consensusParameters);
5003
+ this.validateTransaction(transactionRequest);
4977
5004
  const encodedTransaction = (0, import_utils30.hexlify)(transactionRequest.toTransactionBytes());
4978
5005
  let abis;
4979
5006
  if (isTransactionTypeScript(transactionRequest)) {
@@ -5061,6 +5088,7 @@ Supported fuel-core version: ${supportedVersion}.`
5061
5088
  const missingContractIds = [];
5062
5089
  let outputVariables = 0;
5063
5090
  let dryRunStatus;
5091
+ this.validateTransaction(transactionRequest);
5064
5092
  for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
5065
5093
  const {
5066
5094
  dryRun: [{ receipts: rawReceipts, status }]
@@ -5183,6 +5211,27 @@ Supported fuel-core version: ${supportedVersion}.`
5183
5211
  });
5184
5212
  return results;
5185
5213
  }
5214
+ async autoRefetchConfigs() {
5215
+ const now = Date.now();
5216
+ const diff = now - (this.consensusParametersTimestamp ?? 0);
5217
+ if (diff < 6e4) {
5218
+ return;
5219
+ }
5220
+ const chainInfo = this.getChain();
5221
+ const {
5222
+ consensusParameters: { version: previous }
5223
+ } = chainInfo;
5224
+ const {
5225
+ chain: {
5226
+ latestBlock: {
5227
+ header: { consensusParametersVersion: current }
5228
+ }
5229
+ }
5230
+ } = await this.operations.getConsensusParametersVersion();
5231
+ if (previous !== current) {
5232
+ await this.fetchChainAndNodeInfo(true);
5233
+ }
5234
+ }
5186
5235
  /**
5187
5236
  * Estimates the transaction gas and fee based on the provided transaction request.
5188
5237
  * @param transactionRequest - The transaction request object.
@@ -5191,6 +5240,7 @@ Supported fuel-core version: ${supportedVersion}.`
5191
5240
  async estimateTxGasAndFee(params) {
5192
5241
  const { transactionRequest } = params;
5193
5242
  let { gasPrice } = params;
5243
+ await this.autoRefetchConfigs();
5194
5244
  const chainInfo = this.getChain();
5195
5245
  const { gasPriceFactor, maxGasPerTx } = this.getGasConfig();
5196
5246
  const minGas = transactionRequest.calculateMinGas(chainInfo);
@@ -5304,7 +5354,9 @@ Supported fuel-core version: ${supportedVersion}.`
5304
5354
  if (dryRunStatus && "reason" in dryRunStatus) {
5305
5355
  throw this.extractDryRunError(txRequestClone, receipts, dryRunStatus);
5306
5356
  }
5307
- gasUsed = getGasUsedFromReceipts(receipts);
5357
+ const { maxGasPerTx } = this.getGasConfig();
5358
+ const pristineGasUsed = getGasUsedFromReceipts(receipts);
5359
+ gasUsed = (0, import_math19.bn)(pristineGasUsed.muln(GAS_USED_MODIFIER)).max(maxGasPerTx.sub(minGas));
5308
5360
  txRequestClone.gasLimit = gasUsed;
5309
5361
  ({ maxFee, maxGas, minFee, minGas, gasPrice } = await this.estimateTxGasAndFee({
5310
5362
  transactionRequest: txRequestClone,
@@ -6897,6 +6949,7 @@ var Account = class extends import_interfaces.AbstractAccount {
6897
6949
  `The account ${this.address} does not have enough base asset funds to cover the transaction execution.`
6898
6950
  );
6899
6951
  }
6952
+ this.provider.validateTransaction(request);
6900
6953
  request.updatePredicateGasUsed(estimatedPredicates);
6901
6954
  const requestToReestimate = (0, import_ramda9.clone)(request);
6902
6955
  if (addedSignatures) {