@fuel-ts/account 0.0.0-rc-2045-20240419094011 → 0.0.0-rc-1976-20240419101644

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);
@@ -1167,7 +1165,7 @@ var outputify = (value) => {
1167
1165
  // src/providers/transaction-request/transaction-request.ts
1168
1166
  import { UTXO_ID_LEN as UTXO_ID_LEN2 } from "@fuel-ts/abi-coder";
1169
1167
  import { Address, addressify } from "@fuel-ts/address";
1170
- import { BaseAssetId as BaseAssetId2, ZeroBytes32 as ZeroBytes324 } from "@fuel-ts/address/configs";
1168
+ import { ZeroBytes32 as ZeroBytes324 } from "@fuel-ts/address/configs";
1171
1169
  import { randomBytes } from "@fuel-ts/crypto";
1172
1170
  import { bn as bn7 } from "@fuel-ts/math";
1173
1171
  import {
@@ -1702,6 +1700,8 @@ var BaseTransactionRequest = class {
1702
1700
  outputs = [];
1703
1701
  /** List of witnesses */
1704
1702
  witnesses = [];
1703
+ /** Base asset ID - should be fetched from the chain */
1704
+ baseAssetId;
1705
1705
  /**
1706
1706
  * Constructor for initializing a base transaction request.
1707
1707
  *
@@ -1714,8 +1714,9 @@ var BaseTransactionRequest = class {
1714
1714
  witnessLimit,
1715
1715
  inputs,
1716
1716
  outputs,
1717
- witnesses
1718
- } = {}) {
1717
+ witnesses,
1718
+ baseAssetId
1719
+ }) {
1719
1720
  this.gasPrice = bn7(gasPrice);
1720
1721
  this.maturity = maturity ?? 0;
1721
1722
  this.witnessLimit = witnessLimit ? bn7(witnessLimit) : void 0;
@@ -1723,6 +1724,7 @@ var BaseTransactionRequest = class {
1723
1724
  this.inputs = inputs ?? [];
1724
1725
  this.outputs = outputs ?? [];
1725
1726
  this.witnesses = witnesses ?? [];
1727
+ this.baseAssetId = baseAssetId;
1726
1728
  }
1727
1729
  static getPolicyMeta(req) {
1728
1730
  let policyTypes = 0;
@@ -1946,11 +1948,9 @@ var BaseTransactionRequest = class {
1946
1948
  *
1947
1949
  * @param message - Message resource.
1948
1950
  * @param predicate - Predicate bytes.
1949
- * @param predicateData - Predicate data bytes.
1950
1951
  */
1951
1952
  addMessageInput(message, predicate) {
1952
1953
  const { recipient, sender, amount } = message;
1953
- const assetId = BaseAssetId2;
1954
1954
  let witnessIndex;
1955
1955
  if (predicate) {
1956
1956
  witnessIndex = 0;
@@ -1970,7 +1970,7 @@ var BaseTransactionRequest = class {
1970
1970
  predicate: predicate?.bytes
1971
1971
  };
1972
1972
  this.pushInput(input);
1973
- this.addChangeOutput(recipient, assetId);
1973
+ this.addChangeOutput(recipient, this.baseAssetId);
1974
1974
  }
1975
1975
  /**
1976
1976
  * Adds a single resource to the transaction by adding a coin/message input and a
@@ -2031,12 +2031,12 @@ var BaseTransactionRequest = class {
2031
2031
  * @param amount - Amount of coin.
2032
2032
  * @param assetId - Asset ID of coin.
2033
2033
  */
2034
- addCoinOutput(to, amount, assetId = BaseAssetId2) {
2034
+ addCoinOutput(to, amount, assetId) {
2035
2035
  this.pushOutput({
2036
2036
  type: OutputType2.Coin,
2037
2037
  to: addressify(to).toB256(),
2038
2038
  amount,
2039
- assetId
2039
+ assetId: assetId ?? this.baseAssetId
2040
2040
  });
2041
2041
  return this;
2042
2042
  }
@@ -2063,7 +2063,7 @@ var BaseTransactionRequest = class {
2063
2063
  * @param to - Address of the owner.
2064
2064
  * @param assetId - Asset ID of coin.
2065
2065
  */
2066
- addChangeOutput(to, assetId = BaseAssetId2) {
2066
+ addChangeOutput(to, assetId) {
2067
2067
  const changeOutput = this.getChangeOutputs().find(
2068
2068
  (output) => hexlify7(output.assetId) === assetId
2069
2069
  );
@@ -2071,7 +2071,7 @@ var BaseTransactionRequest = class {
2071
2071
  this.pushOutput({
2072
2072
  type: OutputType2.Change,
2073
2073
  to: addressify(to).toB256(),
2074
- assetId
2074
+ assetId: assetId ?? this.baseAssetId
2075
2075
  });
2076
2076
  }
2077
2077
  }
@@ -2147,7 +2147,7 @@ var BaseTransactionRequest = class {
2147
2147
  ]);
2148
2148
  }
2149
2149
  };
2150
- updateAssetInput(BaseAssetId2, bn7(1e11));
2150
+ updateAssetInput(this.baseAssetId, bn7(1e11));
2151
2151
  quantities.forEach((q) => updateAssetInput(q.assetId, q.amount));
2152
2152
  }
2153
2153
  /**
@@ -2317,12 +2317,7 @@ var CreateTransactionRequest = class extends BaseTransactionRequest {
2317
2317
  *
2318
2318
  * @param createTransactionRequestLike - The initial values for the instance
2319
2319
  */
2320
- constructor({
2321
- bytecodeWitnessIndex,
2322
- salt,
2323
- storageSlots,
2324
- ...rest
2325
- } = {}) {
2320
+ constructor({ bytecodeWitnessIndex, salt, storageSlots, ...rest }) {
2326
2321
  super(rest);
2327
2322
  this.bytecodeWitnessIndex = bytecodeWitnessIndex ?? 0;
2328
2323
  this.salt = hexlify9(salt ?? ZeroBytes326);
@@ -2446,7 +2441,7 @@ var ScriptTransactionRequest = class extends BaseTransactionRequest {
2446
2441
  *
2447
2442
  * @param scriptTransactionRequestLike - The initial values for the instance.
2448
2443
  */
2449
- constructor({ script, scriptData, gasLimit, ...rest } = {}) {
2444
+ constructor({ script, scriptData, gasLimit, ...rest }) {
2450
2445
  super(rest);
2451
2446
  this.gasLimit = bn10(gasLimit);
2452
2447
  this.script = arrayify8(script ?? returnZeroScript.bytes);
@@ -3621,6 +3616,7 @@ var processGqlChain = (chain) => {
3621
3616
  gasPerByte: bn15(feeParams.gasPerByte),
3622
3617
  maxMessageDataLength: bn15(predicateParams.maxMessageDataLength),
3623
3618
  chainId: bn15(consensusParameters.chainId),
3619
+ baseAssetId: consensusParameters.baseAssetId,
3624
3620
  gasCosts
3625
3621
  },
3626
3622
  gasCosts,
@@ -3863,6 +3859,17 @@ var _Provider = class {
3863
3859
  } = this.getChain();
3864
3860
  return chainId.toNumber();
3865
3861
  }
3862
+ /**
3863
+ * Returns the base asset ID
3864
+ *
3865
+ * @returns A promise that resolves to the base asset ID
3866
+ */
3867
+ getBaseAssetId() {
3868
+ const {
3869
+ consensusParameters: { baseAssetId }
3870
+ } = this.getChain();
3871
+ return baseAssetId;
3872
+ }
3866
3873
  /**
3867
3874
  * Submits a transaction to the chain to be executed.
3868
3875
  *
@@ -4926,8 +4933,9 @@ var Account = class extends AbstractAccount {
4926
4933
  * @param assetId - The asset ID to check the balance for.
4927
4934
  * @returns A promise that resolves to the balance amount.
4928
4935
  */
4929
- async getBalance(assetId = BaseAssetId3) {
4930
- const amount = await this.provider.getBalance(this.address, assetId);
4936
+ async getBalance(assetId) {
4937
+ const assetIdToFetch = assetId ?? this.provider.getBaseAssetId();
4938
+ const amount = await this.provider.getBalance(this.address, assetIdToFetch);
4931
4939
  return amount;
4932
4940
  }
4933
4941
  /**
@@ -4965,9 +4973,10 @@ var Account = class extends AbstractAccount {
4965
4973
  * @returns A promise that resolves when the resources are added to the transaction.
4966
4974
  */
4967
4975
  async fund(request, coinQuantities, fee) {
4976
+ const baseAssetId = this.provider.getBaseAssetId();
4968
4977
  const updatedQuantities = addAmountToAsset({
4969
4978
  amount: bn17(fee),
4970
- assetId: BaseAssetId3,
4979
+ assetId: baseAssetId,
4971
4980
  coinQuantities
4972
4981
  });
4973
4982
  const quantitiesDict = {};
@@ -4991,8 +5000,8 @@ var Account = class extends AbstractAccount {
4991
5000
  quantitiesDict[assetId].owned = quantitiesDict[assetId].owned.add(amount);
4992
5001
  cachedUtxos.push(input.id);
4993
5002
  }
4994
- } else if (input.recipient === owner && input.amount && quantitiesDict[BaseAssetId3]) {
4995
- quantitiesDict[BaseAssetId3].owned = quantitiesDict[BaseAssetId3].owned.add(input.amount);
5003
+ } else if (input.recipient === owner && input.amount && quantitiesDict[baseAssetId]) {
5004
+ quantitiesDict[baseAssetId].owned = quantitiesDict[baseAssetId].owned.add(input.amount);
4996
5005
  cachedMessages.push(input.nonce);
4997
5006
  }
4998
5007
  }
@@ -5024,11 +5033,13 @@ var Account = class extends AbstractAccount {
5024
5033
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
5025
5034
  * @returns A promise that resolves to the prepared transaction request.
5026
5035
  */
5027
- async createTransfer(destination, amount, assetId = BaseAssetId3, txParams = {}) {
5036
+ async createTransfer(destination, amount, assetId, txParams = {}) {
5028
5037
  const { minGasPrice } = this.provider.getGasConfig();
5029
- const params = { gasPrice: minGasPrice, ...txParams };
5038
+ const baseAssetId = this.provider.getBaseAssetId();
5039
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
5040
+ const params = { gasPrice: minGasPrice, baseAssetId, ...txParams };
5030
5041
  const request = new ScriptTransactionRequest(params);
5031
- request.addCoinOutput(Address3.fromAddressOrString(destination), amount, assetId);
5042
+ request.addCoinOutput(Address3.fromAddressOrString(destination), amount, assetIdToTransfer);
5032
5043
  const { maxFee, requiredQuantities, gasUsed, estimatedInputs } = await this.provider.getTransactionCost(request, [], {
5033
5044
  estimateTxDependencies: true,
5034
5045
  resourcesOwner: this
@@ -5054,14 +5065,15 @@ var Account = class extends AbstractAccount {
5054
5065
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
5055
5066
  * @returns A promise that resolves to the transaction response.
5056
5067
  */
5057
- async transfer(destination, amount, assetId = BaseAssetId3, txParams = {}) {
5068
+ async transfer(destination, amount, assetId, txParams = {}) {
5058
5069
  if (bn17(amount).lte(0)) {
5059
5070
  throw new FuelError15(
5060
5071
  ErrorCode15.INVALID_TRANSFER_AMOUNT,
5061
5072
  "Transfer amount must be a positive number."
5062
5073
  );
5063
5074
  }
5064
- const request = await this.createTransfer(destination, amount, assetId, txParams);
5075
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
5076
+ const request = await this.createTransfer(destination, amount, assetIdToTransfer, txParams);
5065
5077
  return this.sendTransaction(request, { estimateTxDependencies: false });
5066
5078
  }
5067
5079
  /**
@@ -5073,7 +5085,7 @@ var Account = class extends AbstractAccount {
5073
5085
  * @param txParams - The optional transaction parameters.
5074
5086
  * @returns A promise that resolves to the transaction response.
5075
5087
  */
5076
- async transferToContract(contractId, amount, assetId = BaseAssetId3, txParams = {}) {
5088
+ async transferToContract(contractId, amount, assetId, txParams = {}) {
5077
5089
  if (bn17(amount).lte(0)) {
5078
5090
  throw new FuelError15(
5079
5091
  ErrorCode15.INVALID_TRANSFER_AMOUNT,
@@ -5082,11 +5094,13 @@ var Account = class extends AbstractAccount {
5082
5094
  }
5083
5095
  const contractAddress = Address3.fromAddressOrString(contractId);
5084
5096
  const { minGasPrice } = this.provider.getGasConfig();
5085
- const params = { gasPrice: minGasPrice, ...txParams };
5097
+ const baseAssetId = this.provider.getBaseAssetId();
5098
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
5099
+ const params = { gasPrice: minGasPrice, baseAssetId, ...txParams };
5086
5100
  const { script, scriptData } = await assembleTransferToContractScript({
5087
5101
  hexlifiedContractId: contractAddress.toB256(),
5088
5102
  amountToTransfer: bn17(amount),
5089
- assetId
5103
+ assetId: assetIdToTransfer
5090
5104
  });
5091
5105
  const request = new ScriptTransactionRequest({
5092
5106
  ...params,
@@ -5096,7 +5110,7 @@ var Account = class extends AbstractAccount {
5096
5110
  request.addContractInputAndOutput(contractAddress);
5097
5111
  const { maxFee, requiredQuantities, gasUsed } = await this.provider.getTransactionCost(
5098
5112
  request,
5099
- [{ amount: bn17(amount), assetId: String(assetId) }]
5113
+ [{ amount: bn17(amount), assetId: String(assetIdToTransfer) }]
5100
5114
  );
5101
5115
  request.gasLimit = bn17(params.gasLimit ?? gasUsed);
5102
5116
  this.validateGas({
@@ -5118,6 +5132,7 @@ var Account = class extends AbstractAccount {
5118
5132
  */
5119
5133
  async withdrawToBaseLayer(recipient, amount, txParams = {}) {
5120
5134
  const { minGasPrice } = this.provider.getGasConfig();
5135
+ const baseAssetId = this.provider.getBaseAssetId();
5121
5136
  const recipientAddress = Address3.fromAddressOrString(recipient);
5122
5137
  const recipientDataArray = arrayify14(
5123
5138
  "0x".concat(recipientAddress.toHexString().substring(2).padStart(64, "0"))
@@ -5130,9 +5145,14 @@ var Account = class extends AbstractAccount {
5130
5145
  ...recipientDataArray,
5131
5146
  ...amountDataArray
5132
5147
  ]);
5133
- const params = { script, gasPrice: minGasPrice, ...txParams };
5148
+ const params = {
5149
+ script,
5150
+ gasPrice: minGasPrice,
5151
+ baseAssetId,
5152
+ ...txParams
5153
+ };
5134
5154
  const request = new ScriptTransactionRequest(params);
5135
- const forwardingQuantities = [{ amount: bn17(amount), assetId: BaseAssetId3 }];
5155
+ const forwardingQuantities = [{ amount: bn17(amount), assetId: baseAssetId }];
5136
5156
  const { requiredQuantities, maxFee, gasUsed } = await this.provider.getTransactionCost(
5137
5157
  request,
5138
5158
  forwardingQuantities
@@ -8709,7 +8729,6 @@ import {
8709
8729
  SCRIPT_FIXED_SIZE
8710
8730
  } from "@fuel-ts/abi-coder";
8711
8731
  import { Address as Address9 } from "@fuel-ts/address";
8712
- import { BaseAssetId as BaseAssetId4 } from "@fuel-ts/address/configs";
8713
8732
  import { ErrorCode as ErrorCode24, FuelError as FuelError24 } from "@fuel-ts/errors";
8714
8733
  import { ByteArrayCoder, InputType as InputType7 } from "@fuel-ts/transactions";
8715
8734
  import { arrayify as arrayify20, hexlify as hexlify19 } from "@fuel-ts/utils";
@@ -8787,8 +8806,9 @@ var Predicate = class extends Account {
8787
8806
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
8788
8807
  * @returns A promise that resolves to the prepared transaction request.
8789
8808
  */
8790
- async createTransfer(destination, amount, assetId = BaseAssetId4, txParams = {}) {
8791
- const request = await super.createTransfer(destination, amount, assetId, txParams);
8809
+ async createTransfer(destination, amount, assetId, txParams = {}) {
8810
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
8811
+ const request = await super.createTransfer(destination, amount, assetIdToTransfer, txParams);
8792
8812
  return this.populateTransactionPredicateData(request);
8793
8813
  }
8794
8814
  /**