Skip to content

feat(klesia sdk): add support for tokenId parameter in getBalance and getAccount methods #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions apps/klesia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
},
Expand Down Expand Up @@ -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);
},
Expand Down
4 changes: 2 additions & 2 deletions apps/klesia/src/methods/mina.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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);
});
12 changes: 6 additions & 6 deletions apps/klesia/src/methods/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -196,7 +196,7 @@ const getAccount = async ({ publicKey }: { publicKey: string }) => {
}
}
`,
{ publicKey },
{ publicKey, tokenId },
);
return data.account;
} catch {
Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions packages/utils/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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()])]),
}),
]);

Expand Down