-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.ts
145 lines (130 loc) · 4.07 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Utilities for examples
import { signerToEcdsaValidator } from "@zerodev/ecdsa-validator";
import {
KernelAccountClient,
KernelSmartAccountV1Implementation,
SponsorUserOperationParameters,
createKernelAccount,
createKernelAccountClient,
createKernelAccountV1,
createZeroDevPaymasterClient,
getUserOperationGasPrice,
} from "@zerodev/sdk";
import { getEntryPoint } from "@zerodev/sdk/constants";
import { GetKernelVersion } from "@zerodev/sdk/types";
import { Chain, Hex, Transport, createPublicClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { polygonMumbai, sepolia } from "viem/chains";
import {
EntryPointVersion,
PaymasterActions,
SmartAccount,
} from "viem/account-abstraction";
const zeroDevProjectId = process.env.ZERODEV_PROJECT_ID;
const privateKey = process.env.PRIVATE_KEY;
if (!zeroDevProjectId || !privateKey) {
throw new Error("ZERODEV_PROJECT_ID or PRIVATE_KEY is not set");
}
const signer = privateKeyToAccount(privateKey as Hex);
const chain = sepolia;
const publicClient = createPublicClient({
transport: http(process.env.BUNDLER_RPC),
chain,
});
export const getKernelClient = async <
entryPointVersion extends EntryPointVersion
>(
entryPointVersion_: entryPointVersion,
kernelVersion: GetKernelVersion<entryPointVersion>
) => {
const ecdsaValidator = await signerToEcdsaValidator(publicClient, {
signer,
entryPoint: getEntryPoint(entryPointVersion_),
kernelVersion,
});
const account = await createKernelAccount(publicClient, {
plugins: {
sudo: ecdsaValidator,
},
entryPoint: getEntryPoint(entryPointVersion_),
kernelVersion,
});
console.log("My account:", account.address);
const paymasterClient = createZeroDevPaymasterClient({
chain,
transport: http(process.env.PAYMASTER_RPC),
});
return createKernelAccountClient({
account,
chain,
bundlerTransport: http(process.env.BUNDLER_RPC),
paymaster: paymasterClient,
client: publicClient,
userOperation: {
estimateFeesPerGas: async ({ bundlerClient }) => {
return getUserOperationGasPrice(bundlerClient);
},
},
});
};
export const getKernelV1Account = async () => {
const privateKey = process.env.PRIVATE_KEY as Hex;
if (!privateKey) {
throw new Error("PRIVATE_KEY environment variable not set");
}
const rpcUrl = process.env.BUNDLER_RPC;
if (!rpcUrl) {
throw new Error("BUNDLER_RPC environment variable not set");
}
const publicClient = createPublicClient({
transport: http(rpcUrl),
chain,
});
const signer = privateKeyToAccount(privateKey);
return createKernelAccountV1(publicClient, {
signer,
index: BigInt(0),
entryPoint: getEntryPoint("0.6"),
});
};
export const getKernelV1AccountClient = async ({
account,
paymaster,
}: {
paymaster?: {
/** Retrieves paymaster-related User Operation properties to be used for sending the User Operation. */
getPaymasterData?: PaymasterActions["getPaymasterData"] | undefined;
/** Retrieves paymaster-related User Operation properties to be used for gas estimation. */
getPaymasterStubData?: PaymasterActions["getPaymasterStubData"] | undefined;
};
account: SmartAccount<KernelSmartAccountV1Implementation>;
}) => {
const zeroDevBundlerRpcHost = process.env.BUNDLER_RPC;
return createKernelAccountClient({
account,
chain,
bundlerTransport: http(zeroDevBundlerRpcHost),
paymaster,
});
};
export const getZeroDevPaymasterClient = () => {
if (!process.env.PAYMASTER_RPC)
throw new Error("PAYMASTER_RPC environment variable not set");
const paymasterRpc = process.env.PAYMASTER_RPC;
return createZeroDevPaymasterClient({
chain,
transport: http(paymasterRpc),
});
};
export const getZeroDevERC20PaymasterClient = () => {
if (!process.env.ZERODEV_PROJECT_ID)
throw new Error("ZERODEV_PROJECT_ID environment variable not set");
return createZeroDevPaymasterClient({
chain,
transport: http(
process.env.PAYMASTER_RPC ||
"https://rpc.zerodev.app/api/v2/paymaster/" +
process.env.ZERODEV_PROJECT_ID
),
});
};