@fuel-ts/account 0.92.0 → 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.
@@ -29,7 +29,7 @@ import { randomBytes as randomBytes2 } from "@fuel-ts/crypto";
29
29
  import { ErrorCode as ErrorCode15, FuelError as FuelError15 } from "@fuel-ts/errors";
30
30
  import { AbstractAccount } from "@fuel-ts/interfaces";
31
31
  import { bn as bn19 } from "@fuel-ts/math";
32
- import { arrayify as arrayify14, hexlify as hexlify13, isDefined as isDefined2 } from "@fuel-ts/utils";
32
+ import { arrayify as arrayify14, hexlify as hexlify13, isDefined as isDefined3 } from "@fuel-ts/utils";
33
33
  import { clone as clone4 } from "ramda";
34
34
 
35
35
  // src/providers/coin-quantity.ts
@@ -77,7 +77,7 @@ import {
77
77
  InputMessageCoder,
78
78
  TransactionCoder as TransactionCoder5
79
79
  } from "@fuel-ts/transactions";
80
- import { arrayify as arrayify11, hexlify as hexlify12, DateTime as DateTime2 } from "@fuel-ts/utils";
80
+ import { arrayify as arrayify11, hexlify as hexlify12, DateTime as DateTime2, isDefined as isDefined2 } from "@fuel-ts/utils";
81
81
  import { checkFuelCoreVersionCompatibility } from "@fuel-ts/versions";
82
82
  import { equalBytes } from "@noble/curves/abstract/utils";
83
83
  import { GraphQLClient } from "graphql-request";
@@ -2741,7 +2741,10 @@ var transactionRequestify = (obj) => {
2741
2741
  return CreateTransactionRequest.from(obj);
2742
2742
  }
2743
2743
  default: {
2744
- throw new FuelError8(ErrorCode8.INVALID_TRANSACTION_TYPE, `Invalid transaction type: ${type}.`);
2744
+ throw new FuelError8(
2745
+ ErrorCode8.UNSUPPORTED_TRANSACTION_TYPE,
2746
+ `Unsupported transaction type: ${type}.`
2747
+ );
2745
2748
  }
2746
2749
  }
2747
2750
  };
@@ -2954,8 +2957,8 @@ function getTransactionTypeName(transactionType) {
2954
2957
  return "Script" /* Script */;
2955
2958
  default:
2956
2959
  throw new FuelError10(
2957
- ErrorCode10.INVALID_TRANSACTION_TYPE,
2958
- `Invalid transaction type: ${transactionType}.`
2960
+ ErrorCode10.UNSUPPORTED_TRANSACTION_TYPE,
2961
+ `Unsupported transaction type: ${transactionType}.`
2959
2962
  );
2960
2963
  }
2961
2964
  }
@@ -3703,24 +3706,11 @@ function autoRetryFetch(fetchFn, options, retryAttemptNum = 0) {
3703
3706
  };
3704
3707
  }
3705
3708
 
3706
- // src/providers/utils/merge-quantities.ts
3707
- var mergeQuantities = (...coinQuantities) => {
3708
- const resultMap = {};
3709
- function addToMap({ amount, assetId }) {
3710
- if (resultMap[assetId]) {
3711
- resultMap[assetId] = resultMap[assetId].add(amount);
3712
- } else {
3713
- resultMap[assetId] = amount;
3714
- }
3715
- }
3716
- coinQuantities.forEach((arr) => arr.forEach(addToMap));
3717
- return Object.entries(resultMap).map(([assetId, amount]) => ({ assetId, amount }));
3718
- };
3719
-
3720
3709
  // src/providers/provider.ts
3721
3710
  var MAX_RETRIES = 10;
3722
3711
  var RESOURCES_PAGE_SIZE_LIMIT = 512;
3723
3712
  var BLOCKS_PAGE_SIZE_LIMIT = 5;
3713
+ var DEFAULT_UTXOS_CACHE_TTL = 2e4;
3724
3714
  var processGqlChain = (chain) => {
3725
3715
  const { name, daHeight, consensusParameters, latestBlock } = chain;
3726
3716
  const {
@@ -3810,7 +3800,16 @@ var _Provider = class {
3810
3800
  this.options = { ...this.options, ...options };
3811
3801
  this.url = url;
3812
3802
  this.operations = this.createOperations();
3813
- this.cache = options.cacheUtxo ? new MemoryCache(options.cacheUtxo) : void 0;
3803
+ const { cacheUtxo } = this.options;
3804
+ if (isDefined2(cacheUtxo)) {
3805
+ if (cacheUtxo !== -1) {
3806
+ this.cache = new MemoryCache(cacheUtxo);
3807
+ } else {
3808
+ this.cache = void 0;
3809
+ }
3810
+ } else {
3811
+ this.cache = new MemoryCache(DEFAULT_UTXOS_CACHE_TTL);
3812
+ }
3814
3813
  }
3815
3814
  /** @hidden */
3816
3815
  static clearChainAndNodeCaches() {
@@ -4052,9 +4051,8 @@ Supported fuel-core version: ${supportedVersion}.`
4052
4051
  * @returns A promise that resolves to the transaction response object.
4053
4052
  */
4054
4053
  // #region Provider-sendTransaction
4055
- async sendTransaction(transactionRequestLike, { estimateTxDependencies = true, awaitExecution = false } = {}) {
4054
+ async sendTransaction(transactionRequestLike, { estimateTxDependencies = true } = {}) {
4056
4055
  const transactionRequest = transactionRequestify(transactionRequestLike);
4057
- __privateMethod(this, _cacheInputs, cacheInputs_fn).call(this, transactionRequest.inputs);
4058
4056
  if (estimateTxDependencies) {
4059
4057
  await this.estimateTxDependencies(transactionRequest);
4060
4058
  }
@@ -4063,27 +4061,10 @@ Supported fuel-core version: ${supportedVersion}.`
4063
4061
  if (transactionRequest.type === TransactionType8.Script) {
4064
4062
  abis = transactionRequest.abis;
4065
4063
  }
4066
- if (awaitExecution) {
4067
- const subscription = this.operations.submitAndAwait({ encodedTransaction });
4068
- for await (const { submitAndAwait } of subscription) {
4069
- if (submitAndAwait.type === "SqueezedOutStatus") {
4070
- throw new FuelError13(
4071
- ErrorCode13.TRANSACTION_SQUEEZED_OUT,
4072
- `Transaction Squeezed Out with reason: ${submitAndAwait.reason}`
4073
- );
4074
- }
4075
- if (submitAndAwait.type !== "SubmittedStatus") {
4076
- break;
4077
- }
4078
- }
4079
- const transactionId2 = transactionRequest.getTransactionId(this.getChainId());
4080
- const response = new TransactionResponse(transactionId2, this, abis);
4081
- await response.fetch();
4082
- return response;
4083
- }
4084
4064
  const {
4085
4065
  submit: { id: transactionId }
4086
4066
  } = await this.operations.submit({ encodedTransaction });
4067
+ __privateMethod(this, _cacheInputs, cacheInputs_fn).call(this, transactionRequest.inputs);
4087
4068
  return new TransactionResponse(transactionId, this, abis);
4088
4069
  }
4089
4070
  /**
@@ -4362,6 +4343,8 @@ Supported fuel-core version: ${supportedVersion}.`
4362
4343
  return { receipts: callResult[0].receipts };
4363
4344
  }
4364
4345
  /**
4346
+ * @hidden
4347
+ *
4365
4348
  * Returns a transaction cost to enable user
4366
4349
  * to set gasLimit and also reserve balance amounts
4367
4350
  * on the transaction.
@@ -4371,20 +4354,13 @@ Supported fuel-core version: ${supportedVersion}.`
4371
4354
  *
4372
4355
  * @returns A promise that resolves to the transaction cost object.
4373
4356
  */
4374
- async getTransactionCost(transactionRequestLike, { resourcesOwner, signatureCallback, quantitiesToContract = [] } = {}) {
4357
+ async getTransactionCost(transactionRequestLike, { signatureCallback } = {}) {
4375
4358
  const txRequestClone = clone3(transactionRequestify(transactionRequestLike));
4376
4359
  const isScriptTransaction = txRequestClone.type === TransactionType8.Script;
4377
- const baseAssetId = this.getBaseAssetId();
4378
4360
  const updateMaxFee = txRequestClone.maxFee.eq(0);
4379
- const coinOutputsQuantities = txRequestClone.getCoinOutputsQuantities();
4380
- const allQuantities = mergeQuantities(coinOutputsQuantities, quantitiesToContract);
4381
- txRequestClone.fundWithFakeUtxos(allQuantities, baseAssetId, resourcesOwner?.address);
4382
4361
  if (isScriptTransaction) {
4383
4362
  txRequestClone.gasLimit = bn17(0);
4384
4363
  }
4385
- if (resourcesOwner && "populateTransactionPredicateData" in resourcesOwner) {
4386
- resourcesOwner.populateTransactionPredicateData(txRequestClone);
4387
- }
4388
4364
  const signedRequest = clone3(txRequestClone);
4389
4365
  let addedSignatures = 0;
4390
4366
  if (signatureCallback && isScriptTransaction) {
@@ -4420,7 +4396,6 @@ Supported fuel-core version: ${supportedVersion}.`
4420
4396
  }));
4421
4397
  }
4422
4398
  return {
4423
- requiredQuantities: allQuantities,
4424
4399
  receipts,
4425
4400
  gasUsed,
4426
4401
  gasPrice,
@@ -4436,34 +4411,6 @@ Supported fuel-core version: ${supportedVersion}.`
4436
4411
  updateMaxFee
4437
4412
  };
4438
4413
  }
4439
- /**
4440
- * Get the required quantities and associated resources for a transaction.
4441
- *
4442
- * @param owner - address to add resources from.
4443
- * @param transactionRequestLike - transaction request to populate resources for.
4444
- * @param quantitiesToContract - quantities for the contract (optional).
4445
- *
4446
- * @returns a promise resolving to the required quantities for the transaction.
4447
- */
4448
- async getResourcesForTransaction(owner, transactionRequestLike, quantitiesToContract = []) {
4449
- const ownerAddress = Address2.fromAddressOrString(owner);
4450
- const transactionRequest = transactionRequestify(clone3(transactionRequestLike));
4451
- const transactionCost = await this.getTransactionCost(transactionRequest, {
4452
- quantitiesToContract
4453
- });
4454
- transactionRequest.addResources(
4455
- await this.getResourcesToSpend(ownerAddress, transactionCost.requiredQuantities)
4456
- );
4457
- const { requiredQuantities, ...txCost } = await this.getTransactionCost(transactionRequest, {
4458
- quantitiesToContract
4459
- });
4460
- const resources = await this.getResourcesToSpend(ownerAddress, requiredQuantities);
4461
- return {
4462
- resources,
4463
- requiredQuantities,
4464
- ...txCost
4465
- };
4466
- }
4467
4414
  /**
4468
4415
  * Returns coins for the given owner.
4469
4416
  *
@@ -5092,6 +5039,20 @@ var rawAssets = [
5092
5039
  ];
5093
5040
  var assets = resolveIconPaths(rawAssets, fuelAssetsBaseUrl);
5094
5041
 
5042
+ // src/providers/utils/merge-quantities.ts
5043
+ var mergeQuantities = (...coinQuantities) => {
5044
+ const resultMap = {};
5045
+ function addToMap({ amount, assetId }) {
5046
+ if (resultMap[assetId]) {
5047
+ resultMap[assetId] = resultMap[assetId].add(amount);
5048
+ } else {
5049
+ resultMap[assetId] = amount;
5050
+ }
5051
+ }
5052
+ coinQuantities.forEach((arr) => arr.forEach(addToMap));
5053
+ return Object.entries(resultMap).map(([assetId, amount]) => ({ assetId, amount }));
5054
+ };
5055
+
5095
5056
  // src/utils/formatTransferToContractScriptData.ts
5096
5057
  import { BigNumberCoder as BigNumberCoder2 } from "@fuel-ts/abi-coder";
5097
5058
  import { BN as BN2 } from "@fuel-ts/math";
@@ -5432,9 +5393,8 @@ var Account = class extends AbstractAccount {
5432
5393
  scriptData
5433
5394
  });
5434
5395
  request.addContractInputAndOutput(contractAddress);
5435
- const txCost = await this.provider.getTransactionCost(request, {
5436
- resourcesOwner: this,
5437
- quantitiesToContract: [{ amount: bn19(amount), assetId: String(assetIdToTransfer) }]
5396
+ const txCost = await this.getTransactionCost(request, {
5397
+ quantities: [{ amount: bn19(amount), assetId: String(assetIdToTransfer) }]
5438
5398
  });
5439
5399
  request = this.validateGasLimitAndMaxFee({
5440
5400
  transactionRequest: request,
@@ -5469,8 +5429,8 @@ var Account = class extends AbstractAccount {
5469
5429
  const params = { script, ...txParams };
5470
5430
  const baseAssetId = this.provider.getBaseAssetId();
5471
5431
  let request = new ScriptTransactionRequest(params);
5472
- const quantitiesToContract = [{ amount: bn19(amount), assetId: baseAssetId }];
5473
- const txCost = await this.provider.getTransactionCost(request, { quantitiesToContract });
5432
+ const quantities = [{ amount: bn19(amount), assetId: baseAssetId }];
5433
+ const txCost = await this.getTransactionCost(request, { quantities });
5474
5434
  request = this.validateGasLimitAndMaxFee({
5475
5435
  transactionRequest: request,
5476
5436
  gasUsed: txCost.gasUsed,
@@ -5480,6 +5440,34 @@ var Account = class extends AbstractAccount {
5480
5440
  await this.fund(request, txCost);
5481
5441
  return this.sendTransaction(request);
5482
5442
  }
5443
+ /**
5444
+ * Returns a transaction cost to enable user
5445
+ * to set gasLimit and also reserve balance amounts
5446
+ * on the transaction.
5447
+ *
5448
+ * @param transactionRequestLike - The transaction request object.
5449
+ * @param transactionCostParams - The transaction cost parameters (optional).
5450
+ *
5451
+ * @returns A promise that resolves to the transaction cost object.
5452
+ */
5453
+ async getTransactionCost(transactionRequestLike, { signatureCallback, quantities = [] } = {}) {
5454
+ const txRequestClone = clone4(transactionRequestify(transactionRequestLike));
5455
+ const baseAssetId = this.provider.getBaseAssetId();
5456
+ const coinOutputsQuantities = txRequestClone.getCoinOutputsQuantities();
5457
+ const requiredQuantities = mergeQuantities(coinOutputsQuantities, quantities);
5458
+ const transactionFeeForDryRun = [{ assetId: baseAssetId, amount: bn19("100000000000000000") }];
5459
+ const resources = this.generateFakeResources(
5460
+ mergeQuantities(requiredQuantities, transactionFeeForDryRun)
5461
+ );
5462
+ txRequestClone.addResources(resources);
5463
+ const txCost = await this.provider.getTransactionCost(txRequestClone, {
5464
+ signatureCallback
5465
+ });
5466
+ return {
5467
+ ...txCost,
5468
+ requiredQuantities
5469
+ };
5470
+ }
5483
5471
  /**
5484
5472
  * Sign a message from the account via the connector.
5485
5473
  *
@@ -5516,7 +5504,7 @@ var Account = class extends AbstractAccount {
5516
5504
  * @param sendTransactionParams - The provider send transaction parameters (optional).
5517
5505
  * @returns A promise that resolves to the transaction response.
5518
5506
  */
5519
- async sendTransaction(transactionRequestLike, { estimateTxDependencies = true, awaitExecution } = {}) {
5507
+ async sendTransaction(transactionRequestLike, { estimateTxDependencies = true } = {}) {
5520
5508
  if (this._connector) {
5521
5509
  return this.provider.getTransactionResponse(
5522
5510
  await this._connector.sendTransaction(this.address.toString(), transactionRequestLike)
@@ -5527,7 +5515,6 @@ var Account = class extends AbstractAccount {
5527
5515
  await this.provider.estimateTxDependencies(transactionRequest);
5528
5516
  }
5529
5517
  return this.provider.sendTransaction(transactionRequest, {
5530
- awaitExecution,
5531
5518
  estimateTxDependencies: false
5532
5519
  });
5533
5520
  }
@@ -5572,9 +5559,7 @@ var Account = class extends AbstractAccount {
5572
5559
  /** @hidden * */
5573
5560
  async estimateAndFundTransaction(transactionRequest, txParams) {
5574
5561
  let request = transactionRequest;
5575
- const txCost = await this.provider.getTransactionCost(request, {
5576
- resourcesOwner: this
5577
- });
5562
+ const txCost = await this.getTransactionCost(request);
5578
5563
  request = this.validateGasLimitAndMaxFee({
5579
5564
  transactionRequest: request,
5580
5565
  gasUsed: txCost.gasUsed,
@@ -5592,7 +5577,7 @@ var Account = class extends AbstractAccount {
5592
5577
  txParams: { gasLimit: setGasLimit, maxFee: setMaxFee }
5593
5578
  }) {
5594
5579
  const request = transactionRequestify(transactionRequest);
5595
- if (!isDefined2(setGasLimit)) {
5580
+ if (!isDefined3(setGasLimit)) {
5596
5581
  request.gasLimit = gasUsed;
5597
5582
  } else if (gasUsed.gt(setGasLimit)) {
5598
5583
  throw new FuelError15(
@@ -5600,7 +5585,7 @@ var Account = class extends AbstractAccount {
5600
5585
  `Gas limit '${setGasLimit}' is lower than the required: '${gasUsed}'.`
5601
5586
  );
5602
5587
  }
5603
- if (!isDefined2(setMaxFee)) {
5588
+ if (!isDefined3(setMaxFee)) {
5604
5589
  request.maxFee = maxFee;
5605
5590
  } else if (maxFee.gt(setMaxFee)) {
5606
5591
  throw new FuelError15(
@@ -5893,17 +5878,16 @@ var BaseWalletUnlocked = class extends Account {
5893
5878
  *
5894
5879
  * @param transactionRequestLike - The transaction request to send.
5895
5880
  * @param estimateTxDependencies - Whether to estimate the transaction dependencies.
5896
- * @param awaitExecution - Whether to wait for the transaction to be executed.
5897
5881
  * @returns A promise that resolves to the TransactionResponse object.
5898
5882
  */
5899
- async sendTransaction(transactionRequestLike, { estimateTxDependencies = false, awaitExecution } = {}) {
5883
+ async sendTransaction(transactionRequestLike, { estimateTxDependencies = false } = {}) {
5900
5884
  const transactionRequest = transactionRequestify(transactionRequestLike);
5901
5885
  if (estimateTxDependencies) {
5902
5886
  await this.provider.estimateTxDependencies(transactionRequest);
5903
5887
  }
5904
5888
  return this.provider.sendTransaction(
5905
5889
  await this.populateTransactionWitnessesSignature(transactionRequest),
5906
- { awaitExecution, estimateTxDependencies: false }
5890
+ { estimateTxDependencies: false }
5907
5891
  );
5908
5892
  }
5909
5893
  /**
@@ -8654,11 +8638,12 @@ var seedTestWallet = async (wallet, quantities, utxosAmount = 1) => {
8654
8638
  }
8655
8639
  })
8656
8640
  );
8657
- const txCost = await genesisWallet.provider.getTransactionCost(request);
8641
+ const txCost = await genesisWallet.getTransactionCost(request);
8658
8642
  request.gasLimit = txCost.gasUsed;
8659
8643
  request.maxFee = txCost.maxFee;
8660
8644
  await genesisWallet.fund(request, txCost);
8661
- await genesisWallet.sendTransaction(request, { awaitExecution: true });
8645
+ const submit = await genesisWallet.sendTransaction(request);
8646
+ await submit.waitForResult();
8662
8647
  };
8663
8648
 
8664
8649
  // src/test-utils/generateTestWallet.ts
@@ -8746,7 +8731,9 @@ var launchNode = async ({
8746
8731
  "--snapshot",
8747
8732
  "--consensus-key",
8748
8733
  "--db-type",
8749
- "--poa-instant"
8734
+ "--poa-instant",
8735
+ "--min-gas-price",
8736
+ "--native-executor-version"
8750
8737
  ]);
8751
8738
  const snapshotDir = getFlagValueFromArgs(args, "--snapshot");
8752
8739
  const consensusKey = getFlagValueFromArgs(args, "--consensus-key") || defaultConsensusKey;
@@ -8755,6 +8742,7 @@ var launchNode = async ({
8755
8742
  const poaInstantFlagValue = getFlagValueFromArgs(args, "--poa-instant");
8756
8743
  const poaInstant = poaInstantFlagValue === "true" || poaInstantFlagValue === void 0;
8757
8744
  const nativeExecutorVersion = getFlagValueFromArgs(args, "--native-executor-version") || "0";
8745
+ const minGasPrice = getFlagValueFromArgs(args, "--min-gas-price") || "1";
8758
8746
  const graphQLStartSubstring = "Binding GraphQL provider to";
8759
8747
  const command = fuelCorePath || "fuel-core";
8760
8748
  const ipToUse = ip || "0.0.0.0";
@@ -8793,7 +8781,7 @@ var launchNode = async ({
8793
8781
  ["--ip", ipToUse],
8794
8782
  ["--port", portToUse],
8795
8783
  useInMemoryDb ? ["--db-type", "in-memory"] : ["--db-path", tempDir],
8796
- ["--min-gas-price", "1"],
8784
+ ["--min-gas-price", minGasPrice],
8797
8785
  poaInstant ? ["--poa-instant", "true"] : [],
8798
8786
  ["--native-executor-version", nativeExecutorVersion],
8799
8787
  ["--consensus-key", consensusKey],
@@ -8884,10 +8872,11 @@ var generateWallets = async (count, provider) => {
8884
8872
  };
8885
8873
  var launchNodeAndGetWallets = async ({
8886
8874
  launchNodeOptions,
8875
+ providerOptions,
8887
8876
  walletCount = 10
8888
8877
  } = {}) => {
8889
8878
  const { cleanup: closeNode, ip, port } = await launchNode(launchNodeOptions || {});
8890
- const provider = await Provider.create(`http://${ip}:${port}/v1/graphql`);
8879
+ const provider = await Provider.create(`http://${ip}:${port}/v1/graphql`, providerOptions);
8891
8880
  const wallets = await generateWallets(walletCount, provider);
8892
8881
  const cleanup = () => {
8893
8882
  closeNode();