Skip to content

Commit 12210ee

Browse files
authored
chore: improve TypeScript type definitions and documentation (#643)
* feat: improve TypeScript type definitions and documentation This commit enhances the TypeScript bindings with comprehensive type definitions and detailed JSDoc documentation for better developer experience. Changes: - Add explicit NetworkKind type with "ethereum", "opstack", and "linea" options - Add comprehensive JSDoc documentation for all public APIs - Document all methods with examples and parameter descriptions - Update Network enum to include all supported networks across all kinds - Fix network names to match actual values (e.g., OPTIMISM -> OP_MAINNET) - Add proper type exports for better IDE support - Bump package version to 0.9.0 to align with main crate version * fix: update documentation to use consensus RPC instead of verifiable API - Replace OPTIMISM references with OP_MAINNET in documentation - Update example to use consensus RPC endpoint for OP Stack networks - Use op-mainnet.operationsolarstorm.org as the consensus RPC example * refactor: convert Network enum to type union for consistency - Change Network from enum to type union matching NetworkKind pattern - Update documentation to show string literal values - Update examples to use string literals instead of enum values - Maintains all the same network values but as a cleaner type union
1 parent e12a4fc commit 12210ee

File tree

2 files changed

+237
-14
lines changed

2 files changed

+237
-14
lines changed

helios-ts/lib.ts

Lines changed: 235 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,74 @@ async function ensureInitialized() {
1111
await initPromise;
1212
}
1313

14+
/**
15+
* Supported network kinds for the Helios light client.
16+
*
17+
* @remarks
18+
* - `ethereum` - Standard Ethereum networks (mainnet, testnets)
19+
* - `opstack` - Optimism Stack based L2 networks
20+
* - `linea` - Linea L2 network
21+
*/
22+
export type NetworkKind = "ethereum" | "opstack" | "linea";
23+
1424
/**
1525
* Creates a new HeliosProvider instance.
16-
* An EIP-1193 compliant Ethereum provider. Treat this the same as you
17-
* would window.ethereum when constructing an ethers or web3 provider.
26+
*
27+
* @param config - Configuration object for the provider
28+
* @param kind - The type of network to connect to
29+
* @returns A promise that resolves to an initialized HeliosProvider instance
30+
*
31+
* @remarks
32+
* This function creates an EIP-1193 compliant Ethereum provider that can be used
33+
* with popular web3 libraries like ethers.js or web3.js. Treat this the same as
34+
* you would `window.ethereum` when constructing a provider.
35+
*
36+
* @example
37+
* ```typescript
38+
* const provider = await createHeliosProvider({
39+
* executionRpc: "https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY",
40+
* consensusRpc: "https://www.lightclientdata.org",
41+
* network: Network.MAINNET
42+
* }, "ethereum");
43+
* ```
1844
*/
19-
export async function createHeliosProvider(config: Config, kind: "ethereum" | "opstack"): Promise<HeliosProvider> {
45+
export async function createHeliosProvider(config: Config, kind: NetworkKind): Promise<HeliosProvider> {
2046
await ensureInitialized();
2147
return HeliosProvider.createInternal(config, kind);
2248
}
2349

24-
/// An EIP-1193 compliant Ethereum provider. Treat this the same as you
25-
/// would window.ethereum when constructing an ethers or web3 provider.
50+
/**
51+
* An EIP-1193 compliant Ethereum provider powered by the Helios light client.
52+
*
53+
* @remarks
54+
* HeliosProvider implements the Ethereum Provider API (EIP-1193) and can be used
55+
* as a drop-in replacement for `window.ethereum` in web3 applications. It provides
56+
* trustless access to Ethereum without relying on centralized RPC providers.
57+
*
58+
* The provider supports all standard Ethereum JSON-RPC methods and maintains
59+
* compatibility with popular libraries like ethers.js, web3.js, and viem.
60+
*
61+
* @example
62+
* ```typescript
63+
* // Using with ethers.js
64+
* import { BrowserProvider } from 'ethers';
65+
*
66+
* const heliosProvider = await createHeliosProvider(config, "ethereum");
67+
* const ethersProvider = new BrowserProvider(heliosProvider);
68+
*
69+
* // Using with web3.js
70+
* import Web3 from 'web3';
71+
*
72+
* const heliosProvider = await createHeliosProvider(config, "ethereum");
73+
* const web3 = new Web3(heliosProvider);
74+
* ```
75+
*/
2676
export class HeliosProvider {
2777
#client;
2878
#chainId;
2979
#eventEmitter;
3080

31-
private constructor(config: Config, kind: "ethereum" | "opstack") {
81+
private constructor(config: Config, kind: NetworkKind) {
3282
const executionRpc = config.executionRpc;
3383
const verifiableApi = config.verifiableApi;
3484

@@ -61,15 +111,58 @@ export class HeliosProvider {
61111
}
62112

63113
/** @internal */
64-
static createInternal(config: Config, kind: "ethereum" | "opstack"): HeliosProvider {
114+
static createInternal(config: Config, kind: NetworkKind): HeliosProvider {
65115
return new HeliosProvider(config, kind);
66116
}
67117

68118

119+
/**
120+
* Waits for the light client to sync with the network.
121+
*
122+
* @returns A promise that resolves when the client is fully synced
123+
*
124+
* @remarks
125+
* This method blocks until the light client has synchronized with the network
126+
* and is ready to process requests. It's recommended to call this before
127+
* making any RPC requests to ensure accurate data.
128+
*
129+
* @example
130+
* ```typescript
131+
* const provider = await createHeliosProvider(config, "ethereum");
132+
* await provider.waitSynced();
133+
* console.log("Provider is ready!");
134+
* ```
135+
*/
69136
async waitSynced() {
70137
await this.#client.wait_synced();
71138
}
72139

140+
/**
141+
* Sends an RPC request to the provider.
142+
*
143+
* @param req - The RPC request object containing method and params
144+
* @returns A promise that resolves with the RPC response
145+
* @throws {Error} If the RPC method is not supported or the request fails
146+
*
147+
* @remarks
148+
* This is the main entry point for all Ethereum JSON-RPC requests. It implements
149+
* the EIP-1193 provider interface and supports all standard Ethereum RPC methods.
150+
*
151+
* @example
152+
* ```typescript
153+
* // Get the latest block number
154+
* const blockNumber = await provider.request({
155+
* method: "eth_blockNumber",
156+
* params: []
157+
* });
158+
*
159+
* // Get account balance
160+
* const balance = await provider.request({
161+
* method: "eth_getBalance",
162+
* params: [address, "latest"]
163+
* });
164+
* ```
165+
*/
73166
async request(req: Request): Promise<any> {
74167
try {
75168
return await this.#req(req);
@@ -218,13 +311,61 @@ export class HeliosProvider {
218311
}
219312
}
220313

314+
/**
315+
* Registers an event listener for provider events.
316+
*
317+
* @param eventName - The name of the event to listen for
318+
* @param handler - The callback function to handle the event
319+
*
320+
* @remarks
321+
* Supports standard EIP-1193 provider events including:
322+
* - `message` - For subscription updates
323+
* - `connect` - When the provider connects
324+
* - `disconnect` - When the provider disconnects
325+
* - `chainChanged` - When the chain ID changes
326+
* - `accountsChanged` - When accounts change (if applicable)
327+
*
328+
* @example
329+
* ```typescript
330+
* provider.on("message", (message) => {
331+
* console.log("Received message:", message);
332+
* });
333+
*
334+
* // Subscribe to new blocks
335+
* const subId = await provider.request({
336+
* method: "eth_subscribe",
337+
* params: ["newHeads"]
338+
* });
339+
* ```
340+
*/
221341
on(
222342
eventName: string,
223343
handler: (data: any) => void
224344
): void {
225345
this.#eventEmitter.on(eventName, handler);
226346
}
227347

348+
/**
349+
* Removes an event listener from the provider.
350+
*
351+
* @param eventName - The name of the event to stop listening for
352+
* @param handler - The callback function to remove
353+
*
354+
* @remarks
355+
* Removes a previously registered event listener. The handler must be
356+
* the same function reference that was passed to `on()`.
357+
*
358+
* @example
359+
* ```typescript
360+
* const handler = (data) => console.log(data);
361+
*
362+
* // Add listener
363+
* provider.on("message", handler);
364+
*
365+
* // Remove listener
366+
* provider.removeListener("message", handler);
367+
* ```
368+
*/
228369
removeListener(
229370
eventName: string,
230371
handler: (data: any) => void
@@ -233,20 +374,102 @@ export class HeliosProvider {
233374
}
234375
}
235376

377+
/**
378+
* Configuration options for creating a Helios provider.
379+
*
380+
* @remarks
381+
* Different network kinds require different configuration options:
382+
* - For Ethereum networks: executionRpc, consensusRpc, and optionally checkpoint are required
383+
* - For OpStack networks: executionRpc and verifiableApi are required
384+
* - For Linea networks: executionRpc is required
385+
*/
236386
export type Config = {
387+
/**
388+
* The RPC endpoint for execution layer requests.
389+
* This is required for all network types.
390+
* @example "https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY"
391+
*/
237392
executionRpc?: string;
393+
394+
/**
395+
* The verifiable API endpoint for any networks.
396+
* Not recommended for use currently.
397+
* @example "https://verifiable-api-ethereum.operationsolarstorm.org
398+
*/
238399
verifiableApi?: string;
400+
401+
/**
402+
* The consensus layer RPC endpoint for Ethereum and OP Stack networks.
403+
* Required for Ethereum and OP Stack networks to sync.
404+
* @example "https://www.lightclientdata.org"
405+
*/
239406
consensusRpc?: string;
407+
408+
/**
409+
* A trusted checkpoint for faster initial sync on Ethereum networks.
410+
* Optional but recommended for better performance.
411+
* @example "0x1234567890abcdef..."
412+
*/
240413
checkpoint?: string;
414+
415+
/**
416+
* The network to connect to.
417+
* Defaults to Network.MAINNET for Ethereum networks.
418+
*/
241419
network?: Network;
242-
/** Where to cache checkpoints, default to "localstorage" */
420+
421+
/**
422+
* Where to cache checkpoints for persistence.
423+
* @defaultValue "localstorage"
424+
* @remarks
425+
* - `localstorage` - Store in browser's localStorage (web environments)
426+
* - `config` - Store in configuration (node environments)
427+
*/
243428
dbType?: "localstorage" | "config";
244429
};
245430

246-
export enum Network {
247-
MAINNET = "mainnet",
248-
GOERLI = "goerli",
249-
}
431+
/**
432+
* Supported networks across all network kinds for the Helios provider.
433+
*
434+
* @remarks
435+
* Networks are organized by their network kind:
436+
* - Ethereum networks: "mainnet", "sepolia", "holesky", "hoodi"
437+
* - OP Stack networks: "op-mainnet", "base", "worldchain", "zora", "unichain"
438+
* - Linea networks: "linea", "linea-sepolia"
439+
*
440+
* @example
441+
* ```typescript
442+
* // For Ethereum mainnet
443+
* const config: Config = {
444+
* executionRpc: "https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY",
445+
* consensusRpc: "https://www.lightclientdata.org",
446+
* network: "mainnet"
447+
* };
448+
*
449+
* // For Optimism
450+
* const config: Config = {
451+
* executionRpc: "https://mainnet.optimism.io",
452+
* consensusRpc: "https://op-mainnet.operationsolarstorm.org",
453+
* network: "op-mainnet"
454+
* };
455+
* ```
456+
*/
457+
export type Network =
458+
// Ethereum networks
459+
| "mainnet" // Ethereum mainnet (chain ID: 1)
460+
| "goerli" // Goerli testnet (deprecated)
461+
| "sepolia" // Sepolia testnet (chain ID: 11155111)
462+
| "holesky" // Holesky testnet (chain ID: 17000)
463+
| "hoodi" // Hoodi testnet (chain ID: 560048)
464+
// OP Stack networks
465+
| "op-mainnet" // OP Mainnet (chain ID: 10)
466+
| "base" // Base mainnet (chain ID: 8453)
467+
| "worldchain" // Worldchain mainnet (chain ID: 480)
468+
| "zora" // Zora mainnet (chain ID: 7777777)
469+
| "unichain" // Unichain mainnet (chain ID: 130)
470+
// Linea networks
471+
| "linea" // Linea mainnet (chain ID: 59144)
472+
| "linea-sepolia"; // Linea Sepolia testnet (chain ID: 59141)
250473

251474
type Request = {
252475
method: string;

helios-ts/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)