Skip to content

Commit

Permalink
feat(klesia): init
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Sep 1, 2024
1 parent 37509a8 commit e25fd52
Show file tree
Hide file tree
Showing 134 changed files with 521 additions and 107 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

.turbo
dist

# Logs
Expand Down
1 change: 0 additions & 1 deletion .turbo/cache/1aa298330ad02f31-meta.json

This file was deleted.

Binary file removed .turbo/cache/1aa298330ad02f31.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/1c9102b79471b0f5-meta.json

This file was deleted.

Binary file removed .turbo/cache/1c9102b79471b0f5.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/1d0efba35ca2970e-meta.json

This file was deleted.

Binary file removed .turbo/cache/1d0efba35ca2970e.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/2f46586394088f1d-meta.json

This file was deleted.

Binary file removed .turbo/cache/2f46586394088f1d.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/3dd7889bb40ab82a-meta.json

This file was deleted.

Binary file removed .turbo/cache/3dd7889bb40ab82a.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/41b765d0ea1c4abb-meta.json

This file was deleted.

Binary file removed .turbo/cache/41b765d0ea1c4abb.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/426d85d05f1125d4-meta.json

This file was deleted.

Binary file removed .turbo/cache/426d85d05f1125d4.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/42b97f968f64c135-meta.json

This file was deleted.

Binary file removed .turbo/cache/42b97f968f64c135.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/4883f8ac8b0355de-meta.json

This file was deleted.

Binary file removed .turbo/cache/4883f8ac8b0355de.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/572ab72c4c2dda9e-meta.json

This file was deleted.

Binary file removed .turbo/cache/572ab72c4c2dda9e.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/7ac2a161e1aa81b0-meta.json

This file was deleted.

Binary file removed .turbo/cache/7ac2a161e1aa81b0.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/8bc7f03fd570b2ed-meta.json

This file was deleted.

Binary file removed .turbo/cache/8bc7f03fd570b2ed.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/9b8892abb7e01e4e-meta.json

This file was deleted.

Binary file removed .turbo/cache/9b8892abb7e01e4e.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/9df78ba43790ddfd-meta.json

This file was deleted.

Binary file removed .turbo/cache/9df78ba43790ddfd.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/c71ed518e7beda73-meta.json

This file was deleted.

Binary file removed .turbo/cache/c71ed518e7beda73.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/cf4b544cb2131de8-meta.json

This file was deleted.

Binary file removed .turbo/cache/cf4b544cb2131de8.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/cfb9ea92802d73fa-meta.json

This file was deleted.

Binary file removed .turbo/cache/cfb9ea92802d73fa.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/d9eba1afee15d5d3-meta.json

This file was deleted.

Binary file removed .turbo/cache/d9eba1afee15d5d3.tar.zst
Binary file not shown.
1 change: 0 additions & 1 deletion .turbo/cache/fc0ef2a9abd4deca-meta.json

This file was deleted.

Binary file removed .turbo/cache/fc0ef2a9abd4deca.tar.zst
Binary file not shown.
Empty file removed .turbo/cookies/1.cookie
Empty file.
Empty file removed .turbo/cookies/10.cookie
Empty file.
Empty file removed .turbo/cookies/11.cookie
Empty file.
Empty file removed .turbo/cookies/12.cookie
Empty file.
Empty file removed .turbo/cookies/13.cookie
Empty file.
Empty file removed .turbo/cookies/14.cookie
Empty file.
Empty file removed .turbo/cookies/15.cookie
Empty file.
Empty file removed .turbo/cookies/16.cookie
Empty file.
Empty file removed .turbo/cookies/17.cookie
Empty file.
Empty file removed .turbo/cookies/18.cookie
Empty file.
Empty file removed .turbo/cookies/19.cookie
Empty file.
Empty file removed .turbo/cookies/2.cookie
Empty file.
Empty file removed .turbo/cookies/20.cookie
Empty file.
Empty file removed .turbo/cookies/21.cookie
Empty file.
Empty file removed .turbo/cookies/22.cookie
Empty file.
Empty file removed .turbo/cookies/23.cookie
Empty file.
Empty file removed .turbo/cookies/24.cookie
Empty file.
Empty file removed .turbo/cookies/25.cookie
Empty file.
Empty file removed .turbo/cookies/26.cookie
Empty file.
Empty file removed .turbo/cookies/27.cookie
Empty file.
Empty file removed .turbo/cookies/28.cookie
Empty file.
Empty file removed .turbo/cookies/29.cookie
Empty file.
Empty file removed .turbo/cookies/3.cookie
Empty file.
Empty file removed .turbo/cookies/30.cookie
Empty file.
Empty file removed .turbo/cookies/31.cookie
Empty file.
Empty file removed .turbo/cookies/32.cookie
Empty file.
Empty file removed .turbo/cookies/33.cookie
Empty file.
Empty file removed .turbo/cookies/34.cookie
Empty file.
Empty file removed .turbo/cookies/35.cookie
Empty file.
Empty file removed .turbo/cookies/36.cookie
Empty file.
Empty file removed .turbo/cookies/37.cookie
Empty file.
Empty file removed .turbo/cookies/38.cookie
Empty file.
Empty file removed .turbo/cookies/39.cookie
Empty file.
Empty file removed .turbo/cookies/4.cookie
Empty file.
Empty file removed .turbo/cookies/40.cookie
Empty file.
Empty file removed .turbo/cookies/41.cookie
Empty file.
Empty file removed .turbo/cookies/42.cookie
Empty file.
Empty file removed .turbo/cookies/43.cookie
Empty file.
Empty file removed .turbo/cookies/44.cookie
Empty file.
Empty file removed .turbo/cookies/45.cookie
Empty file.
Empty file removed .turbo/cookies/46.cookie
Empty file.
Empty file removed .turbo/cookies/47.cookie
Empty file.
Empty file removed .turbo/cookies/48.cookie
Empty file.
Empty file removed .turbo/cookies/49.cookie
Empty file.
Empty file removed .turbo/cookies/5.cookie
Empty file.
Empty file removed .turbo/cookies/50.cookie
Empty file.
Empty file removed .turbo/cookies/51.cookie
Empty file.
Empty file removed .turbo/cookies/52.cookie
Empty file.
Empty file removed .turbo/cookies/53.cookie
Empty file.
Empty file removed .turbo/cookies/54.cookie
Empty file.
Empty file removed .turbo/cookies/55.cookie
Empty file.
Empty file removed .turbo/cookies/56.cookie
Empty file.
Empty file removed .turbo/cookies/57.cookie
Empty file.
Empty file removed .turbo/cookies/58.cookie
Empty file.
Empty file removed .turbo/cookies/59.cookie
Empty file.
Empty file removed .turbo/cookies/6.cookie
Empty file.
Empty file removed .turbo/cookies/60.cookie
Empty file.
Empty file removed .turbo/cookies/61.cookie
Empty file.
Empty file removed .turbo/cookies/62.cookie
Empty file.
Empty file removed .turbo/cookies/63.cookie
Empty file.
Empty file removed .turbo/cookies/64.cookie
Empty file.
Empty file removed .turbo/cookies/65.cookie
Empty file.
Empty file removed .turbo/cookies/66.cookie
Empty file.
Empty file removed .turbo/cookies/67.cookie
Empty file.
Empty file removed .turbo/cookies/68.cookie
Empty file.
Empty file removed .turbo/cookies/69.cookie
Empty file.
Empty file removed .turbo/cookies/7.cookie
Empty file.
Empty file removed .turbo/cookies/70.cookie
Empty file.
Empty file removed .turbo/cookies/71.cookie
Empty file.
Empty file removed .turbo/cookies/8.cookie
Empty file.
Empty file removed .turbo/cookies/9.cookie
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions apps/klesia/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MINA_NETWORK=devnet
NODE_API_DEVNET=https://api.minascan.io/node/devnet/v1/graphql
NODE_API_MAINNET=https://api.minascan.io/node/mainnet/v1/graphql
3 changes: 3 additions & 0 deletions apps/klesia/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Klesia

JSON-RPC 2.0 wrapper over common Mina Protocol tools and services.
16 changes: 16 additions & 0 deletions apps/klesia/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@mina-js/klesia",
"scripts": {
"dev": "bun run --hot src/index.ts"
},
"dependencies": {
"@hono/zod-openapi": "^0.16.0",
"@scalar/hono-api-reference": "^0.5.143",
"@urql/core": "^5.0.6",
"dayjs": "^1.11.13",
"hono": "^4.5.10",
"ofetch": "^1.3.4",
"ts-pattern": "^5.3.1",
"zod": "^3.23.8"
}
}
74 changes: 74 additions & 0 deletions apps/klesia/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { OpenAPIHono, createRoute } from "@hono/zod-openapi";
import { apiReference } from "@scalar/hono-api-reference";
import { match } from "ts-pattern";
import { mina } from "./methods/mina";
import { RpcMethodSchema, RpcResponseSchema } from "./schema";
import { buildResponse } from "./utils/build-response";

const api = new OpenAPIHono();

api.doc("/api/openapi", {
openapi: "3.0.0",
info: {
version: "1.0.0",
title: "Klesia API",
},
});

const rpcRoute = createRoute({
method: "post",
path: "/api",
request: {
body: { content: { "application/json": { schema: RpcMethodSchema } } },
},
responses: {
200: {
content: {
"application/json": {
schema: RpcResponseSchema,
},
},
description: "JSON-RPC response.",
},
},
});

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 });
return json(buildResponse(result), 200);
})
.with({ method: "mina_getBalance" }, async ({ params }) => {
const [publicKey] = params;
const result = await mina.getBalance({ publicKey });
return json(buildResponse(result), 200);
})
.with({ method: "mina_blockHash" }, async () => {
const result = await mina.blockHash();
return json(buildResponse(result), 200);
})
.with({ method: "mina_chainId" }, async () => {
const result = await mina.chainId();
return json(buildResponse(result), 200);
})
.with({ method: "mina_sendTransaction" }, async ({ params }) => {
const [signedTransaction, type] = params;
const result = await mina.sendTransaction({ signedTransaction, type });
return json(buildResponse(result), 200);
})
.exhaustive();
});

api.get(
"/api",
apiReference({
spec: {
url: "/api/openapi",
},
}),
);

export default api;
133 changes: 133 additions & 0 deletions apps/klesia/src/methods/mina.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { gql } from "@urql/core";
import { match } from "ts-pattern";
import { getNodeClient } from "../utils/node";

const getTransactionCount = async ({ publicKey }: { publicKey: string }) => {
const client = getNodeClient();
const { data } = await client.query(
gql`
query {
account(publicKey: $publicKey) {
nonce
}
}
`,
{ publicKey },
);
return Number.parseInt(data.account.nonce);
};

const getBalance = async ({ publicKey }: { publicKey: string }) => {
const client = getNodeClient();
const { data } = await client.query(
gql`
query {
account(publicKey: $publicKey) {
balance {
total
}
}
}
`,
{ publicKey },
);
return data.account.balance.total;
};

const blockHash = async () => {
const client = getNodeClient();
const { data } = await client.query(
gql`
query {
daemonStatus {
stateHash
}
}
`,
{},
);
return data.daemonStatus.stateHash;
};

const chainId = async () => {
const client = getNodeClient();
const { data } = await client.query(
gql`
query {
daemonStatus {
chainId
}
}
`,
{},
);
return data.daemonStatus.chainId;
};

const sendTransaction = async ({
signedTransaction,
type,
}: {
// biome-ignore lint/suspicious/noExplicitAny: TODO
signedTransaction: any;
type: "payment" | "delegation" | "zkapp";
}) => {
const client = getNodeClient();
return match(type)
.with("payment", async () => {
const { signature, data: input } = signedTransaction;
const { data } = await client.mutation(
gql`
mutation {
sendPayment(signature: $signature, input: $input) {
payment {
hash
}
}
}
`,
{ signature, input },
);
return data.sendPayment.payment.hash;
})
.with("delegation", async () => {
const { signature, data: input } = signedTransaction;
const { data } = await client.mutation(
gql`
mutation {
sendDelegation(signature: $signature, input: $input) {
delegation {
hash
}
}
}
`,
{ signature, input },
);
return data.sendDelegation.delegation.hash;
})
.with("zkapp", async () => {
const { data } = await client.mutation(
gql`
mutation {
sendZkapp(input: $input) {
zkapp {
hash
}
}
}
`,
{ input: signedTransaction },
);
return data.sendZkapp.zkapp.hash;
})
.exhaustive();
};

export const mina = {
getTransactionCount,
getBalance,
blockHash,
chainId,
sendTransaction,
};
34 changes: 34 additions & 0 deletions apps/klesia/src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { z } from "zod";

export const PublicKeySchema = z.string().length(55);
export const PublicKeyParamsSchema = z.array(PublicKeySchema).length(1);
export const EmptyParamsSchema = z.array(z.string()).length(0).optional();
export const SendTransactionSchema = z.array(z.any(), z.string()).length(2);

export const RpcMethodSchema = z.discriminatedUnion("method", [
z.object({
method: z.literal("mina_getTransactionCount"),
params: PublicKeyParamsSchema,
}),
z.object({
method: z.literal("mina_getBalance"),
params: PublicKeyParamsSchema,
}),
z.object({
method: z.literal("mina_blockHash"),
params: EmptyParamsSchema,
}),
z.object({
method: z.literal("mina_chainId"),
params: EmptyParamsSchema,
}),
z.object({
method: z.literal("mina_sendTransaction"),
params: SendTransactionSchema,
}),
]);

export const RpcResponseSchema = z.object({
jsonrpc: z.literal("2.0"),
result: z.unknown(),
});
4 changes: 4 additions & 0 deletions apps/klesia/src/utils/build-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const buildResponse = (data: unknown) => ({
jsonrpc: "2.0",
result: data,
});
17 changes: 17 additions & 0 deletions apps/klesia/src/utils/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Client, cacheExchange, fetchExchange } from "@urql/core";
import { match } from "ts-pattern";

const MINA_NETWORK = process.env.MINA_NETWORK;

export const getNodeApiUrl = () => {
return match(MINA_NETWORK)
.with("devnet", () => process.env.NODE_API_DEVNET)
.with("mainnet", () => process.env.NODE_API_MAINNET)
.run();
};

export const getNodeClient = () => {
const url = getNodeApiUrl();
if (!url) throw new Error("Invalid network config.");
return new Client({ url, exchanges: [cacheExchange, fetchExchange] });
};
7 changes: 7 additions & 0 deletions apps/klesia/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"strict": true,
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx"
}
}
Binary file modified bun.lockb
Binary file not shown.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"test": "bun run --filter '*' test",
"cleanup": "bun run --filter '*' cleanup",
"lint": "bunx biome check .",
"format": "bunx biome check . --write",
"format:unsafe": "bunx biome check . --write --unsafe"
"format": "bunx biome check . --write --unsafe"
},
"devDependencies": {
"@biomejs/biome": "1.8.3",
Expand All @@ -19,6 +18,6 @@
"turbo": "^2.0.11",
"typescript": "5.5.4"
},
"workspaces": ["packages/*"],
"workspaces": ["packages/*", "apps/*"],
"packageManager": "[email protected]"
}
10 changes: 5 additions & 5 deletions packages/accounts/.turbo/turbo-build.log
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
CLI Cleaning output folder
ESM Build start
CJS Build start
ESM dist/index.mjs 575.00 B
ESM dist/index.mjs.map 1.35 KB
ESM ⚡️ Build success in 6ms
CJS dist/index.js 2.25 KB
CJS dist/index.js.map 1.44 KB
CJS ⚡️ Build success in 6ms
CJS ⚡️ Build success in 8ms
ESM dist/index.mjs 575.00 B
ESM dist/index.mjs.map 1.35 KB
ESM ⚡️ Build success in 8ms
DTS Build start
DTS ⚡️ Build success in 483ms
DTS ⚡️ Build success in 499ms
DTS dist/index.d.mts 631.00 B
DTS dist/index.d.ts 631.00 B
Loading

0 comments on commit e25fd52

Please sign in to comment.