@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.

@@ -24,14 +24,12 @@ import { hexlify as hexlify15 } from "@fuel-ts/utils";
24
24
 
25
25
  // src/account.ts
26
26
  import { Address as Address3 } from "@fuel-ts/address";
27
- import { BaseAssetId as BaseAssetId3 } from "@fuel-ts/address/configs";
28
27
  import { ErrorCode as ErrorCode14, FuelError as FuelError14 } from "@fuel-ts/errors";
29
28
  import { AbstractAccount } from "@fuel-ts/interfaces";
30
29
  import { bn as bn16 } from "@fuel-ts/math";
31
30
  import { arrayify as arrayify14 } from "@fuel-ts/utils";
32
31
 
33
32
  // src/providers/coin-quantity.ts
34
- import { BaseAssetId } from "@fuel-ts/address/configs";
35
33
  import { bn } from "@fuel-ts/math";
36
34
  import { hexlify } from "@fuel-ts/utils";
37
35
  var coinQuantityfy = (coinQuantityLike) => {
@@ -40,11 +38,11 @@ var coinQuantityfy = (coinQuantityLike) => {
40
38
  let max2;
41
39
  if (Array.isArray(coinQuantityLike)) {
42
40
  amount = coinQuantityLike[0];
43
- assetId = coinQuantityLike[1] ?? BaseAssetId;
44
- max2 = coinQuantityLike[2] ?? void 0;
41
+ assetId = coinQuantityLike[1];
42
+ max2 = coinQuantityLike[2];
45
43
  } else {
46
44
  amount = coinQuantityLike.amount;
47
- assetId = coinQuantityLike.assetId ?? BaseAssetId;
45
+ assetId = coinQuantityLike.assetId;
48
46
  max2 = coinQuantityLike.max ?? void 0;
49
47
  }
50
48
  const bnAmount = bn(amount);
@@ -1151,7 +1149,7 @@ var outputify = (value) => {
1151
1149
 
1152
1150
  // src/providers/transaction-request/transaction-request.ts
1153
1151
  import { Address, addressify } from "@fuel-ts/address";
1154
- import { BaseAssetId as BaseAssetId2, ZeroBytes32 as ZeroBytes324 } from "@fuel-ts/address/configs";
1152
+ import { ZeroBytes32 as ZeroBytes324 } from "@fuel-ts/address/configs";
1155
1153
  import { bn as bn6 } from "@fuel-ts/math";
1156
1154
  import {
1157
1155
  PolicyType,
@@ -1531,6 +1529,8 @@ var BaseTransactionRequest = class {
1531
1529
  outputs = [];
1532
1530
  /** List of witnesses */
1533
1531
  witnesses = [];
1532
+ /** Base asset ID - should be fetched from the chain */
1533
+ baseAssetId = ZeroBytes324;
1534
1534
  /**
1535
1535
  * Constructor for initializing a base transaction request.
1536
1536
  *
@@ -1543,7 +1543,8 @@ var BaseTransactionRequest = class {
1543
1543
  witnessLimit,
1544
1544
  inputs,
1545
1545
  outputs,
1546
- witnesses
1546
+ witnesses,
1547
+ baseAssetId
1547
1548
  } = {}) {
1548
1549
  this.gasPrice = bn6(gasPrice);
1549
1550
  this.maturity = maturity ?? 0;
@@ -1552,6 +1553,7 @@ var BaseTransactionRequest = class {
1552
1553
  this.inputs = inputs ?? [];
1553
1554
  this.outputs = outputs ?? [];
1554
1555
  this.witnesses = witnesses ?? [];
1556
+ this.baseAssetId = baseAssetId ?? ZeroBytes324;
1555
1557
  }
1556
1558
  static getPolicyMeta(req) {
1557
1559
  let policyTypes = 0;
@@ -1776,11 +1778,9 @@ var BaseTransactionRequest = class {
1776
1778
  *
1777
1779
  * @param message - Message resource.
1778
1780
  * @param predicate - Predicate bytes.
1779
- * @param predicateData - Predicate data bytes.
1780
1781
  */
1781
1782
  addMessageInput(message, predicate) {
1782
1783
  const { recipient, sender, amount } = message;
1783
- const assetId = BaseAssetId2;
1784
1784
  let witnessIndex;
1785
1785
  if (predicate) {
1786
1786
  witnessIndex = 0;
@@ -1801,7 +1801,7 @@ var BaseTransactionRequest = class {
1801
1801
  predicateData: predicate?.predicateDataBytes
1802
1802
  };
1803
1803
  this.pushInput(input);
1804
- this.addChangeOutput(recipient, assetId);
1804
+ this.addChangeOutput(recipient, this.baseAssetId);
1805
1805
  }
1806
1806
  /**
1807
1807
  * Adds a single resource to the transaction by adding a coin/message input and a
@@ -1862,12 +1862,12 @@ var BaseTransactionRequest = class {
1862
1862
  * @param amount - Amount of coin.
1863
1863
  * @param assetId - Asset ID of coin.
1864
1864
  */
1865
- addCoinOutput(to, amount, assetId = BaseAssetId2) {
1865
+ addCoinOutput(to, amount, assetId) {
1866
1866
  this.pushOutput({
1867
1867
  type: OutputType2.Coin,
1868
1868
  to: addressify(to).toB256(),
1869
1869
  amount,
1870
- assetId
1870
+ assetId: assetId ?? this.baseAssetId
1871
1871
  });
1872
1872
  return this;
1873
1873
  }
@@ -1894,7 +1894,7 @@ var BaseTransactionRequest = class {
1894
1894
  * @param to - Address of the owner.
1895
1895
  * @param assetId - Asset ID of coin.
1896
1896
  */
1897
- addChangeOutput(to, assetId = BaseAssetId2) {
1897
+ addChangeOutput(to, assetId) {
1898
1898
  const changeOutput = this.getChangeOutputs().find(
1899
1899
  (output) => hexlify7(output.assetId) === assetId
1900
1900
  );
@@ -1902,7 +1902,7 @@ var BaseTransactionRequest = class {
1902
1902
  this.pushOutput({
1903
1903
  type: OutputType2.Change,
1904
1904
  to: addressify(to).toB256(),
1905
- assetId
1905
+ assetId: assetId ?? this.baseAssetId
1906
1906
  });
1907
1907
  }
1908
1908
  }
@@ -1984,7 +1984,7 @@ var BaseTransactionRequest = class {
1984
1984
  ]);
1985
1985
  }
1986
1986
  };
1987
- updateAssetInput(BaseAssetId2, bn6(1e11));
1987
+ updateAssetInput(this.baseAssetId, bn6(1e11));
1988
1988
  quantities.forEach((q) => updateAssetInput(q.assetId, q.amount));
1989
1989
  }
1990
1990
  /**
@@ -3406,6 +3406,7 @@ var processGqlChain = (chain) => {
3406
3406
  gasPerByte: bn14(feeParams.gasPerByte),
3407
3407
  maxMessageDataLength: bn14(predicateParams.maxMessageDataLength),
3408
3408
  chainId: bn14(consensusParameters.chainId),
3409
+ baseAssetId: consensusParameters.baseAssetId,
3409
3410
  gasCosts
3410
3411
  },
3411
3412
  gasCosts,
@@ -3648,6 +3649,17 @@ var _Provider = class {
3648
3649
  } = this.getChain();
3649
3650
  return chainId.toNumber();
3650
3651
  }
3652
+ /**
3653
+ * Returns the base asset ID
3654
+ *
3655
+ * @returns A promise that resolves to the base asset ID
3656
+ */
3657
+ getBaseAssetId() {
3658
+ const {
3659
+ consensusParameters: { baseAssetId }
3660
+ } = this.getChain();
3661
+ return baseAssetId;
3662
+ }
3651
3663
  /**
3652
3664
  * Submits a transaction to the chain to be executed.
3653
3665
  *
@@ -4518,8 +4530,9 @@ var Account = class extends AbstractAccount {
4518
4530
  * @param assetId - The asset ID to check the balance for.
4519
4531
  * @returns A promise that resolves to the balance amount.
4520
4532
  */
4521
- async getBalance(assetId = BaseAssetId3) {
4522
- const amount = await this.provider.getBalance(this.address, assetId);
4533
+ async getBalance(assetId) {
4534
+ const assetIdToFetch = assetId ?? this.provider.getBaseAssetId();
4535
+ const amount = await this.provider.getBalance(this.address, assetIdToFetch);
4523
4536
  return amount;
4524
4537
  }
4525
4538
  /**
@@ -4557,9 +4570,10 @@ var Account = class extends AbstractAccount {
4557
4570
  * @returns A promise that resolves when the resources are added to the transaction.
4558
4571
  */
4559
4572
  async fund(request, coinQuantities, fee) {
4573
+ const baseAssetId = this.provider.getBaseAssetId();
4560
4574
  const updatedQuantities = addAmountToAsset({
4561
4575
  amount: bn16(fee),
4562
- assetId: BaseAssetId3,
4576
+ assetId: baseAssetId,
4563
4577
  coinQuantities
4564
4578
  });
4565
4579
  const quantitiesDict = {};
@@ -4583,8 +4597,8 @@ var Account = class extends AbstractAccount {
4583
4597
  quantitiesDict[assetId].owned = quantitiesDict[assetId].owned.add(amount);
4584
4598
  cachedUtxos.push(input.id);
4585
4599
  }
4586
- } else if (input.recipient === owner && input.amount && quantitiesDict[BaseAssetId3]) {
4587
- quantitiesDict[BaseAssetId3].owned = quantitiesDict[BaseAssetId3].owned.add(input.amount);
4600
+ } else if (input.recipient === owner && input.amount && quantitiesDict[baseAssetId]) {
4601
+ quantitiesDict[baseAssetId].owned = quantitiesDict[baseAssetId].owned.add(input.amount);
4588
4602
  cachedMessages.push(input.nonce);
4589
4603
  }
4590
4604
  }
@@ -4616,11 +4630,12 @@ var Account = class extends AbstractAccount {
4616
4630
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
4617
4631
  * @returns A promise that resolves to the prepared transaction request.
4618
4632
  */
4619
- async createTransfer(destination, amount, assetId = BaseAssetId3, txParams = {}) {
4633
+ async createTransfer(destination, amount, assetId, txParams = {}) {
4620
4634
  const { minGasPrice } = this.provider.getGasConfig();
4635
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
4621
4636
  const params = { gasPrice: minGasPrice, ...txParams };
4622
4637
  const request = new ScriptTransactionRequest(params);
4623
- request.addCoinOutput(Address3.fromAddressOrString(destination), amount, assetId);
4638
+ request.addCoinOutput(Address3.fromAddressOrString(destination), amount, assetIdToTransfer);
4624
4639
  const { maxFee, requiredQuantities, gasUsed, estimatedInputs } = await this.provider.getTransactionCost(request, [], {
4625
4640
  estimateTxDependencies: true,
4626
4641
  resourcesOwner: this
@@ -4646,14 +4661,15 @@ var Account = class extends AbstractAccount {
4646
4661
  * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity).
4647
4662
  * @returns A promise that resolves to the transaction response.
4648
4663
  */
4649
- async transfer(destination, amount, assetId = BaseAssetId3, txParams = {}) {
4664
+ async transfer(destination, amount, assetId, txParams = {}) {
4650
4665
  if (bn16(amount).lte(0)) {
4651
4666
  throw new FuelError14(
4652
4667
  ErrorCode14.INVALID_TRANSFER_AMOUNT,
4653
4668
  "Transfer amount must be a positive number."
4654
4669
  );
4655
4670
  }
4656
- const request = await this.createTransfer(destination, amount, assetId, txParams);
4671
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
4672
+ const request = await this.createTransfer(destination, amount, assetIdToTransfer, txParams);
4657
4673
  return this.sendTransaction(request, { estimateTxDependencies: false });
4658
4674
  }
4659
4675
  /**
@@ -4665,7 +4681,7 @@ var Account = class extends AbstractAccount {
4665
4681
  * @param txParams - The optional transaction parameters.
4666
4682
  * @returns A promise that resolves to the transaction response.
4667
4683
  */
4668
- async transferToContract(contractId, amount, assetId = BaseAssetId3, txParams = {}) {
4684
+ async transferToContract(contractId, amount, assetId, txParams = {}) {
4669
4685
  if (bn16(amount).lte(0)) {
4670
4686
  throw new FuelError14(
4671
4687
  ErrorCode14.INVALID_TRANSFER_AMOUNT,
@@ -4674,11 +4690,12 @@ var Account = class extends AbstractAccount {
4674
4690
  }
4675
4691
  const contractAddress = Address3.fromAddressOrString(contractId);
4676
4692
  const { minGasPrice } = this.provider.getGasConfig();
4693
+ const assetIdToTransfer = assetId ?? this.provider.getBaseAssetId();
4677
4694
  const params = { gasPrice: minGasPrice, ...txParams };
4678
4695
  const { script, scriptData } = await assembleTransferToContractScript({
4679
4696
  hexlifiedContractId: contractAddress.toB256(),
4680
4697
  amountToTransfer: bn16(amount),
4681
- assetId
4698
+ assetId: assetIdToTransfer
4682
4699
  });
4683
4700
  const request = new ScriptTransactionRequest({
4684
4701
  ...params,
@@ -4688,7 +4705,7 @@ var Account = class extends AbstractAccount {
4688
4705
  request.addContractInputAndOutput(contractAddress);
4689
4706
  const { maxFee, requiredQuantities, gasUsed } = await this.provider.getTransactionCost(
4690
4707
  request,
4691
- [{ amount: bn16(amount), assetId: String(assetId) }]
4708
+ [{ amount: bn16(amount), assetId: String(assetIdToTransfer) }]
4692
4709
  );
4693
4710
  request.gasLimit = bn16(params.gasLimit ?? gasUsed);
4694
4711
  this.validateGas({
@@ -4710,6 +4727,7 @@ var Account = class extends AbstractAccount {
4710
4727
  */
4711
4728
  async withdrawToBaseLayer(recipient, amount, txParams = {}) {
4712
4729
  const { minGasPrice } = this.provider.getGasConfig();
4730
+ const baseAssetId = this.provider.getBaseAssetId();
4713
4731
  const recipientAddress = Address3.fromAddressOrString(recipient);
4714
4732
  const recipientDataArray = arrayify14(
4715
4733
  "0x".concat(recipientAddress.toHexString().substring(2).padStart(64, "0"))
@@ -4724,7 +4742,7 @@ var Account = class extends AbstractAccount {
4724
4742
  ]);
4725
4743
  const params = { script, gasPrice: minGasPrice, ...txParams };
4726
4744
  const request = new ScriptTransactionRequest(params);
4727
- const forwardingQuantities = [{ amount: bn16(amount), assetId: BaseAssetId3 }];
4745
+ const forwardingQuantities = [{ amount: bn16(amount), assetId: baseAssetId }];
4728
4746
  const { requiredQuantities, maxFee, gasUsed } = await this.provider.getTransactionCost(
4729
4747
  request,
4730
4748
  forwardingQuantities
@@ -7896,7 +7914,7 @@ var generateTestWallet = async (provider, quantities) => {
7896
7914
  };
7897
7915
 
7898
7916
  // src/test-utils/launchNode.ts
7899
- import { BaseAssetId as BaseAssetId4 } from "@fuel-ts/address/configs";
7917
+ import { ZeroBytes32 as ZeroBytes329 } from "@fuel-ts/address/configs";
7900
7918
  import { toHex as toHex2 } from "@fuel-ts/math";
7901
7919
  import { defaultChainConfig, defaultConsensusKey, hexlify as hexlify18 } from "@fuel-ts/utils";
7902
7920
  import { findBinPath } from "@fuel-ts/utils/cli-utils";
@@ -7996,7 +8014,7 @@ var launchNode = async ({
7996
8014
  {
7997
8015
  owner: signer.address.toHexString(),
7998
8016
  amount: toHex2(1e9),
7999
- asset_id: BaseAssetId4
8017
+ asset_id: defaultChainConfig?.consensus_parameters?.base_asset_id ?? ZeroBytes329
8000
8018
  }
8001
8019
  ]
8002
8020
  }
@@ -8062,9 +8080,10 @@ var launchNode = async ({
8062
8080
  })
8063
8081
  );
8064
8082
  var generateWallets = async (count, provider) => {
8083
+ const baseAssetId = provider.getBaseAssetId();
8065
8084
  const wallets = [];
8066
8085
  for (let i = 0; i < count; i += 1) {
8067
- const wallet = await generateTestWallet(provider, [[1e3, BaseAssetId4]]);
8086
+ const wallet = await generateTestWallet(provider, [[1e3, baseAssetId]]);
8068
8087
  wallets.push(wallet);
8069
8088
  }
8070
8089
  return wallets;