@somnia-chain/contracts-sdk 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.
@@ -0,0 +1,56 @@
1
+ import { registry } from "./gen/registry.js";
2
+ export * from "./gen/registry.js";
3
+ export type Registry = typeof registry;
4
+ export type NetworkSlug = keyof Registry & string;
5
+ export type ProjectSlug<N extends NetworkSlug> = keyof Registry[N]["contracts"] & string;
6
+ export type ContractName<N extends NetworkSlug, P extends ProjectSlug<N>> = keyof Registry[N]["contracts"][P] & string;
7
+ export type Address = `0x${string}`;
8
+ /** One deployment's record; narrow it by passing slugs and a name. */
9
+ export type ContractEntry<N extends NetworkSlug = NetworkSlug, P extends ProjectSlug<N> = ProjectSlug<N>, C extends ContractName<N, P> = ContractName<N, P>> = Registry[N]["contracts"][P][C];
10
+ type DistributedOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
11
+ /** One network's chain information, without the address book. */
12
+ export type NetworkInfo<N extends NetworkSlug = NetworkSlug> = DistributedOmit<Registry[N], "contracts">;
13
+ /** Every network's chain information, retired ones included. */
14
+ export declare const networks: readonly NetworkInfo[];
15
+ /** One network's chain information, without the address book. */
16
+ export declare function getNetwork<N extends NetworkSlug>(network: N): Omit<Registry[N], "contracts">;
17
+ /**
18
+ * All networks that answer at a chain id. Plural on purpose: chain ids are
19
+ * not unique across Somnia networks (a regenesis reuses the id with none of
20
+ * the state), so an id can name a retired and a live network at once. Filter
21
+ * on `status` if only the live one matters.
22
+ */
23
+ export declare function getNetworksByChainId(chainId: number): NetworkInfo[];
24
+ /**
25
+ * A viem-`Chain`-shaped object for a network, structurally compatible with
26
+ * `viem`'s `Chain` without depending on it. The registry records no public
27
+ * RPC endpoint — deliberately — so you bring your own.
28
+ */
29
+ export declare function toChain<N extends NetworkSlug>(network: N, opts: {
30
+ rpcUrl: string;
31
+ wsUrl?: string;
32
+ }): {
33
+ id: Registry[N]["chainId"];
34
+ name: Registry[N]["name"];
35
+ nativeCurrency: Registry[N]["nativeCurrency"];
36
+ rpcUrls: {
37
+ default: {
38
+ http: readonly [string];
39
+ webSocket?: readonly [string];
40
+ };
41
+ };
42
+ blockExplorers?: {
43
+ default: {
44
+ name: string;
45
+ url: string;
46
+ };
47
+ };
48
+ };
49
+ /** One deployment: address, kind, version, status and — where recorded — the ABI. */
50
+ export declare function getContract<N extends NetworkSlug, P extends ProjectSlug<N>, C extends ContractName<N, P>>(network: N, project: P, name: C): Registry[N]["contracts"][P][C];
51
+ export declare function getAddress<N extends NetworkSlug, P extends ProjectSlug<N>, C extends ContractName<N, P>>(network: N, project: P, name: C): Registry[N]["contracts"][P][C] extends {
52
+ address: infer A;
53
+ } ? A : never;
54
+ export declare function getAbi<N extends NetworkSlug, P extends ProjectSlug<N>, C extends ContractName<N, P>>(network: N, project: P, name: C): Registry[N]["contracts"][P][C] extends {
55
+ abi: infer A;
56
+ } ? A : undefined;
package/dist/index.js ADDED
@@ -0,0 +1,61 @@
1
+ import { registry } from "./gen/registry.js";
2
+ export * from "./gen/registry.js";
3
+ /** Every network's chain information, retired ones included. */
4
+ export const networks = Object.keys(registry).map((slug) => getNetwork(slug));
5
+ /** One network's chain information, without the address book. */
6
+ export function getNetwork(network) {
7
+ const entry = registry[network];
8
+ if (!entry)
9
+ throw new Error(`unknown network: ${String(network)}`);
10
+ const { contracts: _contracts, ...info } = entry;
11
+ return info;
12
+ }
13
+ /**
14
+ * All networks that answer at a chain id. Plural on purpose: chain ids are
15
+ * not unique across Somnia networks (a regenesis reuses the id with none of
16
+ * the state), so an id can name a retired and a live network at once. Filter
17
+ * on `status` if only the live one matters.
18
+ */
19
+ export function getNetworksByChainId(chainId) {
20
+ return Object.keys(registry)
21
+ .filter((slug) => registry[slug].chainId === chainId)
22
+ .map((slug) => getNetwork(slug));
23
+ }
24
+ /**
25
+ * A viem-`Chain`-shaped object for a network, structurally compatible with
26
+ * `viem`'s `Chain` without depending on it. The registry records no public
27
+ * RPC endpoint — deliberately — so you bring your own.
28
+ */
29
+ export function toChain(network, opts) {
30
+ const info = getNetwork(network);
31
+ const explorer = info.explorerUrls?.[0];
32
+ return {
33
+ id: registry[network].chainId,
34
+ name: registry[network].name,
35
+ nativeCurrency: registry[network].nativeCurrency,
36
+ rpcUrls: {
37
+ default: {
38
+ http: [opts.rpcUrl],
39
+ ...(opts.wsUrl ? { webSocket: [opts.wsUrl] } : {}),
40
+ },
41
+ },
42
+ ...(explorer ? { blockExplorers: { default: { name: "Explorer", url: explorer } } } : {}),
43
+ };
44
+ }
45
+ /** One deployment: address, kind, version, status and — where recorded — the ABI. */
46
+ export function getContract(network, project, name) {
47
+ const book = registry[network]?.contracts;
48
+ if (!book)
49
+ throw new Error(`unknown network: ${String(network)}`);
50
+ const entry = book[project]?.[name];
51
+ if (!entry)
52
+ throw new Error(`unknown contract: ${String(project)}/${String(name)} on ${String(network)}`);
53
+ return entry;
54
+ }
55
+ export function getAddress(network, project, name) {
56
+ return getContract(network, project, name)
57
+ .address;
58
+ }
59
+ export function getAbi(network, project, name) {
60
+ return getContract(network, project, name).abi;
61
+ }
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@somnia-chain/contracts-sdk",
3
+ "version": "0.1.0",
4
+ "description": "Somnia contract addresses, ABIs and chain information, typed. Generated from the Somnia contract registry.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md"
19
+ ],
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/somnia-chain/contract-registry.git",
26
+ "directory": "sdk"
27
+ },
28
+ "keywords": [
29
+ "somnia",
30
+ "contracts",
31
+ "addresses",
32
+ "abi",
33
+ "web3"
34
+ ]
35
+ }