@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/.tsbuildinfo +1 -1
- package/dist/index.cjs +136 -149
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +109 -122
- package/dist/index.js.map +1 -1
- package/dist/managers/GasManager.d.ts +36 -0
- package/dist/managers/TransactionManager.d.ts +51 -48
- package/dist/managers/index.d.ts +1 -0
- package/dist/structures/Account.d.ts +15 -10
- package/dist/structures/Action.d.ts +20 -4
- package/dist/structures/Block.d.ts +3 -3
- package/dist/structures/MentaClient.d.ts +28 -51
- package/dist/structures/TransactionRecord.d.ts +138 -0
- package/dist/structures/index.d.ts +1 -2
- package/dist/types/MentaClient.d.ts +16 -0
- package/dist/types/PersistenceAdapter.d.ts +4 -4
- package/dist/utils/withGasSupport.d.ts +35 -0
- package/package.json +2 -1
- package/src/managers/GasManager.ts +50 -0
- package/src/managers/PersistenceManager.ts +1 -1
- package/src/managers/TransactionManager.ts +85 -76
- package/src/managers/index.ts +2 -1
- package/src/structures/Account.ts +17 -11
- package/src/structures/Block.ts +5 -5
- package/src/structures/MentaClient.ts +41 -52
- package/src/structures/{Transaction.ts → TransactionRecord.ts} +1 -1
- package/src/structures/index.ts +1 -2
- package/src/types/MentaClient.ts +17 -0
- package/src/types/PersistenceAdapter.ts +4 -4
- package/src/utils/withGasSupport.ts +50 -0
- package/src/structures/Action.ts +0 -51
package/dist/index.js
CHANGED
|
@@ -13,37 +13,7 @@ import {
|
|
|
13
13
|
getTransactionCount,
|
|
14
14
|
traceFilter
|
|
15
15
|
} from "@mentaproject/core/actions";
|
|
16
|
-
|
|
17
|
-
// src/structures/Action.ts
|
|
18
|
-
import { simulateCalls } from "@mentaproject/core/actions";
|
|
19
|
-
var Action = class {
|
|
20
|
-
constructor(client, _call) {
|
|
21
|
-
this.client = client;
|
|
22
|
-
this._call = _call;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Returns the Call object for use with TransactionBuilder.
|
|
26
|
-
*/
|
|
27
|
-
call() {
|
|
28
|
-
return this._call;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Simulates the action without executing.
|
|
32
|
-
*/
|
|
33
|
-
async simulate() {
|
|
34
|
-
return simulateCalls(this.client, { calls: [this._call] });
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Executes the action directly.
|
|
38
|
-
*
|
|
39
|
-
* @returns Transaction hash
|
|
40
|
-
*/
|
|
41
|
-
async execute() {
|
|
42
|
-
return this.client.sendTransaction(this._call);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
// src/structures/Account.ts
|
|
16
|
+
import { Transaction } from "@mentaproject/transactions";
|
|
47
17
|
import { toHex } from "@mentaproject/core/utils";
|
|
48
18
|
import { getContractType } from "@mentaproject/contracts";
|
|
49
19
|
|
|
@@ -115,6 +85,17 @@ async function toJSON({ obj, depth = 0 }) {
|
|
|
115
85
|
return convertBigintsToStrings(data);
|
|
116
86
|
}
|
|
117
87
|
|
|
88
|
+
// src/utils/withGasSupport.ts
|
|
89
|
+
function withGasSupport(tx, gasManager) {
|
|
90
|
+
const enhanced = tx;
|
|
91
|
+
enhanced.withGas = async (strategy) => {
|
|
92
|
+
const config = await gasManager.estimate(strategy);
|
|
93
|
+
tx.withGasConfig(config);
|
|
94
|
+
return tx;
|
|
95
|
+
};
|
|
96
|
+
return enhanced;
|
|
97
|
+
}
|
|
98
|
+
|
|
118
99
|
// src/structures/Account.ts
|
|
119
100
|
var Account = class {
|
|
120
101
|
constructor(client, data, persistenceManager) {
|
|
@@ -133,25 +114,30 @@ var Account = class {
|
|
|
133
114
|
return await getContractType(this.client.rpc, this.address);
|
|
134
115
|
}
|
|
135
116
|
/**
|
|
136
|
-
* Creates
|
|
117
|
+
* Creates a Transaction to send ETH to this account.
|
|
118
|
+
* Enhanced with withGas() support for easy gas configuration.
|
|
137
119
|
*
|
|
138
120
|
* @param amount The amount of ETH to send, in wei.
|
|
139
|
-
* @returns
|
|
121
|
+
* @returns A Transaction with withGas(), .call(), .simulate(), and .execute() methods.
|
|
140
122
|
*
|
|
141
123
|
* @example
|
|
142
124
|
* ```ts
|
|
143
|
-
* //
|
|
144
|
-
* await account.sendEth(parseEther("1"))
|
|
125
|
+
* // With gas config
|
|
126
|
+
* const pending = await account.sendEth(parseEther("1"))
|
|
127
|
+
* .withGas("fast")
|
|
128
|
+
* .execute();
|
|
129
|
+
* const receipt = await pending.confirm();
|
|
145
130
|
*
|
|
146
|
-
* // Batch with
|
|
147
|
-
*
|
|
131
|
+
* // Batch with MulticallTransaction
|
|
132
|
+
* multicall.addCall(account.sendEth(parseEther("1")));
|
|
148
133
|
* ```
|
|
149
134
|
*/
|
|
150
135
|
sendEth(amount) {
|
|
151
|
-
|
|
136
|
+
const tx = new Transaction(this.client.rpc, {
|
|
152
137
|
to: this.address,
|
|
153
138
|
value: amount
|
|
154
139
|
});
|
|
140
|
+
return withGasSupport(tx, this.client.gas);
|
|
155
141
|
}
|
|
156
142
|
async ETHBalance() {
|
|
157
143
|
return await getBalance(this.client.rpc, { address: this.address });
|
|
@@ -225,10 +211,10 @@ var Account = class {
|
|
|
225
211
|
}
|
|
226
212
|
};
|
|
227
213
|
|
|
228
|
-
// src/structures/
|
|
214
|
+
// src/structures/TransactionRecord.ts
|
|
229
215
|
import { getBlock, getTransactionReceipt, traceTransaction, waitForTransactionReceipt } from "@mentaproject/core/actions";
|
|
230
216
|
import { hexToBigInt } from "@mentaproject/core/utils";
|
|
231
|
-
var
|
|
217
|
+
var TransactionRecord = class {
|
|
232
218
|
/**
|
|
233
219
|
* Creates an instance of Transaction.
|
|
234
220
|
* @description Initializes a new Transaction instance with the provided RPC client and transaction data.
|
|
@@ -372,7 +358,7 @@ var Block = class {
|
|
|
372
358
|
this.withdrawalsRoot = data.withdrawalsRoot;
|
|
373
359
|
this.miner = new Account(this.client, { address: data.miner });
|
|
374
360
|
if (!data.transactions || data.transactions.length === 0) {
|
|
375
|
-
const parsed = data.transactions.map((data2) => typeof data2 === "string" ? data2 : new
|
|
361
|
+
const parsed = data.transactions.map((data2) => typeof data2 === "string" ? data2 : new TransactionRecord(this.client, data2));
|
|
376
362
|
this.transactions = parsed;
|
|
377
363
|
}
|
|
378
364
|
}
|
|
@@ -446,6 +432,7 @@ var BlockManager = class {
|
|
|
446
432
|
};
|
|
447
433
|
|
|
448
434
|
// src/managers/TransactionManager.ts
|
|
435
|
+
import { MulticallTransaction } from "@mentaproject/transactions";
|
|
449
436
|
import { getTransaction, sendTransaction } from "@mentaproject/core/actions";
|
|
450
437
|
|
|
451
438
|
// src/utils/bigint.ts
|
|
@@ -502,36 +489,51 @@ function parseBingints(obj) {
|
|
|
502
489
|
|
|
503
490
|
// src/managers/TransactionManager.ts
|
|
504
491
|
var TransactionManager = class {
|
|
505
|
-
/**
|
|
506
|
-
* Creates an instance of `TransactionManager`.
|
|
507
|
-
* @constructor
|
|
508
|
-
* @param {MentaClient} client - The instance of MentaClient used to interact with the blockchain.
|
|
509
|
-
*/
|
|
510
492
|
constructor(client) {
|
|
511
493
|
this.client = client;
|
|
512
494
|
}
|
|
513
495
|
/**
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
* @
|
|
518
|
-
*
|
|
519
|
-
* @
|
|
520
|
-
*
|
|
496
|
+
* Creates a new MulticallTransaction for batching multiple calls.
|
|
497
|
+
* Enhanced with withGas() support for easy gas configuration.
|
|
498
|
+
*
|
|
499
|
+
* @returns A MulticallTransaction builder with withGas() support
|
|
500
|
+
*
|
|
501
|
+
* @example
|
|
502
|
+
* ```ts
|
|
503
|
+
* const pending = await client.transactions.create()
|
|
504
|
+
* .addCall(account.sendEth(parseEther("1")))
|
|
505
|
+
* .withGas("fast")
|
|
506
|
+
* .execute();
|
|
507
|
+
* ```
|
|
508
|
+
*/
|
|
509
|
+
create() {
|
|
510
|
+
const tx = new MulticallTransaction(this.client.rpc);
|
|
511
|
+
return withGasSupport(tx, this.client.gas);
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Retrieves a transaction record by its hash.
|
|
515
|
+
*
|
|
516
|
+
* @param hash - The transaction hash
|
|
517
|
+
* @returns A TransactionRecord with receipt, block, etc.
|
|
518
|
+
*
|
|
521
519
|
* @example
|
|
522
|
-
*
|
|
523
|
-
* const
|
|
524
|
-
* console.log(
|
|
520
|
+
* ```ts
|
|
521
|
+
* const tx = await client.transactions.fromHash(hash);
|
|
522
|
+
* console.log(tx.from, tx.to, tx.value);
|
|
523
|
+
* ```
|
|
524
|
+
*/
|
|
525
|
+
async fromHash(hash) {
|
|
526
|
+
return this.get({ hash });
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Retrieves a transaction by its hash or block number and index.
|
|
525
530
|
*/
|
|
526
531
|
async get(params) {
|
|
527
532
|
const data = await getTransaction(this.client.rpc, params);
|
|
528
|
-
return new
|
|
533
|
+
return new TransactionRecord(this.client, data);
|
|
529
534
|
}
|
|
530
535
|
/**
|
|
531
536
|
* Parse a transaction object from a JSON converted transaction data object.
|
|
532
|
-
*
|
|
533
|
-
* @param {J} data - The JSON converted transaction data object.
|
|
534
|
-
* @returns {Transaction} A Transaction instance representing the parsed transaction.
|
|
535
537
|
*/
|
|
536
538
|
parse(data) {
|
|
537
539
|
const parsed = parseBingints(data);
|
|
@@ -540,32 +542,11 @@ var TransactionManager = class {
|
|
|
540
542
|
from: parsed.from.address,
|
|
541
543
|
to: parsed.to ? parsed.to.address : null
|
|
542
544
|
};
|
|
543
|
-
return new
|
|
545
|
+
return new TransactionRecord(this.client, rawData);
|
|
544
546
|
}
|
|
545
547
|
/**
|
|
546
548
|
* Sends a transaction to the blockchain network.
|
|
547
|
-
* @
|
|
548
|
-
* is successfully sent, it retrieves and returns the full transaction details.
|
|
549
|
-
* @param {SendTransactionParameters} params - The parameters required to send the transaction,
|
|
550
|
-
* such as `to`, `value`, `data`, `gasLimit`, etc.
|
|
551
|
-
* @returns {Promise<Transaction>} A promise that resolves to a {@link Transaction} instance
|
|
552
|
-
* representing the sent transaction, including its hash and other details.
|
|
553
|
-
* @example
|
|
554
|
-
* import { MentaClient } from '@mentaproject/client';
|
|
555
|
-
* import { http } from '@mentaproject/core';
|
|
556
|
-
*
|
|
557
|
-
* // This example assumes you have an account with funds, which is available to the client.
|
|
558
|
-
* const client = new MentaClient({
|
|
559
|
-
* chain: 'mainnet',
|
|
560
|
-
* transport: http('http://rpcurlhere.com')
|
|
561
|
-
* });
|
|
562
|
-
*
|
|
563
|
-
* const transactionResult = await client.transactions.send({
|
|
564
|
-
* to: '0xRecipientAddress...', // Replace with a valid recipient address
|
|
565
|
-
* value: 1000000000000000000n, // 1 ETH in wei
|
|
566
|
-
* });
|
|
567
|
-
*
|
|
568
|
-
* console.log('Transaction sent with hash:', transactionResult.hash);
|
|
549
|
+
* @deprecated Use client.transactions.create() for new transactions
|
|
569
550
|
*/
|
|
570
551
|
async send(params) {
|
|
571
552
|
const hash = await sendTransaction(this.client.rpc, params);
|
|
@@ -709,6 +690,36 @@ var PersistenceManager = class {
|
|
|
709
690
|
}
|
|
710
691
|
};
|
|
711
692
|
|
|
693
|
+
// src/managers/GasManager.ts
|
|
694
|
+
import { GasTracker } from "@mentaproject/transactions";
|
|
695
|
+
var GasManager = class {
|
|
696
|
+
constructor(client) {
|
|
697
|
+
this.client = client;
|
|
698
|
+
this.tracker = new GasTracker(client);
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Gets all gas price estimates (slow, medium, fast).
|
|
702
|
+
*/
|
|
703
|
+
async getPrices() {
|
|
704
|
+
return this.tracker.getEstimates();
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* Gets a specific gas estimate by strategy.
|
|
708
|
+
*
|
|
709
|
+
* @param strategy - "slow", "medium", or "fast"
|
|
710
|
+
*/
|
|
711
|
+
async estimate(strategy) {
|
|
712
|
+
const prices = await this.getPrices();
|
|
713
|
+
return prices[strategy];
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* Gets the underlying GasTracker for advanced usage.
|
|
717
|
+
*/
|
|
718
|
+
getTracker() {
|
|
719
|
+
return this.tracker;
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
|
|
712
723
|
// src/types/MentaClient.ts
|
|
713
724
|
import { watchBlockNumber, watchBlocks } from "@mentaproject/core/actions";
|
|
714
725
|
var ClientEvents = {
|
|
@@ -765,30 +776,15 @@ function withCache(client, cache) {
|
|
|
765
776
|
var MentaClient = class {
|
|
766
777
|
/**
|
|
767
778
|
* Creates an instance of MentaClient.
|
|
768
|
-
* @param {MentaClientConfig} params - The configuration parameters for the client.
|
|
769
|
-
* @description The constructor initializes the RPC client and the various managers based on the provided configuration.
|
|
770
|
-
* It also applies caching and persistence if specified in the configuration.
|
|
771
|
-
* @example
|
|
772
|
-
* import { MentaClient } from '@mentaproject/client';
|
|
773
|
-
*
|
|
774
|
-
* // Basic initialization
|
|
775
|
-
* const client = new MentaClient({
|
|
776
|
-
* url: 'http://rpcurlhere.com',
|
|
777
|
-
* });
|
|
778
|
-
*
|
|
779
|
-
* // Initialization with a persistent cache
|
|
780
|
-
* const clientWithCache = new MentaClient({
|
|
781
|
-
* url: 'http://rpcurlhere.com',
|
|
782
|
-
* cache: {
|
|
783
|
-
* ttl: 60000, // 1 minute
|
|
784
|
-
* },
|
|
785
|
-
* });
|
|
786
779
|
*/
|
|
787
780
|
constructor(params) {
|
|
788
781
|
this.params = params;
|
|
789
782
|
this.blocks = new BlockManager(this);
|
|
790
783
|
this.transactions = new TransactionManager(this);
|
|
791
784
|
this.contracts = new ContractManager(this);
|
|
785
|
+
if (params.gas?.strategy) {
|
|
786
|
+
this.defaultGasStrategy = params.gas.strategy;
|
|
787
|
+
}
|
|
792
788
|
if (params.persistenceAdapter) {
|
|
793
789
|
this.persistenceManager = new PersistenceManager(
|
|
794
790
|
this,
|
|
@@ -801,7 +797,6 @@ var MentaClient = class {
|
|
|
801
797
|
}
|
|
802
798
|
/**
|
|
803
799
|
* The underlying RPC client used for blockchain interactions.
|
|
804
|
-
* @description This client is responsible for low-level communication with the RPC node.
|
|
805
800
|
*/
|
|
806
801
|
get rpc() {
|
|
807
802
|
if (!this._rpc) {
|
|
@@ -811,7 +806,6 @@ var MentaClient = class {
|
|
|
811
806
|
}
|
|
812
807
|
/**
|
|
813
808
|
* The account associated with the client.
|
|
814
|
-
* @description This account is used for signing transactions and interacting with the blockchain.
|
|
815
809
|
*/
|
|
816
810
|
get account() {
|
|
817
811
|
if (!this._account) {
|
|
@@ -819,25 +813,17 @@ var MentaClient = class {
|
|
|
819
813
|
}
|
|
820
814
|
return this._account;
|
|
821
815
|
}
|
|
816
|
+
/**
|
|
817
|
+
* Manager for gas estimation and configuration.
|
|
818
|
+
*/
|
|
819
|
+
get gas() {
|
|
820
|
+
if (!this._gas) {
|
|
821
|
+
throw new Error("Gas manager not initialized");
|
|
822
|
+
}
|
|
823
|
+
return this._gas;
|
|
824
|
+
}
|
|
822
825
|
/**
|
|
823
826
|
* Subscribes to a specific client event.
|
|
824
|
-
* @template TEvent The type of the event to listen for.
|
|
825
|
-
* @param {TEvent} event - The event to listen for (e.g., 'block', 'transaction').
|
|
826
|
-
* @param {GetListenerCallback<TEvent>} callback - The callback function to execute when the event is triggered.
|
|
827
|
-
* @returns {() => void} An unsubscribe function to stop listening to the event.
|
|
828
|
-
* @description This method allows the client to listen for real-time events from the blockchain, such as new blocks or transactions.
|
|
829
|
-
* @example
|
|
830
|
-
* import { MentaClient } from '@mentaproject/client';
|
|
831
|
-
*
|
|
832
|
-
* const client = new MentaClient({ url: 'http://rpcurlhere.com' });
|
|
833
|
-
*
|
|
834
|
-
* // Subscribe to new blocks
|
|
835
|
-
* const unsubscribe = client.on('block', (block) => {
|
|
836
|
-
* console.log('New block received:', block);
|
|
837
|
-
* });
|
|
838
|
-
*
|
|
839
|
-
* // To stop listening
|
|
840
|
-
* // unsubscribe();
|
|
841
827
|
*/
|
|
842
828
|
on(event, callback) {
|
|
843
829
|
const eventFunction = ClientEvents[event];
|
|
@@ -860,6 +846,7 @@ var MentaClient = class {
|
|
|
860
846
|
validatorAddress: this.params.validatorAddress
|
|
861
847
|
});
|
|
862
848
|
this._account = this.accounts.get(this.rpc.account.address);
|
|
849
|
+
this._gas = new GasManager(this.rpc);
|
|
863
850
|
}
|
|
864
851
|
};
|
|
865
852
|
|
|
@@ -960,14 +947,14 @@ var MemoryCache = class {
|
|
|
960
947
|
export {
|
|
961
948
|
Account,
|
|
962
949
|
AccountManager,
|
|
963
|
-
Action,
|
|
964
950
|
Block,
|
|
965
951
|
BlockManager,
|
|
966
952
|
ContractManager,
|
|
953
|
+
GasManager,
|
|
967
954
|
MemoryCache,
|
|
968
955
|
MentaClient,
|
|
969
|
-
Transaction,
|
|
970
956
|
TransactionManager,
|
|
957
|
+
TransactionRecord,
|
|
971
958
|
bigIntMax,
|
|
972
959
|
bigIntMin,
|
|
973
960
|
bigIntReviver,
|