Skip to content

Commit

Permalink
feat(connect): add prepareTransactionRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Sep 21, 2024
1 parent 4cb9be7 commit b868505
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 4 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions packages/connect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"cleanup": "rimraf dist .turbo"
},
"dependencies": {
"@mina-js/shared": "workspace:*",
"@mina-js/accounts": "workspace:*",
"@mina-js/klesia-sdk": "workspace:*",
"@mina-js/providers": "workspace:*",
Expand Down
4 changes: 4 additions & 0 deletions packages/connect/src/__snapshots__/client.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
exports[`matches snapshot of local source 1`] = `
{
"createNullifier": [Function: AsyncFunction],
"estimateFees": [Function: AsyncFunction],
"getAccounts": [Function: AsyncFunction],
"getBalance": [Function: AsyncFunction],
"getChainId": [Function: AsyncFunction],
"getTransactionCount": [Function: AsyncFunction],
"prepareTransactionRequest": [Function: AsyncFunction],
"signFields": [Function: AsyncFunction],
"signMessage": [Function: AsyncFunction],
"signTransaction": [Function: AsyncFunction],
Expand All @@ -16,10 +18,12 @@ exports[`matches snapshot of local source 1`] = `
exports[`matches snapshot of json-rpc source 1`] = `
{
"createNullifier": [Function: AsyncFunction],
"estimateFees": [Function: AsyncFunction],
"getAccounts": [Function: AsyncFunction],
"getBalance": [Function: AsyncFunction],
"getChainId": [Function: AsyncFunction],
"getTransactionCount": [Function: AsyncFunction],
"prepareTransactionRequest": [Function: AsyncFunction],
"signFields": [Function: AsyncFunction],
"signMessage": [Function: AsyncFunction],
"signTransaction": [Function: AsyncFunction],
Expand Down
14 changes: 14 additions & 0 deletions packages/connect/src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,18 @@ describe("local source", () => {
const signature = await client.signMessage({ message: "hello" });
expect(signature.data).toEqual("hello");
});

it("fills missing transaction fields", async () => {
const account = privateKeyToAccount({
privateKey: Test.accounts[0].privateKey,
});
const client = createWalletClient({ account, network: "devnet" });
const transaction = await client.prepareTransactionRequest({
from: PUBLIC_KEY,
to: PUBLIC_KEY,
amount: 1_000_000_000n,
});
expect(transaction.fee).toBeDefined();
expect(transaction.nonce).toBeDefined();
});
});
37 changes: 33 additions & 4 deletions packages/connect/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { Account, SignTransaction } from "@mina-js/accounts";
import type { SignMessage } from "@mina-js/accounts";
import type { SignFields } from "@mina-js/accounts";
import type { CreateNullifier } from "@mina-js/accounts";
import type {
Account,
CreateNullifier,
SignFields,
SignMessage,
SignTransaction,
} from "@mina-js/accounts";
import { createClient } from "@mina-js/klesia-sdk";
import type { PartiallyFormedTransactionProperties } from "@mina-js/shared";
import { match } from "ts-pattern";
import { createStore } from "./store";

Expand Down Expand Up @@ -101,6 +105,29 @@ export const createWalletClient = ({
if (account.type !== "local") throw new Error("Account type not supported");
return account.createNullifier(params);
};
const estimateFees = async () => {
const { result } = await klesiaClient.request<"mina_estimateFees">({
method: "mina_estimateFees",
});
return result;
};
const prepareTransactionRequest = async (
transaction: PartiallyFormedTransactionProperties,
) => {
let fee = transaction.fee;
let nonce = transaction.nonce;
if (!nonce) {
nonce = await getTransactionCount();
}
if (!fee) {
fee = BigInt((await estimateFees()).medium);
}
return {
...transaction,
fee,
nonce,
};
};
return {
getAccounts,
getBalance,
Expand All @@ -110,5 +137,7 @@ export const createWalletClient = ({
signMessage,
signFields,
createNullifier,
estimateFees,
prepareTransactionRequest,
};
};
6 changes: 6 additions & 0 deletions packages/shared/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import type { z } from "zod";
import type {
LiteralSchema,
NullifierSchema,
PartiallyFormedTransactionPayload,
PrivateKeySchema,
PublicKeySchema,
SignedFieldsSchema,
SignedMessageSchema,
SignedTransactionSchema,
TransactionPayload,
TransactionReceiptSchema,
} from "./validation";

Expand All @@ -17,6 +19,10 @@ export type Literal = z.infer<typeof LiteralSchema>;
export type Json = Literal | { [key: string]: Json } | Json[];
export type PublicKey = z.infer<typeof PublicKeySchema>;
export type PrivateKey = z.infer<typeof PrivateKeySchema>;
export type TransactionProperties = z.infer<typeof TransactionPayload>;
export type PartiallyFormedTransactionProperties = z.infer<
typeof PartiallyFormedTransactionPayload
>;

/**
* Return types
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export const TransactionPayload = z
})
.strict();

export const PartiallyFormedTransactionPayload = TransactionPayload.extend({
fee: z.coerce.bigint().optional(),
nonce: z.coerce.bigint().optional(),
});

/**
* Return type schemas
*/
Expand Down

0 comments on commit b868505

Please sign in to comment.