Skip to content

Commit

Permalink
feat(klesia): add simple docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Sep 2, 2024
1 parent e25e977 commit f54eec6
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 18 deletions.
2 changes: 2 additions & 0 deletions apps/klesia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"@urql/core": "^5.0.6",
"dayjs": "^1.11.13",
"hono": "^4.5.10",
"hono-rate-limiter": "^0.4.0",
"nanoid": "^5.0.7",
"ofetch": "^1.3.4",
"ts-pattern": "^5.3.1",
"zod": "^3.23.8"
Expand Down
1 change: 1 addition & 0 deletions apps/klesia/src/custom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "*.md";
49 changes: 49 additions & 0 deletions apps/klesia/src/docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Klesia RPC

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.

## Methods

### mina_getTransactionCount

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.

#### Parameters

Array of strings:
- `publicKey` - Address to check for transaction count.

---

### mina_getBalance

Returns the balance of the account of given address.

#### Parameters

Array of strings:
- `publicKey` - Address to check for transaction count.

---

### mina_blockHash

Returns the hash of the most recent block.

---

### mina_chainId

Returns the currently configured chain ID.

---

### mina_sendTransaction

Broadcasts a signed transaction to the network.

#### Parameters

Array of strings:
- `input` - Signed transaction or zkApp input.
- `type` - Transaction type. Can be `payment`, `delegation`, or `zkapp`.
24 changes: 21 additions & 3 deletions apps/klesia/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { OpenAPIHono, createRoute } from "@hono/zod-openapi";
import { PublicKeySchema } from "@mina-js/shared";
import { apiReference } from "@scalar/hono-api-reference";
import { rateLimiter } from "hono-rate-limiter";
import { getConnInfo } from "hono/bun";
import { logger } from "hono/logger";
import { nanoid } from "nanoid";
import { match } from "ts-pattern";
import rpcDocs from "./docs.md" with { type: "text" };
import { mina } from "./methods/mina";
import { RpcMethodSchema, RpcResponseSchema } from "./schema";
import { buildResponse } from "./utils/build-response";
import { PublicKeySchema } from "@mina-js/shared";

const api = new OpenAPIHono();

api.use(logger());
api.use(
rateLimiter({
keyGenerator: (c) => getConnInfo(c).remote.address ?? nanoid(),
limit: 10,
}),
);

api.doc("/api/openapi", {
openapi: "3.0.0",
Expand All @@ -22,6 +32,7 @@ api.doc("/api/openapi", {
const rpcRoute = createRoute({
method: "post",
path: "/api",
description: rpcDocs,
request: {
body: { content: { "application/json": { schema: RpcMethodSchema } } },
},
Expand All @@ -37,17 +48,23 @@ const rpcRoute = createRoute({
},
});

api.get("/", ({ redirect }) => redirect("/api", 301));

api.openapi(rpcRoute, async ({ req, json }) => {
const body = req.valid("json");
return match(body)
.with({ method: "mina_getTransactionCount" }, async ({ params }) => {
const [publicKey] = params;
const result = await mina.getTransactionCount({ publicKey: PublicKeySchema.parse(publicKey) });
const result = await mina.getTransactionCount({
publicKey: PublicKeySchema.parse(publicKey),
});
return json(buildResponse(result), 200);
})
.with({ method: "mina_getBalance" }, async ({ params }) => {
const [publicKey] = params;
const result = await mina.getBalance({ publicKey: PublicKeySchema.parse(publicKey) });
const result = await mina.getBalance({
publicKey: PublicKeySchema.parse(publicKey),
});
return json(buildResponse(result), 200);
})
.with({ method: "mina_blockHash" }, async () => {
Expand All @@ -72,6 +89,7 @@ api.get(
spec: {
url: "/api/openapi",
},
theme: "deepSpace",
}),
);

Expand Down
8 changes: 5 additions & 3 deletions apps/klesia/src/methods/mina.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SignedTransactionSchema } from "@mina-js/shared";
import { gql } from "@urql/core";
import { match } from "ts-pattern";
import { getNodeClient } from "../utils/node";
import { SignedTransactionSchema } from "@mina-js/shared";

const getTransactionCount = async ({ publicKey }: { publicKey: string }) => {
const client = getNodeClient();
Expand Down Expand Up @@ -76,7 +76,8 @@ const sendTransaction = async ({
const client = getNodeClient();
return match(type)
.with("payment", async () => {
const { signature, data: input } = SignedTransactionSchema.parse(signedTransaction)
const { signature, data: input } =
SignedTransactionSchema.parse(signedTransaction);
const { data } = await client.mutation(
gql`
mutation {
Expand All @@ -92,7 +93,8 @@ const sendTransaction = async ({
return data.sendPayment.payment.hash;
})
.with("delegation", async () => {
const { signature, data: input } = SignedTransactionSchema.parse(signedTransaction)
const { signature, data: input } =
SignedTransactionSchema.parse(signedTransaction);
const { data } = await client.mutation(
gql`
mutation {
Expand Down
Binary file modified bun.lockb
Binary file not shown.
24 changes: 12 additions & 12 deletions turbo.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"$schema": "https://turbo.build/schema.json",
"tasks": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**"]
},
"cleanup": {},
"dev": {
"persistent": true,
"cache": false
}
}
"$schema": "https://turbo.build/schema.json",
"tasks": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**"]
},
"cleanup": {},
"dev": {
"persistent": true,
"cache": false
}
}
}

0 comments on commit f54eec6

Please sign in to comment.