Skip to content

Commit f54eec6

Browse files
committed
feat(klesia): add simple docs
1 parent e25e977 commit f54eec6

File tree

7 files changed

+90
-18
lines changed

7 files changed

+90
-18
lines changed

apps/klesia/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"@urql/core": "^5.0.6",
1414
"dayjs": "^1.11.13",
1515
"hono": "^4.5.10",
16+
"hono-rate-limiter": "^0.4.0",
17+
"nanoid": "^5.0.7",
1618
"ofetch": "^1.3.4",
1719
"ts-pattern": "^5.3.1",
1820
"zod": "^3.23.8"

apps/klesia/src/custom.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module "*.md";

apps/klesia/src/docs.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Klesia RPC
2+
3+
Klesia RPC is a JSON-RPC 2.0 wrapper over common Mina Protocol tools and services. The intention is to provide an outstanding Developer Experience for Mina Protocol's zkApp Developers.
4+
5+
## Methods
6+
7+
### mina_getTransactionCount
8+
9+
Returns the number of transactions sent from an address. Usually you may want to use this number to determine the nonce for the next transaction.
10+
11+
#### Parameters
12+
13+
Array of strings:
14+
- `publicKey` - Address to check for transaction count.
15+
16+
---
17+
18+
### mina_getBalance
19+
20+
Returns the balance of the account of given address.
21+
22+
#### Parameters
23+
24+
Array of strings:
25+
- `publicKey` - Address to check for transaction count.
26+
27+
---
28+
29+
### mina_blockHash
30+
31+
Returns the hash of the most recent block.
32+
33+
---
34+
35+
### mina_chainId
36+
37+
Returns the currently configured chain ID.
38+
39+
---
40+
41+
### mina_sendTransaction
42+
43+
Broadcasts a signed transaction to the network.
44+
45+
#### Parameters
46+
47+
Array of strings:
48+
- `input` - Signed transaction or zkApp input.
49+
- `type` - Transaction type. Can be `payment`, `delegation`, or `zkapp`.

apps/klesia/src/index.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import { OpenAPIHono, createRoute } from "@hono/zod-openapi";
2+
import { PublicKeySchema } from "@mina-js/shared";
23
import { apiReference } from "@scalar/hono-api-reference";
4+
import { rateLimiter } from "hono-rate-limiter";
5+
import { getConnInfo } from "hono/bun";
36
import { logger } from "hono/logger";
7+
import { nanoid } from "nanoid";
48
import { match } from "ts-pattern";
9+
import rpcDocs from "./docs.md" with { type: "text" };
510
import { mina } from "./methods/mina";
611
import { RpcMethodSchema, RpcResponseSchema } from "./schema";
712
import { buildResponse } from "./utils/build-response";
8-
import { PublicKeySchema } from "@mina-js/shared";
913

1014
const api = new OpenAPIHono();
1115

1216
api.use(logger());
17+
api.use(
18+
rateLimiter({
19+
keyGenerator: (c) => getConnInfo(c).remote.address ?? nanoid(),
20+
limit: 10,
21+
}),
22+
);
1323

1424
api.doc("/api/openapi", {
1525
openapi: "3.0.0",
@@ -22,6 +32,7 @@ api.doc("/api/openapi", {
2232
const rpcRoute = createRoute({
2333
method: "post",
2434
path: "/api",
35+
description: rpcDocs,
2536
request: {
2637
body: { content: { "application/json": { schema: RpcMethodSchema } } },
2738
},
@@ -37,17 +48,23 @@ const rpcRoute = createRoute({
3748
},
3849
});
3950

51+
api.get("/", ({ redirect }) => redirect("/api", 301));
52+
4053
api.openapi(rpcRoute, async ({ req, json }) => {
4154
const body = req.valid("json");
4255
return match(body)
4356
.with({ method: "mina_getTransactionCount" }, async ({ params }) => {
4457
const [publicKey] = params;
45-
const result = await mina.getTransactionCount({ publicKey: PublicKeySchema.parse(publicKey) });
58+
const result = await mina.getTransactionCount({
59+
publicKey: PublicKeySchema.parse(publicKey),
60+
});
4661
return json(buildResponse(result), 200);
4762
})
4863
.with({ method: "mina_getBalance" }, async ({ params }) => {
4964
const [publicKey] = params;
50-
const result = await mina.getBalance({ publicKey: PublicKeySchema.parse(publicKey) });
65+
const result = await mina.getBalance({
66+
publicKey: PublicKeySchema.parse(publicKey),
67+
});
5168
return json(buildResponse(result), 200);
5269
})
5370
.with({ method: "mina_blockHash" }, async () => {
@@ -72,6 +89,7 @@ api.get(
7289
spec: {
7390
url: "/api/openapi",
7491
},
92+
theme: "deepSpace",
7593
}),
7694
);
7795

apps/klesia/src/methods/mina.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { SignedTransactionSchema } from "@mina-js/shared";
12
import { gql } from "@urql/core";
23
import { match } from "ts-pattern";
34
import { getNodeClient } from "../utils/node";
4-
import { SignedTransactionSchema } from "@mina-js/shared";
55

66
const getTransactionCount = async ({ publicKey }: { publicKey: string }) => {
77
const client = getNodeClient();
@@ -76,7 +76,8 @@ const sendTransaction = async ({
7676
const client = getNodeClient();
7777
return match(type)
7878
.with("payment", async () => {
79-
const { signature, data: input } = SignedTransactionSchema.parse(signedTransaction)
79+
const { signature, data: input } =
80+
SignedTransactionSchema.parse(signedTransaction);
8081
const { data } = await client.mutation(
8182
gql`
8283
mutation {
@@ -92,7 +93,8 @@ const sendTransaction = async ({
9293
return data.sendPayment.payment.hash;
9394
})
9495
.with("delegation", async () => {
95-
const { signature, data: input } = SignedTransactionSchema.parse(signedTransaction)
96+
const { signature, data: input } =
97+
SignedTransactionSchema.parse(signedTransaction);
9698
const { data } = await client.mutation(
9799
gql`
98100
mutation {

bun.lockb

760 Bytes
Binary file not shown.

turbo.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
2-
"$schema": "https://turbo.build/schema.json",
3-
"tasks": {
4-
"build": {
5-
"dependsOn": ["^build"],
6-
"outputs": ["dist/**"]
7-
},
8-
"cleanup": {},
9-
"dev": {
10-
"persistent": true,
11-
"cache": false
12-
}
13-
}
2+
"$schema": "https://turbo.build/schema.json",
3+
"tasks": {
4+
"build": {
5+
"dependsOn": ["^build"],
6+
"outputs": ["dist/**"]
7+
},
8+
"cleanup": {},
9+
"dev": {
10+
"persistent": true,
11+
"cache": false
12+
}
13+
}
1414
}

0 commit comments

Comments
 (0)