@mentaproject/client 0.1.23 → 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/managers/PersistenceManager.d.ts +3 -3
- package/dist/managers/TransactionManager.d.ts +1 -1
- package/dist/structures/Account.d.ts +1 -1
- package/dist/structures/Account.js +21 -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/dist/types/PersistenceAdapter.d.ts +3 -2
- package/package.json +4 -4
- package/src/index.ts +21 -0
- package/src/managers/AccountManager.ts +51 -0
- package/src/managers/BlockManager.ts +51 -0
- package/src/managers/ContractManager.ts +60 -0
- package/src/managers/PersistenceManager.ts +59 -0
- package/src/managers/TransactionManager.ts +92 -0
- package/src/managers/index.ts +4 -0
- package/src/structures/Account.ts +251 -0
- package/src/structures/Block.ts +185 -0
- package/src/structures/Cache.ts +126 -0
- package/src/structures/MentaClient.ts +152 -0
- package/src/structures/Transaction.ts +220 -0
- package/src/structures/index.ts +5 -0
- package/src/types/Account.ts +44 -0
- package/src/types/Block.ts +14 -0
- package/src/types/Cache.ts +55 -0
- package/src/types/JsonRPC.ts +126 -0
- package/src/types/MentaClient.ts +79 -0
- package/src/types/PersistenceAdapter.ts +44 -0
- package/src/types/Transaction.ts +52 -0
- package/src/types/Utils.ts +30 -0
- package/src/types/index.ts +8 -0
- package/src/utils/bigint.ts +59 -0
- package/src/utils/toJSON.ts +123 -0
- package/src/utils/withCache.ts +50 -0
- package/test.ts +0 -89
- package/transactions.json +0 -1302
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module MentaClient
|
|
3
|
+
*/
|
|
4
|
+
import type { CoreClient, MentaAccountClient } from "@mentaproject/core/types";
|
|
5
|
+
import { createMentaAccount } from "@mentaproject/core/clients";
|
|
6
|
+
|
|
7
|
+
import { BlockManager } from "../managers/BlockManager";
|
|
8
|
+
import { TransactionManager } from "../managers/TransactionManager";
|
|
9
|
+
import { ContractManager } from "../managers/ContractManager";
|
|
10
|
+
import { AccountManager } from "../managers/AccountManager";
|
|
11
|
+
import { PersistenceManager } from "../managers/PersistenceManager";
|
|
12
|
+
import {
|
|
13
|
+
ClientEvents,
|
|
14
|
+
GetListenerCallback,
|
|
15
|
+
MentaClientConfig,
|
|
16
|
+
} from "../types/MentaClient";
|
|
17
|
+
import { withCache } from "../utils/withCache";
|
|
18
|
+
import { createClient } from "@mentaproject/core";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The main client for interacting with the Menta API.
|
|
22
|
+
* It provides managers for blocks, transactions, contracts, and accounts.
|
|
23
|
+
*
|
|
24
|
+
* @description This class serves as the primary entry point for interacting with the Menta blockchain.
|
|
25
|
+
* It encapsulates the core RPC client and provides specialized managers for various blockchain entities,
|
|
26
|
+
* simplifying common operations.
|
|
27
|
+
*/
|
|
28
|
+
export class MentaClient {
|
|
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
|
+
}
|
|
40
|
+
|
|
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;
|
|
61
|
+
|
|
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
|
+
};
|
|
92
|
+
|
|
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);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async init() {
|
|
134
|
+
let coreClient = createClient(this.params) as CoreClient;
|
|
135
|
+
|
|
136
|
+
if (this.params.cache) {
|
|
137
|
+
coreClient = withCache(coreClient, this.params.cache);
|
|
138
|
+
}
|
|
139
|
+
|
|
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
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { AccessList, Address, Hash, Hex, Transaction as RawTransaction, WaitForTransactionReceiptReturnType } from '@mentaproject/core/types';
|
|
2
|
+
import { getBlock, getTransactionReceipt, traceTransaction, waitForTransactionReceipt } from '@mentaproject/core/actions';
|
|
3
|
+
import { hexToBigInt } from '@mentaproject/core/utils';
|
|
4
|
+
import { TransactionReceiptNotFoundError } from 'viem';
|
|
5
|
+
|
|
6
|
+
import { Account } from './Account';
|
|
7
|
+
import { Block } from './Block';
|
|
8
|
+
import { MentaClient } from './MentaClient';
|
|
9
|
+
|
|
10
|
+
import { toJSON } from '../utils/toJSON';
|
|
11
|
+
import { JSONTransaction, TransactionCall } from '../types';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Represents a blockchain transaction with its properties and methods.
|
|
15
|
+
*
|
|
16
|
+
* @description This class encapsulates the data and functionalities related to an Ethereum-like transaction,
|
|
17
|
+
* providing methods to interact with the blockchain to retrieve transaction-related information such as
|
|
18
|
+
* internal calls, receipts, and the block it belongs to.
|
|
19
|
+
*/
|
|
20
|
+
export class Transaction implements Omit<RawTransaction, "from" | "to"> {
|
|
21
|
+
/**
|
|
22
|
+
* @description The access list for the transaction.
|
|
23
|
+
*/
|
|
24
|
+
public accessList?: AccessList;
|
|
25
|
+
/**
|
|
26
|
+
* @description The blob versioned hashes.
|
|
27
|
+
*/
|
|
28
|
+
public blobVersionedHashes?: readonly Hex[];
|
|
29
|
+
/**
|
|
30
|
+
* @description The gas limit for the transaction.
|
|
31
|
+
*/
|
|
32
|
+
public gas: bigint;
|
|
33
|
+
/**
|
|
34
|
+
* @description The gas price for the transaction.
|
|
35
|
+
*/
|
|
36
|
+
public gasPrice?: bigint;
|
|
37
|
+
/**
|
|
38
|
+
* @description The hash of the transaction.
|
|
39
|
+
*/
|
|
40
|
+
public hash: Hash;
|
|
41
|
+
/**
|
|
42
|
+
* @description The input data for the transaction.
|
|
43
|
+
*/
|
|
44
|
+
public input: Hex;
|
|
45
|
+
/**
|
|
46
|
+
* @description The maximum fee per gas.
|
|
47
|
+
*/
|
|
48
|
+
public maxFeePerGas?: bigint;
|
|
49
|
+
/**
|
|
50
|
+
* @description The maximum priority fee per gas.
|
|
51
|
+
*/
|
|
52
|
+
public maxPriorityFeePerGas?: bigint;
|
|
53
|
+
/**
|
|
54
|
+
* @description The nonce of the transaction.
|
|
55
|
+
*/
|
|
56
|
+
public nonce: number;
|
|
57
|
+
/**
|
|
58
|
+
* @description The r value of the signature.
|
|
59
|
+
*/
|
|
60
|
+
public r: Hex;
|
|
61
|
+
/**
|
|
62
|
+
* @description The s value of the signature.
|
|
63
|
+
*/
|
|
64
|
+
public s: Hex;
|
|
65
|
+
/**
|
|
66
|
+
* @description The index of the transaction within the block.
|
|
67
|
+
*/
|
|
68
|
+
public transactionIndex: number | null;
|
|
69
|
+
/**
|
|
70
|
+
* @description The type of the transaction.
|
|
71
|
+
*/
|
|
72
|
+
public type: "legacy" | "eip2930" | "eip1559" | "eip4844" | "eip7702";
|
|
73
|
+
/**
|
|
74
|
+
* @description The v value of the signature.
|
|
75
|
+
*/
|
|
76
|
+
public v: bigint;
|
|
77
|
+
/**
|
|
78
|
+
* @description The value transferred in the transaction.
|
|
79
|
+
*/
|
|
80
|
+
public value: bigint;
|
|
81
|
+
/**
|
|
82
|
+
* @description The y parity of the signature.
|
|
83
|
+
*/
|
|
84
|
+
public yParity?: number;
|
|
85
|
+
/**
|
|
86
|
+
* @description The hash of the block containing this transaction.
|
|
87
|
+
*/
|
|
88
|
+
public blockHash: Hash | null;
|
|
89
|
+
/**
|
|
90
|
+
* @description The number of the block containing this transaction.
|
|
91
|
+
*/
|
|
92
|
+
public blockNumber: bigint | null;
|
|
93
|
+
/**
|
|
94
|
+
* @description The hex representation of the type of the transaction.
|
|
95
|
+
*/
|
|
96
|
+
public typeHex: `0x${string}` | null;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @description The sender account of the transaction.
|
|
100
|
+
*/
|
|
101
|
+
public from?: Account;
|
|
102
|
+
/**
|
|
103
|
+
* @description The recipient account of the transaction.
|
|
104
|
+
*/
|
|
105
|
+
public to?: Account;
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Creates an instance of Transaction.
|
|
110
|
+
* @description Initializes a new Transaction instance with the provided RPC client and transaction data.
|
|
111
|
+
* @param {MentaClient} client - The instance of MentaClient used to interact with the blockchain.
|
|
112
|
+
* @param {ITransactionData} data The raw transaction data.
|
|
113
|
+
*/
|
|
114
|
+
constructor(protected client: MentaClient, data: RawTransaction) {
|
|
115
|
+
this.accessList = data.accessList;
|
|
116
|
+
this.blobVersionedHashes = data.blobVersionedHashes;
|
|
117
|
+
this.gas = data.gas;
|
|
118
|
+
this.gasPrice = data.gasPrice;
|
|
119
|
+
this.hash = data.hash;
|
|
120
|
+
this.input = data.input;
|
|
121
|
+
this.maxFeePerGas = data.maxFeePerGas;
|
|
122
|
+
this.maxPriorityFeePerGas = data.maxPriorityFeePerGas;
|
|
123
|
+
this.nonce = data.nonce;
|
|
124
|
+
this.r = data.r;
|
|
125
|
+
this.s = data.s;
|
|
126
|
+
this.transactionIndex = data.transactionIndex;
|
|
127
|
+
this.type = data.type;
|
|
128
|
+
this.v = data.v;
|
|
129
|
+
this.value = data.value;
|
|
130
|
+
this.yParity = data.yParity;
|
|
131
|
+
this.blockHash = data.blockHash;
|
|
132
|
+
this.blockNumber = data.blockNumber;
|
|
133
|
+
this.typeHex = data.typeHex;
|
|
134
|
+
|
|
135
|
+
this.from = data.from ? this.client.accounts.get(data.from) : undefined;
|
|
136
|
+
this.to = data.to ? this.client.accounts.get(data.to) : undefined;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Retrieves the internal calls made by this transaction.
|
|
141
|
+
* @description Fetches and processes the transaction traces to extract internal call details.
|
|
142
|
+
* @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.
|
|
143
|
+
*/
|
|
144
|
+
public async calls(): Promise<TransactionCall[] | undefined> {
|
|
145
|
+
const traces = await traceTransaction(this.client.rpc, this.hash);
|
|
146
|
+
if (!traces) return undefined;
|
|
147
|
+
|
|
148
|
+
const calls = traces.filter(t => t.type === "call" && t.action !== undefined);
|
|
149
|
+
|
|
150
|
+
return calls.map(c => ({
|
|
151
|
+
from: new Account(this.client, { address: c.action!.from as Address }),
|
|
152
|
+
to: new Account(this.client, { address: c.action!.to as Address }),
|
|
153
|
+
value: hexToBigInt(c.action!.value as Hex),
|
|
154
|
+
input: c.action!.input as Hex,
|
|
155
|
+
gas: hexToBigInt(c.action!.gas as Hex),
|
|
156
|
+
callType: c.action!.callType,
|
|
157
|
+
})) as TransactionCall[];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Waits for the transaction receipt to be available.
|
|
162
|
+
* @description Asynchronously waits for the transaction to be confirmed on the blockchain and its receipt to be available.
|
|
163
|
+
* @param {number} [confirmations] The number of confirmations to wait for. Defaults to 1.
|
|
164
|
+
* @returns {Promise<WaitForTransactionReceiptReturnType>} A promise that resolves to the transaction receipt once the specified number of confirmations are met.
|
|
165
|
+
*/
|
|
166
|
+
async waitForReceipt(confirmations?: number): Promise<WaitForTransactionReceiptReturnType> {
|
|
167
|
+
return await waitForTransactionReceipt(this.client.rpc, {
|
|
168
|
+
hash: this.hash,
|
|
169
|
+
confirmations: confirmations || 1,
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Retrieves the transaction receipt.
|
|
175
|
+
* @description Fetches the transaction receipt for the current transaction hash.
|
|
176
|
+
* @returns {Promise<WaitForTransactionReceiptReturnType>} A promise that resolves to the transaction receipt.
|
|
177
|
+
*/
|
|
178
|
+
async receipt(): Promise<WaitForTransactionReceiptReturnType | undefined> {
|
|
179
|
+
try {
|
|
180
|
+
return await getTransactionReceipt(this.client.rpc, { hash: this.hash });
|
|
181
|
+
} catch (error) {
|
|
182
|
+
if (error instanceof TransactionReceiptNotFoundError) {
|
|
183
|
+
return undefined;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Retrieves the block containing this transaction.
|
|
190
|
+
* @description Fetches the block data using the transaction's block hash and returns a Block instance.
|
|
191
|
+
* @returns {Promise<Block>} A promise that resolves to a Block instance representing the block that includes this transaction.
|
|
192
|
+
*/
|
|
193
|
+
async block(): Promise<Block> {
|
|
194
|
+
const data = await getBlock(this.client.rpc, { blockHash: this.blockHash! });
|
|
195
|
+
return new Block(this.client, data);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Converts the Transaction instance to a JSON representation.
|
|
200
|
+
* @description Serializes the transaction instance into a JSON object, including nested representations of related entities like block, receipt, and internal calls based on the specified depth.
|
|
201
|
+
* @param {number} [depth=1] The depth of the conversion. Controls how deeply nested objects are serialized.
|
|
202
|
+
* @returns {Promise<object>} A promise that resolves to the JSON representation of the transaction.
|
|
203
|
+
*/
|
|
204
|
+
async toJSON<D extends number = 1>(depth: D): Promise<JSONTransaction<D>> {
|
|
205
|
+
return await toJSON({
|
|
206
|
+
obj: {
|
|
207
|
+
...this,
|
|
208
|
+
block: this.block,
|
|
209
|
+
receipt: this.receipt,
|
|
210
|
+
calls: async () => {
|
|
211
|
+
const calls = await this.calls();
|
|
212
|
+
if (!calls) return undefined;
|
|
213
|
+
|
|
214
|
+
return Promise.all(calls.map(async c => await toJSON({ obj: c, depth: depth })));
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
depth
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module AccountTypes
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Address } from "@mentaproject/core";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Defines the options for retrieving transactions associated with an account.
|
|
9
|
+
*/
|
|
10
|
+
export type GetTransactionsParams = FetchTransactionParams & {
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Parameters for retrieving transactions emitted by an account by exploring blocks.
|
|
15
|
+
* @interface IGetTransactionReceiptParams
|
|
16
|
+
*/
|
|
17
|
+
export type FetchTransactionParams = {
|
|
18
|
+
/**
|
|
19
|
+
* The block number to start exploring from.
|
|
20
|
+
* @type {bigint}
|
|
21
|
+
*/
|
|
22
|
+
fromBlock?: bigint;
|
|
23
|
+
/**
|
|
24
|
+
* The block number to stop exploring at.
|
|
25
|
+
* @type {bigint}
|
|
26
|
+
*/
|
|
27
|
+
toBlock?: bigint;
|
|
28
|
+
/**
|
|
29
|
+
* The maximum number of items to retrieve.
|
|
30
|
+
* @type {number}
|
|
31
|
+
*/
|
|
32
|
+
limit?: number;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type AccountData = {
|
|
36
|
+
address: Address;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type JSONAccount<depth extends number> = depth extends 0 ? AccountData : AccountData & {
|
|
40
|
+
isContract: boolean;
|
|
41
|
+
ETHBalance: string;
|
|
42
|
+
contractType: string;
|
|
43
|
+
transactionCount: number;
|
|
44
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Address, Hash, Hex, Transaction as RawTransaction, Withdrawal } from "@mentaproject/core/types";
|
|
2
|
+
import { Block as RawBlock } from "@mentaproject/core";
|
|
3
|
+
import { JSONAccount } from "./Account"
|
|
4
|
+
import { WithBigintStringified } from "./Utils";
|
|
5
|
+
import { JSONTransaction } from "./Transaction";
|
|
6
|
+
|
|
7
|
+
export type JSONBlock<depth extends number> = depth extends 0
|
|
8
|
+
? WithBigintStringified<Omit<RawBlock, "miner" | "transactions">> & {
|
|
9
|
+
transactions?: JSONTransaction<depth>[] | Hash[],
|
|
10
|
+
}
|
|
11
|
+
: WithBigintStringified<Omit<RawBlock, "miner" | "transactions">> & {
|
|
12
|
+
miner: JSONAccount<depth>,
|
|
13
|
+
transactions?: JSONTransaction<depth>[] | Hash[],
|
|
14
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Methods } from "./JsonRPC";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @interface ICacheEntry
|
|
5
|
+
* Represents a single entry in the cache.
|
|
6
|
+
* @template T The type of the cached value.
|
|
7
|
+
*/
|
|
8
|
+
export interface ICacheEntry<T> {
|
|
9
|
+
/** The cached value. */
|
|
10
|
+
value: T;
|
|
11
|
+
/** The block number at which this entry expires. */
|
|
12
|
+
expiry: bigint;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @interface ICacheConfig
|
|
17
|
+
* Defines the configuration options for a cache.
|
|
18
|
+
*/
|
|
19
|
+
export interface ICacheConfig {
|
|
20
|
+
/** The maximum number of items the cache can hold. */
|
|
21
|
+
maxSize: number;
|
|
22
|
+
/** The default time-to-live (in blocks) for cache entries if no specific policy is defined. */
|
|
23
|
+
defaultTtl: number;
|
|
24
|
+
/** A map of RPC methods to their specific time-to-live policies (in blocks). */
|
|
25
|
+
ttlPolicies: Partial<Record<Methods, number>>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @interface ICache
|
|
30
|
+
* Defines the interface for a cache implementation.
|
|
31
|
+
*/
|
|
32
|
+
export interface ICache {
|
|
33
|
+
/** The configuration for the cache. */
|
|
34
|
+
config: ICacheConfig;
|
|
35
|
+
/**
|
|
36
|
+
* Sets the current block number for TTL calculations.
|
|
37
|
+
* @param {bigint} blockNumber The new current block number.
|
|
38
|
+
*/
|
|
39
|
+
setBlockNumber(blockNumber: bigint): void;
|
|
40
|
+
/**
|
|
41
|
+
* Retrieves a value from the cache.
|
|
42
|
+
* @template T The type of the value.
|
|
43
|
+
* @param {string} key The key for the cache entry.
|
|
44
|
+
* @returns {T | undefined} The cached value, or undefined if not found or expired.
|
|
45
|
+
*/
|
|
46
|
+
get<T>(key: string): T | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Sets a value in the cache.
|
|
49
|
+
* @template T The type of the value.
|
|
50
|
+
* @param {string} key The key for the cache entry.
|
|
51
|
+
* @param {T} value The value to store.
|
|
52
|
+
* @param {number} [ttl] The time-to-live (in blocks) for the entry. If not provided, `defaultTtl` from config is used.
|
|
53
|
+
*/
|
|
54
|
+
set<T>(key: string, value: T, ttl?: number): void;
|
|
55
|
+
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module JsonRPC
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Defines standard Ethereum JSON-RPC methods.
|
|
7
|
+
*/
|
|
8
|
+
export type StandardMethods =
|
|
9
|
+
/** Returns a list of addresses owned by client. */
|
|
10
|
+
| "eth_accounts"
|
|
11
|
+
/** Returns the current blob base fee. */
|
|
12
|
+
| "eth_blobBaseFee"
|
|
13
|
+
/** Returns the number of most recent block. */
|
|
14
|
+
| "eth_blockNumber"
|
|
15
|
+
/** Executes a new message call immediately without creating a transaction on the blockchain. */
|
|
16
|
+
| "eth_call"
|
|
17
|
+
/** Returns the current chain ID. */
|
|
18
|
+
| "eth_chainId"
|
|
19
|
+
/** Creates an EIP-2930 type access list for a transaction. */
|
|
20
|
+
| "eth_createAccessList"
|
|
21
|
+
/** Generates and returns an estimate of how much gas is necessary to allow the transaction to complete. */
|
|
22
|
+
| "eth_estimateGas"
|
|
23
|
+
/** Returns a historical log of gas prices. */
|
|
24
|
+
| "eth_feeHistory"
|
|
25
|
+
/** Returns the current price per gas in wei. */
|
|
26
|
+
| "eth_gasPrice"
|
|
27
|
+
/** Returns the balance of the account of given address. */
|
|
28
|
+
| "eth_getBalance"
|
|
29
|
+
/** Returns information about a block by hash. */
|
|
30
|
+
| "eth_getBlockByHash"
|
|
31
|
+
/** Returns information about a block by block number. */
|
|
32
|
+
| "eth_getBlockByNumber"
|
|
33
|
+
/** Returns all transaction receipts for a given block. */
|
|
34
|
+
| "eth_getBlockReceipts"
|
|
35
|
+
/** Returns the number of transactions in a block from a block matching the given block hash. */
|
|
36
|
+
| "eth_getBlockTransactionCountByHash"
|
|
37
|
+
/** Returns the number of transactions in a block from a block matching the given block number. */
|
|
38
|
+
| "eth_getBlockTransactionCountByNumber"
|
|
39
|
+
/** Returns the code at a given address. */
|
|
40
|
+
| "eth_getCode"
|
|
41
|
+
/** Returns an array of all logs matching a given filter object. */
|
|
42
|
+
| "eth_getLogs"
|
|
43
|
+
/** Returns the account and storage proof for the specified address and storage points. */
|
|
44
|
+
| "eth_getProof"
|
|
45
|
+
/** Returns the value from a storage position at a given address. */
|
|
46
|
+
| "eth_getStorageAt"
|
|
47
|
+
/** Returns information about a transaction by block hash and transaction index position. */
|
|
48
|
+
| "eth_getTransactionByBlockHashAndIndex"
|
|
49
|
+
/** Returns information about a transaction by block number and transaction index position. */
|
|
50
|
+
| "eth_getTransactionByBlockNumberAndIndex"
|
|
51
|
+
/** Returns the information about a transaction requested by transaction hash. */
|
|
52
|
+
| "eth_getTransactionByHash"
|
|
53
|
+
/** Returns the number of transactions sent from an address. */
|
|
54
|
+
| "eth_getTransactionCount"
|
|
55
|
+
/** Returns the receipt of a transaction by transaction hash. */
|
|
56
|
+
| "eth_getTransactionReceipt"
|
|
57
|
+
/** Returns information about an uncle of a block by hash and uncle index position. */
|
|
58
|
+
| "eth_getUncleByBlockHashAndIndex"
|
|
59
|
+
/** Returns information about an uncle of a block by number and uncle index position. */
|
|
60
|
+
| "eth_getUncleByBlockNumberAndIndex"
|
|
61
|
+
/** Returns the number of uncles in a block from a block matching the given block hash. */
|
|
62
|
+
| "eth_getUncleCountByBlockHash"
|
|
63
|
+
/** Returns the number of uncles in a block from a block matching the given block number. */
|
|
64
|
+
| "eth_getUncleCountByBlockNumber"
|
|
65
|
+
/** Returns the hash of the current block, the seedHash, and the boundary condition to be met. */
|
|
66
|
+
| "eth_getWork"
|
|
67
|
+
/** Returns the number of hashes per second that the node is mining with. */
|
|
68
|
+
| "eth_hashrate"
|
|
69
|
+
/** Returns a fee per gas that a user is willing to pay for their transaction to be included in the current block. */
|
|
70
|
+
| "eth_maxPriorityFeePerGas"
|
|
71
|
+
/** Returns true if client is actively mining new blocks. */
|
|
72
|
+
| "eth_mining"
|
|
73
|
+
/** Returns the current Ethereum protocol version. */
|
|
74
|
+
| "eth_protocolVersion"
|
|
75
|
+
/** Submits a signed transaction for inclusion in the blockchain. */
|
|
76
|
+
| "eth_sendRawTransaction"
|
|
77
|
+
/** Simulates a transaction execution. */
|
|
78
|
+
| "eth_simulateV1"
|
|
79
|
+
/** Used for submitting a proof-of-work solution. */
|
|
80
|
+
| "eth_submitWork"
|
|
81
|
+
/** Returns an object with data about the sync status or false. */
|
|
82
|
+
| "eth_syncing";
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Defines JSON-RPC methods related to filters.
|
|
86
|
+
*/
|
|
87
|
+
export type filterMethods =
|
|
88
|
+
/** Polling method for a filter, which returns an array of logs or hashes. */
|
|
89
|
+
| "eth_getFilterChanges"
|
|
90
|
+
/** Returns an array of all logs matching a given filter id. */
|
|
91
|
+
| "eth_getFilterLogs"
|
|
92
|
+
/** Creates a filter in the node, to notify when a new block arrives. */
|
|
93
|
+
| "eth_newBlockFilter"
|
|
94
|
+
/** Creates a filter object, based on filter options, to notify when the state changes (logs). */
|
|
95
|
+
| "eth_newFilter"
|
|
96
|
+
/** Uninstalls a filter with given id. */
|
|
97
|
+
| "eth_uninstallFilter";
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Defines JSON-RPC methods related to subscriptions.
|
|
101
|
+
*/
|
|
102
|
+
export type subscriptionMethods =
|
|
103
|
+
/** Subscribes to events. */
|
|
104
|
+
| "eth_subscribe"
|
|
105
|
+
/** Unsubscribes from events. */
|
|
106
|
+
| "eth_unsubscribe";
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Defines JSON-RPC methods related to tracing.
|
|
110
|
+
*/
|
|
111
|
+
export type traceMethods =
|
|
112
|
+
/** Returns traces for a given block. */
|
|
113
|
+
| "trace_block"
|
|
114
|
+
/** Returns a trace of the given transaction. */
|
|
115
|
+
| "trace_call"
|
|
116
|
+
/** Returns traces for multiple transactions. */
|
|
117
|
+
| "trace_callMany"
|
|
118
|
+
/** Returns traces matching the given filter. */
|
|
119
|
+
| "trace_filter"
|
|
120
|
+
/** Returns a trace of the given transaction. */
|
|
121
|
+
| "trace_transaction";
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Union type of all supported JSON-RPC methods.
|
|
125
|
+
*/
|
|
126
|
+
export type Methods = StandardMethods | filterMethods | subscriptionMethods | traceMethods;
|