@mentaproject/client 0.1.24 → 0.1.25
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/structures/Account.d.ts +1 -1
- package/dist/structures/Account.js +20 -19
- package/dist/structures/MentaClient.d.ts +5 -2
- package/dist/structures/MentaClient.js +24 -5
- package/dist/types/MentaClient.d.ts +10 -2
- package/dist/types/MentaClient.js +0 -2
- package/package.json +3 -3
- package/src/structures/Account.ts +235 -190
- package/src/structures/MentaClient.ts +124 -99
- package/src/types/MentaClient.ts +46 -34
- package/transactions.json +0 -1302
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module MentaClient
|
|
3
3
|
*/
|
|
4
|
-
import type { CoreClient } from "@mentaproject/core/types";
|
|
5
|
-
import {
|
|
4
|
+
import type { CoreClient, MentaAccountClient } from "@mentaproject/core/types";
|
|
5
|
+
import { createMentaAccount } from "@mentaproject/core/clients";
|
|
6
6
|
|
|
7
7
|
import { BlockManager } from "../managers/BlockManager";
|
|
8
8
|
import { TransactionManager } from "../managers/TransactionManager";
|
|
9
9
|
import { ContractManager } from "../managers/ContractManager";
|
|
10
10
|
import { AccountManager } from "../managers/AccountManager";
|
|
11
11
|
import { PersistenceManager } from "../managers/PersistenceManager";
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
ClientEvents,
|
|
14
|
+
GetListenerCallback,
|
|
15
|
+
MentaClientConfig,
|
|
16
|
+
} from "../types/MentaClient";
|
|
13
17
|
import { withCache } from "../utils/withCache";
|
|
18
|
+
import { createClient } from "@mentaproject/core";
|
|
14
19
|
|
|
15
20
|
/**
|
|
16
21
|
* The main client for interacting with the Menta API.
|
|
@@ -21,107 +26,127 @@ import { withCache } from "../utils/withCache";
|
|
|
21
26
|
* simplifying common operations.
|
|
22
27
|
*/
|
|
23
28
|
export class MentaClient {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
protected _rpc?: MentaAccountClient;
|
|
30
|
+
/**
|
|
31
|
+
* The underlying RPC client used for blockchain interactions.
|
|
32
|
+
* @description This client is responsible for low-level communication with the RPC node.
|
|
33
|
+
*/
|
|
34
|
+
public get rpc(): MentaAccountClient {
|
|
35
|
+
if (!this._rpc) {
|
|
36
|
+
throw new Error("RPC client not initialized");
|
|
37
|
+
}
|
|
38
|
+
return this._rpc;
|
|
39
|
+
}
|
|
29
40
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Manager for block-related operations.
|
|
43
|
+
* @description Provides methods to query and interact with blockchain blocks.
|
|
44
|
+
*/
|
|
45
|
+
public blocks!: BlockManager;
|
|
46
|
+
/**
|
|
47
|
+
* Manager for transaction-related operations.
|
|
48
|
+
* @description Provides methods to send, query, and manage blockchain transactions.
|
|
49
|
+
*/
|
|
50
|
+
public transactions!: TransactionManager;
|
|
51
|
+
/**
|
|
52
|
+
* Manager for contract-related operations.
|
|
53
|
+
* @description Provides methods to deploy, interact with, and query smart contracts.
|
|
54
|
+
*/
|
|
55
|
+
public contracts!: ContractManager;
|
|
56
|
+
/**
|
|
57
|
+
* Manager for account-related operations.
|
|
58
|
+
* @description Provides methods to manage and interact with blockchain accounts.
|
|
59
|
+
*/
|
|
60
|
+
public accounts!: AccountManager;
|
|
50
61
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
62
|
+
/**
|
|
63
|
+
* Subscribes to a specific client event.
|
|
64
|
+
* @template TEvent The type of the event to listen for.
|
|
65
|
+
* @param {TEvent} event - The event to listen for (e.g., 'block', 'transaction').
|
|
66
|
+
* @param {GetListenerCallback<TEvent>} callback - The callback function to execute when the event is triggered.
|
|
67
|
+
* @returns {() => void} An unsubscribe function to stop listening to the event.
|
|
68
|
+
* @description This method allows the client to listen for real-time events from the blockchain, such as new blocks or transactions.
|
|
69
|
+
* @example
|
|
70
|
+
* import { MentaClient } from '@mentaproject/client';
|
|
71
|
+
*
|
|
72
|
+
* const client = new MentaClient({ url: 'http://rpcurlhere.com' });
|
|
73
|
+
*
|
|
74
|
+
* // Subscribe to new blocks
|
|
75
|
+
* const unsubscribe = client.on('block', (block) => {
|
|
76
|
+
* console.log('New block received:', block);
|
|
77
|
+
* });
|
|
78
|
+
*
|
|
79
|
+
* // To stop listening
|
|
80
|
+
* // unsubscribe();
|
|
81
|
+
*/
|
|
82
|
+
on<TEvent extends keyof typeof ClientEvents>(
|
|
83
|
+
event: TEvent,
|
|
84
|
+
callback: GetListenerCallback<TEvent>,
|
|
85
|
+
) {
|
|
86
|
+
const eventFunction = ClientEvents[event];
|
|
87
|
+
const capitalizedEvent = event.charAt(0).toUpperCase() + event.slice(1);
|
|
88
|
+
const params = {
|
|
89
|
+
[`on${capitalizedEvent}`]: callback,
|
|
90
|
+
pollingInterval: 1000,
|
|
91
|
+
};
|
|
81
92
|
|
|
82
|
-
|
|
93
|
+
return (eventFunction as any)(this.rpc, params);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Creates an instance of MentaClient.
|
|
98
|
+
* @param {MentaClientConfig} params - The configuration parameters for the client.
|
|
99
|
+
* @description The constructor initializes the RPC client and the various managers based on the provided configuration.
|
|
100
|
+
* It also applies caching and persistence if specified in the configuration.
|
|
101
|
+
* @example
|
|
102
|
+
* import { MentaClient } from '@mentaproject/client';
|
|
103
|
+
*
|
|
104
|
+
* // Basic initialization
|
|
105
|
+
* const client = new MentaClient({
|
|
106
|
+
* url: 'http://rpcurlhere.com',
|
|
107
|
+
* });
|
|
108
|
+
*
|
|
109
|
+
* // Initialization with a persistent cache
|
|
110
|
+
* const clientWithCache = new MentaClient({
|
|
111
|
+
* url: 'http://rpcurlhere.com',
|
|
112
|
+
* cache: {
|
|
113
|
+
* ttl: 60000, // 1 minute
|
|
114
|
+
* },
|
|
115
|
+
* });
|
|
116
|
+
*/
|
|
117
|
+
constructor(protected params: MentaClientConfig) {
|
|
118
|
+
this.blocks = new BlockManager(this);
|
|
119
|
+
this.transactions = new TransactionManager(this);
|
|
120
|
+
this.contracts = new ContractManager(this);
|
|
121
|
+
|
|
122
|
+
if (params.persistenceAdapter) {
|
|
123
|
+
this.persistenceManager = new PersistenceManager(
|
|
124
|
+
this,
|
|
125
|
+
params.persistenceAdapter,
|
|
126
|
+
);
|
|
127
|
+
this.accounts = new AccountManager(this, this.persistenceManager);
|
|
128
|
+
} else {
|
|
129
|
+
this.accounts = new AccountManager(this);
|
|
83
130
|
}
|
|
131
|
+
}
|
|
84
132
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
* @param {MentaClientConfig} params - The configuration parameters for the client.
|
|
88
|
-
* @description The constructor initializes the RPC client and the various managers based on the provided configuration.
|
|
89
|
-
* It also applies caching and persistence if specified in the configuration.
|
|
90
|
-
* @example
|
|
91
|
-
* import { MentaClient } from '@mentaproject/client';
|
|
92
|
-
*
|
|
93
|
-
* // Basic initialization
|
|
94
|
-
* const client = new MentaClient({
|
|
95
|
-
* url: 'http://rpcurlhere.com',
|
|
96
|
-
* });
|
|
97
|
-
*
|
|
98
|
-
* // Initialization with a persistent cache
|
|
99
|
-
* const clientWithCache = new MentaClient({
|
|
100
|
-
* url: 'http://rpcurlhere.com',
|
|
101
|
-
* cache: {
|
|
102
|
-
* ttl: 60000, // 1 minute
|
|
103
|
-
* },
|
|
104
|
-
* });
|
|
105
|
-
*/
|
|
106
|
-
constructor(params: MentaClientConfig) {
|
|
107
|
-
this.rpc = createClient(params) as CoreClient;
|
|
108
|
-
if (params.cache) this.rpc = withCache(this.rpc, params.cache);
|
|
133
|
+
async init() {
|
|
134
|
+
let coreClient = createClient(this.params) as CoreClient;
|
|
109
135
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
this.contracts = new ContractManager(this);
|
|
113
|
-
if (params.persistenceAdapter) {
|
|
114
|
-
this.persistenceManager = new PersistenceManager(this, params.persistenceAdapter);
|
|
115
|
-
this.accounts = new AccountManager(this, this.persistenceManager);
|
|
116
|
-
} else {
|
|
117
|
-
this.accounts = new AccountManager(this);
|
|
118
|
-
}
|
|
136
|
+
if (this.params.cache) {
|
|
137
|
+
coreClient = withCache(coreClient, this.params.cache);
|
|
119
138
|
}
|
|
120
139
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
140
|
+
this._rpc = await createMentaAccount(coreClient, {
|
|
141
|
+
signer: this.params.signer,
|
|
142
|
+
bundlerTransport: this.params.bundlerTransport,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Optional manager for persistence-related operations.
|
|
148
|
+
* @description This manager is initialized if a persistence adapter is provided in the client configuration,
|
|
149
|
+
* allowing for data storage and retrieval.
|
|
150
|
+
*/
|
|
151
|
+
public persistenceManager?: PersistenceManager;
|
|
152
|
+
}
|
package/src/types/MentaClient.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module MentaClientTypes
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
CoreClientConfig,
|
|
6
|
+
PasskeySigner,
|
|
7
|
+
Transport,
|
|
8
|
+
} from "@mentaproject/core/types";
|
|
5
9
|
import { ICache } from "./Cache";
|
|
6
10
|
import { watchBlockNumber, watchBlocks } from "@mentaproject/core/actions";
|
|
7
11
|
import { IPersistenceAdapter } from "./PersistenceAdapter";
|
|
@@ -11,13 +15,13 @@ import { IPersistenceAdapter } from "./PersistenceAdapter";
|
|
|
11
15
|
* @interface CacheConfig
|
|
12
16
|
*/
|
|
13
17
|
export interface CacheConfig {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
18
|
+
/**
|
|
19
|
+
* Time-to-live policies for different RPC methods.
|
|
20
|
+
* Keys are RPC method names (e.g., "eth_getBlockByNumber"), and values are
|
|
21
|
+
* the TTL in milliseconds for caching responses from those methods.
|
|
22
|
+
*/
|
|
23
|
+
ttl_policies: Record<string, number>;
|
|
24
|
+
}
|
|
21
25
|
|
|
22
26
|
/**
|
|
23
27
|
* Configuration options for the MentaClient.
|
|
@@ -25,33 +29,41 @@ export interface CacheConfig {
|
|
|
25
29
|
* @interface MentaClientConfig
|
|
26
30
|
*/
|
|
27
31
|
export interface MentaClientConfig extends CoreClientConfig {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Optional cache implementation for the client.
|
|
34
|
+
* If provided, the client will use this cache for RPC responses.
|
|
35
|
+
*/
|
|
36
|
+
cache?: ICache;
|
|
37
|
+
/**
|
|
38
|
+
* Optional persistence adapter for local data storage.
|
|
39
|
+
* If provided, the client will use this adapter to persist and retrieve data.
|
|
40
|
+
*/
|
|
41
|
+
persistenceAdapter?: IPersistenceAdapter;
|
|
42
|
+
/**
|
|
43
|
+
* Bundler transport used for userOperations
|
|
44
|
+
*/
|
|
45
|
+
bundlerTransport: Transport;
|
|
46
|
+
/**
|
|
47
|
+
* Passkey compatible signer used for signing user operations
|
|
48
|
+
*/
|
|
49
|
+
signer: PasskeySigner;
|
|
50
|
+
}
|
|
39
51
|
|
|
40
52
|
/**
|
|
41
53
|
* Defines the available client events and their corresponding watch functions.
|
|
42
54
|
* These functions are used to subscribe to real-time updates from the client.
|
|
43
55
|
*/
|
|
44
56
|
export const ClientEvents = {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Event for new blocks.
|
|
59
|
+
* Uses the {@link watchBlocks} function from `@mentaproject/core`.
|
|
60
|
+
*/
|
|
61
|
+
block: watchBlocks,
|
|
62
|
+
/**
|
|
63
|
+
* Event for new block numbers.
|
|
64
|
+
* Uses the {@link watchBlockNumber} function from `@mentaproject/core`.
|
|
65
|
+
*/
|
|
66
|
+
blockNumber: watchBlockNumber,
|
|
55
67
|
};
|
|
56
68
|
|
|
57
69
|
/**
|
|
@@ -60,8 +72,8 @@ export const ClientEvents = {
|
|
|
60
72
|
* @template TEvent The name of the event (e.g., 'block' or 'blockNumber') for which to get the listener callback type.
|
|
61
73
|
*/
|
|
62
74
|
export type GetListenerCallback<TEvent extends keyof typeof ClientEvents> =
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
75
|
+
Parameters<(typeof ClientEvents)[TEvent]>[1] extends infer P
|
|
76
|
+
? P extends { [K in `on${Capitalize<TEvent>}`]: infer TCallback }
|
|
77
|
+
? TCallback
|
|
78
|
+
: never
|
|
79
|
+
: never;
|