Skip to content

Commit b334abc

Browse files
committed
✨ Update to @erpc-cloud/config package, cleanup generated types, import builder
1 parent 29ac156 commit b334abc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+924
-1725
lines changed

.changeset/rich-cheetahs-retire.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@konfeature/erpc-config-generator": minor
3+
---
4+
5+
- Introduce the builder pattern using `initErpcConfig`
6+
- Remove auto generated type and move to the exported type of `@erpc-cloud/config`
7+
- Review upstreams builder helpers for a wider support of the matching pattern
8+
- Remove YAML file generation, since erpc now support js/ts config file
9+
- Remove free rpc benchmark for now, currently envisaging multiple way to re-introduce it later, maybe using dynamic config or the selection eval policy?

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,6 @@ dist
173173

174174
# Finder (MacOS) folder config
175175
.DS_Store
176+
177+
# Example output
178+
example/*.js

README.md

Lines changed: 78 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,109 @@
22

33
A TypeScript-based configuration builder for [eRPC](https://github.com/erpc/erpc), the RPC load balancer for blockchain.
44

5-
> ⚠️ **Deprecation**: This project is slowly being moved directly inside [eRPC](https://github.com/erpc/erpc).
6-
> [ERPC config package](https://www.npmjs.com/package/@erpc-cloud/config)
5+
It now used the exported [`@erpc-cloud/config`](https://github.com/erpc/erpc) package from eRPC for all the types.
6+
7+
This package add the builder pattern and provide a more type safe way to create your configuration, with a few static cli helpers.
78

8-
- [x] Simple typescript config
9-
- [ ] https://github.com/erpc/erpc/pull/106 (Typescript chained builder pattern + strong types)
10-
- [ ] OxLib support for strongly typed rpc methods / evm related stuff
11-
- [ ] Helpers implementation (networks, upstreams, auth, caching, failsafe, etc)
12-
- [ ] And much more to come 👀
139

1410
## 🚧 Work in Progress
1511

1612
This project is currently under active development. Features and API may change.
1713

1814
## Features
1915

20-
- [x] TypeScript types for eRPC config
21-
- [x] Write YAML config file for eRPC config
22-
- [x] Helpers for networks, upstream, and auth configurations
16+
- [x] Builder pattern with type safe rate limits
17+
- [x] Helpers for networks, upstream
2318
- [x] Support for various blockchain networks and RPC providers
24-
- [x] Automatic generation of free RPC upstreams using `evm+free` type
2519
- [x] Command to fetch best free RPC URLs for a given chain
26-
- [ ] More stuff within the CLI (config validity check, docker file generation, version specification etc)
27-
- [ ] Cleaner rate limit configuration and auto completion
28-
- [ ] Builder pattern with chaining (like `createProject(...).addRateLimits(...)/* a few more steps */.write(...)`)
20+
- [x] CLI with pre validation + bundling command
21+
- [x] Matcher pattern for rpc methods
22+
- [ ] Automatic generation of free RPC upstreams using `evm+free` type
23+
- [ ] More stuff within the CLI (docker file generation, initial config file, cli config updates)
24+
- [ ] And much more to come 👀
2925

3026
## Installation
3127

3228
```bash
33-
bun add @konfeature/erpc-config-generator
29+
bun add @konfeature/erpc-config-generator @erpc-cloud/config
3430
```
3531

3632
## Usage
3733

3834
1. Create a TypeScript file (e.g., `erpc-config.ts`) that exports a full `Config` object as default:
3935

4036
```typescript
41-
import { Config, buildEvmNetworks, buildRateLimit, buildAlchemyUpstream, buildProject, envVariable } from '@konfeature/erpc-config-generator';
42-
43-
// ... (configuration setup)
44-
45-
const config: Config = {
46-
// Your eRPC configuration
47-
};
48-
49-
export default config;
37+
import { initErpcConfig } from "@konfeature/erpc-config-generator";
38+
39+
export default initErpcConfig({
40+
logLevel: "info",
41+
})
42+
.addRateLimiters({
43+
// Rate limiter budget for the porject
44+
projectBudget: [
45+
{
46+
method: "*",
47+
period: "1s",
48+
waitTime: "1m",
49+
maxCount: 1000,
50+
},
51+
],
52+
// Rate limiter for the upstream
53+
upstreamBudget: [
54+
{
55+
method: "*",
56+
period: "1s",
57+
waitTime: "1m",
58+
maxCount: 1000,
59+
},
60+
],
61+
})
62+
.addProject({
63+
// Project with one upstream
64+
id: "main-project",
65+
upstreams: [
66+
{
67+
id: "upstream-1",
68+
endpoint: "http://localhost:3000",
69+
rateLimitBudget: "upstreamBudget",
70+
},
71+
],
72+
rateLimitBudget: "projectBudget",
73+
})
74+
.build()
5075
```
5176

52-
2. Run the CLI command to generate the YAML configuration:
77+
> Note the `.build()` at the end, it's mandatory for it to export nicely and be recognized by the CLI.
78+
79+
2. Run the CLI command to generate the bundled JS configuration:
5380

5481
```bash
5582
bunx erpc-config
5683
```
5784

58-
This will read the `erpc-config.ts` file in the current directory and output the configuration to `erpc.yaml`.
85+
This will read the `erpc-config.ts` file in the current directory and output the configuration to `erpc.js`.
86+
87+
> **Note:** You can bundle the configuration with the bundler of your choice (bun build, esbuild, terser, etc.)
5988
6089
## CLI Usage
6190

6291
The eRPC Config Generator CLI provides two main commands: `generate` (default) and `validate`.
6392

64-
### Generate Command
93+
### Package Command
6594

66-
Generate an eRPC YAML config file from the TypeScript config:
95+
Package an eRPC JS config file from the TypeScript config:
6796

6897
```
6998
erpc-config [options]
7099
```
71100
or
72101
```
73-
erpc-config generate [options]
102+
erpc-config package [options]
74103
```
75104

76105
Options:
77106
--config The path to the config file (default: "./erpc-config.ts")
78-
--out The output file path (default: "./erpc.yaml")
107+
--out The output file path (default: "./erpc.js")
79108

80109
Examples:
81110

@@ -84,12 +113,11 @@ Examples:
84113
bun erpc-config
85114

86115
# Specify custom input and output paths
87-
bun erpc-config --config ./configs/my-erpc-config.ts --out ./configs/my-erpc-config.yaml
116+
bun erpc-config --config ./configs/my-erpc-config.ts --out ./configs/my-erpc-config.js
88117

89-
# Explicitly use the generate command
90-
bun erpc-config generate --config ./custom-config.ts
118+
# Explicitly use the package command
119+
bun erpc-config package --config ./custom-config.ts
91120
```
92-
Note: When using `evm+free` upstream type, the generator will automatically fetch and include the 15 best free RPC URLs for the specified chain in the output YAML.
93121

94122
### Validate Command
95123

@@ -112,24 +140,6 @@ bun erpc-config validate
112140
bun erpc-config validate --config ./configs/my-erpc-config.ts
113141
```
114142

115-
### Free RPC Command
116-
117-
Fetch the 15 best free RPC URLs for a given chain:
118-
119-
```
120-
erpc-config free-rpc --chain-id <chainId>
121-
```
122-
123-
Options:
124-
--chain-id The chain ID to fetch free RPC URLs for (required)
125-
126-
Example:
127-
128-
```bash
129-
# Fetch free RPC URLs for Ethereum mainnet (chain ID 1)
130-
bun erpc-config free-rpc --chain-id 1
131-
```
132-
133143
### General CLI Options
134144

135145
- `--help`, `-h`: Show help information
@@ -139,13 +149,18 @@ You can run these commands using `bun`, `npm`, or any other package runner of yo
139149

140150
## API Overview
141151

142-
### Environment Variables
152+
### Builder Methods
153+
154+
- **`initErpcConfig(config: InitConfig)`**: Initializes the configuration with basic settings such as `logLevel` and `server` properties.
143155

144-
- `envVariable(name: string)`: Helper function to reference environment variables in the config.
156+
- **`addRateLimiters(budgets: Record\<TKeys,RateLimitRuleConfig[]>)`**: Adds rate limiters to your configuration.
145157

146-
### Rate Limits
158+
- **`decorate(scope: "upstreams" | "networks", value: Record\<TStoreKey, NetworkConfig | UpstreamConfig>)`**: Adds values to the store for `networks` or `upstreams`.
147159

148-
- `buildRateLimit<TRpc extends RpcSchema = EIP1474Methods>({ id, rules })`: Create a rate limit configuration with customizable RPC schema, for type completion.
160+
- **Scope**: Determines which store (`networks` or `upstreams`) to push values to.
161+
- **Value**: The static values to add the the builder store
162+
163+
- **`addProject(project: ProjectConfig | ({config: Config, store: Store} => ProjectConfig))`**: Adds a project to your configuration.
149164

150165
### Networks
151166

@@ -159,46 +174,22 @@ You can run these commands using `bun`, `npm`, or any other package runner of yo
159174
- `buildEvmUpstream<TRpc extends RpcSchema>({ id, endpoint, ...options })`: Configure a generic EVM upstream provider with customizable RPC schema.
160175
- `buildFreeUpstreams({ chains, ...options})`: Configure placeholder upstreams that will be filled with the 15 best free RPCs for the specified chain.
161176

162-
### Projects
163-
164-
- `buildProject(projectConfig)`: Create a project configuration.
165-
166-
### Authentication
167-
168-
- `buildNetworkAuthStrategy<TRpc extends RpcSchema>({ network, ...baseOptions })`: Configure network-based authentication.
169-
- `buildSecretAuthStrategy<TRpc extends RpcSchema>({ secret, ...baseOptions })`: Configure secret-based authentication.
170-
- `buildJwtAuthStrategy<TRpc extends RpcSchema>({ jwt, ...baseOptions })`: Configure JWT-based authentication.
171-
- `buildSiweAuthStrategy<TRpc extends RpcSchema>({ siwe, ...baseOptions })`: Configure Sign-In with Ethereum authentication.
172-
173-
### Types
174-
175-
The library exports two custom utility types:
176-
177-
- `RpcMethodWithRegex<TRpc>`: Represents RPC methods, including support for wildcard patterns.
178-
- `OptionalRateLimit<T>`: Makes the `rateLimitBudget` field optional in a type that includes it.
179-
180-
All other exported types (such as `Config`, `ServerConfig`, `DatabaseConfig`, etc.) are TypeScript representations of the original eRPC Go config type definitions. These provide strong typing for configuration objects and can be imported and used in your TypeScript projects.
181-
182-
For a complete list of available types, refer to the type definitions in the library.
183-
184177
## Dependencies
185178

186179
This package relies on the following main dependencies:
187180

188181
- [viem](https://viem.sh/): (Peer Dependency) A TypeScript interface for Ethereum, providing lightweight and type-safe modules for interacting with the blockchain.
189182

190-
- [@ubiquity-dao/rpc-handler](https://github.com/ubiquity/rpc-handler): (Optional Peer Dependency) Used for fetching and comparing free RPC URLs from chainlist. Only required if you intend to use the free RPC features.
191-
192183
- [erpc](https://www.erpc.cloud/): The core RPC load balancer that this config generator is designed for. It offers fault-tolerant EVM RPC load balancing with reorg-aware permanent caching and auto-discovery of node providers.
193184

194185
- [gluegun](https://github.com/infinitered/gluegun): A toolkit used for building the command-line interface of this config generator, offering a robust set of utilities for creating TypeScript-powered CLI apps.
195186

196-
Please ensure you have the required peer dependencies installed in your project when using this package. The `@ubiquity-dao/rpc-handler` is only necessary if you plan to utilize the free RPC functionality.
197-
198187
## Usage examples
199188

200189
For a simple setup, please refer to the [simple example](example/simple.ts) in the repository.
201190

191+
You can check a more [complete example here](/typescript/config/example/full.ts).
192+
202193
## Real-World Usage
203194

204195
For a comprehensive, real-world example of how to use the eRPC Config Generator, you can refer to the Frak indexer repository:
@@ -216,17 +207,17 @@ The entire configuration is contained in a single file:
216207

217208
- `erpc-config.ts`: The main configuration file that exports the complete eRPC config
218209

219-
To generate the YAML configuration from this setup, the project uses:
210+
To generate the JS configuration from this setup, the project uses:
220211

221212
```bash
222-
pnpm erpc-config
213+
bun erpc-config
223214
```
224215

225-
The resulting YAML configuration is output to:
216+
The resulting JS configuration is output to:
226217

227-
- `erpc.yaml`: The generated eRPC configuration file
218+
- `erpc.js`: The generated eRPC configuration file
228219

229-
This example showcases how to integrate the eRPC Config Generator into a larger project, using pnpm as the package manager. It's a great reference for structuring your own eRPC configuration in a production environment.
220+
This example showcases how to integrate the eRPC Config Generator into a larger project, using bun as the package manager. It's a great reference for structuring your own eRPC configuration in a production environment.
230221

231222
You can use this as a template for your own project, adjusting the network configurations, upstreams, and other settings as needed for your specific use case.
232223

biome.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "node_modules/@biomejs/biome/configuration_schema.json",
33
"files": {
4-
"ignore": ["src/generatedTypes/**", "dist/**", "src/bin/**"],
4+
"ignore": ["src/generatedTypes/**", "dist/**", "src/bin/**", "example/*.js"],
55
"include": ["example/**", "src/**", "cli/**"]
66
},
77
"organizeImports": {

bun.lockb

-16.8 KB
Binary file not shown.

cli/generate-types.ts

Lines changed: 0 additions & 62 deletions
This file was deleted.

cli/tygo.yaml

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)