@mentaproject/client 0.2.0 → 0.2.2

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.
package/dist/index.cjs CHANGED
@@ -144,10 +144,11 @@ __export(src_exports, {
144
144
  Block: () => Block,
145
145
  BlockManager: () => BlockManager,
146
146
  ContractManager: () => ContractManager,
147
+ GasManager: () => GasManager,
147
148
  MemoryCache: () => MemoryCache,
148
149
  MentaClient: () => MentaClient,
149
- Transaction: () => Transaction2,
150
150
  TransactionManager: () => TransactionManager,
151
+ TransactionRecord: () => TransactionRecord,
151
152
  bigIntMax: () => bigIntMax,
152
153
  bigIntMin: () => bigIntMin,
153
154
  bigIntReviver: () => bigIntReviver,
@@ -230,6 +231,17 @@ async function toJSON({ obj, depth = 0 }) {
230
231
  return convertBigintsToStrings(data);
231
232
  }
232
233
 
234
+ // src/utils/withGasSupport.ts
235
+ function withGasSupport(tx, gasManager) {
236
+ const enhanced = tx;
237
+ enhanced.withGas = async (strategy) => {
238
+ const config = await gasManager.estimate(strategy);
239
+ tx.withGasConfig(config);
240
+ return tx;
241
+ };
242
+ return enhanced;
243
+ }
244
+
233
245
  // src/structures/Account.ts
234
246
  var Account = class {
235
247
  constructor(client, data, persistenceManager) {
@@ -249,14 +261,17 @@ var Account = class {
249
261
  }
250
262
  /**
251
263
  * Creates a Transaction to send ETH to this account.
264
+ * Enhanced with withGas() support for easy gas configuration.
252
265
  *
253
266
  * @param amount The amount of ETH to send, in wei.
254
- * @returns A Transaction with .call(), .simulate(), and .execute() methods.
267
+ * @returns A Transaction with withGas(), .call(), .simulate(), and .execute() methods.
255
268
  *
256
269
  * @example
257
270
  * ```ts
258
- * // Direct execution
259
- * const pending = await account.sendEth(parseEther("1")).execute();
271
+ * // With gas config
272
+ * const pending = await account.sendEth(parseEther("1"))
273
+ * .withGas("fast")
274
+ * .execute();
260
275
  * const receipt = await pending.confirm();
261
276
  *
262
277
  * // Batch with MulticallTransaction
@@ -264,10 +279,11 @@ var Account = class {
264
279
  * ```
265
280
  */
266
281
  sendEth(amount) {
267
- return new import_transactions.Transaction(this.client.rpc, {
282
+ const tx = new import_transactions.Transaction(this.client.rpc, {
268
283
  to: this.address,
269
284
  value: amount
270
285
  });
286
+ return withGasSupport(tx, this.client.gas);
271
287
  }
272
288
  async ETHBalance() {
273
289
  return await (0, import_actions.getBalance)(this.client.rpc, { address: this.address });
@@ -341,15 +357,15 @@ var Account = class {
341
357
  }
342
358
  };
343
359
 
344
- // src/structures/Transaction.ts
360
+ // src/structures/TransactionRecord.ts
345
361
  var import_actions2 = require("@mentaproject/core/actions");
346
362
  var import_utils2 = require("@mentaproject/core/utils");
347
363
 
348
364
  // node_modules/viem/_esm/index.js
349
365
  init_transaction();
350
366
 
351
- // src/structures/Transaction.ts
352
- var Transaction2 = class {
367
+ // src/structures/TransactionRecord.ts
368
+ var TransactionRecord = class {
353
369
  /**
354
370
  * Creates an instance of Transaction.
355
371
  * @description Initializes a new Transaction instance with the provided RPC client and transaction data.
@@ -493,7 +509,7 @@ var Block = class {
493
509
  this.withdrawalsRoot = data.withdrawalsRoot;
494
510
  this.miner = new Account(this.client, { address: data.miner });
495
511
  if (!data.transactions || data.transactions.length === 0) {
496
- const parsed = data.transactions.map((data2) => typeof data2 === "string" ? data2 : new Transaction2(this.client, data2));
512
+ const parsed = data.transactions.map((data2) => typeof data2 === "string" ? data2 : new TransactionRecord(this.client, data2));
497
513
  this.transactions = parsed;
498
514
  }
499
515
  }
@@ -629,25 +645,27 @@ var TransactionManager = class {
629
645
  }
630
646
  /**
631
647
  * Creates a new MulticallTransaction for batching multiple calls.
648
+ * Enhanced with withGas() support for easy gas configuration.
632
649
  *
633
- * @returns A MulticallTransaction builder
650
+ * @returns A MulticallTransaction builder with withGas() support
634
651
  *
635
652
  * @example
636
653
  * ```ts
637
654
  * const pending = await client.transactions.create()
638
655
  * .addCall(account.sendEth(parseEther("1")))
639
- * .addCall(erc20.transfer(to, amount))
656
+ * .withGas("fast")
640
657
  * .execute();
641
658
  * ```
642
659
  */
643
660
  create() {
644
- return new import_transactions2.MulticallTransaction(this.client.rpc);
661
+ const tx = new import_transactions2.MulticallTransaction(this.client.rpc);
662
+ return withGasSupport(tx, this.client.gas);
645
663
  }
646
664
  /**
647
- * Retrieves a rich transaction by its hash.
665
+ * Retrieves a transaction record by its hash.
648
666
  *
649
667
  * @param hash - The transaction hash
650
- * @returns A rich Transaction with receipt, block, etc.
668
+ * @returns A TransactionRecord with receipt, block, etc.
651
669
  *
652
670
  * @example
653
671
  * ```ts
@@ -663,7 +681,7 @@ var TransactionManager = class {
663
681
  */
664
682
  async get(params) {
665
683
  const data = await (0, import_actions4.getTransaction)(this.client.rpc, params);
666
- return new Transaction2(this.client, data);
684
+ return new TransactionRecord(this.client, data);
667
685
  }
668
686
  /**
669
687
  * Parse a transaction object from a JSON converted transaction data object.
@@ -675,7 +693,7 @@ var TransactionManager = class {
675
693
  from: parsed.from.address,
676
694
  to: parsed.to ? parsed.to.address : null
677
695
  };
678
- return new Transaction2(this.client, rawData);
696
+ return new TransactionRecord(this.client, rawData);
679
697
  }
680
698
  /**
681
699
  * Sends a transaction to the blockchain network.
@@ -823,6 +841,36 @@ var PersistenceManager = class {
823
841
  }
824
842
  };
825
843
 
844
+ // src/managers/GasManager.ts
845
+ var import_transactions3 = require("@mentaproject/transactions");
846
+ var GasManager = class {
847
+ constructor(client) {
848
+ this.client = client;
849
+ this.tracker = new import_transactions3.GasTracker(client);
850
+ }
851
+ /**
852
+ * Gets all gas price estimates (slow, medium, fast).
853
+ */
854
+ async getPrices() {
855
+ return this.tracker.getEstimates();
856
+ }
857
+ /**
858
+ * Gets a specific gas estimate by strategy.
859
+ *
860
+ * @param strategy - "slow", "medium", or "fast"
861
+ */
862
+ async estimate(strategy) {
863
+ const prices = await this.getPrices();
864
+ return prices[strategy];
865
+ }
866
+ /**
867
+ * Gets the underlying GasTracker for advanced usage.
868
+ */
869
+ getTracker() {
870
+ return this.tracker;
871
+ }
872
+ };
873
+
826
874
  // src/types/MentaClient.ts
827
875
  var import_actions6 = require("@mentaproject/core/actions");
828
876
  var ClientEvents = {
@@ -879,30 +927,15 @@ function withCache(client, cache) {
879
927
  var MentaClient = class {
880
928
  /**
881
929
  * Creates an instance of MentaClient.
882
- * @param {MentaClientConfig} params - The configuration parameters for the client.
883
- * @description The constructor initializes the RPC client and the various managers based on the provided configuration.
884
- * It also applies caching and persistence if specified in the configuration.
885
- * @example
886
- * import { MentaClient } from '@mentaproject/client';
887
- *
888
- * // Basic initialization
889
- * const client = new MentaClient({
890
- * url: 'http://rpcurlhere.com',
891
- * });
892
- *
893
- * // Initialization with a persistent cache
894
- * const clientWithCache = new MentaClient({
895
- * url: 'http://rpcurlhere.com',
896
- * cache: {
897
- * ttl: 60000, // 1 minute
898
- * },
899
- * });
900
930
  */
901
931
  constructor(params) {
902
932
  this.params = params;
903
933
  this.blocks = new BlockManager(this);
904
934
  this.transactions = new TransactionManager(this);
905
935
  this.contracts = new ContractManager(this);
936
+ if (params.gas?.strategy) {
937
+ this.defaultGasStrategy = params.gas.strategy;
938
+ }
906
939
  if (params.persistenceAdapter) {
907
940
  this.persistenceManager = new PersistenceManager(
908
941
  this,
@@ -915,7 +948,6 @@ var MentaClient = class {
915
948
  }
916
949
  /**
917
950
  * The underlying RPC client used for blockchain interactions.
918
- * @description This client is responsible for low-level communication with the RPC node.
919
951
  */
920
952
  get rpc() {
921
953
  if (!this._rpc) {
@@ -925,7 +957,6 @@ var MentaClient = class {
925
957
  }
926
958
  /**
927
959
  * The account associated with the client.
928
- * @description This account is used for signing transactions and interacting with the blockchain.
929
960
  */
930
961
  get account() {
931
962
  if (!this._account) {
@@ -933,25 +964,17 @@ var MentaClient = class {
933
964
  }
934
965
  return this._account;
935
966
  }
967
+ /**
968
+ * Manager for gas estimation and configuration.
969
+ */
970
+ get gas() {
971
+ if (!this._gas) {
972
+ throw new Error("Gas manager not initialized");
973
+ }
974
+ return this._gas;
975
+ }
936
976
  /**
937
977
  * Subscribes to a specific client event.
938
- * @template TEvent The type of the event to listen for.
939
- * @param {TEvent} event - The event to listen for (e.g., 'block', 'transaction').
940
- * @param {GetListenerCallback<TEvent>} callback - The callback function to execute when the event is triggered.
941
- * @returns {() => void} An unsubscribe function to stop listening to the event.
942
- * @description This method allows the client to listen for real-time events from the blockchain, such as new blocks or transactions.
943
- * @example
944
- * import { MentaClient } from '@mentaproject/client';
945
- *
946
- * const client = new MentaClient({ url: 'http://rpcurlhere.com' });
947
- *
948
- * // Subscribe to new blocks
949
- * const unsubscribe = client.on('block', (block) => {
950
- * console.log('New block received:', block);
951
- * });
952
- *
953
- * // To stop listening
954
- * // unsubscribe();
955
978
  */
956
979
  on(event, callback) {
957
980
  const eventFunction = ClientEvents[event];
@@ -974,6 +997,7 @@ var MentaClient = class {
974
997
  validatorAddress: this.params.validatorAddress
975
998
  });
976
999
  this._account = this.accounts.get(this.rpc.account.address);
1000
+ this._gas = new GasManager(this.rpc);
977
1001
  }
978
1002
  };
979
1003
 
@@ -1078,10 +1102,11 @@ var MemoryCache = class {
1078
1102
  Block,
1079
1103
  BlockManager,
1080
1104
  ContractManager,
1105
+ GasManager,
1081
1106
  MemoryCache,
1082
1107
  MentaClient,
1083
- Transaction,
1084
1108
  TransactionManager,
1109
+ TransactionRecord,
1085
1110
  bigIntMax,
1086
1111
  bigIntMin,
1087
1112
  bigIntReviver,