@fuel-ts/account 0.0.0-rc-1832-20240404143349 → 0.0.0-rc-1976-20240404171500

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 ErrorCode15, FuelError as FuelError15 } from "@fuel-ts/errors";
34
33
  import { AbstractAccount } from "@fuel-ts/interfaces";
35
34
  import { bn as bn17 } 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 bn7 } from "@fuel-ts/math";
1161
1159
  import {
1162
1160
  PolicyType,
@@ -1690,6 +1688,8 @@ var BaseTransactionRequest = class {
1690
1688
  outputs = [];
1691
1689
  /** List of witnesses */
1692
1690
  witnesses = [];
1691
+ /** Base asset ID - should be fetched from the chain */
1692
+ baseAssetId = ZeroBytes324;
1693
1693
  /**
1694
1694
  * Constructor for initializing a base transaction request.
1695
1695
  *
@@ -1702,7 +1702,8 @@ var BaseTransactionRequest = class {
1702
1702
  witnessLimit,
1703
1703
  inputs,
1704
1704
  outputs,
1705
- witnesses
1705
+ witnesses,
1706
+ baseAssetId
1706
1707
  } = {}) {
1707
1708
  this.gasPrice = bn7(gasPrice);
1708
1709
  this.maturity = maturity ?? 0;
@@ -1711,6 +1712,7 @@ var BaseTransactionRequest = class {
1711
1712
  this.inputs = inputs ?? [];
1712
1713
  this.outputs = outputs ?? [];
1713
1714
  this.witnesses = witnesses ?? [];
1715
+ this.baseAssetId = baseAssetId ?? ZeroBytes324;
1714
1716
  }
1715
1717
  static getPolicyMeta(req) {
1716
1718
  let policyTypes = 0;
@@ -1935,11 +1937,9 @@ var BaseTransactionRequest = class {
1935
1937
  *
1936
1938
  * @param message - Message resource.
1937
1939
  * @param predicate - Predicate bytes.
1938
- * @param predicateData - Predicate data bytes.
1939
1940
  */
1940
1941
  addMessageInput(message, predicate) {
1941
1942
  const { recipient, sender, amount } = message;
1942
- const assetId = BaseAssetId2;
1943
1943
  let witnessIndex;
1944
1944
  if (predicate) {
1945
1945
  witnessIndex = 0;
@@ -1960,7 +1960,7 @@ var BaseTransactionRequest = class {
1960
1960
  predicateData: predicate?.predicateDataBytes
1961
1961
  };
1962
1962
  this.pushInput(input);
1963
- this.addChangeOutput(recipient, assetId);
1963
+ this.addChangeOutput(recipient, this.baseAssetId);
1964
1964
  }
1965
1965
  /**
1966
1966
  * Adds a single resource to the transaction by adding a coin/message input and a
@@ -2021,12 +2021,12 @@ var BaseTransactionRequest = class {
2021
2021
  * @param amount - Amount of coin.
2022
2022
  * @param assetId - Asset ID of coin.
2023
2023
  */
2024
- addCoinOutput(to, amount, assetId = BaseAssetId2) {
2024
+ addCoinOutput(to, amount, assetId) {
2025
2025
  this.pushOutput({
2026
2026
  type: OutputType2.Coin,
2027
2027
  to: addressify(to).toB256(),
2028
2028
  amount,
2029
- assetId
2029
+ assetId: assetId ?? this.baseAssetId
2030
2030
  });
2031
2031
  return this;
2032
2032
  }
@@ -2053,7 +2053,7 @@ var BaseTransactionRequest = class {
2053
2053
  * @param to - Address of the owner.
2054
2054
  * @param assetId - Asset ID of coin.
2055
2055
  */
2056
- addChangeOutput(to, assetId = BaseAssetId2) {
2056
+ addChangeOutput(to, assetId) {
2057
2057
  const changeOutput = this.getChangeOutputs().find(
2058
2058
  (output) => hexlify7(output.assetId) === assetId
2059
2059
  );
@@ -2061,7 +2061,7 @@ var BaseTransactionRequest = class {
2061
2061
  this.pushOutput({
2062
2062
  type: OutputType2.Change,
2063
2063
  to: addressify(to).toB256(),
2064
- assetId
2064
+ assetId: assetId ?? this.baseAssetId
2065
2065
  });
2066
2066
  }
2067
2067
  }
@@ -2143,7 +2143,7 @@ var BaseTransactionRequest = class {
2143
2143
  ]);
2144
2144
  }
2145
2145
  };
2146
- updateAssetInput(BaseAssetId2, bn7(1e11));
2146
+ updateAssetInput(this.baseAssetId, bn7(1e11));
2147
2147
  quantities.forEach((q) => updateAssetInput(q.assetId, q.amount));
2148
2148
  }
2149
2149
  /**
@@ -3617,6 +3617,7 @@ var processGqlChain = (chain) => {
3617
3617
  gasPerByte: bn15(feeParams.gasPerByte),
3618
3618
  maxMessageDataLength: bn15(predicateParams.maxMessageDataLength),
3619
3619
  chainId: bn15(consensusParameters.chainId),
3620
+ baseAssetId: consensusParameters.baseAssetId,
3620
3621
  gasCosts
3621
3622
  },
3622
3623
  gasCosts,
@@ -3859,6 +3860,17 @@ var _Provider = class {
3859
3860
  } = this.getChain();
3860
3861
  return chainId.toNumber();
3861
3862
  }
3863
+ /**
3864
+ * Returns the base asset ID
3865
+ *
3866
+ * @returns A promise that resolves to the base asset ID
3867
+ */
3868
+ getBaseAssetId() {
3869
+ const {
3870
+ consensusParameters: { baseAssetId }
3871
+ } = this.getChain();
3872
+ return baseAssetId;
3873
+ }
3862
3874
  /**
3863
3875
  * Submits a transaction to the chain to be executed.
3864
3876
  *
@@ -4895,8 +4907,9 @@ var Account = class extends AbstractAccount {
4895
4907
  * @param assetId - The asset ID to check the balance for.
4896
4908
  * @returns A promise that resolves to the balance amount.
4897
4909
  */
4898
- async getBalance(assetId = BaseAssetId3) {
4899
- const amount = await this.provider.getBalance(this.address, assetId);
4910
+ async getBalance(assetId) {
4911
+ const assetIdToFetch = assetId ?? this.provider.getBaseAssetId();
4912
+ const amount = await this.provider.getBalance(this.address, assetIdToFetch);
4900
4913
  return amount;
4901
4914
  }
4902
4915
  /**
@@ -4934,9 +4947,10 @@ var Account = class extends AbstractAccount {
4934
4947
  * @returns A promise that resolves when the resources are added to the transaction.
4935
4948
  */
4936
4949
  async fund(request, coinQuantities, fee) {
4950
+ const baseAssetId = this.provider.getBaseAssetId();
4937
4951
  const updatedQuantities = addAmountToAsset({
4938
4952
  amount: bn17(fee),
4939
- assetId: BaseAssetId3,
4953
+ assetId: baseAssetId,
4940
4954
  coinQuantities
4941
4955
  });
4942
4956
  const quantitiesDict = {};
@@ -4960,8 +4974,8 @@ var Account = class extends AbstractAccount {
4960
4974
  quantitiesDict[assetId].owned = quantitiesDict[assetId].owned.add(amount);
4961
4975
  cachedUtxos.push(input.id);
4962
4976
  }
4963
- } else if (input.recipient === owner && input.amount && quantitiesDict[BaseAssetId3]) {
4964
- quantitiesDict[BaseAssetId3].owned = quantitiesDict[BaseAssetId3].owned.add(input.amount);
4977
+ } else if (input.recipient === owner && input.amount && quantitiesDict[baseAssetId]) {
4978
+ quantitiesDict[baseAssetId].owned = quantitiesDict[baseAssetId].owned.add(input.amount);
4965
4979
  cachedMessages.push(input.nonce);
4966
4980
  }
4967
4981
  }
@@ -4993,11 +5007,12 @@ var Account = class extends AbstractAccount {
4993
5007
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
4994
5008
  * @returns A promise that resolves to the prepared transaction request.
4995
5009
  */
4996
- async createTransfer(destination, amount, assetId = BaseAssetId3, txParams = {}) {
5010
+ async createTransfer(destination, amount, assetId, txParams = {}) {
4997
5011
  const { minGasPrice } = this.provider.getGasConfig();
5012
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
4998
5013
  const params = { gasPrice: minGasPrice, ...txParams };
4999
5014
  const request = new ScriptTransactionRequest(params);
5000
- request.addCoinOutput(Address3.fromAddressOrString(destination), amount, assetId);
5015
+ request.addCoinOutput(Address3.fromAddressOrString(destination), amount, assetIdToTransfer);
5001
5016
  const { maxFee, requiredQuantities, gasUsed, estimatedInputs } = await this.provider.getTransactionCost(request, [], {
5002
5017
  estimateTxDependencies: true,
5003
5018
  resourcesOwner: this
@@ -5023,14 +5038,15 @@ var Account = class extends AbstractAccount {
5023
5038
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
5024
5039
  * @returns A promise that resolves to the transaction response.
5025
5040
  */
5026
- async transfer(destination, amount, assetId = BaseAssetId3, txParams = {}) {
5041
+ async transfer(destination, amount, assetId, txParams = {}) {
5027
5042
  if (bn17(amount).lte(0)) {
5028
5043
  throw new FuelError15(
5029
5044
  ErrorCode15.INVALID_TRANSFER_AMOUNT,
5030
5045
  "Transfer amount must be a positive number."
5031
5046
  );
5032
5047
  }
5033
- const request = await this.createTransfer(destination, amount, assetId, txParams);
5048
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
5049
+ const request = await this.createTransfer(destination, amount, assetIdToTransfer, txParams);
5034
5050
  return this.sendTransaction(request, { estimateTxDependencies: false });
5035
5051
  }
5036
5052
  /**
@@ -5042,7 +5058,7 @@ var Account = class extends AbstractAccount {
5042
5058
  * @param txParams - The optional transaction parameters.
5043
5059
  * @returns A promise that resolves to the transaction response.
5044
5060
  */
5045
- async transferToContract(contractId, amount, assetId = BaseAssetId3, txParams = {}) {
5061
+ async transferToContract(contractId, amount, assetId, txParams = {}) {
5046
5062
  if (bn17(amount).lte(0)) {
5047
5063
  throw new FuelError15(
5048
5064
  ErrorCode15.INVALID_TRANSFER_AMOUNT,
@@ -5051,11 +5067,12 @@ var Account = class extends AbstractAccount {
5051
5067
  }
5052
5068
  const contractAddress = Address3.fromAddressOrString(contractId);
5053
5069
  const { minGasPrice } = this.provider.getGasConfig();
5070
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
5054
5071
  const params = { gasPrice: minGasPrice, ...txParams };
5055
5072
  const { script, scriptData } = await assembleTransferToContractScript({
5056
5073
  hexlifiedContractId: contractAddress.toB256(),
5057
5074
  amountToTransfer: bn17(amount),
5058
- assetId
5075
+ assetId: assetIdToTransfer
5059
5076
  });
5060
5077
  const request = new ScriptTransactionRequest({
5061
5078
  ...params,
@@ -5065,7 +5082,7 @@ var Account = class extends AbstractAccount {
5065
5082
  request.addContractInputAndOutput(contractAddress);
5066
5083
  const { maxFee, requiredQuantities, gasUsed } = await this.provider.getTransactionCost(
5067
5084
  request,
5068
- [{ amount: bn17(amount), assetId: String(assetId) }]
5085
+ [{ amount: bn17(amount), assetId: String(assetIdToTransfer) }]
5069
5086
  );
5070
5087
  request.gasLimit = bn17(params.gasLimit ?? gasUsed);
5071
5088
  this.validateGas({
@@ -5087,6 +5104,7 @@ var Account = class extends AbstractAccount {
5087
5104
  */
5088
5105
  async withdrawToBaseLayer(recipient, amount, txParams = {}) {
5089
5106
  const { minGasPrice } = this.provider.getGasConfig();
5107
+ const baseAssetId = this.provider.getBaseAssetId();
5090
5108
  const recipientAddress = Address3.fromAddressOrString(recipient);
5091
5109
  const recipientDataArray = arrayify14(
5092
5110
  "0x".concat(recipientAddress.toHexString().substring(2).padStart(64, "0"))
@@ -5101,7 +5119,7 @@ var Account = class extends AbstractAccount {
5101
5119
  ]);
5102
5120
  const params = { script, gasPrice: minGasPrice, ...txParams };
5103
5121
  const request = new ScriptTransactionRequest(params);
5104
- const forwardingQuantities = [{ amount: bn17(amount), assetId: BaseAssetId3 }];
5122
+ const forwardingQuantities = [{ amount: bn17(amount), assetId: baseAssetId }];
5105
5123
  const { requiredQuantities, maxFee, gasUsed } = await this.provider.getTransactionCost(
5106
5124
  request,
5107
5125
  forwardingQuantities
@@ -8678,7 +8696,6 @@ import {
8678
8696
  SCRIPT_FIXED_SIZE
8679
8697
  } from "@fuel-ts/abi-coder";
8680
8698
  import { Address as Address9 } from "@fuel-ts/address";
8681
- import { BaseAssetId as BaseAssetId4 } from "@fuel-ts/address/configs";
8682
8699
  import { ErrorCode as ErrorCode24, FuelError as FuelError24 } from "@fuel-ts/errors";
8683
8700
  import { ByteArrayCoder, InputType as InputType7 } from "@fuel-ts/transactions";
8684
8701
  import { arrayify as arrayify20, hexlify as hexlify19 } from "@fuel-ts/utils";
@@ -8757,8 +8774,9 @@ var Predicate = class extends Account {
8757
8774
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
8758
8775
  * @returns A promise that resolves to the prepared transaction request.
8759
8776
  */
8760
- async createTransfer(destination, amount, assetId = BaseAssetId4, txParams = {}) {
8761
- const request = await super.createTransfer(destination, amount, assetId, txParams);
8777
+ async createTransfer(destination, amount, assetId, txParams = {}) {
8778
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
8779
+ const request = await super.createTransfer(destination, amount, assetIdToTransfer, txParams);
8762
8780
  return this.populateTransactionPredicateData(request);
8763
8781
  }
8764
8782
  /**