diff --git a/apps/klesia/src/index.ts b/apps/klesia/src/index.ts index 9886658..ae0834f 100644 --- a/apps/klesia/src/index.ts +++ b/apps/klesia/src/index.ts @@ -13,6 +13,7 @@ import { nanoid } from "nanoid"; import { match } from "ts-pattern"; import { mina } from "./methods/mina"; import { buildResponse } from "./utils/build-response"; +import { z } from "zod"; export const api = new OpenAPIHono(); @@ -81,9 +82,10 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => { .with( { method: KlesiaRpcMethod.enum.mina_getBalance }, async ({ params }) => { - const [publicKey] = params; + const [publicKey, tokenId] = params; const result = await mina.getBalance({ publicKey: PublicKeySchema.parse(publicKey), + tokenId: tokenId !== undefined ? z.string().parse(tokenId) : "1", }); return json(buildResponse({ result }), 200); }, @@ -123,9 +125,10 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => { .with( { method: KlesiaRpcMethod.enum.mina_getAccount }, async ({ params }) => { - const [publicKey] = params; + const [publicKey, tokenId] = params; const result = await mina.getAccount({ publicKey: PublicKeySchema.parse(publicKey), + tokenId: tokenId !== undefined ? z.string().parse(tokenId) : "1", }); return json(buildResponse({ result }), 200); }, diff --git a/apps/klesia/src/methods/mina.spec.ts b/apps/klesia/src/methods/mina.spec.ts index db59be0..35e65d2 100644 --- a/apps/klesia/src/methods/mina.spec.ts +++ b/apps/klesia/src/methods/mina.spec.ts @@ -9,7 +9,7 @@ it("should return transactions count", async () => { }); it("should return balance", async () => { - const result = await mina.getBalance({ publicKey: TEST_PKEY }); + const result = await mina.getBalance({ publicKey: TEST_PKEY, tokenId: "1" }); expect(BigInt(result)).toBeGreaterThan(0); }); @@ -24,7 +24,7 @@ it("should return network id", async () => { }); it("should get account info", async () => { - const result = await mina.getAccount({ publicKey: TEST_PKEY }); + const result = await mina.getAccount({ publicKey: TEST_PKEY, tokenId: "1" }); expect(BigInt(result.nonce)).toBeGreaterThanOrEqual(0); expect(BigInt(result.balance.total)).toBeGreaterThanOrEqual(0); }); diff --git a/apps/klesia/src/methods/mina.ts b/apps/klesia/src/methods/mina.ts index 43b2bc4..df446a1 100644 --- a/apps/klesia/src/methods/mina.ts +++ b/apps/klesia/src/methods/mina.ts @@ -32,20 +32,20 @@ const getTransactionCount = async ({ publicKey }: { publicKey: string }) => { } }; -const getBalance = async ({ publicKey }: { publicKey: string }) => { +const getBalance = async ({ publicKey, tokenId }: { publicKey: string, tokenId: string }) => { const client = getNodeClient(); try { const { data } = await client.query( gql` query { - account(publicKey: $publicKey) { + account(publicKey: $publicKey, token: $tokenId) { balance { total } } } `, - { publicKey }, + { publicKey, tokenId }, ); return data.account.balance.total; } catch { @@ -143,13 +143,13 @@ const sendTransaction = async ({ .exhaustive(); }; -const getAccount = async ({ publicKey }: { publicKey: string }) => { +const getAccount = async ({ publicKey, tokenId }: { publicKey: string, tokenId: string }) => { const client = getNodeClient(); try { const { data } = await client.query( gql` query { - account(publicKey: $publicKey) { + account(publicKey: $publicKey, token: $tokenId) { publicKey token nonce @@ -196,7 +196,7 @@ const getAccount = async ({ publicKey }: { publicKey: string }) => { } } `, - { publicKey }, + { publicKey, tokenId }, ); return data.account; } catch { diff --git a/bun.lockb b/bun.lockb index f13a023..83c711e 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/packages/utils/src/validation.ts b/packages/utils/src/validation.ts index 9bfa729..2540f2c 100644 --- a/packages/utils/src/validation.ts +++ b/packages/utils/src/validation.ts @@ -179,7 +179,7 @@ export const KlesiaRpcMethodSchema = z.discriminatedUnion("method", [ }), z.object({ method: z.literal(KlesiaRpcMethod.enum.mina_getBalance), - params: PublicKeyParamsSchema, + params: z.union([PublicKeyParamsSchema, z.tuple([PublicKeySchema, z.string()])]), }), z.object({ method: z.literal(KlesiaRpcMethod.enum.mina_blockHash), @@ -195,7 +195,7 @@ export const KlesiaRpcMethodSchema = z.discriminatedUnion("method", [ }), z.object({ method: z.literal(KlesiaRpcMethod.enum.mina_getAccount), - params: PublicKeyParamsSchema, + params: z.union([PublicKeyParamsSchema, z.tuple([PublicKeySchema, z.string()])]), }), ]);