Skip to content

Commit

Permalink
feat(klesia sdk): slim down
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Nov 4, 2024
1 parent 4057f3d commit fdb1786
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 209 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
- run: bun i --no-save
- run: bun run build
- run: bun run test
- run: bunx pkg-pr-new publish './packages/klesia-sdk' './packages/accounts' './packages/connect' './packages/providers' './packages/utils' './apps/klesia'
- run: bunx pkg-pr-new publish './packages/klesia-sdk' './packages/accounts' './packages/connect' './packages/providers' './packages/utils'
5 changes: 0 additions & 5 deletions apps/klesia/docs/index.txt

This file was deleted.

45 changes: 0 additions & 45 deletions apps/klesia/docs/rpc.txt

This file was deleted.

10 changes: 4 additions & 6 deletions apps/klesia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
"./schema": {
"types": "./dist/schema.d.ts",
"default": "./dist/schema.js"
},
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
Expand All @@ -21,15 +17,17 @@
"test": "bun test --rerun-each 3",
"cleanup": "rimraf dist .turbo node_modules"
},
"devDependencies": {
"@mina-js/utils": "workspace:*"
},
"dependencies": {
"@hono/node-server": "^1.12.2",
"@hono/zod-openapi": "^0.16.0",
"@mina-js/utils": "workspace:*",
"@urql/core": "^5.0.6",
"bigint-quantile": "^0.0.2",
"dayjs": "^1.11.13",
"dotenv": "^16.4.5",
"hono": "^4.5.10",
"hono": "4.6.9",
"hono-rate-limiter": "^0.4.0",
"nanoid": "^5.0.7",
"ofetch": "^1.3.4",
Expand Down
69 changes: 35 additions & 34 deletions apps/klesia/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { getConnInfo } from "@hono/node-server/conninfo";
import { OpenAPIHono, createRoute } from "@hono/zod-openapi";
import { PublicKeySchema } from "@mina-js/utils";
import {
KlesiaRpcMethod,
KlesiaRpcMethodSchema,
KlesiaRpcResponseSchema,
PublicKeySchema,
} from "@mina-js/utils";
import { rateLimiter } from "hono-rate-limiter";
import { cors } from "hono/cors";
import { logger } from "hono/logger";
import { nanoid } from "nanoid";
import { match } from "ts-pattern";
import mainDocs from "../docs/index.txt";
import rpcDocs from "../docs/rpc.txt";
import { mina } from "./methods/mina";
import { RpcMethod, RpcMethodSchema, RpcResponseSchema } from "./schema";
import { buildResponse } from "./utils/build-response";

export const api = new OpenAPIHono();
Expand All @@ -35,22 +37,22 @@ api.doc("/api/openapi", {
info: {
version: "1.0.0",
title: "Klesia RPC",
description: mainDocs,
},
});

const rpcRoute = createRoute({
method: "post",
path: "/api",
description: rpcDocs,
request: {
body: { content: { "application/json": { schema: RpcMethodSchema } } },
body: {
content: { "application/json": { schema: KlesiaRpcMethodSchema } },
},
},
responses: {
200: {
content: {
"application/json": {
schema: RpcResponseSchema,
schema: KlesiaRpcResponseSchema,
},
},
description: "JSON-RPC response.",
Expand All @@ -59,10 +61,10 @@ const rpcRoute = createRoute({
});

export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
const body = req.valid("json");
const body = KlesiaRpcMethodSchema.parse(await req.json());
return match(body)
.with(
{ method: RpcMethod.enum.mina_getTransactionCount },
{ method: KlesiaRpcMethod.enum.mina_getTransactionCount },
async ({ params }) => {
const [publicKey] = params;
const result = await mina.getTransactionCount({
Expand All @@ -76,14 +78,17 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
);
},
)
.with({ method: RpcMethod.enum.mina_getBalance }, async ({ params }) => {
const [publicKey] = params;
const result = await mina.getBalance({
publicKey: PublicKeySchema.parse(publicKey),
});
return json(buildResponse({ result }), 200);
})
.with({ method: RpcMethod.enum.mina_blockHash }, async () => {
.with(
{ method: KlesiaRpcMethod.enum.mina_getBalance },
async ({ params }) => {
const [publicKey] = params;
const result = await mina.getBalance({
publicKey: PublicKeySchema.parse(publicKey),
});
return json(buildResponse({ result }), 200);
},
)
.with({ method: KlesiaRpcMethod.enum.mina_blockHash }, async () => {
if (process.env.MINA_NETWORK === "zeko_devnet") {
return json(
buildResponse({
Expand All @@ -98,12 +103,12 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
const result = await mina.blockHash();
return json(buildResponse({ result }), 200);
})
.with({ method: RpcMethod.enum.mina_chainId }, async () => {
.with({ method: KlesiaRpcMethod.enum.mina_chainId }, async () => {
const result = await mina.chainId();
return json(buildResponse({ result }), 200);
})
.with(
{ method: RpcMethod.enum.mina_sendTransaction },
{ method: KlesiaRpcMethod.enum.mina_sendTransaction },
async ({ params }) => {
const [signedTransaction, type] = params;
const result = await mina.sendTransaction({ signedTransaction, type });
Expand All @@ -115,21 +120,17 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
);
},
)
.with({ method: RpcMethod.enum.mina_getAccount }, async ({ params }) => {
const [publicKey] = params;
const result = await mina.getAccount({
publicKey: PublicKeySchema.parse(publicKey),
});
return json(buildResponse({ result }), 200);
})
.with(
{ method: KlesiaRpcMethod.enum.mina_getAccount },
async ({ params }) => {
const [publicKey] = params;
const result = await mina.getAccount({
publicKey: PublicKeySchema.parse(publicKey),
});
return json(buildResponse({ result }), 200);
},
)
.exhaustive();
});

export type KlesiaRpc = typeof klesiaRpcRoute;
export {
KlesiaNetwork,
RpcMethod,
type RpcMethodType,
type RpcResponseType,
type RpcErrorType,
} from "./schema";
105 changes: 0 additions & 105 deletions apps/klesia/src/schema.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/klesia/src/utils/node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { KlesiaNetwork } from "@mina-js/utils";
import { Client, cacheExchange, fetchExchange } from "@urql/core";
import { match } from "ts-pattern";
import { z } from "zod";
import { KlesiaNetwork } from "../schema";

const MINA_NETWORK = KlesiaNetwork.parse(process.env.MINA_NETWORK ?? "devnet");
const NODE_API_DEVNET = z
Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion packages/klesia-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
"peerDependencies": {
"typescript": "^5.0.0"
},
"devDependencies": {
"@mina-js/utils": "workspace:*"
},
"dependencies": {
"@mina-js/klesia": "workspace:*",
"micro-ftch": "^0.4.0",
"ts-pattern": "^5.3.1"
}
Expand Down
10 changes: 5 additions & 5 deletions packages/klesia-sdk/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
KlesiaNetwork,
type RpcRequestType,
type RpcResponseType,
} from "@mina-js/klesia/schema";
type KlesiaRpcRequestType,
type KlesiaRpcResponseType,
} from "@mina-js/utils";
import { ftch, jsonrpc } from "micro-ftch";
import { match } from "ts-pattern";

Expand All @@ -25,9 +25,9 @@ export const createClient = ({ network, customUrl }: CreateClientProps) => {
)
.exhaustive();
const rpc = jsonrpc(net, baseUrl);
const request = async <T extends string>(req: RpcRequestType) => {
const request = async <T extends string>(req: KlesiaRpcRequestType) => {
const params = req.params ?? [];
const json: Extract<RpcResponseType, { method: T }>["result"] =
const json: Extract<KlesiaRpcResponseType, { method: T }>["result"] =
await rpc.call(req.method, ...params);
return json;
};
Expand Down
5 changes: 2 additions & 3 deletions packages/providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
"test": "bun test",
"cleanup": "rimraf dist .turbo"
},
"dependencies": {
"@mina-js/utils": "workspace:*",
"zod": "3.23.8"
"devDependencies": {
"@mina-js/utils": "workspace:*"
},
"peerDependencies": {
"typescript": "^5.0.0"
Expand Down
Loading

0 comments on commit fdb1786

Please sign in to comment.