Skip to content

Commit

Permalink
Merge pull request #16 from whats-good/kerem/config-5
Browse files Browse the repository at this point in the history
Kerem/config 5
  • Loading branch information
mechanical-turk authored Dec 5, 2023
2 parents ef49e3e + 9582453 commit 03fd6b9
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 120 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-waves-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@whatsgood/nexus": minor
---

default registry no longer global singleton
4 changes: 2 additions & 2 deletions packages/nexus/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { globalSingletonRegistry } from "./registry/global-singleton-registry";
import { createDefaultRegistry } from "./registry/default-registry";
import type { Registry } from "./registry";

const RpxRelayRecoveryModeSchema = z.enum(["none", "cycle"]);
Expand Down Expand Up @@ -89,6 +89,6 @@ export class Config {
this.globalAccessKey = params.globalAccessKey;
this.recoveryMode = params.recoveryMode ?? "cycle";

this.registry = params.registry || globalSingletonRegistry;
this.registry = params.registry || createDefaultRegistry();
}
}
69 changes: 59 additions & 10 deletions packages/nexus/src/nexus/nexus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,85 @@ import {
import { RequestHandler } from "../request-handler/request-handler";
import { Config } from "../config";
import type { ConfigConstructorParams } from "../config";
import { createDefaultRegistry } from "../registry";

type EmptyServerContext = Record<string, never>;

type ServerContextConfigMap<TServerContext = EmptyServerContext> = {
type ServerContextConfigMap<TServerContext> = {
[K in keyof ConfigConstructorParams]:
| ((ctx: TServerContext, request: Request) => ConfigConstructorParams[K])
// TODO: make this promisable
| ConfigConstructorParams[K];
| ConfigConstructorParams[K]
| ((
serverContext: TServerContext,
request: Request
) => ConfigConstructorParams[K]);
};

export class NexusServer<TServerContext = EmptyServerContext>
implements ServerAdapterBaseObject<TServerContext>
{
private readonly requestHandler = new RequestHandler();
private readonly defaultRegistry = createDefaultRegistry();

private constructor(
private readonly options: ServerContextConfigMap<TServerContext>
) {}

private getValueOrExecute<T>(
valueOrFunction:
| T
| ((serverContext: TServerContext, request: Request) => T),
serverContext: TServerContext,
request: Request
): T {
if (typeof valueOrFunction === "function") {
return (
valueOrFunction as (
serverContext: TServerContext,
request: Request
) => T
)(serverContext, request);
}

return valueOrFunction;
}

public handle = async (
request: Request,
serverContext: TServerContext
): Promise<Response> => {
const configParams = Object.fromEntries(
Object.entries(this.options).map(([key, value]) => [
key,
typeof value === "function" ? value(serverContext, request) : value,
])
const registryParam = this.getValueOrExecute(
this.options.registry,
serverContext,
request
);
const config = new Config(configParams as ConfigConstructorParams);

// TODO: cleanup

const configParams: ConfigConstructorParams = {
registry: registryParam || this.defaultRegistry,
chains: this.getValueOrExecute(
this.options.chains,
serverContext,
request
),
providers: this.getValueOrExecute(
this.options.providers,
serverContext,
request
),
globalAccessKey: this.getValueOrExecute(
this.options.globalAccessKey,
serverContext,
request
),
recoveryMode: this.getValueOrExecute(
this.options.recoveryMode,
serverContext,
request
),
};

const config = new Config(configParams);

return this.requestHandler.handle(config, request);
};
Expand Down
103 changes: 103 additions & 0 deletions packages/nexus/src/registry/default-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { Registry } from "./registry";

export const createDefaultRegistry = () => {
const defaultRegistry = new Registry();

defaultRegistry
.network("ethereum", ["eth"])
.chain(1, "mainnet")
.chain(4, "rinkeby")
.chain(5, "goerli")
.chain(11155111, "sepolia")

.network("base")
.chain(8453, "mainnet")
.chain(84531, "goerli")
.chain(84532, "sepolia")

.network("polygon")
.chain(80001, "mumbai")
.network("local", ["hardhat", "foundry"])
.chain(31337, "local");

defaultRegistry
.provider("alchemy")
.support(1, {
baseURL: "https://eth-mainnet.alchemyapi.io/v2",
type: "url-append-key",
})
.support(5, {
baseURL: "https://eth-goerli.alchemyapi.io/v2",
type: "url-append-key",
})
.support(11155111, {
baseURL: "https://eth-sepolia.alchemyapi.io/v2",
type: "url-append-key",
})
.support(80001, {
baseURL: "https://polygon-mumbai.alchemyapi.io/v2",
type: "url-append-key",
})
.support(84531, {
baseURL: "https://base-goerli.g.alchemy.com/v2",
type: "url-append-key",
})

.provider("base")
.support(8453, {
type: "url",
url: "https://mainnet.base.org",
// isProduction: false,
})
.support(84531, {
type: "url",
url: "https://goerli.base.org",
// isProduction: false,
})
.support(84532, {
type: "url",
url: "https://sepolia.base.org",
})

.provider("infura")
.support(1, {
type: "url-append-key",
baseURL: "https://mainnet.infura.io/v3",
})
.support(5, {
type: "url-append-key",
baseURL: "https://goerli.infura.io/v3",
})
.support(11155111, {
type: "url-append-key",
baseURL: "https://sepolia.infura.io/v3",
})
.support(80001, {
type: "url-append-key",
baseURL: "https://polygon-mumbai.infura.io/v3",
})

.provider("ankr")
.support(1, {
type: "url-append-key",
baseURL: "https://rpc.ankr.com/eth",
})
.support(5, {
type: "url-append-key",
baseURL: "https://rpc.ankr.com/eth_goerli",
})
.support(11155111, {
type: "url-append-key",
baseURL: "https://rpc.ankr.com/eth_sepolia",
})
.support(80001, {
type: "url-append-key",
baseURL: "https://rpc.ankr.com/polygon_mumbai",
})
.support(84531, {
type: "url-append-key",
baseURL: "https://rpc.ankr.com/base_goerli",
});

return defaultRegistry;
};
99 changes: 0 additions & 99 deletions packages/nexus/src/registry/global-singleton-registry.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/nexus/src/registry/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./registry";
export * from "./global-singleton-registry";
export * from "./default-registry";
18 changes: 10 additions & 8 deletions packages/nexus/src/rpc-endpoint/rpc-endpoint-pool-factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { globalSingletonRegistry } from "../registry/global-singleton-registry";
import { createDefaultRegistry } from "../registry/default-registry";
import { Config } from "../config";
import { RpcEndpointPoolFactory } from "./rpc-endpoint-pool-factory";

Expand All @@ -21,16 +21,18 @@ describe("provider factory", () => {

const factory = new RpcEndpointPoolFactory(config);

const registry = createDefaultRegistry();

const chains = {
ethMainnet: globalSingletonRegistry.getChainByNames("ethereum", "mainnet"),
baseGoerli: globalSingletonRegistry.getChainByNames("base", "goerli"),
rinkeby: globalSingletonRegistry.getChainByNames("ethereum", "rinkeby"),
ethMainnet: registry.getChainByNames("ethereum", "mainnet"),
baseGoerli: registry.getChainByNames("base", "goerli"),
rinkeby: registry.getChainByNames("ethereum", "rinkeby"),
};
const providers = {
alchemy: globalSingletonRegistry.getServiceProviderByName("alchemy"),
infura: globalSingletonRegistry.getServiceProviderByName("infura"),
ankr: globalSingletonRegistry.getServiceProviderByName("ankr"),
base: globalSingletonRegistry.getServiceProviderByName("base"),
alchemy: registry.getServiceProviderByName("alchemy"),
infura: registry.getServiceProviderByName("infura"),
ankr: registry.getServiceProviderByName("ankr"),
base: registry.getServiceProviderByName("base"),
};

describe("routes", () => {
Expand Down

0 comments on commit 03fd6b9

Please sign in to comment.