@fuel-ts/account 0.0.0-rc-1832-20240403171523 → 0.0.0-rc-1976-20240403225009

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.

Potentially problematic release.


This version of @fuel-ts/account might be problematic. Click here for more details.

package/dist/index.mjs CHANGED
@@ -29,14 +29,12 @@ var __privateMethod = (obj, member, method) => {
29
29
 
30
30
  // src/account.ts
31
31
  import { Address as Address3 } from "@fuel-ts/address";
32
- import { BaseAssetId as BaseAssetId3 } from "@fuel-ts/address/configs";
33
32
  import { ErrorCode as ErrorCode14, FuelError as FuelError14 } from "@fuel-ts/errors";
34
33
  import { AbstractAccount } from "@fuel-ts/interfaces";
35
34
  import { bn as bn16 } from "@fuel-ts/math";
36
35
  import { arrayify as arrayify14 } from "@fuel-ts/utils";
37
36
 
38
37
  // src/providers/coin-quantity.ts
39
- import { BaseAssetId } from "@fuel-ts/address/configs";
40
38
  import { bn } from "@fuel-ts/math";
41
39
  import { hexlify } from "@fuel-ts/utils";
42
40
  var coinQuantityfy = (coinQuantityLike) => {
@@ -45,11 +43,11 @@ var coinQuantityfy = (coinQuantityLike) => {
45
43
  let max2;
46
44
  if (Array.isArray(coinQuantityLike)) {
47
45
  amount = coinQuantityLike[0];
48
- assetId = coinQuantityLike[1] ?? BaseAssetId;
49
- max2 = coinQuantityLike[2] ?? void 0;
46
+ assetId = coinQuantityLike[1];
47
+ max2 = coinQuantityLike[2];
50
48
  } else {
51
49
  amount = coinQuantityLike.amount;
52
- assetId = coinQuantityLike.assetId ?? BaseAssetId;
50
+ assetId = coinQuantityLike.assetId;
53
51
  max2 = coinQuantityLike.max ?? void 0;
54
52
  }
55
53
  const bnAmount = bn(amount);
@@ -1156,7 +1154,7 @@ var outputify = (value) => {
1156
1154
 
1157
1155
  // src/providers/transaction-request/transaction-request.ts
1158
1156
  import { Address, addressify } from "@fuel-ts/address";
1159
- import { BaseAssetId as BaseAssetId2, ZeroBytes32 as ZeroBytes324 } from "@fuel-ts/address/configs";
1157
+ import { ZeroBytes32 as ZeroBytes324 } from "@fuel-ts/address/configs";
1160
1158
  import { bn as bn6 } from "@fuel-ts/math";
1161
1159
  import {
1162
1160
  PolicyType,
@@ -1610,6 +1608,8 @@ var BaseTransactionRequest = class {
1610
1608
  outputs = [];
1611
1609
  /** List of witnesses */
1612
1610
  witnesses = [];
1611
+ /** Base asset ID - should be fetched from the chain */
1612
+ baseAssetId = ZeroBytes324;
1613
1613
  /**
1614
1614
  * Constructor for initializing a base transaction request.
1615
1615
  *
@@ -1622,7 +1622,8 @@ var BaseTransactionRequest = class {
1622
1622
  witnessLimit,
1623
1623
  inputs,
1624
1624
  outputs,
1625
- witnesses
1625
+ witnesses,
1626
+ baseAssetId
1626
1627
  } = {}) {
1627
1628
  this.gasPrice = bn6(gasPrice);
1628
1629
  this.maturity = maturity ?? 0;
@@ -1631,6 +1632,7 @@ var BaseTransactionRequest = class {
1631
1632
  this.inputs = inputs ?? [];
1632
1633
  this.outputs = outputs ?? [];
1633
1634
  this.witnesses = witnesses ?? [];
1635
+ this.baseAssetId = baseAssetId ?? ZeroBytes324;
1634
1636
  }
1635
1637
  static getPolicyMeta(req) {
1636
1638
  let policyTypes = 0;
@@ -1855,11 +1857,9 @@ var BaseTransactionRequest = class {
1855
1857
  *
1856
1858
  * @param message - Message resource.
1857
1859
  * @param predicate - Predicate bytes.
1858
- * @param predicateData - Predicate data bytes.
1859
1860
  */
1860
1861
  addMessageInput(message, predicate) {
1861
1862
  const { recipient, sender, amount } = message;
1862
- const assetId = BaseAssetId2;
1863
1863
  let witnessIndex;
1864
1864
  if (predicate) {
1865
1865
  witnessIndex = 0;
@@ -1880,7 +1880,7 @@ var BaseTransactionRequest = class {
1880
1880
  predicateData: predicate?.predicateDataBytes
1881
1881
  };
1882
1882
  this.pushInput(input);
1883
- this.addChangeOutput(recipient, assetId);
1883
+ this.addChangeOutput(recipient, this.baseAssetId);
1884
1884
  }
1885
1885
  /**
1886
1886
  * Adds a single resource to the transaction by adding a coin/message input and a
@@ -1941,12 +1941,12 @@ var BaseTransactionRequest = class {
1941
1941
  * @param amount - Amount of coin.
1942
1942
  * @param assetId - Asset ID of coin.
1943
1943
  */
1944
- addCoinOutput(to, amount, assetId = BaseAssetId2) {
1944
+ addCoinOutput(to, amount, assetId) {
1945
1945
  this.pushOutput({
1946
1946
  type: OutputType2.Coin,
1947
1947
  to: addressify(to).toB256(),
1948
1948
  amount,
1949
- assetId
1949
+ assetId: assetId ?? this.baseAssetId
1950
1950
  });
1951
1951
  return this;
1952
1952
  }
@@ -1973,7 +1973,7 @@ var BaseTransactionRequest = class {
1973
1973
  * @param to - Address of the owner.
1974
1974
  * @param assetId - Asset ID of coin.
1975
1975
  */
1976
- addChangeOutput(to, assetId = BaseAssetId2) {
1976
+ addChangeOutput(to, assetId) {
1977
1977
  const changeOutput = this.getChangeOutputs().find(
1978
1978
  (output) => hexlify7(output.assetId) === assetId
1979
1979
  );
@@ -1981,7 +1981,7 @@ var BaseTransactionRequest = class {
1981
1981
  this.pushOutput({
1982
1982
  type: OutputType2.Change,
1983
1983
  to: addressify(to).toB256(),
1984
- assetId
1984
+ assetId: assetId ?? this.baseAssetId
1985
1985
  });
1986
1986
  }
1987
1987
  }
@@ -2063,7 +2063,7 @@ var BaseTransactionRequest = class {
2063
2063
  ]);
2064
2064
  }
2065
2065
  };
2066
- updateAssetInput(BaseAssetId2, bn6(1e11));
2066
+ updateAssetInput(this.baseAssetId, bn6(1e11));
2067
2067
  quantities.forEach((q) => updateAssetInput(q.assetId, q.amount));
2068
2068
  }
2069
2069
  /**
@@ -3532,6 +3532,7 @@ var processGqlChain = (chain) => {
3532
3532
  gasPerByte: bn14(feeParams.gasPerByte),
3533
3533
  maxMessageDataLength: bn14(predicateParams.maxMessageDataLength),
3534
3534
  chainId: bn14(consensusParameters.chainId),
3535
+ baseAssetId: consensusParameters.baseAssetId,
3535
3536
  gasCosts
3536
3537
  },
3537
3538
  gasCosts,
@@ -3774,6 +3775,17 @@ var _Provider = class {
3774
3775
  } = this.getChain();
3775
3776
  return chainId.toNumber();
3776
3777
  }
3778
+ /**
3779
+ * Returns the base asset ID
3780
+ *
3781
+ * @returns A promise that resolves to the base asset ID
3782
+ */
3783
+ getBaseAssetId() {
3784
+ const {
3785
+ consensusParameters: { baseAssetId }
3786
+ } = this.getChain();
3787
+ return baseAssetId;
3788
+ }
3777
3789
  /**
3778
3790
  * Submits a transaction to the chain to be executed.
3779
3791
  *
@@ -4810,8 +4822,9 @@ var Account = class extends AbstractAccount {
4810
4822
  * @param assetId - The asset ID to check the balance for.
4811
4823
  * @returns A promise that resolves to the balance amount.
4812
4824
  */
4813
- async getBalance(assetId = BaseAssetId3) {
4814
- const amount = await this.provider.getBalance(this.address, assetId);
4825
+ async getBalance(assetId) {
4826
+ const assetIdToFetch = assetId ?? this.provider.getBaseAssetId();
4827
+ const amount = await this.provider.getBalance(this.address, assetIdToFetch);
4815
4828
  return amount;
4816
4829
  }
4817
4830
  /**
@@ -4849,9 +4862,10 @@ var Account = class extends AbstractAccount {
4849
4862
  * @returns A promise that resolves when the resources are added to the transaction.
4850
4863
  */
4851
4864
  async fund(request, coinQuantities, fee) {
4865
+ const baseAssetId = this.provider.getBaseAssetId();
4852
4866
  const updatedQuantities = addAmountToAsset({
4853
4867
  amount: bn16(fee),
4854
- assetId: BaseAssetId3,
4868
+ assetId: baseAssetId,
4855
4869
  coinQuantities
4856
4870
  });
4857
4871
  const quantitiesDict = {};
@@ -4875,8 +4889,8 @@ var Account = class extends AbstractAccount {
4875
4889
  quantitiesDict[assetId].owned = quantitiesDict[assetId].owned.add(amount);
4876
4890
  cachedUtxos.push(input.id);
4877
4891
  }
4878
- } else if (input.recipient === owner && input.amount && quantitiesDict[BaseAssetId3]) {
4879
- quantitiesDict[BaseAssetId3].owned = quantitiesDict[BaseAssetId3].owned.add(input.amount);
4892
+ } else if (input.recipient === owner && input.amount && quantitiesDict[baseAssetId]) {
4893
+ quantitiesDict[baseAssetId].owned = quantitiesDict[baseAssetId].owned.add(input.amount);
4880
4894
  cachedMessages.push(input.nonce);
4881
4895
  }
4882
4896
  }
@@ -4908,11 +4922,12 @@ var Account = class extends AbstractAccount {
4908
4922
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
4909
4923
  * @returns A promise that resolves to the prepared transaction request.
4910
4924
  */
4911
- async createTransfer(destination, amount, assetId = BaseAssetId3, txParams = {}) {
4925
+ async createTransfer(destination, amount, assetId, txParams = {}) {
4912
4926
  const { minGasPrice } = this.provider.getGasConfig();
4927
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
4913
4928
  const params = { gasPrice: minGasPrice, ...txParams };
4914
4929
  const request = new ScriptTransactionRequest(params);
4915
- request.addCoinOutput(Address3.fromAddressOrString(destination), amount, assetId);
4930
+ request.addCoinOutput(Address3.fromAddressOrString(destination), amount, assetIdToTransfer);
4916
4931
  const { maxFee, requiredQuantities, gasUsed, estimatedInputs } = await this.provider.getTransactionCost(request, [], {
4917
4932
  estimateTxDependencies: true,
4918
4933
  resourcesOwner: this
@@ -4938,14 +4953,15 @@ var Account = class extends AbstractAccount {
4938
4953
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
4939
4954
  * @returns A promise that resolves to the transaction response.
4940
4955
  */
4941
- async transfer(destination, amount, assetId = BaseAssetId3, txParams = {}) {
4956
+ async transfer(destination, amount, assetId, txParams = {}) {
4942
4957
  if (bn16(amount).lte(0)) {
4943
4958
  throw new FuelError14(
4944
4959
  ErrorCode14.INVALID_TRANSFER_AMOUNT,
4945
4960
  "Transfer amount must be a positive number."
4946
4961
  );
4947
4962
  }
4948
- const request = await this.createTransfer(destination, amount, assetId, txParams);
4963
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
4964
+ const request = await this.createTransfer(destination, amount, assetIdToTransfer, txParams);
4949
4965
  return this.sendTransaction(request, { estimateTxDependencies: false });
4950
4966
  }
4951
4967
  /**
@@ -4957,7 +4973,7 @@ var Account = class extends AbstractAccount {
4957
4973
  * @param txParams - The optional transaction parameters.
4958
4974
  * @returns A promise that resolves to the transaction response.
4959
4975
  */
4960
- async transferToContract(contractId, amount, assetId = BaseAssetId3, txParams = {}) {
4976
+ async transferToContract(contractId, amount, assetId, txParams = {}) {
4961
4977
  if (bn16(amount).lte(0)) {
4962
4978
  throw new FuelError14(
4963
4979
  ErrorCode14.INVALID_TRANSFER_AMOUNT,
@@ -4966,11 +4982,12 @@ var Account = class extends AbstractAccount {
4966
4982
  }
4967
4983
  const contractAddress = Address3.fromAddressOrString(contractId);
4968
4984
  const { minGasPrice } = this.provider.getGasConfig();
4985
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
4969
4986
  const params = { gasPrice: minGasPrice, ...txParams };
4970
4987
  const { script, scriptData } = await assembleTransferToContractScript({
4971
4988
  hexlifiedContractId: contractAddress.toB256(),
4972
4989
  amountToTransfer: bn16(amount),
4973
- assetId
4990
+ assetId: assetIdToTransfer
4974
4991
  });
4975
4992
  const request = new ScriptTransactionRequest({
4976
4993
  ...params,
@@ -4980,7 +4997,7 @@ var Account = class extends AbstractAccount {
4980
4997
  request.addContractInputAndOutput(contractAddress);
4981
4998
  const { maxFee, requiredQuantities, gasUsed } = await this.provider.getTransactionCost(
4982
4999
  request,
4983
- [{ amount: bn16(amount), assetId: String(assetId) }]
5000
+ [{ amount: bn16(amount), assetId: String(assetIdToTransfer) }]
4984
5001
  );
4985
5002
  request.gasLimit = bn16(params.gasLimit ?? gasUsed);
4986
5003
  this.validateGas({
@@ -5002,6 +5019,7 @@ var Account = class extends AbstractAccount {
5002
5019
  */
5003
5020
  async withdrawToBaseLayer(recipient, amount, txParams = {}) {
5004
5021
  const { minGasPrice } = this.provider.getGasConfig();
5022
+ const baseAssetId = this.provider.getBaseAssetId();
5005
5023
  const recipientAddress = Address3.fromAddressOrString(recipient);
5006
5024
  const recipientDataArray = arrayify14(
5007
5025
  "0x".concat(recipientAddress.toHexString().substring(2).padStart(64, "0"))
@@ -5016,7 +5034,7 @@ var Account = class extends AbstractAccount {
5016
5034
  ]);
5017
5035
  const params = { script, gasPrice: minGasPrice, ...txParams };
5018
5036
  const request = new ScriptTransactionRequest(params);
5019
- const forwardingQuantities = [{ amount: bn16(amount), assetId: BaseAssetId3 }];
5037
+ const forwardingQuantities = [{ amount: bn16(amount), assetId: baseAssetId }];
5020
5038
  const { requiredQuantities, maxFee, gasUsed } = await this.provider.getTransactionCost(
5021
5039
  request,
5022
5040
  forwardingQuantities
@@ -8593,7 +8611,6 @@ import {
8593
8611
  SCRIPT_FIXED_SIZE
8594
8612
  } from "@fuel-ts/abi-coder";
8595
8613
  import { Address as Address9 } from "@fuel-ts/address";
8596
- import { BaseAssetId as BaseAssetId4 } from "@fuel-ts/address/configs";
8597
8614
  import { ErrorCode as ErrorCode23, FuelError as FuelError23 } from "@fuel-ts/errors";
8598
8615
  import { ByteArrayCoder, InputType as InputType7 } from "@fuel-ts/transactions";
8599
8616
  import { arrayify as arrayify20, hexlify as hexlify19 } from "@fuel-ts/utils";
@@ -8672,8 +8689,9 @@ var Predicate = class extends Account {
8672
8689
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
8673
8690
  * @returns A promise that resolves to the prepared transaction request.
8674
8691
  */
8675
- async createTransfer(destination, amount, assetId = BaseAssetId4, txParams = {}) {
8676
- const request = await super.createTransfer(destination, amount, assetId, txParams);
8692
+ async createTransfer(destination, amount, assetId, txParams = {}) {
8693
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
8694
+ const request = await super.createTransfer(destination, amount, assetIdToTransfer, txParams);
8677
8695
  return this.populateTransactionPredicateData(request);
8678
8696
  }
8679
8697
  /**