@fuel-ts/account 0.0.0-rc-2143-20240424152333 → 0.0.0-rc-1976-20240424153519

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 {
@@ -1942,15 +1940,13 @@ var BaseTransactionRequest = class {
1942
1940
  }
1943
1941
  /**
1944
1942
  * Adds a single message input to the transaction and a change output for the
1945
- * baseAssetId, if one it was not added yet.
1943
+ * asset against the message
1946
1944
  *
1947
1945
  * @param message - Message resource.
1948
1946
  * @param predicate - Predicate bytes.
1949
- * @param predicateData - Predicate data bytes.
1950
1947
  */
1951
1948
  addMessageInput(message, predicate) {
1952
- const { recipient, sender, amount } = message;
1953
- const assetId = BaseAssetId2;
1949
+ const { recipient, sender, amount, assetId } = message;
1954
1950
  let witnessIndex;
1955
1951
  if (predicate) {
1956
1952
  witnessIndex = 0;
@@ -2031,7 +2027,7 @@ var BaseTransactionRequest = class {
2031
2027
  * @param amount - Amount of coin.
2032
2028
  * @param assetId - Asset ID of coin.
2033
2029
  */
2034
- addCoinOutput(to, amount, assetId = BaseAssetId2) {
2030
+ addCoinOutput(to, amount, assetId) {
2035
2031
  this.pushOutput({
2036
2032
  type: OutputType2.Coin,
2037
2033
  to: addressify(to).toB256(),
@@ -2063,7 +2059,7 @@ var BaseTransactionRequest = class {
2063
2059
  * @param to - Address of the owner.
2064
2060
  * @param assetId - Asset ID of coin.
2065
2061
  */
2066
- addChangeOutput(to, assetId = BaseAssetId2) {
2062
+ addChangeOutput(to, assetId) {
2067
2063
  const changeOutput = this.getChangeOutputs().find(
2068
2064
  (output) => hexlify7(output.assetId) === assetId
2069
2065
  );
@@ -2120,8 +2116,9 @@ var BaseTransactionRequest = class {
2120
2116
  * quantities array.
2121
2117
  *
2122
2118
  * @param quantities - CoinQuantity Array.
2119
+ * @param baseAssetId - The base asset to fund the transaction.
2123
2120
  */
2124
- fundWithFakeUtxos(quantities, resourcesOwner) {
2121
+ fundWithFakeUtxos(quantities, baseAssetId, resourcesOwner) {
2125
2122
  const findAssetInput = (assetId) => this.inputs.find((input) => {
2126
2123
  if ("assetId" in input) {
2127
2124
  return input.assetId === assetId;
@@ -2147,7 +2144,7 @@ var BaseTransactionRequest = class {
2147
2144
  ]);
2148
2145
  }
2149
2146
  };
2150
- updateAssetInput(BaseAssetId2, bn7(1e11));
2147
+ updateAssetInput(baseAssetId, bn7(1e11));
2151
2148
  quantities.forEach((q) => updateAssetInput(q.assetId, q.amount));
2152
2149
  }
2153
2150
  /**
@@ -2317,12 +2314,7 @@ var CreateTransactionRequest = class extends BaseTransactionRequest {
2317
2314
  *
2318
2315
  * @param createTransactionRequestLike - The initial values for the instance
2319
2316
  */
2320
- constructor({
2321
- bytecodeWitnessIndex,
2322
- salt,
2323
- storageSlots,
2324
- ...rest
2325
- } = {}) {
2317
+ constructor({ bytecodeWitnessIndex, salt, storageSlots, ...rest }) {
2326
2318
  super(rest);
2327
2319
  this.bytecodeWitnessIndex = bytecodeWitnessIndex ?? 0;
2328
2320
  this.salt = hexlify9(salt ?? ZeroBytes326);
@@ -3583,7 +3575,7 @@ function autoRetryFetch(fetchFn, options, retryAttemptNum = 0) {
3583
3575
  }
3584
3576
 
3585
3577
  // src/providers/utils/merge-quantities.ts
3586
- var mergeQuantities = (arr1, arr2) => {
3578
+ var mergeQuantities = (...coinQuantities) => {
3587
3579
  const resultMap = {};
3588
3580
  function addToMap({ amount, assetId }) {
3589
3581
  if (resultMap[assetId]) {
@@ -3592,8 +3584,7 @@ var mergeQuantities = (arr1, arr2) => {
3592
3584
  resultMap[assetId] = amount;
3593
3585
  }
3594
3586
  }
3595
- arr1.forEach(addToMap);
3596
- arr2.forEach(addToMap);
3587
+ coinQuantities.forEach((arr) => arr.forEach(addToMap));
3597
3588
  return Object.entries(resultMap).map(([assetId, amount]) => ({ assetId, amount }));
3598
3589
  };
3599
3590
 
@@ -3621,6 +3612,7 @@ var processGqlChain = (chain) => {
3621
3612
  gasPerByte: bn15(feeParams.gasPerByte),
3622
3613
  maxMessageDataLength: bn15(predicateParams.maxMessageDataLength),
3623
3614
  chainId: bn15(consensusParameters.chainId),
3615
+ baseAssetId: consensusParameters.baseAssetId,
3624
3616
  gasCosts
3625
3617
  },
3626
3618
  gasCosts,
@@ -3863,6 +3855,17 @@ var _Provider = class {
3863
3855
  } = this.getChain();
3864
3856
  return chainId.toNumber();
3865
3857
  }
3858
+ /**
3859
+ * Returns the base asset ID
3860
+ *
3861
+ * @returns A promise that resolves to the base asset ID
3862
+ */
3863
+ getBaseAssetId() {
3864
+ const {
3865
+ consensusParameters: { baseAssetId }
3866
+ } = this.getChain();
3867
+ return baseAssetId;
3868
+ }
3866
3869
  /**
3867
3870
  * Submits a transaction to the chain to be executed.
3868
3871
  *
@@ -4093,9 +4096,10 @@ var _Provider = class {
4093
4096
  const { minGasPrice } = this.getGasConfig();
4094
4097
  const setGasPrice = max(txRequestClone.gasPrice, minGasPrice);
4095
4098
  const isScriptTransaction = txRequestClone.type === TransactionType8.Script;
4099
+ const baseAssetId = this.getBaseAssetId();
4096
4100
  const coinOutputsQuantities = txRequestClone.getCoinOutputsQuantities();
4097
4101
  const allQuantities = mergeQuantities(coinOutputsQuantities, forwardingQuantities);
4098
- txRequestClone.fundWithFakeUtxos(allQuantities, resourcesOwner?.address);
4102
+ txRequestClone.fundWithFakeUtxos(allQuantities, baseAssetId, resourcesOwner?.address);
4099
4103
  if (isScriptTransaction) {
4100
4104
  txRequestClone.gasLimit = bn15(0);
4101
4105
  }
@@ -4926,8 +4930,9 @@ var Account = class extends AbstractAccount {
4926
4930
  * @param assetId - The asset ID to check the balance for.
4927
4931
  * @returns A promise that resolves to the balance amount.
4928
4932
  */
4929
- async getBalance(assetId = BaseAssetId3) {
4930
- const amount = await this.provider.getBalance(this.address, assetId);
4933
+ async getBalance(assetId) {
4934
+ const assetIdToFetch = assetId ?? this.provider.getBaseAssetId();
4935
+ const amount = await this.provider.getBalance(this.address, assetIdToFetch);
4931
4936
  return amount;
4932
4937
  }
4933
4938
  /**
@@ -4965,9 +4970,10 @@ var Account = class extends AbstractAccount {
4965
4970
  * @returns A promise that resolves when the resources are added to the transaction.
4966
4971
  */
4967
4972
  async fund(request, coinQuantities, fee) {
4973
+ const baseAssetId = this.provider.getBaseAssetId();
4968
4974
  const updatedQuantities = addAmountToAsset({
4969
4975
  amount: bn17(fee),
4970
- assetId: BaseAssetId3,
4976
+ assetId: baseAssetId,
4971
4977
  coinQuantities
4972
4978
  });
4973
4979
  const quantitiesDict = {};
@@ -4991,8 +4997,8 @@ var Account = class extends AbstractAccount {
4991
4997
  quantitiesDict[assetId].owned = quantitiesDict[assetId].owned.add(amount);
4992
4998
  cachedUtxos.push(input.id);
4993
4999
  }
4994
- } else if (input.recipient === owner && input.amount && quantitiesDict[BaseAssetId3]) {
4995
- quantitiesDict[BaseAssetId3].owned = quantitiesDict[BaseAssetId3].owned.add(input.amount);
5000
+ } else if (input.recipient === owner && input.amount && quantitiesDict[baseAssetId]) {
5001
+ quantitiesDict[baseAssetId].owned = quantitiesDict[baseAssetId].owned.add(input.amount);
4996
5002
  cachedMessages.push(input.nonce);
4997
5003
  }
4998
5004
  }
@@ -5024,11 +5030,12 @@ var Account = class extends AbstractAccount {
5024
5030
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
5025
5031
  * @returns A promise that resolves to the prepared transaction request.
5026
5032
  */
5027
- async createTransfer(destination, amount, assetId = BaseAssetId3, txParams = {}) {
5033
+ async createTransfer(destination, amount, assetId, txParams = {}) {
5028
5034
  const { minGasPrice } = this.provider.getGasConfig();
5035
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
5029
5036
  const params = { gasPrice: minGasPrice, ...txParams };
5030
5037
  const request = new ScriptTransactionRequest(params);
5031
- request.addCoinOutput(Address3.fromAddressOrString(destination), amount, assetId);
5038
+ request.addCoinOutput(Address3.fromAddressOrString(destination), amount, assetIdToTransfer);
5032
5039
  const { maxFee, requiredQuantities, gasUsed, estimatedInputs } = await this.provider.getTransactionCost(request, [], {
5033
5040
  estimateTxDependencies: true,
5034
5041
  resourcesOwner: this
@@ -5054,14 +5061,15 @@ var Account = class extends AbstractAccount {
5054
5061
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
5055
5062
  * @returns A promise that resolves to the transaction response.
5056
5063
  */
5057
- async transfer(destination, amount, assetId = BaseAssetId3, txParams = {}) {
5064
+ async transfer(destination, amount, assetId, txParams = {}) {
5058
5065
  if (bn17(amount).lte(0)) {
5059
5066
  throw new FuelError15(
5060
5067
  ErrorCode15.INVALID_TRANSFER_AMOUNT,
5061
5068
  "Transfer amount must be a positive number."
5062
5069
  );
5063
5070
  }
5064
- const request = await this.createTransfer(destination, amount, assetId, txParams);
5071
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
5072
+ const request = await this.createTransfer(destination, amount, assetIdToTransfer, txParams);
5065
5073
  return this.sendTransaction(request, { estimateTxDependencies: false });
5066
5074
  }
5067
5075
  /**
@@ -5073,7 +5081,7 @@ var Account = class extends AbstractAccount {
5073
5081
  * @param txParams - The optional transaction parameters.
5074
5082
  * @returns A promise that resolves to the transaction response.
5075
5083
  */
5076
- async transferToContract(contractId, amount, assetId = BaseAssetId3, txParams = {}) {
5084
+ async transferToContract(contractId, amount, assetId, txParams = {}) {
5077
5085
  if (bn17(amount).lte(0)) {
5078
5086
  throw new FuelError15(
5079
5087
  ErrorCode15.INVALID_TRANSFER_AMOUNT,
@@ -5082,11 +5090,12 @@ var Account = class extends AbstractAccount {
5082
5090
  }
5083
5091
  const contractAddress = Address3.fromAddressOrString(contractId);
5084
5092
  const { minGasPrice } = this.provider.getGasConfig();
5093
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
5085
5094
  const params = { gasPrice: minGasPrice, ...txParams };
5086
5095
  const { script, scriptData } = await assembleTransferToContractScript({
5087
5096
  hexlifiedContractId: contractAddress.toB256(),
5088
5097
  amountToTransfer: bn17(amount),
5089
- assetId
5098
+ assetId: assetIdToTransfer
5090
5099
  });
5091
5100
  const request = new ScriptTransactionRequest({
5092
5101
  ...params,
@@ -5096,7 +5105,7 @@ var Account = class extends AbstractAccount {
5096
5105
  request.addContractInputAndOutput(contractAddress);
5097
5106
  const { maxFee, requiredQuantities, gasUsed } = await this.provider.getTransactionCost(
5098
5107
  request,
5099
- [{ amount: bn17(amount), assetId: String(assetId) }]
5108
+ [{ amount: bn17(amount), assetId: String(assetIdToTransfer) }]
5100
5109
  );
5101
5110
  request.gasLimit = bn17(params.gasLimit ?? gasUsed);
5102
5111
  this.validateGas({
@@ -5118,6 +5127,7 @@ var Account = class extends AbstractAccount {
5118
5127
  */
5119
5128
  async withdrawToBaseLayer(recipient, amount, txParams = {}) {
5120
5129
  const { minGasPrice } = this.provider.getGasConfig();
5130
+ const baseAssetId = this.provider.getBaseAssetId();
5121
5131
  const recipientAddress = Address3.fromAddressOrString(recipient);
5122
5132
  const recipientDataArray = arrayify14(
5123
5133
  "0x".concat(recipientAddress.toHexString().substring(2).padStart(64, "0"))
@@ -5130,9 +5140,13 @@ var Account = class extends AbstractAccount {
5130
5140
  ...recipientDataArray,
5131
5141
  ...amountDataArray
5132
5142
  ]);
5133
- const params = { script, gasPrice: minGasPrice, ...txParams };
5143
+ const params = {
5144
+ script,
5145
+ gasPrice: minGasPrice,
5146
+ ...txParams
5147
+ };
5134
5148
  const request = new ScriptTransactionRequest(params);
5135
- const forwardingQuantities = [{ amount: bn17(amount), assetId: BaseAssetId3 }];
5149
+ const forwardingQuantities = [{ amount: bn17(amount), assetId: baseAssetId }];
5136
5150
  const { requiredQuantities, maxFee, gasUsed } = await this.provider.getTransactionCost(
5137
5151
  request,
5138
5152
  forwardingQuantities
@@ -8709,7 +8723,6 @@ import {
8709
8723
  SCRIPT_FIXED_SIZE
8710
8724
  } from "@fuel-ts/abi-coder";
8711
8725
  import { Address as Address9 } from "@fuel-ts/address";
8712
- import { BaseAssetId as BaseAssetId4 } from "@fuel-ts/address/configs";
8713
8726
  import { ErrorCode as ErrorCode24, FuelError as FuelError24 } from "@fuel-ts/errors";
8714
8727
  import { ByteArrayCoder, InputType as InputType7 } from "@fuel-ts/transactions";
8715
8728
  import { arrayify as arrayify20, hexlify as hexlify19 } from "@fuel-ts/utils";
@@ -8787,8 +8800,9 @@ var Predicate = class extends Account {
8787
8800
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
8788
8801
  * @returns A promise that resolves to the prepared transaction request.
8789
8802
  */
8790
- async createTransfer(destination, amount, assetId = BaseAssetId4, txParams = {}) {
8791
- const request = await super.createTransfer(destination, amount, assetId, txParams);
8803
+ async createTransfer(destination, amount, assetId, txParams = {}) {
8804
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
8805
+ const request = await super.createTransfer(destination, amount, assetIdToTransfer, txParams);
8792
8806
  return this.populateTransactionPredicateData(request);
8793
8807
  }
8794
8808
  /**