Skip to content

Commit b5e48fd

Browse files
feat: add @mina-js/klesia-utils package with handleJsonRpcRequest (#11)
Introduce a new package @mina-js/klesia-utils that exports the handleJsonRpcRequest function. This utility converts JSON-RPC requests into Mina node GraphQL requests, enabling seamless integration between different request formats.
1 parent 96c164f commit b5e48fd

File tree

21 files changed

+493
-415
lines changed

21 files changed

+493
-415
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ jobs:
1414
- run: bun i --no-save
1515
- run: bun run build
1616
- run: bun run test
17-
- run: bunx pkg-pr-new publish './packages/klesia-sdk' './packages/accounts' './packages/connect' './packages/providers' './packages/utils'
17+
- run: bunx pkg-pr-new publish './packages/klesia-sdk' './packages/klesia-utils' './packages/accounts' './packages/connect' './packages/providers' './packages/utils'

apps/klesia/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@
1818
"cleanup": "rimraf dist .turbo node_modules"
1919
},
2020
"devDependencies": {
21+
"@mina-js/klesia-utils": "workspace:*",
2122
"@mina-js/utils": "workspace:*"
2223
},
2324
"dependencies": {
2425
"@hono/node-server": "1.13.8",
2526
"@hono/zod-openapi": "0.19.2",
26-
"@urql/core": "5.1.1",
27-
"bigint-quantile": "0.0.2",
28-
"dayjs": "1.11.13",
2927
"dotenv": "16.4.7",
3028
"hono": "4.7.4",
3129
"hono-rate-limiter": "0.4.2",
3230
"nanoid": "5.1.3",
33-
"ofetch": "1.4.1",
3431
"ts-pattern": "5.6.2",
3532
"zod": "3.24.2"
3633
}

apps/klesia/src/index.ts

Lines changed: 7 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import { getConnInfo } from "@hono/node-server/conninfo";
22
import { OpenAPIHono, createRoute } from "@hono/zod-openapi";
33
import {
4-
KlesiaRpcMethod,
54
KlesiaRpcMethodSchema,
65
KlesiaRpcResponseSchema,
7-
PublicKeySchema,
8-
} from "@mina-js/utils";
6+
} from "@mina-js/klesia-utils";
7+
import { handleJsonRpcRequest } from "@mina-js/klesia-utils";
98
import { rateLimiter } from "hono-rate-limiter";
109
import { cors } from "hono/cors";
1110
import { logger } from "hono/logger";
1211
import { nanoid } from "nanoid";
13-
import { match } from "ts-pattern";
14-
import { mina } from "./methods/mina";
15-
import { buildResponse } from "./utils/build-response";
16-
import { z } from "zod";
12+
import { getNodeApiUrl } from "./utils/node";
1713

1814
export const api = new OpenAPIHono();
1915

@@ -62,78 +58,10 @@ const rpcRoute = createRoute({
6258
});
6359

6460
export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
65-
const body = KlesiaRpcMethodSchema.parse(await req.json());
66-
return match(body)
67-
.with(
68-
{ method: KlesiaRpcMethod.enum.mina_getTransactionCount },
69-
async ({ params }) => {
70-
const [publicKey] = params;
71-
const result = await mina.getTransactionCount({
72-
publicKey: PublicKeySchema.parse(publicKey),
73-
});
74-
return json(
75-
buildResponse({
76-
result,
77-
}),
78-
200,
79-
);
80-
},
81-
)
82-
.with(
83-
{ method: KlesiaRpcMethod.enum.mina_getBalance },
84-
async ({ params }) => {
85-
const [publicKey, tokenId] = params;
86-
const result = await mina.getBalance({
87-
publicKey: PublicKeySchema.parse(publicKey),
88-
tokenId: tokenId !== undefined ? z.string().parse(tokenId) : "1",
89-
});
90-
return json(buildResponse({ result }), 200);
91-
},
92-
)
93-
.with({ method: KlesiaRpcMethod.enum.mina_blockHash }, async () => {
94-
if (process.env.MINA_NETWORK === "zeko_devnet") {
95-
return json(
96-
buildResponse({
97-
error: {
98-
code: -32600,
99-
message: "Network not supported.",
100-
},
101-
}),
102-
200,
103-
);
104-
}
105-
const result = await mina.blockHash();
106-
return json(buildResponse({ result }), 200);
107-
})
108-
.with({ method: KlesiaRpcMethod.enum.mina_networkId }, async () => {
109-
const result = await mina.networkId();
110-
return json(buildResponse({ result }), 200);
111-
})
112-
.with(
113-
{ method: KlesiaRpcMethod.enum.mina_sendTransaction },
114-
async ({ params }) => {
115-
const [signedTransaction, type] = params;
116-
const result = await mina.sendTransaction({ signedTransaction, type });
117-
return json(
118-
buildResponse({
119-
result,
120-
}),
121-
200,
122-
);
123-
},
124-
)
125-
.with(
126-
{ method: KlesiaRpcMethod.enum.mina_getAccount },
127-
async ({ params }) => {
128-
const [publicKey, tokenId] = params;
129-
const result = await mina.getAccount({
130-
publicKey: PublicKeySchema.parse(publicKey),
131-
tokenId: tokenId !== undefined ? z.string().parse(tokenId) : "1",
132-
});
133-
return json(buildResponse({ result }), 200);
134-
},
135-
)
136-
.exhaustive();
61+
return json(
62+
await handleJsonRpcRequest(getNodeApiUrl(), await req.json()),
63+
200,
64+
);
13765
});
13866

13967
export type KlesiaRpc = typeof klesiaRpcRoute;

apps/klesia/src/methods/mina.ts

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

apps/klesia/src/utils/node.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { KlesiaNetwork } from "@mina-js/utils";
2-
import { Client, cacheExchange, fetchExchange } from "@urql/core";
1+
import { KlesiaNetwork } from "@mina-js/klesia-utils";
32
import { match } from "ts-pattern";
43
import { z } from "zod";
54

@@ -27,9 +26,3 @@ export const getNodeApiUrl = () => {
2726
.with("zeko_devnet", () => NODE_API_ZEKO_DEVNET)
2827
.exhaustive();
2928
};
30-
31-
export const getNodeClient = () => {
32-
const url = getNodeApiUrl();
33-
if (!url) throw new Error("Invalid network config.");
34-
return new Client({ url, exchanges: [cacheExchange, fetchExchange] });
35-
};

bun.lockb

-1.29 KB
Binary file not shown.

packages/klesia-sdk/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"typescript": "^5.0.0"
2121
},
2222
"devDependencies": {
23-
"@mina-js/utils": "workspace:*"
23+
"@mina-js/utils": "workspace:*",
24+
"@mina-js/klesia-utils": "workspace:*"
2425
},
2526
"dependencies": {
2627
"micro-ftch": "0.4.2",

packages/klesia-sdk/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
KlesiaNetwork,
33
type KlesiaRpcRequestType,
44
type KlesiaRpcResponseType,
5-
} from "@mina-js/utils";
5+
} from "@mina-js/klesia-utils";
66
import { ftch, jsonrpc } from "micro-ftch";
77
import { match } from "ts-pattern";
88

0 commit comments

Comments
 (0)