@mentaproject/client 0.1.38 → 0.2.1

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
@@ -141,14 +141,14 @@ var src_exports = {};
141
141
  __export(src_exports, {
142
142
  Account: () => Account,
143
143
  AccountManager: () => AccountManager,
144
- Action: () => Action,
145
144
  Block: () => Block,
146
145
  BlockManager: () => BlockManager,
147
146
  ContractManager: () => ContractManager,
147
+ GasManager: () => GasManager,
148
148
  MemoryCache: () => MemoryCache,
149
149
  MentaClient: () => MentaClient,
150
- Transaction: () => Transaction,
151
150
  TransactionManager: () => TransactionManager,
151
+ TransactionRecord: () => TransactionRecord,
152
152
  bigIntMax: () => bigIntMax,
153
153
  bigIntMin: () => bigIntMin,
154
154
  bigIntReviver: () => bigIntReviver,
@@ -158,38 +158,8 @@ __export(src_exports, {
158
158
  module.exports = __toCommonJS(src_exports);
159
159
 
160
160
  // src/structures/Account.ts
161
- var import_actions2 = require("@mentaproject/core/actions");
162
-
163
- // src/structures/Action.ts
164
161
  var import_actions = require("@mentaproject/core/actions");
165
- var Action = class {
166
- constructor(client, _call) {
167
- this.client = client;
168
- this._call = _call;
169
- }
170
- /**
171
- * Returns the Call object for use with TransactionBuilder.
172
- */
173
- call() {
174
- return this._call;
175
- }
176
- /**
177
- * Simulates the action without executing.
178
- */
179
- async simulate() {
180
- return (0, import_actions.simulateCalls)(this.client, { calls: [this._call] });
181
- }
182
- /**
183
- * Executes the action directly.
184
- *
185
- * @returns Transaction hash
186
- */
187
- async execute() {
188
- return this.client.sendTransaction(this._call);
189
- }
190
- };
191
-
192
- // src/structures/Account.ts
162
+ var import_transactions = require("@mentaproject/transactions");
193
163
  var import_utils = require("@mentaproject/core/utils");
194
164
  var import_contracts = require("@mentaproject/contracts");
195
165
 
@@ -261,6 +231,17 @@ async function toJSON({ obj, depth = 0 }) {
261
231
  return convertBigintsToStrings(data);
262
232
  }
263
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
+
264
245
  // src/structures/Account.ts
265
246
  var Account = class {
266
247
  constructor(client, data, persistenceManager) {
@@ -269,7 +250,7 @@ var Account = class {
269
250
  this.persistenceManager = persistenceManager;
270
251
  }
271
252
  async isContract() {
272
- const code = await (0, import_actions2.getCode)(this.client.rpc, { address: this.address });
253
+ const code = await (0, import_actions.getCode)(this.client.rpc, { address: this.address });
273
254
  if (!code || code === "0x") return false;
274
255
  return true;
275
256
  }
@@ -279,31 +260,36 @@ var Account = class {
279
260
  return await (0, import_contracts.getContractType)(this.client.rpc, this.address);
280
261
  }
281
262
  /**
282
- * Creates an Action to send ETH to this account.
263
+ * Creates a Transaction to send ETH to this account.
264
+ * Enhanced with withGas() support for easy gas configuration.
283
265
  *
284
266
  * @param amount The amount of ETH to send, in wei.
285
- * @returns An Action with .call(), .simulate(), and .execute() methods.
267
+ * @returns A Transaction with withGas(), .call(), .simulate(), and .execute() methods.
286
268
  *
287
269
  * @example
288
270
  * ```ts
289
- * // Direct execution
290
- * await account.sendEth(parseEther("1")).execute();
271
+ * // With gas config
272
+ * const pending = await account.sendEth(parseEther("1"))
273
+ * .withGas("fast")
274
+ * .execute();
275
+ * const receipt = await pending.confirm();
291
276
  *
292
- * // Batch with TransactionBuilder
293
- * builder.addCall(account.sendEth(parseEther("1")));
277
+ * // Batch with MulticallTransaction
278
+ * multicall.addCall(account.sendEth(parseEther("1")));
294
279
  * ```
295
280
  */
296
281
  sendEth(amount) {
297
- return new Action(this.client.rpc, {
282
+ const tx = new import_transactions.Transaction(this.client.rpc, {
298
283
  to: this.address,
299
284
  value: amount
300
285
  });
286
+ return withGasSupport(tx, this.client.gas);
301
287
  }
302
288
  async ETHBalance() {
303
- return await (0, import_actions2.getBalance)(this.client.rpc, { address: this.address });
289
+ return await (0, import_actions.getBalance)(this.client.rpc, { address: this.address });
304
290
  }
305
291
  async transactionCount() {
306
- return await (0, import_actions2.getTransactionCount)(this.client.rpc, { address: this.address });
292
+ return await (0, import_actions.getTransactionCount)(this.client.rpc, { address: this.address });
307
293
  }
308
294
  async transactions(params, forceFetch = false) {
309
295
  if (!this.persistenceManager || forceFetch) {
@@ -346,14 +332,14 @@ var Account = class {
346
332
  toBlock,
347
333
  limit = 50
348
334
  }) {
349
- const lastBlock = await (0, import_actions2.getBlockNumber)(this.client.rpc);
335
+ const lastBlock = await (0, import_actions.getBlockNumber)(this.client.rpc);
350
336
  const onBlockRange = async ({ fromBlock: fromBlock2, toBlock: toBlock2 }, stop) => {
351
- const outgoing = await (0, import_actions2.traceFilter)(this.client.rpc, {
337
+ const outgoing = await (0, import_actions.traceFilter)(this.client.rpc, {
352
338
  fromBlock: (0, import_utils.toHex)(fromBlock2),
353
339
  toBlock: (0, import_utils.toHex)(toBlock2),
354
340
  fromAddress: this.address
355
341
  });
356
- const incoming = await (0, import_actions2.traceFilter)(this.client.rpc, {
342
+ const incoming = await (0, import_actions.traceFilter)(this.client.rpc, {
357
343
  fromBlock: (0, import_utils.toHex)(fromBlock2),
358
344
  toBlock: (0, import_utils.toHex)(toBlock2),
359
345
  toAddress: this.address
@@ -361,7 +347,7 @@ var Account = class {
361
347
  const traces = outgoing.concat(incoming).sort((a, b) => a.blockNumber - b.blockNumber);
362
348
  return traces.map((t) => t.transactionHash);
363
349
  };
364
- return await (0, import_actions2.fetchByBlockRange)({
350
+ return await (0, import_actions.fetchByBlockRange)({
365
351
  toBlock: BigInt(toBlock !== void 0 ? toBlock : 0),
366
352
  fromBlock: BigInt(fromBlock !== void 0 ? fromBlock : lastBlock),
367
353
  direction: "backward",
@@ -371,15 +357,15 @@ var Account = class {
371
357
  }
372
358
  };
373
359
 
374
- // src/structures/Transaction.ts
375
- var import_actions3 = require("@mentaproject/core/actions");
360
+ // src/structures/TransactionRecord.ts
361
+ var import_actions2 = require("@mentaproject/core/actions");
376
362
  var import_utils2 = require("@mentaproject/core/utils");
377
363
 
378
364
  // node_modules/viem/_esm/index.js
379
365
  init_transaction();
380
366
 
381
- // src/structures/Transaction.ts
382
- var Transaction = class {
367
+ // src/structures/TransactionRecord.ts
368
+ var TransactionRecord = class {
383
369
  /**
384
370
  * Creates an instance of Transaction.
385
371
  * @description Initializes a new Transaction instance with the provided RPC client and transaction data.
@@ -416,7 +402,7 @@ var Transaction = class {
416
402
  * @returns {Promise<TransactionCall[] | undefined>} A promise that resolves to an array of call objects, each representing an internal call with sender, recipient, value, input data, gas used, and call type.
417
403
  */
418
404
  async calls() {
419
- const traces = await (0, import_actions3.traceTransaction)(this.client.rpc, this.hash);
405
+ const traces = await (0, import_actions2.traceTransaction)(this.client.rpc, this.hash);
420
406
  if (!traces) return void 0;
421
407
  const calls = traces.filter((t) => t.type === "call" && t.action !== void 0);
422
408
  return calls.map((c) => ({
@@ -435,7 +421,7 @@ var Transaction = class {
435
421
  * @returns {Promise<WaitForTransactionReceiptReturnType>} A promise that resolves to the transaction receipt once the specified number of confirmations are met.
436
422
  */
437
423
  async waitForReceipt(confirmations) {
438
- return await (0, import_actions3.waitForTransactionReceipt)(this.client.rpc, {
424
+ return await (0, import_actions2.waitForTransactionReceipt)(this.client.rpc, {
439
425
  hash: this.hash,
440
426
  confirmations: confirmations || 1
441
427
  });
@@ -447,7 +433,7 @@ var Transaction = class {
447
433
  */
448
434
  async receipt() {
449
435
  try {
450
- return await (0, import_actions3.getTransactionReceipt)(this.client.rpc, { hash: this.hash });
436
+ return await (0, import_actions2.getTransactionReceipt)(this.client.rpc, { hash: this.hash });
451
437
  } catch (error) {
452
438
  if (error instanceof TransactionReceiptNotFoundError) {
453
439
  return void 0;
@@ -460,7 +446,7 @@ var Transaction = class {
460
446
  * @returns {Promise<Block>} A promise that resolves to a Block instance representing the block that includes this transaction.
461
447
  */
462
448
  async block() {
463
- const data = await (0, import_actions3.getBlock)(this.client.rpc, { blockHash: this.blockHash });
449
+ const data = await (0, import_actions2.getBlock)(this.client.rpc, { blockHash: this.blockHash });
464
450
  return new Block(this.client, data);
465
451
  }
466
452
  /**
@@ -523,7 +509,7 @@ var Block = class {
523
509
  this.withdrawalsRoot = data.withdrawalsRoot;
524
510
  this.miner = new Account(this.client, { address: data.miner });
525
511
  if (!data.transactions || data.transactions.length === 0) {
526
- const parsed = data.transactions.map((data2) => typeof data2 === "string" ? data2 : new Transaction(this.client, data2));
512
+ const parsed = data.transactions.map((data2) => typeof data2 === "string" ? data2 : new TransactionRecord(this.client, data2));
527
513
  this.transactions = parsed;
528
514
  }
529
515
  }
@@ -553,7 +539,7 @@ var import_clients = require("@mentaproject/core/clients");
553
539
  var import_core = require("@mentaproject/core");
554
540
 
555
541
  // src/managers/BlockManager.ts
556
- var import_actions4 = require("@mentaproject/core/actions");
542
+ var import_actions3 = require("@mentaproject/core/actions");
557
543
  var BlockManager = class {
558
544
  /**
559
545
  * Creates an instance of BlockManager.
@@ -591,13 +577,14 @@ var BlockManager = class {
591
577
  * fetchBlock();
592
578
  */
593
579
  async get(params) {
594
- const data = await (0, import_actions4.getBlock)(this.client.rpc, params);
580
+ const data = await (0, import_actions3.getBlock)(this.client.rpc, params);
595
581
  return new Block(this.client, data);
596
582
  }
597
583
  };
598
584
 
599
585
  // src/managers/TransactionManager.ts
600
- var import_actions5 = require("@mentaproject/core/actions");
586
+ var import_transactions2 = require("@mentaproject/transactions");
587
+ var import_actions4 = require("@mentaproject/core/actions");
601
588
 
602
589
  // src/utils/bigint.ts
603
590
  var bigIntMax = (...args) => args.reduce((m, e) => e > m ? e : m);
@@ -653,36 +640,51 @@ function parseBingints(obj) {
653
640
 
654
641
  // src/managers/TransactionManager.ts
655
642
  var TransactionManager = class {
656
- /**
657
- * Creates an instance of `TransactionManager`.
658
- * @constructor
659
- * @param {MentaClient} client - The instance of MentaClient used to interact with the blockchain.
660
- */
661
643
  constructor(client) {
662
644
  this.client = client;
663
645
  }
664
646
  /**
665
- * Retrieves a transaction by its hash or block number and index.
666
- * @description This method fetches detailed information about a specific transaction
667
- * from the blockchain using the provided parameters.
668
- * @param {GetTransactionParameters} params - The parameters for retrieving the transaction,
669
- * typically including `hash` or `blockNumber` and `index`.
670
- * @returns {Promise<Transaction>} A promise that resolves to a {@link Transaction} instance
671
- * containing the retrieved transaction data.
647
+ * Creates a new MulticallTransaction for batching multiple calls.
648
+ * Enhanced with withGas() support for easy gas configuration.
649
+ *
650
+ * @returns A MulticallTransaction builder with withGas() support
651
+ *
672
652
  * @example
673
- * const txHash = '0x...'; // Replace with a valid transaction hash
674
- * const transaction = await client.transactions.get({ hash: txHash });
675
- * console.log(transaction);
653
+ * ```ts
654
+ * const pending = await client.transactions.create()
655
+ * .addCall(account.sendEth(parseEther("1")))
656
+ * .withGas("fast")
657
+ * .execute();
658
+ * ```
659
+ */
660
+ create() {
661
+ const tx = new import_transactions2.MulticallTransaction(this.client.rpc);
662
+ return withGasSupport(tx, this.client.gas);
663
+ }
664
+ /**
665
+ * Retrieves a transaction record by its hash.
666
+ *
667
+ * @param hash - The transaction hash
668
+ * @returns A TransactionRecord with receipt, block, etc.
669
+ *
670
+ * @example
671
+ * ```ts
672
+ * const tx = await client.transactions.fromHash(hash);
673
+ * console.log(tx.from, tx.to, tx.value);
674
+ * ```
675
+ */
676
+ async fromHash(hash) {
677
+ return this.get({ hash });
678
+ }
679
+ /**
680
+ * Retrieves a transaction by its hash or block number and index.
676
681
  */
677
682
  async get(params) {
678
- const data = await (0, import_actions5.getTransaction)(this.client.rpc, params);
679
- return new Transaction(this.client, data);
683
+ const data = await (0, import_actions4.getTransaction)(this.client.rpc, params);
684
+ return new TransactionRecord(this.client, data);
680
685
  }
681
686
  /**
682
687
  * Parse a transaction object from a JSON converted transaction data object.
683
- *
684
- * @param {J} data - The JSON converted transaction data object.
685
- * @returns {Transaction} A Transaction instance representing the parsed transaction.
686
688
  */
687
689
  parse(data) {
688
690
  const parsed = parseBingints(data);
@@ -691,35 +693,14 @@ var TransactionManager = class {
691
693
  from: parsed.from.address,
692
694
  to: parsed.to ? parsed.to.address : null
693
695
  };
694
- return new Transaction(this.client, rawData);
696
+ return new TransactionRecord(this.client, rawData);
695
697
  }
696
698
  /**
697
699
  * Sends a transaction to the blockchain network.
698
- * @description This method submits a new transaction to the blockchain. After the transaction
699
- * is successfully sent, it retrieves and returns the full transaction details.
700
- * @param {SendTransactionParameters} params - The parameters required to send the transaction,
701
- * such as `to`, `value`, `data`, `gasLimit`, etc.
702
- * @returns {Promise<Transaction>} A promise that resolves to a {@link Transaction} instance
703
- * representing the sent transaction, including its hash and other details.
704
- * @example
705
- * import { MentaClient } from '@mentaproject/client';
706
- * import { http } from '@mentaproject/core';
707
- *
708
- * // This example assumes you have an account with funds, which is available to the client.
709
- * const client = new MentaClient({
710
- * chain: 'mainnet',
711
- * transport: http('http://rpcurlhere.com')
712
- * });
713
- *
714
- * const transactionResult = await client.transactions.send({
715
- * to: '0xRecipientAddress...', // Replace with a valid recipient address
716
- * value: 1000000000000000000n, // 1 ETH in wei
717
- * });
718
- *
719
- * console.log('Transaction sent with hash:', transactionResult.hash);
700
+ * @deprecated Use client.transactions.create() for new transactions
720
701
  */
721
702
  async send(params) {
722
- const hash = await (0, import_actions5.sendTransaction)(this.client.rpc, params);
703
+ const hash = await (0, import_actions4.sendTransaction)(this.client.rpc, params);
723
704
  return await this.get({ hash });
724
705
  }
725
706
  };
@@ -817,7 +798,7 @@ var AccountManager = class {
817
798
  };
818
799
 
819
800
  // src/managers/PersistenceManager.ts
820
- var import_actions6 = require("@mentaproject/core/actions");
801
+ var import_actions5 = require("@mentaproject/core/actions");
821
802
  var PersistenceManager = class {
822
803
  /**
823
804
  * Creates an instance of PersistenceManager.
@@ -846,7 +827,7 @@ var PersistenceManager = class {
846
827
  async syncTransactions(account, limit = 1e5) {
847
828
  const lastSyncedBlock = await this.persistenceAdapter.getLastSyncedBlock(account.address);
848
829
  const lastBlock = lastSyncedBlock ?? 0n;
849
- const fromBlock = lastSyncedBlock ? lastSyncedBlock + 1n : await (0, import_actions6.getBlockNumber)(this.client.rpc);
830
+ const fromBlock = lastSyncedBlock ? lastSyncedBlock + 1n : await (0, import_actions5.getBlockNumber)(this.client.rpc);
850
831
  const newTransactions = await account.transactions({
851
832
  fromBlock,
852
833
  toBlock: lastBlock,
@@ -860,23 +841,53 @@ var PersistenceManager = class {
860
841
  }
861
842
  };
862
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
+
863
874
  // src/types/MentaClient.ts
864
- var import_actions7 = require("@mentaproject/core/actions");
875
+ var import_actions6 = require("@mentaproject/core/actions");
865
876
  var ClientEvents = {
866
877
  /**
867
878
  * Event for new blocks.
868
879
  * Uses the {@link watchBlocks} function from `@mentaproject/core`.
869
880
  */
870
- block: import_actions7.watchBlocks,
881
+ block: import_actions6.watchBlocks,
871
882
  /**
872
883
  * Event for new block numbers.
873
884
  * Uses the {@link watchBlockNumber} function from `@mentaproject/core`.
874
885
  */
875
- blockNumber: import_actions7.watchBlockNumber
886
+ blockNumber: import_actions6.watchBlockNumber
876
887
  };
877
888
 
878
889
  // src/utils/withCache.ts
879
- var import_actions8 = require("@mentaproject/core/actions");
890
+ var import_actions7 = require("@mentaproject/core/actions");
880
891
  var GET_NONCE_SELECTOR = "0x35567e1a";
881
892
  function isNonceCall(method, params) {
882
893
  if (method !== "eth_call") return false;
@@ -884,7 +895,7 @@ function isNonceCall(method, params) {
884
895
  return typeof callData === "string" && callData.startsWith(GET_NONCE_SELECTOR);
885
896
  }
886
897
  function withCache(client, cache) {
887
- (0, import_actions8.watchBlockNumber)(client, {
898
+ (0, import_actions7.watchBlockNumber)(client, {
888
899
  onBlockNumber: (blockNumber) => {
889
900
  cache.setBlockNumber(blockNumber);
890
901
  },
@@ -916,30 +927,15 @@ function withCache(client, cache) {
916
927
  var MentaClient = class {
917
928
  /**
918
929
  * Creates an instance of MentaClient.
919
- * @param {MentaClientConfig} params - The configuration parameters for the client.
920
- * @description The constructor initializes the RPC client and the various managers based on the provided configuration.
921
- * It also applies caching and persistence if specified in the configuration.
922
- * @example
923
- * import { MentaClient } from '@mentaproject/client';
924
- *
925
- * // Basic initialization
926
- * const client = new MentaClient({
927
- * url: 'http://rpcurlhere.com',
928
- * });
929
- *
930
- * // Initialization with a persistent cache
931
- * const clientWithCache = new MentaClient({
932
- * url: 'http://rpcurlhere.com',
933
- * cache: {
934
- * ttl: 60000, // 1 minute
935
- * },
936
- * });
937
930
  */
938
931
  constructor(params) {
939
932
  this.params = params;
940
933
  this.blocks = new BlockManager(this);
941
934
  this.transactions = new TransactionManager(this);
942
935
  this.contracts = new ContractManager(this);
936
+ if (params.gas?.strategy) {
937
+ this.defaultGasStrategy = params.gas.strategy;
938
+ }
943
939
  if (params.persistenceAdapter) {
944
940
  this.persistenceManager = new PersistenceManager(
945
941
  this,
@@ -952,7 +948,6 @@ var MentaClient = class {
952
948
  }
953
949
  /**
954
950
  * The underlying RPC client used for blockchain interactions.
955
- * @description This client is responsible for low-level communication with the RPC node.
956
951
  */
957
952
  get rpc() {
958
953
  if (!this._rpc) {
@@ -962,7 +957,6 @@ var MentaClient = class {
962
957
  }
963
958
  /**
964
959
  * The account associated with the client.
965
- * @description This account is used for signing transactions and interacting with the blockchain.
966
960
  */
967
961
  get account() {
968
962
  if (!this._account) {
@@ -970,25 +964,17 @@ var MentaClient = class {
970
964
  }
971
965
  return this._account;
972
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
+ }
973
976
  /**
974
977
  * Subscribes to a specific client event.
975
- * @template TEvent The type of the event to listen for.
976
- * @param {TEvent} event - The event to listen for (e.g., 'block', 'transaction').
977
- * @param {GetListenerCallback<TEvent>} callback - The callback function to execute when the event is triggered.
978
- * @returns {() => void} An unsubscribe function to stop listening to the event.
979
- * @description This method allows the client to listen for real-time events from the blockchain, such as new blocks or transactions.
980
- * @example
981
- * import { MentaClient } from '@mentaproject/client';
982
- *
983
- * const client = new MentaClient({ url: 'http://rpcurlhere.com' });
984
- *
985
- * // Subscribe to new blocks
986
- * const unsubscribe = client.on('block', (block) => {
987
- * console.log('New block received:', block);
988
- * });
989
- *
990
- * // To stop listening
991
- * // unsubscribe();
992
978
  */
993
979
  on(event, callback) {
994
980
  const eventFunction = ClientEvents[event];
@@ -1011,6 +997,7 @@ var MentaClient = class {
1011
997
  validatorAddress: this.params.validatorAddress
1012
998
  });
1013
999
  this._account = this.accounts.get(this.rpc.account.address);
1000
+ this._gas = new GasManager(this.rpc);
1014
1001
  }
1015
1002
  };
1016
1003
 
@@ -1112,14 +1099,14 @@ var MemoryCache = class {
1112
1099
  0 && (module.exports = {
1113
1100
  Account,
1114
1101
  AccountManager,
1115
- Action,
1116
1102
  Block,
1117
1103
  BlockManager,
1118
1104
  ContractManager,
1105
+ GasManager,
1119
1106
  MemoryCache,
1120
1107
  MentaClient,
1121
- Transaction,
1122
1108
  TransactionManager,
1109
+ TransactionRecord,
1123
1110
  bigIntMax,
1124
1111
  bigIntMin,
1125
1112
  bigIntReviver,