Skip to content

Commit b868505

Browse files
committed
feat(connect): add prepareTransactionRequest
1 parent 4cb9be7 commit b868505

File tree

7 files changed

+63
-4
lines changed

7 files changed

+63
-4
lines changed

bun.lockb

32 Bytes
Binary file not shown.

packages/connect/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"cleanup": "rimraf dist .turbo"
2323
},
2424
"dependencies": {
25+
"@mina-js/shared": "workspace:*",
2526
"@mina-js/accounts": "workspace:*",
2627
"@mina-js/klesia-sdk": "workspace:*",
2728
"@mina-js/providers": "workspace:*",

packages/connect/src/__snapshots__/client.spec.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
exports[`matches snapshot of local source 1`] = `
44
{
55
"createNullifier": [Function: AsyncFunction],
6+
"estimateFees": [Function: AsyncFunction],
67
"getAccounts": [Function: AsyncFunction],
78
"getBalance": [Function: AsyncFunction],
89
"getChainId": [Function: AsyncFunction],
910
"getTransactionCount": [Function: AsyncFunction],
11+
"prepareTransactionRequest": [Function: AsyncFunction],
1012
"signFields": [Function: AsyncFunction],
1113
"signMessage": [Function: AsyncFunction],
1214
"signTransaction": [Function: AsyncFunction],
@@ -16,10 +18,12 @@ exports[`matches snapshot of local source 1`] = `
1618
exports[`matches snapshot of json-rpc source 1`] = `
1719
{
1820
"createNullifier": [Function: AsyncFunction],
21+
"estimateFees": [Function: AsyncFunction],
1922
"getAccounts": [Function: AsyncFunction],
2023
"getBalance": [Function: AsyncFunction],
2124
"getChainId": [Function: AsyncFunction],
2225
"getTransactionCount": [Function: AsyncFunction],
26+
"prepareTransactionRequest": [Function: AsyncFunction],
2327
"signFields": [Function: AsyncFunction],
2428
"signMessage": [Function: AsyncFunction],
2529
"signTransaction": [Function: AsyncFunction],

packages/connect/src/client.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,18 @@ describe("local source", () => {
7575
const signature = await client.signMessage({ message: "hello" });
7676
expect(signature.data).toEqual("hello");
7777
});
78+
79+
it("fills missing transaction fields", async () => {
80+
const account = privateKeyToAccount({
81+
privateKey: Test.accounts[0].privateKey,
82+
});
83+
const client = createWalletClient({ account, network: "devnet" });
84+
const transaction = await client.prepareTransactionRequest({
85+
from: PUBLIC_KEY,
86+
to: PUBLIC_KEY,
87+
amount: 1_000_000_000n,
88+
});
89+
expect(transaction.fee).toBeDefined();
90+
expect(transaction.nonce).toBeDefined();
91+
});
7892
});

packages/connect/src/client.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import type { Account, SignTransaction } from "@mina-js/accounts";
2-
import type { SignMessage } from "@mina-js/accounts";
3-
import type { SignFields } from "@mina-js/accounts";
4-
import type { CreateNullifier } from "@mina-js/accounts";
1+
import type {
2+
Account,
3+
CreateNullifier,
4+
SignFields,
5+
SignMessage,
6+
SignTransaction,
7+
} from "@mina-js/accounts";
58
import { createClient } from "@mina-js/klesia-sdk";
9+
import type { PartiallyFormedTransactionProperties } from "@mina-js/shared";
610
import { match } from "ts-pattern";
711
import { createStore } from "./store";
812

@@ -101,6 +105,29 @@ export const createWalletClient = ({
101105
if (account.type !== "local") throw new Error("Account type not supported");
102106
return account.createNullifier(params);
103107
};
108+
const estimateFees = async () => {
109+
const { result } = await klesiaClient.request<"mina_estimateFees">({
110+
method: "mina_estimateFees",
111+
});
112+
return result;
113+
};
114+
const prepareTransactionRequest = async (
115+
transaction: PartiallyFormedTransactionProperties,
116+
) => {
117+
let fee = transaction.fee;
118+
let nonce = transaction.nonce;
119+
if (!nonce) {
120+
nonce = await getTransactionCount();
121+
}
122+
if (!fee) {
123+
fee = BigInt((await estimateFees()).medium);
124+
}
125+
return {
126+
...transaction,
127+
fee,
128+
nonce,
129+
};
130+
};
104131
return {
105132
getAccounts,
106133
getBalance,
@@ -110,5 +137,7 @@ export const createWalletClient = ({
110137
signMessage,
111138
signFields,
112139
createNullifier,
140+
estimateFees,
141+
prepareTransactionRequest,
113142
};
114143
};

packages/shared/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import type { z } from "zod";
22
import type {
33
LiteralSchema,
44
NullifierSchema,
5+
PartiallyFormedTransactionPayload,
56
PrivateKeySchema,
67
PublicKeySchema,
78
SignedFieldsSchema,
89
SignedMessageSchema,
910
SignedTransactionSchema,
11+
TransactionPayload,
1012
TransactionReceiptSchema,
1113
} from "./validation";
1214

@@ -17,6 +19,10 @@ export type Literal = z.infer<typeof LiteralSchema>;
1719
export type Json = Literal | { [key: string]: Json } | Json[];
1820
export type PublicKey = z.infer<typeof PublicKeySchema>;
1921
export type PrivateKey = z.infer<typeof PrivateKeySchema>;
22+
export type TransactionProperties = z.infer<typeof TransactionPayload>;
23+
export type PartiallyFormedTransactionProperties = z.infer<
24+
typeof PartiallyFormedTransactionPayload
25+
>;
2026

2127
/**
2228
* Return types

packages/shared/src/validation.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export const TransactionPayload = z
3939
})
4040
.strict();
4141

42+
export const PartiallyFormedTransactionPayload = TransactionPayload.extend({
43+
fee: z.coerce.bigint().optional(),
44+
nonce: z.coerce.bigint().optional(),
45+
});
46+
4247
/**
4348
* Return type schemas
4449
*/

0 commit comments

Comments
 (0)