@codkit/config 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +70 -2
- package/dist/chunk-N3OT5BNF.cjs +655 -0
- package/dist/chunk-N3OT5BNF.cjs.map +1 -0
- package/dist/chunk-ZRZNU5KJ.js +655 -0
- package/dist/chunk-ZRZNU5KJ.js.map +1 -0
- package/dist/index.cjs +76 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +76 -0
- package/dist/index.js.map +1 -0
- package/dist/rpc-DaYsu4c7.d.cts +669 -0
- package/dist/rpc-DaYsu4c7.d.ts +669 -0
- package/dist/viem.cjs +30 -0
- package/dist/viem.cjs.map +1 -0
- package/dist/viem.d.cts +20 -0
- package/dist/viem.d.ts +20 -0
- package/dist/viem.js +30 -0
- package/dist/viem.js.map +1 -0
- package/package.json +73 -4
- package/src/chains.ts +160 -0
- package/src/env.ts +59 -0
- package/src/evm-chains.ts +438 -0
- package/src/explorer.ts +13 -0
- package/src/index.ts +5 -0
- package/src/rpc.ts +111 -0
- package/src/types.ts +42 -0
- package/src/viem.ts +33 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/dell/Projects/codkit/packages/config/dist/viem.cjs","../src/viem.ts"],"names":["http"],"mappings":"AAAA;AACE;AACA;AACF,wDAA6B;AAC7B;AACA;ACGA,4BAAkG;AAK3F,SAAS,SAAA,CAAU,KAAA,EAAiB,KAAA,EAAmB,CAAC,CAAA,EAAU;AACvE,EAAA,MAAM,EAAA,EAAI,wCAAA,KAAc,CAAA;AACxB,EAAA,GAAA,CAAI,CAAA,CAAE,UAAA,IAAc,MAAA,GAAS,CAAA,CAAE,QAAA,IAAY,KAAA,CAAA,EAAW,MAAM,IAAI,KAAA,CAAM,CAAA,WAAA,EAAc,CAAA,CAAE,GAAG,CAAA,oBAAA,CAAsB,CAAA;AAC/G,EAAA,MAAMA,MAAAA,EAAO,0CAAA,CAAW,EAAG,IAAI,CAAA;AAC/B,EAAA,OAAO,+BAAA;AAAY,IACjB,EAAA,EAAI,CAAA,CAAE,OAAA;AAAA,IACN,IAAA,EAAM,CAAA,CAAE,IAAA;AAAA,IACR,cAAA,EAAgB,CAAA,CAAE,cAAA;AAAA,IAClB,OAAA,EAAS,EAAE,OAAA,EAAS,EAAE,IAAA,EAAAA,KAAAA,EAAM,GAAI,CAAA,CAAE,OAAA,GAAU,EAAE,SAAA,EAAW,CAAC,GAAG,CAAA,CAAE,MAAM,EAAE,EAAG,EAAE,CAAA;AAAA,IAC5E,GAAI,CAAA,CAAE,SAAA,GAAY,EAAE,cAAA,EAAgB,EAAE,OAAA,EAAS,EAAE,IAAA,EAAM,CAAA,CAAE,QAAA,CAAS,IAAA,EAAM,GAAA,EAAK,CAAA,CAAE,QAAA,CAAS,IAAI,EAAE,EAAE,CAAA;AAAA,IAChG,OAAA,EAAS,CAAA,CAAE;AAAA,EACb,CAAC,CAAA;AACH;AAGO,SAAS,aAAA,CAAc,KAAA,EAAiB,KAAA,EAAoD,CAAC,CAAA,EAAc;AAChH,EAAA,MAAM,KAAA,EAAO,0CAAA,KAAW,EAAO,IAAI,CAAA;AACnC,EAAA,MAAM,WAAA,EAAa,IAAA,CAAK,GAAA,CAAI,CAAC,CAAA,EAAA,GAAM,wBAAA,CAAK,EAAG,IAAA,CAAK,IAAI,CAAC,CAAA;AACrD,EAAA,OAAO,UAAA,CAAW,OAAA,IAAW,EAAA,EAAI,UAAA,CAAW,CAAC,EAAA,EAAK,4BAAA,UAAmB,CAAA;AACvE;ADPA;AACE;AACA;AACF,qEAAC","file":"/home/dell/Projects/codkit/packages/config/dist/viem.cjs","sourcesContent":[null,"/**\n * viem / wagmi glue. Import from `@codkit/config/viem` (needs `viem`).\n *\n * ```ts\n * const chains = [viemChain(\"base-sepolia\")] as const;\n * createConfig({ chains, transports: { [chains[0].id]: viemTransport(\"base-sepolia\") } });\n * ```\n */\nimport { type Chain, defineChain, fallback, type HttpTransportConfig, http, type Transport } from \"viem\";\nimport { type ChainRef, getChain } from \"./chains.js\";\nimport { getRpcUrls, type RpcOptions } from \"./rpc.js\";\n\n/** A viem `Chain` whose default RPCs are the resolved ones (override → provider → public). */\nexport function viemChain(chain: ChainRef, opts: RpcOptions = {}): Chain {\n const c = getChain(chain);\n if (c.ecosystem !== \"evm\" || c.chainId === undefined) throw new Error(`viemChain: ${c.key} is not an EVM chain`);\n const http = getRpcUrls(c, opts);\n return defineChain({\n id: c.chainId,\n name: c.name,\n nativeCurrency: c.nativeCurrency,\n rpcUrls: { default: { http, ...(c.wsUrls && { webSocket: [...c.wsUrls] }) } },\n ...(c.explorer && { blockExplorers: { default: { name: c.explorer.name, url: c.explorer.url } } }),\n testnet: c.testnet,\n });\n}\n\n/** One `http` transport per resolved RPC, wrapped in viem's `fallback` (or a single `http` when there's one). */\nexport function viemTransport(chain: ChainRef, opts: RpcOptions & { http?: HttpTransportConfig } = {}): Transport {\n const urls = getRpcUrls(chain, opts);\n const transports = urls.map((u) => http(u, opts.http));\n return transports.length === 1 ? transports[0]! : fallback(transports);\n}\n"]}
|
package/dist/viem.d.cts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Chain, HttpTransportConfig, Transport } from 'viem';
|
|
2
|
+
import { ChainRef, RpcOptions } from './index.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* viem / wagmi glue. Import from `@codkit/config/viem` (needs `viem`).
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* const chains = [viemChain("base-sepolia")] as const;
|
|
9
|
+
* createConfig({ chains, transports: { [chains[0].id]: viemTransport("base-sepolia") } });
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** A viem `Chain` whose default RPCs are the resolved ones (override → provider → public). */
|
|
14
|
+
declare function viemChain(chain: ChainRef, opts?: RpcOptions): Chain;
|
|
15
|
+
/** One `http` transport per resolved RPC, wrapped in viem's `fallback` (or a single `http` when there's one). */
|
|
16
|
+
declare function viemTransport(chain: ChainRef, opts?: RpcOptions & {
|
|
17
|
+
http?: HttpTransportConfig;
|
|
18
|
+
}): Transport;
|
|
19
|
+
|
|
20
|
+
export { viemChain, viemTransport };
|
package/dist/viem.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Chain, HttpTransportConfig, Transport } from 'viem';
|
|
2
|
+
import { ChainRef, RpcOptions } from './index.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* viem / wagmi glue. Import from `@codkit/config/viem` (needs `viem`).
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* const chains = [viemChain("base-sepolia")] as const;
|
|
9
|
+
* createConfig({ chains, transports: { [chains[0].id]: viemTransport("base-sepolia") } });
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** A viem `Chain` whose default RPCs are the resolved ones (override → provider → public). */
|
|
14
|
+
declare function viemChain(chain: ChainRef, opts?: RpcOptions): Chain;
|
|
15
|
+
/** One `http` transport per resolved RPC, wrapped in viem's `fallback` (or a single `http` when there's one). */
|
|
16
|
+
declare function viemTransport(chain: ChainRef, opts?: RpcOptions & {
|
|
17
|
+
http?: HttpTransportConfig;
|
|
18
|
+
}): Transport;
|
|
19
|
+
|
|
20
|
+
export { viemChain, viemTransport };
|
package/dist/viem.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getChain,
|
|
3
|
+
getRpcUrls
|
|
4
|
+
} from "./chunk-ZRZNU5KJ.js";
|
|
5
|
+
|
|
6
|
+
// src/viem.ts
|
|
7
|
+
import { defineChain, fallback, http } from "viem";
|
|
8
|
+
function viemChain(chain, opts = {}) {
|
|
9
|
+
const c = getChain(chain);
|
|
10
|
+
if (c.ecosystem !== "evm" || c.chainId === void 0) throw new Error(`viemChain: ${c.key} is not an EVM chain`);
|
|
11
|
+
const http2 = getRpcUrls(c, opts);
|
|
12
|
+
return defineChain({
|
|
13
|
+
id: c.chainId,
|
|
14
|
+
name: c.name,
|
|
15
|
+
nativeCurrency: c.nativeCurrency,
|
|
16
|
+
rpcUrls: { default: { http: http2, ...c.wsUrls && { webSocket: [...c.wsUrls] } } },
|
|
17
|
+
...c.explorer && { blockExplorers: { default: { name: c.explorer.name, url: c.explorer.url } } },
|
|
18
|
+
testnet: c.testnet
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function viemTransport(chain, opts = {}) {
|
|
22
|
+
const urls = getRpcUrls(chain, opts);
|
|
23
|
+
const transports = urls.map((u) => http(u, opts.http));
|
|
24
|
+
return transports.length === 1 ? transports[0] : fallback(transports);
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
viemChain,
|
|
28
|
+
viemTransport
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=viem.js.map
|
package/dist/viem.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/viem.ts"],"sourcesContent":["/**\n * viem / wagmi glue. Import from `@codkit/config/viem` (needs `viem`).\n *\n * ```ts\n * const chains = [viemChain(\"base-sepolia\")] as const;\n * createConfig({ chains, transports: { [chains[0].id]: viemTransport(\"base-sepolia\") } });\n * ```\n */\nimport { type Chain, defineChain, fallback, type HttpTransportConfig, http, type Transport } from \"viem\";\nimport { type ChainRef, getChain } from \"./chains.js\";\nimport { getRpcUrls, type RpcOptions } from \"./rpc.js\";\n\n/** A viem `Chain` whose default RPCs are the resolved ones (override → provider → public). */\nexport function viemChain(chain: ChainRef, opts: RpcOptions = {}): Chain {\n const c = getChain(chain);\n if (c.ecosystem !== \"evm\" || c.chainId === undefined) throw new Error(`viemChain: ${c.key} is not an EVM chain`);\n const http = getRpcUrls(c, opts);\n return defineChain({\n id: c.chainId,\n name: c.name,\n nativeCurrency: c.nativeCurrency,\n rpcUrls: { default: { http, ...(c.wsUrls && { webSocket: [...c.wsUrls] }) } },\n ...(c.explorer && { blockExplorers: { default: { name: c.explorer.name, url: c.explorer.url } } }),\n testnet: c.testnet,\n });\n}\n\n/** One `http` transport per resolved RPC, wrapped in viem's `fallback` (or a single `http` when there's one). */\nexport function viemTransport(chain: ChainRef, opts: RpcOptions & { http?: HttpTransportConfig } = {}): Transport {\n const urls = getRpcUrls(chain, opts);\n const transports = urls.map((u) => http(u, opts.http));\n return transports.length === 1 ? transports[0]! : fallback(transports);\n}\n"],"mappings":";;;;;;AAQA,SAAqB,aAAa,UAAoC,YAA4B;AAK3F,SAAS,UAAU,OAAiB,OAAmB,CAAC,GAAU;AACvE,QAAM,IAAI,SAAS,KAAK;AACxB,MAAI,EAAE,cAAc,SAAS,EAAE,YAAY,OAAW,OAAM,IAAI,MAAM,cAAc,EAAE,GAAG,sBAAsB;AAC/G,QAAMA,QAAO,WAAW,GAAG,IAAI;AAC/B,SAAO,YAAY;AAAA,IACjB,IAAI,EAAE;AAAA,IACN,MAAM,EAAE;AAAA,IACR,gBAAgB,EAAE;AAAA,IAClB,SAAS,EAAE,SAAS,EAAE,MAAAA,OAAM,GAAI,EAAE,UAAU,EAAE,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,EAAG,EAAE;AAAA,IAC5E,GAAI,EAAE,YAAY,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,MAAM,KAAK,EAAE,SAAS,IAAI,EAAE,EAAE;AAAA,IAChG,SAAS,EAAE;AAAA,EACb,CAAC;AACH;AAGO,SAAS,cAAc,OAAiB,OAAoD,CAAC,GAAc;AAChH,QAAM,OAAO,WAAW,OAAO,IAAI;AACnC,QAAM,aAAa,KAAK,IAAI,CAAC,MAAM,KAAK,GAAG,KAAK,IAAI,CAAC;AACrD,SAAO,WAAW,WAAW,IAAI,WAAW,CAAC,IAAK,SAAS,UAAU;AACvE;","names":["http"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,75 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codkit/config",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Chain registry (EVM, Solana, Sui), RPC resolution with fallbacks and keyed providers, explorer links, and a typed env schema for dapps.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=22"
|
|
8
|
+
},
|
|
9
|
+
"author": "Young John",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/deyoungjohn/codkit.git",
|
|
13
|
+
"directory": "packages/config"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/deyoungjohn/codkit/tree/main/packages/config#readme",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"main": "./dist/index.cjs",
|
|
22
|
+
"module": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"import": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"require": {
|
|
31
|
+
"types": "./dist/index.d.cts",
|
|
32
|
+
"default": "./dist/index.cjs"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"./viem": {
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./dist/viem.d.ts",
|
|
38
|
+
"default": "./dist/viem.js"
|
|
39
|
+
},
|
|
40
|
+
"require": {
|
|
41
|
+
"types": "./dist/viem.d.cts",
|
|
42
|
+
"default": "./dist/viem.cjs"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"./package.json": "./package.json"
|
|
46
|
+
},
|
|
47
|
+
"files": [
|
|
48
|
+
"dist",
|
|
49
|
+
"src",
|
|
50
|
+
"README.md"
|
|
51
|
+
],
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsup",
|
|
54
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
55
|
+
"test": "vitest run",
|
|
56
|
+
"clean": "rm -rf dist .turbo",
|
|
57
|
+
"gen": "node scripts/gen-evm.mjs",
|
|
58
|
+
"example": "tsx --env-file-if-exists=../../.env examples/resolve.ts",
|
|
59
|
+
"prepublishOnly": "pnpm run build"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"zod": "^4.1.0"
|
|
63
|
+
},
|
|
64
|
+
"peerDependencies": {
|
|
65
|
+
"viem": "^2.48.0"
|
|
66
|
+
},
|
|
67
|
+
"peerDependenciesMeta": {
|
|
68
|
+
"viem": {
|
|
69
|
+
"optional": true
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"viem": "^2.58.0"
|
|
74
|
+
}
|
|
75
|
+
}
|
package/src/chains.ts
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { evmChains } from "./evm-chains.js";
|
|
2
|
+
import type { ChainConfig } from "./types.js";
|
|
3
|
+
|
|
4
|
+
const SOL = { name: "Solana", symbol: "SOL", decimals: 9 } as const;
|
|
5
|
+
const SUI = { name: "Sui", symbol: "SUI", decimals: 9 } as const;
|
|
6
|
+
const solanaExplorer = (cluster?: string) => ({
|
|
7
|
+
name: "Solana Explorer",
|
|
8
|
+
url: "https://explorer.solana.com",
|
|
9
|
+
tx: `/tx/{hash}${cluster ? `?cluster=${cluster}` : ""}`,
|
|
10
|
+
address: `/address/{address}${cluster ? `?cluster=${cluster}` : ""}`,
|
|
11
|
+
});
|
|
12
|
+
const suiscan = (net: string) => ({ name: "Suiscan", url: `https://suiscan.xyz/${net}`, tx: "/tx/{hash}", address: "/account/{address}" });
|
|
13
|
+
|
|
14
|
+
export const solanaChains = [
|
|
15
|
+
{
|
|
16
|
+
key: "solana",
|
|
17
|
+
id: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
|
|
18
|
+
walletChain: "solana:mainnet",
|
|
19
|
+
ecosystem: "solana",
|
|
20
|
+
name: "Solana",
|
|
21
|
+
testnet: false,
|
|
22
|
+
cluster: "mainnet-beta",
|
|
23
|
+
nativeCurrency: SOL,
|
|
24
|
+
rpcUrls: ["https://api.mainnet-beta.solana.com"],
|
|
25
|
+
wsUrls: ["wss://api.mainnet-beta.solana.com"],
|
|
26
|
+
explorer: solanaExplorer(),
|
|
27
|
+
providers: { alchemy: "solana-mainnet", helius: "mainnet" },
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
key: "solana-devnet",
|
|
31
|
+
id: "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1",
|
|
32
|
+
walletChain: "solana:devnet",
|
|
33
|
+
ecosystem: "solana",
|
|
34
|
+
name: "Solana Devnet",
|
|
35
|
+
testnet: true,
|
|
36
|
+
cluster: "devnet",
|
|
37
|
+
nativeCurrency: SOL,
|
|
38
|
+
rpcUrls: ["https://api.devnet.solana.com"],
|
|
39
|
+
wsUrls: ["wss://api.devnet.solana.com"],
|
|
40
|
+
explorer: solanaExplorer("devnet"),
|
|
41
|
+
providers: { alchemy: "solana-devnet", helius: "devnet" },
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
key: "solana-testnet",
|
|
45
|
+
id: "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z",
|
|
46
|
+
walletChain: "solana:testnet",
|
|
47
|
+
ecosystem: "solana",
|
|
48
|
+
name: "Solana Testnet",
|
|
49
|
+
testnet: true,
|
|
50
|
+
cluster: "testnet",
|
|
51
|
+
nativeCurrency: SOL,
|
|
52
|
+
rpcUrls: ["https://api.testnet.solana.com"],
|
|
53
|
+
wsUrls: ["wss://api.testnet.solana.com"],
|
|
54
|
+
explorer: solanaExplorer("testnet"),
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
key: "solana-localnet",
|
|
58
|
+
id: "solana:localnet",
|
|
59
|
+
walletChain: "solana:localnet",
|
|
60
|
+
ecosystem: "solana",
|
|
61
|
+
name: "Solana Localnet",
|
|
62
|
+
testnet: true,
|
|
63
|
+
cluster: "localnet",
|
|
64
|
+
nativeCurrency: SOL,
|
|
65
|
+
rpcUrls: ["http://127.0.0.1:8899"],
|
|
66
|
+
wsUrls: ["ws://127.0.0.1:8900"],
|
|
67
|
+
explorer: { ...solanaExplorer("custom"), tx: "/tx/{hash}?cluster=custom&customUrl=http%3A%2F%2F127.0.0.1%3A8899" },
|
|
68
|
+
},
|
|
69
|
+
] as const satisfies readonly ChainConfig[];
|
|
70
|
+
|
|
71
|
+
export const suiChains = [
|
|
72
|
+
{
|
|
73
|
+
key: "sui",
|
|
74
|
+
id: "sui:mainnet",
|
|
75
|
+
ecosystem: "sui",
|
|
76
|
+
name: "Sui",
|
|
77
|
+
testnet: false,
|
|
78
|
+
nativeCurrency: SUI,
|
|
79
|
+
rpcUrls: ["https://fullnode.mainnet.sui.io:443"],
|
|
80
|
+
graphqlUrl: "https://graphql.mainnet.sui.io/graphql",
|
|
81
|
+
explorer: suiscan("mainnet"),
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
key: "sui-testnet",
|
|
85
|
+
id: "sui:testnet",
|
|
86
|
+
ecosystem: "sui",
|
|
87
|
+
name: "Sui Testnet",
|
|
88
|
+
testnet: true,
|
|
89
|
+
nativeCurrency: SUI,
|
|
90
|
+
rpcUrls: ["https://fullnode.testnet.sui.io:443"],
|
|
91
|
+
graphqlUrl: "https://graphql.testnet.sui.io/graphql",
|
|
92
|
+
explorer: suiscan("testnet"),
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
key: "sui-devnet",
|
|
96
|
+
id: "sui:devnet",
|
|
97
|
+
ecosystem: "sui",
|
|
98
|
+
name: "Sui Devnet",
|
|
99
|
+
testnet: true,
|
|
100
|
+
nativeCurrency: SUI,
|
|
101
|
+
rpcUrls: ["https://fullnode.devnet.sui.io:443"],
|
|
102
|
+
graphqlUrl: "https://graphql.devnet.sui.io/graphql",
|
|
103
|
+
explorer: suiscan("devnet"),
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
key: "sui-localnet",
|
|
107
|
+
id: "sui:localnet",
|
|
108
|
+
ecosystem: "sui",
|
|
109
|
+
name: "Sui Localnet",
|
|
110
|
+
testnet: true,
|
|
111
|
+
nativeCurrency: SUI,
|
|
112
|
+
rpcUrls: ["http://127.0.0.1:9000"],
|
|
113
|
+
},
|
|
114
|
+
] as const satisfies readonly ChainConfig[];
|
|
115
|
+
|
|
116
|
+
const all = [...evmChains, ...solanaChains, ...suiChains];
|
|
117
|
+
|
|
118
|
+
export type ChainKey = (typeof all)[number]["key"];
|
|
119
|
+
|
|
120
|
+
/** Every built-in chain by key: `chains["base-sepolia"]`. */
|
|
121
|
+
export const chains = Object.fromEntries(all.map((c) => [c.key, c])) as { [K in ChainKey]: Extract<(typeof all)[number], { key: K }> };
|
|
122
|
+
|
|
123
|
+
/** A chain key, CAIP-2 id, Wallet Standard id (`solana:devnet`), EVM chain id, or a `ChainConfig` (for custom chains). */
|
|
124
|
+
export type ChainRef = ChainKey | (string & {}) | number | ChainConfig;
|
|
125
|
+
|
|
126
|
+
const index = new Map<string | number, ChainConfig>();
|
|
127
|
+
for (const c of all as readonly ChainConfig[]) {
|
|
128
|
+
index.set(c.key, c);
|
|
129
|
+
index.set(c.id, c);
|
|
130
|
+
if (c.walletChain) index.set(c.walletChain, c);
|
|
131
|
+
if (c.chainId !== undefined) index.set(c.chainId, c);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function findChain(ref: ChainRef): ChainConfig | undefined {
|
|
135
|
+
return typeof ref === "object" ? ref : index.get(ref);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function getChain(ref: ChainRef): ChainConfig {
|
|
139
|
+
const c = findChain(ref);
|
|
140
|
+
if (!c) throw new Error(`unknown chain ${JSON.stringify(ref)}; pass a ChainConfig from defineChain() for custom chains`);
|
|
141
|
+
return c;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Built-in chains, optionally filtered: `listChains({ ecosystem: "evm", testnet: true })`. */
|
|
145
|
+
export function listChains(filter: { ecosystem?: ChainConfig["ecosystem"]; testnet?: boolean } = {}): ChainConfig[] {
|
|
146
|
+
return (all as readonly ChainConfig[]).filter(
|
|
147
|
+
(c) =>
|
|
148
|
+
(filter.ecosystem === undefined || c.ecosystem === filter.ecosystem) &&
|
|
149
|
+
(filter.testnet === undefined || c.testnet === filter.testnet),
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Declare a chain that isn't built in. Validates the shape; use the result anywhere a `ChainRef` is accepted. */
|
|
154
|
+
export function defineChain<const C extends ChainConfig>(chain: C): C {
|
|
155
|
+
if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(chain.key)) throw new Error(`defineChain: key "${chain.key}" must be kebab-case`);
|
|
156
|
+
if (!/^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/.test(chain.id)) throw new Error(`defineChain: id "${chain.id}" is not CAIP-2`);
|
|
157
|
+
if (chain.ecosystem === "evm" && chain.id !== `eip155:${chain.chainId}`) throw new Error("defineChain: EVM id must be eip155:<chainId>");
|
|
158
|
+
if (!chain.rpcUrls.length) throw new Error("defineChain: at least one rpcUrl is required");
|
|
159
|
+
return chain;
|
|
160
|
+
}
|
package/src/env.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type ChainRef, getChain } from "./chains.js";
|
|
3
|
+
import { type Env, envStem } from "./rpc.js";
|
|
4
|
+
|
|
5
|
+
const urlList = z
|
|
6
|
+
.string()
|
|
7
|
+
.trim()
|
|
8
|
+
.refine((v) => v.split(",").every((u) => /^(https?|wss?):\/\/\S+$/.test(u.trim())), "comma-separated http(s):// or ws(s):// URLs");
|
|
9
|
+
|
|
10
|
+
export interface RpcEnvOptions {
|
|
11
|
+
/** Prefix for every variable, e.g. `NEXT_PUBLIC_` or `VITE_` for browser builds. Default none. */
|
|
12
|
+
prefix?: string;
|
|
13
|
+
/** Also accept `WALLETCONNECT_PROJECT_ID` (used by `@codkit/wallet`). Default true. */
|
|
14
|
+
walletConnect?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A zod schema for the variables `getRpcUrls` reads, for the chains you use:
|
|
19
|
+
* `<KEY>_RPC_URL`, `<KEY>_WS_URL`, `ALCHEMY_API_KEY`, `HELIUS_API_KEY`, `WALLETCONNECT_PROJECT_ID`. All optional.
|
|
20
|
+
* Extend it with your own: `rpcEnvSchema(["base-sepolia"]).extend({ DATABASE_URL: z.url() })`.
|
|
21
|
+
*/
|
|
22
|
+
export function rpcEnvSchema(chains: ChainRef[], opts: RpcEnvOptions = {}) {
|
|
23
|
+
const p = opts.prefix ?? "";
|
|
24
|
+
const shape: Record<string, z.ZodOptional<z.ZodType<string>>> = {
|
|
25
|
+
[`${p}ALCHEMY_API_KEY`]: z.string().trim().min(1).optional(),
|
|
26
|
+
[`${p}HELIUS_API_KEY`]: z.string().trim().min(1).optional(),
|
|
27
|
+
};
|
|
28
|
+
if (opts.walletConnect ?? true)
|
|
29
|
+
shape[`${p}WALLETCONNECT_PROJECT_ID`] = z
|
|
30
|
+
.string()
|
|
31
|
+
.trim()
|
|
32
|
+
.regex(/^[0-9a-f]{32}$/, "32 hex chars")
|
|
33
|
+
.optional();
|
|
34
|
+
for (const c of chains.map(getChain)) {
|
|
35
|
+
shape[`${p}${envStem(c)}_RPC_URL`] = urlList.optional();
|
|
36
|
+
shape[`${p}${envStem(c)}_WS_URL`] = urlList.optional();
|
|
37
|
+
}
|
|
38
|
+
return z.object(shape);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class EnvError extends Error {
|
|
42
|
+
constructor(readonly issues: { name: string; message: string }[]) {
|
|
43
|
+
super(`invalid environment:\n${issues.map((i) => ` ${i.name}: ${i.message}`).join("\n")}`);
|
|
44
|
+
this.name = "EnvError";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Validate env vars against a schema, reporting every bad variable at once (values are never echoed).
|
|
50
|
+
* `source` defaults to `process.env`; on the client pass the variables explicitly.
|
|
51
|
+
*/
|
|
52
|
+
export function parseEnv<S extends z.ZodType>(schema: S, source?: Env): z.infer<S> {
|
|
53
|
+
const env = source ?? (globalThis as { process?: { env?: Env } }).process?.env ?? {};
|
|
54
|
+
// empty strings mean "unset", as in most .env files
|
|
55
|
+
const cleaned = Object.fromEntries(Object.entries(env).filter(([, v]) => v !== undefined && v.trim() !== ""));
|
|
56
|
+
const r = schema.safeParse(cleaned);
|
|
57
|
+
if (r.success) return r.data;
|
|
58
|
+
throw new EnvError(r.error.issues.map((i) => ({ name: i.path.join(".") || "(root)", message: i.message })));
|
|
59
|
+
}
|