Skip to content

Commit

Permalink
🔧 Use erpc config builder
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Aug 19, 2024
1 parent 82f19cb commit 4e9b2a7
Show file tree
Hide file tree
Showing 5 changed files with 1,378 additions and 1,277 deletions.
207 changes: 207 additions & 0 deletions packages/erpc/erpc-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import {
type Config,
type ProjectConfig,
buildAlchemyUpstream,
buildEnvioUpstream,
buildEvmNetworks,
buildPimlicoUpstream,
buildProject,
buildRateLimit,
buildSecretAuthStrategy,
bundlersMethods,
envVariable,
} from "@konfeature/erpc-config-generator";
import type { RpcMethodWithRegex } from "@konfeature/erpc-config-generator";
import type { EIP1474Methods } from "viem";
import {
arbitrum,
arbitrumSepolia,
base,
baseSepolia,
optimism,
optimismSepolia,
polygon,
} from "viem/chains";

/* -------------------------------------------------------------------------- */
/* Config generator for the Frak eRPC config */
/* -------------------------------------------------------------------------- */

// Build every rate limits
const envioRateLimits = buildRateLimit({
id: "envio-rate-limit",
rules: [
{
method: "*",
maxCount: 1_000,
period: "1s",
},
],
});
const alchemyRateLimits = buildRateLimit({
id: "alchemy-rate-limit",
rules: [
{
method: "*",
maxCount: 200,
period: "1s",
},
],
});
const pimlicoRateLimits = buildRateLimit({
id: "pimlico-rate-limit",
rules: [
{
method: "*",
maxCount: 400,
period: "1s",
},
],
});

// Each networks we will use
const mainnetNetworks = buildEvmNetworks({
chains: [polygon, arbitrum, optimism, base],
generic: {
// Some failsafe config
failsafe: {
timeout: {
duration: "30s",
},
retry: {
maxAttempts: 5,
delay: "500ms",
backoffMaxDelay: "10s",
backoffFactor: 0.5,
jitter: "200ms",
},
hedge: {
delay: "1s",
maxCount: 2,
},
},
},
});
const testnetNetworks = buildEvmNetworks({
chains: [arbitrumSepolia, optimismSepolia, baseSepolia],
generic: {
// Some failsafe config
failsafe: {
timeout: {
duration: "60s",
},
retry: {
maxAttempts: 3,
delay: "1s",
backoffMaxDelay: "20s",
backoffFactor: 0.5,
jitter: "500ms",
},
},
// Overide finality depth
evm: {
finalityDepth: 64,
},
},
});
const networks = [...mainnetNetworks, ...testnetNetworks];

const pimlicoSpecificMethods: RpcMethodWithRegex<EIP1474Methods>[] = [
...bundlersMethods,
"pm_*",
"pimlico_*",
];

// Build each upstream we will use
const upstreams = [
buildEnvioUpstream({
rateLimitBudget: envioRateLimits.id,
ignoreMethods: [...pimlicoSpecificMethods, "alchemy_*"],
}),
buildAlchemyUpstream({
apiKey: envVariable("ALCHEMY_API_KEY"),
rateLimitBudget: alchemyRateLimits.id,
ignoreMethods: pimlicoSpecificMethods,
}),
];
const pimlicoUpstream = buildPimlicoUpstream({
apiKey: envVariable("PIMLICO_API_KEY"),
rateLimitBudget: pimlicoRateLimits.id,
ignoreMethods: ["*"],
allowMethods: pimlicoSpecificMethods,
});

// Build the ponder indexing project
const ponderProject: ProjectConfig = buildProject({
id: "ponder-rpc",
networks,
upstreams,
auth: {
strategies: [
buildSecretAuthStrategy({
secret: {
value: envVariable("PONDER_RPC_SECRET"),
},
}),
],
},
});

// Build the nexus rpc project
// todo: add authentication + more restrictie cors origin
const nexusProject: ProjectConfig = buildProject({
id: "nexus-rpc",
networks,
upstreams: [...upstreams, pimlicoUpstream],
cors: {
allowedOrigins: [
"https://nexus.frak.id",
"https://nexus-dev.frak.id",
"http://localhost:3000",
],
allowedMethods: ["GET", "POST", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization"],
exposedHeaders: ["X-Request-ID"],
allowCredentials: true,
maxAge: 3600,
},
auth: {
strategies: [
buildSecretAuthStrategy({
secret: {
value: envVariable("NEXUS_RPC_SECRET"),
},
}),
],
},
});

// Build the global config
const config: Config = {
logLevel: envVariable("ERPC_LOG_LEVEL"),
database: {
evmJsonRpcCache: {
driver: "postgresql",
postgresql: {
connectionUri: envVariable("ERPC_DATABASE_URL"),
table: "rpc_cache",
},
},
},
server: {
httpHost: "0.0.0.0",
httpPort: 8080,
maxTimeout: "60s",
},
metrics: {
enabled: true,
host: "0.0.0.0",
port: 4001,
},
projects: [ponderProject, nexusProject],
rateLimiters: {
budgets: [envioRateLimits, alchemyRateLimits, pimlicoRateLimits],
},
};

export default config;
4 changes: 3 additions & 1 deletion packages/erpc/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@frak-indexer/erpc",
"private": true,
"type": "module",
"scripts": {
"format": "biome check --write .",
"format:check": "biome check .",
"build": "erpc-config",
"lint": "biome lint .",
"typecheck": "tsc"
},
Expand All @@ -14,6 +14,8 @@
"@types/node": "^22.4.0",
"aws-cdk-lib": "2.142.1",
"sst": "2.43.6",
"@konfeature/erpc-config-generator": "0.0.5",
"viem": "^2.19.7",
"typescript": "^5.3.2"
},
"engines": {
Expand Down
22 changes: 22 additions & 0 deletions packages/erpc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,

"verbatimModuleSyntax": false,
"esModuleInterop": true,
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,

"moduleResolution": "bundler",
"module": "ESNext",
"noEmit": true,
"lib": ["ES2022"],
"target": "ES2022",

"skipLibCheck": true
},
"include": ["./**/*.ts"],
"exclude": ["node_modules"]
}
4 changes: 3 additions & 1 deletion packages/ponder/ponder.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const maxBlockRange = 10000;
* @returns
*/
const getErpcTransport = (chainId: number) =>
http(`${process.env.ERPC_BASE_URL}/${chainId}?token=${process.env.PONDER_RPC_SECRET}`);
http(
`${process.env.ERPC_BASE_URL}/${chainId}?token=${process.env.PONDER_RPC_SECRET}`
);

/**
* Ponder configuration
Expand Down
Loading

0 comments on commit 4e9b2a7

Please sign in to comment.