Skip to content

Commit 9649399

Browse files
committed
fix(klesia): handle unfunded accounts
1 parent ce28b1d commit 9649399

File tree

18 files changed

+220
-76
lines changed

18 files changed

+220
-76
lines changed

apps/docs/src/pages/about.mdx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,9 @@ MinaJS provides a set of services to interact with Mina Protocol. The services a
2323
graph TD
2424
classDef MinaJS fill:#C4A7E7,color:#000000
2525
26-
subgraph Klesia
27-
G[Klesia RPC]:::MinaJS --> H[Mina Node]
28-
H[Mina Node] --> G[Klesia RPC]
29-
G[Klesia RPC]:::MinaJS --> J[Fallback Mina Node]
30-
J[Fallback Mina Node] --> G[Klesia RPC]
31-
end
32-
3326
subgraph zkApp
3427
B[zkApp UI] --> C[MinaJS Connect]
3528
C[MinaJS Connect]:::MinaJS --> B[zkApp UI]
36-
B[zkApp UI] --> I1[Klesia SDK - zkApp]
37-
I1[Klesia SDK - zkApp]:::MinaJS --> G[Klesia RPC]
38-
I1[Klesia SDK - zkApp]:::MinaJS --> B[zkApp UI]
39-
G[Klesia RPC]:::MinaJS --> I1[Klesia SDK - zkApp]
4029
end
4130
4231
subgraph Mina Wallet
@@ -46,11 +35,20 @@ graph TD
4635
E[Wallet UI & Connector] --> D[MinaJS Provider]
4736
E[Wallet UI & Connector] --> F[MinaJS Account]
4837
F[MinaJS Account]:::MinaJS --> E[Wallet UI & Connector]
49-
I2[Klesia SDK - Wallet]:::MinaJS --> G[Klesia RPC]
50-
G[Klesia RPC]:::MinaJS --> I2[Klesia SDK - Wallet]
5138
E[Wallet UI & Connector] --> I2[Klesia SDK - Wallet]
5239
I2[Klesia SDK - Wallet]:::MinaJS --> E[Wallet UI & Connector]
5340
end
41+
42+
subgraph Klesia
43+
I2[Klesia SDK - Wallet]:::MinaJS --> G[Klesia RPC]
44+
G[Klesia RPC]:::MinaJS --> H[Mina Node]
45+
H[Mina Node] --> G[Klesia RPC]
46+
G[Klesia RPC]:::MinaJS --> J[Fallback Mina Node]
47+
J[Fallback Mina Node] --> G[Klesia RPC]
48+
G[Klesia RPC]:::MinaJS --> I2[Klesia SDK - Wallet]
49+
C[MinaJS Connect]:::MinaJS --> G[Klesia RPC]
50+
G[Klesia RPC] --> C[MinaJS Connect]
51+
end
5452
```
5553

5654
## Supported Chains

apps/klesia/src/index.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
7171
});
7272
return json(
7373
buildResponse({
74-
method: RpcMethod.enum.mina_getTransactionCount,
7574
result,
7675
}),
7776
200,
@@ -83,16 +82,12 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
8382
const result = await mina.getBalance({
8483
publicKey: PublicKeySchema.parse(publicKey),
8584
});
86-
return json(
87-
buildResponse({ method: RpcMethod.enum.mina_getBalance, result }),
88-
200,
89-
);
85+
return json(buildResponse({ result }), 200);
9086
})
9187
.with({ method: RpcMethod.enum.mina_blockHash }, async () => {
9288
if (process.env.MINA_NETWORK === "zeko_devnet") {
9389
return json(
9490
buildResponse({
95-
method: RpcMethod.enum.mina_blockHash,
9691
error: {
9792
code: -32600,
9893
message: "Network not supported.",
@@ -102,17 +97,11 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
10297
);
10398
}
10499
const result = await mina.blockHash();
105-
return json(
106-
buildResponse({ method: RpcMethod.enum.mina_blockHash, result }),
107-
200,
108-
);
100+
return json(buildResponse({ result }), 200);
109101
})
110102
.with({ method: RpcMethod.enum.mina_chainId }, async () => {
111103
const result = await mina.chainId();
112-
return json(
113-
buildResponse({ method: RpcMethod.enum.mina_chainId, result }),
114-
200,
115-
);
104+
return json(buildResponse({ result }), 200);
116105
})
117106
.with(
118107
{ method: RpcMethod.enum.mina_sendTransaction },
@@ -121,7 +110,6 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
121110
const result = await mina.sendTransaction({ signedTransaction, type });
122111
return json(
123112
buildResponse({
124-
method: RpcMethod.enum.mina_sendTransaction,
125113
result,
126114
}),
127115
200,
@@ -133,10 +121,7 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
133121
const result = await mina.getAccount({
134122
publicKey: PublicKeySchema.parse(publicKey),
135123
});
136-
return json(
137-
buildResponse({ method: RpcMethod.enum.mina_getAccount, result }),
138-
200,
139-
);
124+
return json(buildResponse({ result }), 200);
140125
})
141126
.exhaustive();
142127
});

apps/klesia/src/methods/mina.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,42 @@ import { getNodeClient } from "../utils/node";
55

66
const getTransactionCount = async ({ publicKey }: { publicKey: string }) => {
77
const client = getNodeClient();
8-
const { data } = await client.query(
9-
gql`
10-
query {
11-
account(publicKey: $publicKey) {
12-
nonce
8+
try {
9+
const { data } = await client.query(
10+
gql`
11+
query {
12+
account(publicKey: $publicKey) {
13+
nonce
14+
}
1315
}
14-
}
15-
`,
16-
{ publicKey },
17-
);
18-
return data.account.nonce;
16+
`,
17+
{ publicKey },
18+
);
19+
return data.account.nonce;
20+
} catch {
21+
return "0";
22+
}
1923
};
2024

2125
const getBalance = async ({ publicKey }: { publicKey: string }) => {
2226
const client = getNodeClient();
23-
const { data } = await client.query(
24-
gql`
25-
query {
26-
account(publicKey: $publicKey) {
27-
balance {
28-
total
27+
try {
28+
const { data } = await client.query(
29+
gql`
30+
query {
31+
account(publicKey: $publicKey) {
32+
balance {
33+
total
34+
}
2935
}
3036
}
31-
}
32-
`,
33-
{ publicKey },
34-
);
35-
return data.account.balance.total;
37+
`,
38+
{ publicKey },
39+
);
40+
return data.account.balance.total;
41+
} catch {
42+
return "0";
43+
}
3644
};
3745

3846
const blockHash = async () => {
@@ -129,8 +137,9 @@ const sendTransaction = async ({
129137

130138
const getAccount = async ({ publicKey }: { publicKey: string }) => {
131139
const client = getNodeClient();
132-
const { data } = await client.query(
133-
gql`
140+
try {
141+
const { data } = await client.query(
142+
gql`
134143
query {
135144
account(publicKey: $publicKey) {
136145
nonce
@@ -140,12 +149,18 @@ const getAccount = async ({ publicKey }: { publicKey: string }) => {
140149
}
141150
}
142151
`,
143-
{ publicKey },
144-
);
145-
return {
146-
nonce: data.account.nonce,
147-
balance: data.account.balance.total,
148-
};
152+
{ publicKey },
153+
);
154+
return {
155+
nonce: data.account.nonce,
156+
balance: data.account.balance.total,
157+
};
158+
} catch {
159+
return {
160+
nonce: "0",
161+
balance: "0",
162+
};
163+
}
149164
};
150165

151166
export const mina = {
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import type { RpcErrorType, RpcMethodType } from "../schema";
1+
import type { RpcErrorType } from "../schema";
22

33
export const buildResponse = ({
44
result,
55
error,
6-
method,
7-
}: { result?: unknown; error?: RpcErrorType; method: RpcMethodType }) => {
6+
}: { result?: unknown; error?: RpcErrorType }) => {
87
if (error) {
98
return {
109
jsonrpc: "2.0",
1110
error,
12-
method,
1311
};
1412
}
15-
return { jsonrpc: "2.0", result, method };
13+
return { jsonrpc: "2.0", result };
1614
};

bun.lockb

56 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"private": true,
44
"scripts": {
55
"build": "turbo build",
6+
"build:packages": "turbo build --filter=\"./packages/*\"",
67
"test": "turbo test",
78
"cleanup": "bun run --filter '*' cleanup && rimraf node_modules .turbo",
89
"lint": "bunx biome check .",
910
"format": "bunx biome check . --write --unsafe"
1011
},
1112
"devDependencies": {
1213
"@biomejs/biome": "1.8.3",
13-
"@happy-dom/global-registrator": "^14.12.3",
1414
"@tsconfig/bun": "1.0.7",
1515
"@types/bun": "1.1.6",
1616
"rimraf": "^6.0.1",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { expect, it } from "bun:test";
2-
import { mnemonic } from "../test/constants";
2+
import { Test } from "@mina-js/shared";
33
import { mnemonicToAccount } from "./mnemonic-to-account";
44

55
it("matches the snapshot", () => {
6-
const mnemonicAccount = mnemonicToAccount({ mnemonic });
6+
const mnemonicAccount = mnemonicToAccount({ mnemonic: Test.mnemonic });
77
expect(mnemonicAccount).toMatchSnapshot();
88
});

packages/accounts/src/accounts/private-key-to-account.spec.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import { expect, it } from "bun:test";
2-
import { accounts } from "../test/constants";
2+
import { Test } from "@mina-js/shared";
33
import { privateKeyToAccount } from "./private-key-to-account";
44

55
it("matches default values", () => {
6-
const account = privateKeyToAccount({ privateKey: accounts[0].privateKey });
6+
const account = privateKeyToAccount({
7+
privateKey: Test.accounts[0].privateKey,
8+
});
79
expect(account).toMatchSnapshot();
810
});
911

1012
it("signs a message", async () => {
11-
const account = privateKeyToAccount({ privateKey: accounts[0].privateKey });
13+
const account = privateKeyToAccount({
14+
privateKey: Test.accounts[0].privateKey,
15+
});
1216
const message = "hello world";
1317
const signedMessage = await account.signMessage({ message });
1418
expect(signedMessage).toMatchSnapshot();
1519
});
1620

1721
it("signs a transaction", async () => {
18-
const account = privateKeyToAccount({ privateKey: accounts[0].privateKey });
22+
const account = privateKeyToAccount({
23+
privateKey: Test.accounts[0].privateKey,
24+
});
1925
const transaction = {
2026
nonce: 1n,
2127
from: "B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5",
@@ -28,14 +34,18 @@ it("signs a transaction", async () => {
2834
});
2935

3036
it("creates a nullifier", async () => {
31-
const account = privateKeyToAccount({ privateKey: accounts[0].privateKey });
37+
const account = privateKeyToAccount({
38+
privateKey: Test.accounts[0].privateKey,
39+
});
3240
const message = [1n, 2n, 3n];
3341
const nullifier = await account.createNullifier({ message });
3442
expect(typeof nullifier.private.c).toBe("bigint");
3543
});
3644

3745
it("signs fields", async () => {
38-
const account = privateKeyToAccount({ privateKey: accounts[0].privateKey });
46+
const account = privateKeyToAccount({
47+
privateKey: Test.accounts[0].privateKey,
48+
});
3949
const fields = [1n, 2n, 3n];
4050
const signedFields = await account.signFields({ fields });
4151
expect(signedFields).toMatchSnapshot();

packages/accounts/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ export { HDKey } from "@scure/bip32";
22
export { hex } from "@scure/base";
33
export { wordlist as english } from "@scure/bip39/wordlists/english";
44

5+
export type { Account } from "./types";
56
export { generateMnemonic } from "./accounts/generate-mnemonic";
67
export { generatePrivateKey } from "./accounts/generate-private-key";
78
export { hdKeyToAccount } from "./accounts/hd-key-to-account";
89
export { mnemonicToAccount } from "./accounts/mnemonic-to-account";
910
export { privateKeyToAccount } from "./accounts/private-key-to-account";
11+
export { toAccount } from "./accounts/to-account";

packages/accounts/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export type HDAccount = Simplify<
6767
}
6868
>;
6969

70+
export type Account<publicKey extends PublicKey = PublicKey> =
71+
| JsonRpcAccount<publicKey>
72+
| LocalAccount<publicKey>;
73+
7074
export type PrivateKeyAccount = Simplify<LocalAccount<"privateKey">>;
7175

7276
export type { HDKey };

0 commit comments

Comments
 (0)