Skip to content

Commit 96c164f

Browse files
feat(klesia sdk): add support for tokenId parameter in getBalance and getAccount methods (#10)
1 parent 9089ec3 commit 96c164f

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

apps/klesia/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { nanoid } from "nanoid";
1313
import { match } from "ts-pattern";
1414
import { mina } from "./methods/mina";
1515
import { buildResponse } from "./utils/build-response";
16+
import { z } from "zod";
1617

1718
export const api = new OpenAPIHono();
1819

@@ -81,9 +82,10 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
8182
.with(
8283
{ method: KlesiaRpcMethod.enum.mina_getBalance },
8384
async ({ params }) => {
84-
const [publicKey] = params;
85+
const [publicKey, tokenId] = params;
8586
const result = await mina.getBalance({
8687
publicKey: PublicKeySchema.parse(publicKey),
88+
tokenId: tokenId !== undefined ? z.string().parse(tokenId) : "1",
8789
});
8890
return json(buildResponse({ result }), 200);
8991
},
@@ -123,9 +125,10 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
123125
.with(
124126
{ method: KlesiaRpcMethod.enum.mina_getAccount },
125127
async ({ params }) => {
126-
const [publicKey] = params;
128+
const [publicKey, tokenId] = params;
127129
const result = await mina.getAccount({
128130
publicKey: PublicKeySchema.parse(publicKey),
131+
tokenId: tokenId !== undefined ? z.string().parse(tokenId) : "1",
129132
});
130133
return json(buildResponse({ result }), 200);
131134
},

apps/klesia/src/methods/mina.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ it("should return transactions count", async () => {
99
});
1010

1111
it("should return balance", async () => {
12-
const result = await mina.getBalance({ publicKey: TEST_PKEY });
12+
const result = await mina.getBalance({ publicKey: TEST_PKEY, tokenId: "1" });
1313
expect(BigInt(result)).toBeGreaterThan(0);
1414
});
1515

@@ -24,7 +24,7 @@ it("should return network id", async () => {
2424
});
2525

2626
it("should get account info", async () => {
27-
const result = await mina.getAccount({ publicKey: TEST_PKEY });
27+
const result = await mina.getAccount({ publicKey: TEST_PKEY, tokenId: "1" });
2828
expect(BigInt(result.nonce)).toBeGreaterThanOrEqual(0);
2929
expect(BigInt(result.balance.total)).toBeGreaterThanOrEqual(0);
3030
});

apps/klesia/src/methods/mina.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ const getTransactionCount = async ({ publicKey }: { publicKey: string }) => {
3232
}
3333
};
3434

35-
const getBalance = async ({ publicKey }: { publicKey: string }) => {
35+
const getBalance = async ({ publicKey, tokenId }: { publicKey: string, tokenId: string }) => {
3636
const client = getNodeClient();
3737
try {
3838
const { data } = await client.query(
3939
gql`
4040
query {
41-
account(publicKey: $publicKey) {
41+
account(publicKey: $publicKey, token: $tokenId) {
4242
balance {
4343
total
4444
}
4545
}
4646
}
4747
`,
48-
{ publicKey },
48+
{ publicKey, tokenId },
4949
);
5050
return data.account.balance.total;
5151
} catch {
@@ -143,13 +143,13 @@ const sendTransaction = async ({
143143
.exhaustive();
144144
};
145145

146-
const getAccount = async ({ publicKey }: { publicKey: string }) => {
146+
const getAccount = async ({ publicKey, tokenId }: { publicKey: string, tokenId: string }) => {
147147
const client = getNodeClient();
148148
try {
149149
const { data } = await client.query(
150150
gql`
151151
query {
152-
account(publicKey: $publicKey) {
152+
account(publicKey: $publicKey, token: $tokenId) {
153153
publicKey
154154
token
155155
nonce
@@ -196,7 +196,7 @@ const getAccount = async ({ publicKey }: { publicKey: string }) => {
196196
}
197197
}
198198
`,
199-
{ publicKey },
199+
{ publicKey, tokenId },
200200
);
201201
return data.account;
202202
} catch {

bun.lockb

-9.72 KB
Binary file not shown.

packages/utils/src/validation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export const KlesiaRpcMethodSchema = z.discriminatedUnion("method", [
179179
}),
180180
z.object({
181181
method: z.literal(KlesiaRpcMethod.enum.mina_getBalance),
182-
params: PublicKeyParamsSchema,
182+
params: z.union([PublicKeyParamsSchema, z.tuple([PublicKeySchema, z.string()])]),
183183
}),
184184
z.object({
185185
method: z.literal(KlesiaRpcMethod.enum.mina_blockHash),
@@ -195,7 +195,7 @@ export const KlesiaRpcMethodSchema = z.discriminatedUnion("method", [
195195
}),
196196
z.object({
197197
method: z.literal(KlesiaRpcMethod.enum.mina_getAccount),
198-
params: PublicKeyParamsSchema,
198+
params: z.union([PublicKeyParamsSchema, z.tuple([PublicKeySchema, z.string()])]),
199199
}),
200200
]);
201201

0 commit comments

Comments
 (0)