@fuel-ts/account 0.92.1 → 0.93.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
@@ -66,6 +66,7 @@ __export(src_exports, {
66
66
  ChainName: () => ChainName,
67
67
  ChangeOutputCollisionError: () => ChangeOutputCollisionError,
68
68
  CreateTransactionRequest: () => CreateTransactionRequest,
69
+ DEFAULT_UTXOS_CACHE_TTL: () => DEFAULT_UTXOS_CACHE_TTL,
69
70
  Fuel: () => Fuel,
70
71
  FuelConnector: () => FuelConnector,
71
72
  FuelConnectorEventType: () => FuelConnectorEventType,
@@ -2978,7 +2979,10 @@ var transactionRequestify = (obj) => {
2978
2979
  return CreateTransactionRequest.from(obj);
2979
2980
  }
2980
2981
  default: {
2981
- throw new import_errors9.FuelError(import_errors9.ErrorCode.INVALID_TRANSACTION_TYPE, `Invalid transaction type: ${type}.`);
2982
+ throw new import_errors9.FuelError(
2983
+ import_errors9.ErrorCode.UNSUPPORTED_TRANSACTION_TYPE,
2984
+ `Unsupported transaction type: ${type}.`
2985
+ );
2982
2986
  }
2983
2987
  }
2984
2988
  };
@@ -3234,8 +3238,8 @@ function getTransactionTypeName(transactionType) {
3234
3238
  return "Script" /* Script */;
3235
3239
  default:
3236
3240
  throw new import_errors11.FuelError(
3237
- import_errors11.ErrorCode.INVALID_TRANSACTION_TYPE,
3238
- `Invalid transaction type: ${transactionType}.`
3241
+ import_errors11.ErrorCode.UNSUPPORTED_TRANSACTION_TYPE,
3242
+ `Unsupported transaction type: ${transactionType}.`
3239
3243
  );
3240
3244
  }
3241
3245
  }
@@ -3989,24 +3993,11 @@ function autoRetryFetch(fetchFn, options, retryAttemptNum = 0) {
3989
3993
  };
3990
3994
  }
3991
3995
 
3992
- // src/providers/utils/merge-quantities.ts
3993
- var mergeQuantities = (...coinQuantities) => {
3994
- const resultMap = {};
3995
- function addToMap({ amount, assetId }) {
3996
- if (resultMap[assetId]) {
3997
- resultMap[assetId] = resultMap[assetId].add(amount);
3998
- } else {
3999
- resultMap[assetId] = amount;
4000
- }
4001
- }
4002
- coinQuantities.forEach((arr) => arr.forEach(addToMap));
4003
- return Object.entries(resultMap).map(([assetId, amount]) => ({ assetId, amount }));
4004
- };
4005
-
4006
3996
  // src/providers/provider.ts
4007
3997
  var MAX_RETRIES = 10;
4008
3998
  var RESOURCES_PAGE_SIZE_LIMIT = 512;
4009
3999
  var BLOCKS_PAGE_SIZE_LIMIT = 5;
4000
+ var DEFAULT_UTXOS_CACHE_TTL = 2e4;
4010
4001
  var processGqlChain = (chain) => {
4011
4002
  const { name, daHeight, consensusParameters, latestBlock } = chain;
4012
4003
  const {
@@ -4096,7 +4087,16 @@ var _Provider = class {
4096
4087
  this.options = { ...this.options, ...options };
4097
4088
  this.url = url;
4098
4089
  this.operations = this.createOperations();
4099
- this.cache = options.cacheUtxo ? new MemoryCache(options.cacheUtxo) : void 0;
4090
+ const { cacheUtxo } = this.options;
4091
+ if ((0, import_utils23.isDefined)(cacheUtxo)) {
4092
+ if (cacheUtxo !== -1) {
4093
+ this.cache = new MemoryCache(cacheUtxo);
4094
+ } else {
4095
+ this.cache = void 0;
4096
+ }
4097
+ } else {
4098
+ this.cache = new MemoryCache(DEFAULT_UTXOS_CACHE_TTL);
4099
+ }
4100
4100
  }
4101
4101
  /** @hidden */
4102
4102
  static clearChainAndNodeCaches() {
@@ -4338,9 +4338,8 @@ Supported fuel-core version: ${supportedVersion}.`
4338
4338
  * @returns A promise that resolves to the transaction response object.
4339
4339
  */
4340
4340
  // #region Provider-sendTransaction
4341
- async sendTransaction(transactionRequestLike, { estimateTxDependencies = true, awaitExecution = false } = {}) {
4341
+ async sendTransaction(transactionRequestLike, { estimateTxDependencies = true } = {}) {
4342
4342
  const transactionRequest = transactionRequestify(transactionRequestLike);
4343
- __privateMethod(this, _cacheInputs, cacheInputs_fn).call(this, transactionRequest.inputs);
4344
4343
  if (estimateTxDependencies) {
4345
4344
  await this.estimateTxDependencies(transactionRequest);
4346
4345
  }
@@ -4349,27 +4348,10 @@ Supported fuel-core version: ${supportedVersion}.`
4349
4348
  if (transactionRequest.type === import_transactions20.TransactionType.Script) {
4350
4349
  abis = transactionRequest.abis;
4351
4350
  }
4352
- if (awaitExecution) {
4353
- const subscription = this.operations.submitAndAwait({ encodedTransaction });
4354
- for await (const { submitAndAwait } of subscription) {
4355
- if (submitAndAwait.type === "SqueezedOutStatus") {
4356
- throw new import_errors14.FuelError(
4357
- import_errors14.ErrorCode.TRANSACTION_SQUEEZED_OUT,
4358
- `Transaction Squeezed Out with reason: ${submitAndAwait.reason}`
4359
- );
4360
- }
4361
- if (submitAndAwait.type !== "SubmittedStatus") {
4362
- break;
4363
- }
4364
- }
4365
- const transactionId2 = transactionRequest.getTransactionId(this.getChainId());
4366
- const response = new TransactionResponse(transactionId2, this, abis);
4367
- await response.fetch();
4368
- return response;
4369
- }
4370
4351
  const {
4371
4352
  submit: { id: transactionId }
4372
4353
  } = await this.operations.submit({ encodedTransaction });
4354
+ __privateMethod(this, _cacheInputs, cacheInputs_fn).call(this, transactionRequest.inputs);
4373
4355
  return new TransactionResponse(transactionId, this, abis);
4374
4356
  }
4375
4357
  /**
@@ -4648,6 +4630,8 @@ Supported fuel-core version: ${supportedVersion}.`
4648
4630
  return { receipts: callResult[0].receipts };
4649
4631
  }
4650
4632
  /**
4633
+ * @hidden
4634
+ *
4651
4635
  * Returns a transaction cost to enable user
4652
4636
  * to set gasLimit and also reserve balance amounts
4653
4637
  * on the transaction.
@@ -4657,20 +4641,13 @@ Supported fuel-core version: ${supportedVersion}.`
4657
4641
  *
4658
4642
  * @returns A promise that resolves to the transaction cost object.
4659
4643
  */
4660
- async getTransactionCost(transactionRequestLike, { resourcesOwner, signatureCallback, quantitiesToContract = [] } = {}) {
4644
+ async getTransactionCost(transactionRequestLike, { signatureCallback } = {}) {
4661
4645
  const txRequestClone = (0, import_ramda3.clone)(transactionRequestify(transactionRequestLike));
4662
4646
  const isScriptTransaction = txRequestClone.type === import_transactions20.TransactionType.Script;
4663
- const baseAssetId = this.getBaseAssetId();
4664
4647
  const updateMaxFee = txRequestClone.maxFee.eq(0);
4665
- const coinOutputsQuantities = txRequestClone.getCoinOutputsQuantities();
4666
- const allQuantities = mergeQuantities(coinOutputsQuantities, quantitiesToContract);
4667
- txRequestClone.fundWithFakeUtxos(allQuantities, baseAssetId, resourcesOwner?.address);
4668
4648
  if (isScriptTransaction) {
4669
4649
  txRequestClone.gasLimit = (0, import_math17.bn)(0);
4670
4650
  }
4671
- if (resourcesOwner && "populateTransactionPredicateData" in resourcesOwner) {
4672
- resourcesOwner.populateTransactionPredicateData(txRequestClone);
4673
- }
4674
4651
  const signedRequest = (0, import_ramda3.clone)(txRequestClone);
4675
4652
  let addedSignatures = 0;
4676
4653
  if (signatureCallback && isScriptTransaction) {
@@ -4706,7 +4683,6 @@ Supported fuel-core version: ${supportedVersion}.`
4706
4683
  }));
4707
4684
  }
4708
4685
  return {
4709
- requiredQuantities: allQuantities,
4710
4686
  receipts,
4711
4687
  gasUsed,
4712
4688
  gasPrice,
@@ -4722,34 +4698,6 @@ Supported fuel-core version: ${supportedVersion}.`
4722
4698
  updateMaxFee
4723
4699
  };
4724
4700
  }
4725
- /**
4726
- * Get the required quantities and associated resources for a transaction.
4727
- *
4728
- * @param owner - address to add resources from.
4729
- * @param transactionRequestLike - transaction request to populate resources for.
4730
- * @param quantitiesToContract - quantities for the contract (optional).
4731
- *
4732
- * @returns a promise resolving to the required quantities for the transaction.
4733
- */
4734
- async getResourcesForTransaction(owner, transactionRequestLike, quantitiesToContract = []) {
4735
- const ownerAddress = import_address3.Address.fromAddressOrString(owner);
4736
- const transactionRequest = transactionRequestify((0, import_ramda3.clone)(transactionRequestLike));
4737
- const transactionCost = await this.getTransactionCost(transactionRequest, {
4738
- quantitiesToContract
4739
- });
4740
- transactionRequest.addResources(
4741
- await this.getResourcesToSpend(ownerAddress, transactionCost.requiredQuantities)
4742
- );
4743
- const { requiredQuantities, ...txCost } = await this.getTransactionCost(transactionRequest, {
4744
- quantitiesToContract
4745
- });
4746
- const resources = await this.getResourcesToSpend(ownerAddress, requiredQuantities);
4747
- return {
4748
- resources,
4749
- requiredQuantities,
4750
- ...txCost
4751
- };
4752
- }
4753
4701
  /**
4754
4702
  * Returns coins for the given owner.
4755
4703
  *
@@ -5553,6 +5501,20 @@ var rawAssets = [
5553
5501
  ];
5554
5502
  var assets = resolveIconPaths(rawAssets, fuelAssetsBaseUrl);
5555
5503
 
5504
+ // src/providers/utils/merge-quantities.ts
5505
+ var mergeQuantities = (...coinQuantities) => {
5506
+ const resultMap = {};
5507
+ function addToMap({ amount, assetId }) {
5508
+ if (resultMap[assetId]) {
5509
+ resultMap[assetId] = resultMap[assetId].add(amount);
5510
+ } else {
5511
+ resultMap[assetId] = amount;
5512
+ }
5513
+ }
5514
+ coinQuantities.forEach((arr) => arr.forEach(addToMap));
5515
+ return Object.entries(resultMap).map(([assetId, amount]) => ({ assetId, amount }));
5516
+ };
5517
+
5556
5518
  // src/utils/formatTransferToContractScriptData.ts
5557
5519
  var import_abi_coder6 = require("@fuel-ts/abi-coder");
5558
5520
  var import_math19 = require("@fuel-ts/math");
@@ -5893,9 +5855,8 @@ var Account = class extends import_interfaces.AbstractAccount {
5893
5855
  scriptData
5894
5856
  });
5895
5857
  request.addContractInputAndOutput(contractAddress);
5896
- const txCost = await this.provider.getTransactionCost(request, {
5897
- resourcesOwner: this,
5898
- quantitiesToContract: [{ amount: (0, import_math20.bn)(amount), assetId: String(assetIdToTransfer) }]
5858
+ const txCost = await this.getTransactionCost(request, {
5859
+ quantities: [{ amount: (0, import_math20.bn)(amount), assetId: String(assetIdToTransfer) }]
5899
5860
  });
5900
5861
  request = this.validateGasLimitAndMaxFee({
5901
5862
  transactionRequest: request,
@@ -5930,8 +5891,8 @@ var Account = class extends import_interfaces.AbstractAccount {
5930
5891
  const params = { script, ...txParams };
5931
5892
  const baseAssetId = this.provider.getBaseAssetId();
5932
5893
  let request = new ScriptTransactionRequest(params);
5933
- const quantitiesToContract = [{ amount: (0, import_math20.bn)(amount), assetId: baseAssetId }];
5934
- const txCost = await this.provider.getTransactionCost(request, { quantitiesToContract });
5894
+ const quantities = [{ amount: (0, import_math20.bn)(amount), assetId: baseAssetId }];
5895
+ const txCost = await this.getTransactionCost(request, { quantities });
5935
5896
  request = this.validateGasLimitAndMaxFee({
5936
5897
  transactionRequest: request,
5937
5898
  gasUsed: txCost.gasUsed,
@@ -5941,6 +5902,34 @@ var Account = class extends import_interfaces.AbstractAccount {
5941
5902
  await this.fund(request, txCost);
5942
5903
  return this.sendTransaction(request);
5943
5904
  }
5905
+ /**
5906
+ * Returns a transaction cost to enable user
5907
+ * to set gasLimit and also reserve balance amounts
5908
+ * on the transaction.
5909
+ *
5910
+ * @param transactionRequestLike - The transaction request object.
5911
+ * @param transactionCostParams - The transaction cost parameters (optional).
5912
+ *
5913
+ * @returns A promise that resolves to the transaction cost object.
5914
+ */
5915
+ async getTransactionCost(transactionRequestLike, { signatureCallback, quantities = [] } = {}) {
5916
+ const txRequestClone = (0, import_ramda4.clone)(transactionRequestify(transactionRequestLike));
5917
+ const baseAssetId = this.provider.getBaseAssetId();
5918
+ const coinOutputsQuantities = txRequestClone.getCoinOutputsQuantities();
5919
+ const requiredQuantities = mergeQuantities(coinOutputsQuantities, quantities);
5920
+ const transactionFeeForDryRun = [{ assetId: baseAssetId, amount: (0, import_math20.bn)("100000000000000000") }];
5921
+ const resources = this.generateFakeResources(
5922
+ mergeQuantities(requiredQuantities, transactionFeeForDryRun)
5923
+ );
5924
+ txRequestClone.addResources(resources);
5925
+ const txCost = await this.provider.getTransactionCost(txRequestClone, {
5926
+ signatureCallback
5927
+ });
5928
+ return {
5929
+ ...txCost,
5930
+ requiredQuantities
5931
+ };
5932
+ }
5944
5933
  /**
5945
5934
  * Sign a message from the account via the connector.
5946
5935
  *
@@ -5977,7 +5966,7 @@ var Account = class extends import_interfaces.AbstractAccount {
5977
5966
  * @param sendTransactionParams - The provider send transaction parameters (optional).
5978
5967
  * @returns A promise that resolves to the transaction response.
5979
5968
  */
5980
- async sendTransaction(transactionRequestLike, { estimateTxDependencies = true, awaitExecution } = {}) {
5969
+ async sendTransaction(transactionRequestLike, { estimateTxDependencies = true } = {}) {
5981
5970
  if (this._connector) {
5982
5971
  return this.provider.getTransactionResponse(
5983
5972
  await this._connector.sendTransaction(this.address.toString(), transactionRequestLike)
@@ -5988,7 +5977,6 @@ var Account = class extends import_interfaces.AbstractAccount {
5988
5977
  await this.provider.estimateTxDependencies(transactionRequest);
5989
5978
  }
5990
5979
  return this.provider.sendTransaction(transactionRequest, {
5991
- awaitExecution,
5992
5980
  estimateTxDependencies: false
5993
5981
  });
5994
5982
  }
@@ -6033,9 +6021,7 @@ var Account = class extends import_interfaces.AbstractAccount {
6033
6021
  /** @hidden * */
6034
6022
  async estimateAndFundTransaction(transactionRequest, txParams) {
6035
6023
  let request = transactionRequest;
6036
- const txCost = await this.provider.getTransactionCost(request, {
6037
- resourcesOwner: this
6038
- });
6024
+ const txCost = await this.getTransactionCost(request);
6039
6025
  request = this.validateGasLimitAndMaxFee({
6040
6026
  transactionRequest: request,
6041
6027
  gasUsed: txCost.gasUsed,
@@ -6350,17 +6336,16 @@ var BaseWalletUnlocked = class extends Account {
6350
6336
  *
6351
6337
  * @param transactionRequestLike - The transaction request to send.
6352
6338
  * @param estimateTxDependencies - Whether to estimate the transaction dependencies.
6353
- * @param awaitExecution - Whether to wait for the transaction to be executed.
6354
6339
  * @returns A promise that resolves to the TransactionResponse object.
6355
6340
  */
6356
- async sendTransaction(transactionRequestLike, { estimateTxDependencies = false, awaitExecution } = {}) {
6341
+ async sendTransaction(transactionRequestLike, { estimateTxDependencies = false } = {}) {
6357
6342
  const transactionRequest = transactionRequestify(transactionRequestLike);
6358
6343
  if (estimateTxDependencies) {
6359
6344
  await this.provider.estimateTxDependencies(transactionRequest);
6360
6345
  }
6361
6346
  return this.provider.sendTransaction(
6362
6347
  await this.populateTransactionWitnessesSignature(transactionRequest),
6363
- { awaitExecution, estimateTxDependencies: false }
6348
+ { estimateTxDependencies: false }
6364
6349
  );
6365
6350
  }
6366
6351
  /**
@@ -10424,6 +10409,7 @@ __publicField(Fuel, "defaultConfig", {});
10424
10409
  ChainName,
10425
10410
  ChangeOutputCollisionError,
10426
10411
  CreateTransactionRequest,
10412
+ DEFAULT_UTXOS_CACHE_TTL,
10427
10413
  Fuel,
10428
10414
  FuelConnector,
10429
10415
  FuelConnectorEventType,