@keypo/typescript-sdk
Version:
A TypeScript SDK for using Keypo
80 lines (79 loc) • 2.89 kB
JavaScript
import { createPublicClient, http } from "viem";
import { create7702KernelAccount, create7702KernelAccountClient } from "@zerodev/ecdsa-validator";
import { createZeroDevPaymasterClient, getUserOperationGasPrice } from "@zerodev/sdk";
import { getEntryPoint, KERNEL_V3_3 } from "@zerodev/sdk/constants";
// Helper to create a kernel client from a userSigner
export async function getKernelClient(userSigner, chain, bundlerRpcUrl, authorization, debug) {
if (debug) {
console.log("[DEBUG] getKernelClient called with:", {
userSigner,
chain,
bundlerRpcUrl,
authorization
});
}
try {
const publicClient = createPublicClient({
transport: http(bundlerRpcUrl),
chain,
});
if (debug) {
console.log("[DEBUG] publicClient:", publicClient);
}
const account = await create7702KernelAccount(publicClient, {
signer: userSigner,
entryPoint: getEntryPoint("0.7"),
kernelVersion: KERNEL_V3_3,
eip7702Auth: authorization
});
if (debug) {
console.log("[DEBUG] kernelAccount:", account);
}
const paymasterClient = createZeroDevPaymasterClient({
chain,
transport: http(bundlerRpcUrl),
});
if (debug) {
console.log("[DEBUG] paymasterClient:", paymasterClient);
}
const kernelClient = create7702KernelAccountClient({
account,
chain,
bundlerTransport: http(bundlerRpcUrl),
paymaster: paymasterClient,
client: publicClient,
userOperation: {
estimateFeesPerGas: async ({ bundlerClient }) => {
try {
return await getUserOperationGasPrice(bundlerClient);
}
catch (error) {
if (debug) {
console.warn("[DEBUG] Gas price estimation failed, using fallback:", error.message);
}
// Fallback gas price estimation
return {
maxFeePerGas: BigInt(2000000000), // 2 gwei
maxPriorityFeePerGas: BigInt(1000000000), // 1 gwei
};
}
}
}
});
if (debug) {
console.log("[DEBUG] kernelClient:", kernelClient);
}
return kernelClient;
}
catch (error) {
console.error("Error creating kernel client:", error);
if (debug) {
console.error("Error details:", {
message: error.message,
cause: error.cause,
stack: error.stack
});
}
throw error;
}
}