-
Notifications
You must be signed in to change notification settings - Fork 9
/
tutorial-1.ts
154 lines (130 loc) · 4.29 KB
/
tutorial-1.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
146
147
148
149
150
151
152
153
154
import "dotenv/config"
import { writeFileSync } from "fs"
import { toSafeSmartAccount } from "permissionless/accounts"
import { Hex, createPublicClient, getContract, http } from "viem"
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"
import { sepolia } from "viem/chains"
import { createPimlicoClient } from "permissionless/clients/pimlico"
import { createBundlerClient, entryPoint07Address } from "viem/account-abstraction"
import { createSmartAccountClient } from "permissionless"
const apiKey = process.env.PIMLICO_API_KEY
if (!apiKey) throw new Error("Missing PIMLICO_API_KEY")
const privateKey =
(process.env.PRIVATE_KEY as Hex) ??
(() => {
const pk = generatePrivateKey()
writeFileSync(".env", `PRIVATE_KEY=${pk}`)
return pk
})()
export const publicClient = createPublicClient({
chain: sepolia,
transport: http("https://rpc.ankr.com/eth_sepolia"),
})
const account = await toSafeSmartAccount({
client: publicClient,
owner: privateKeyToAccount(privateKey),
entryPoint: {
address: entryPoint07Address,
version: "0.7"
}, // global entrypoint
version: "1.4.1",
})
console.log({
accountAddress: await account.getAddress(),
})
console.log(`Smart account address: https://sepolia.etherscan.io/address/${account.address}`)
const pimlicoUrl = `https://api.pimlico.io/v2/sepolia/rpc?apikey=${apiKey}`
const pimlicoClient = createPimlicoClient({
transport: http(pimlicoUrl),
entryPoint: {
address: entryPoint07Address,
version: "0.7",
}
})
{
const smartAccountClient = createSmartAccountClient({
account,
chain: sepolia,
bundlerTransport: http(pimlicoUrl),
paymaster: pimlicoClient,
userOperation: {
estimateFeesPerGas: async () => {
return (await pimlicoClient.getUserOperationGasPrice()).fast
},
}
})
const txHash = await smartAccountClient.sendTransaction({
to: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
value: 0n,
data: "0x1234",
})
console.log(`User operation with single transaction included: https://sepolia.etherscan.io/tx/${txHash}`)
const contract = getContract({
address: "0x6D7A849791a8E869892f11E01c2A5f3b25a497B6",
abi: [{"inputs":[],"name":"getLastGreeter","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"greet","outputs":[],"stateMutability":"nonpayable","type":"function"}],
client: {
public: publicClient,
wallet: smartAccountClient,
}
})
const txHash2 = await contract.write.greet()
console.log(`User operation with contract call included: https://sepolia.etherscan.io/tx/${txHash2}`)
const txHashMultiple = await smartAccountClient.sendTransaction({
calls: [
{
to: "0x3c44cdddb6a900fa2b585dd299e03d12fa4293bc",
value: 0n,
data: "0x1234",
},
{
abi: [{"inputs":[],"name":"getLastGreeter","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"greet","outputs":[],"stateMutability":"nonpayable","type":"function"}],
functionName: "greet",
args: [],
to: "0x6D7A849791a8E869892f11E01c2A5f3b25a497B6"
}
],
})
console.log(`User operation with multiple transactions included: https://sepolia.etherscan.io/tx/${txHashMultiple}`)
}
{
const bundlerClient = createBundlerClient({
account,
chain: sepolia,
transport: http(pimlicoUrl),
paymaster: pimlicoClient,
userOperation: {
estimateFeesPerGas: async () => {
return (await pimlicoClient.getUserOperationGasPrice()).fast
},
}
})
const userOpHash = await bundlerClient.sendUserOperation({
calls: [{
to: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
value: 0n,
data: "0x1234",
}]
})
const receipt = await bundlerClient.waitForUserOperationReceipt({
hash: userOpHash,
})
console.log(`User operation included: https://sepolia.etherscan.io/tx/${receipt.receipt.transactionHash}`)
const txHashMultiple = await bundlerClient.sendUserOperation({
calls: [
{
to: "0x3c44cdddb6a900fa2b585dd299e03d12fa4293bc",
value: 0n,
data: "0x1234",
},
{
to: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
value: 0n,
data: "0x1234",
}
],
})
const receipt2 = await bundlerClient.getUserOperationReceipt({
hash: txHashMultiple,
})
console.log(`User operation included: https://sepolia.etherscan.io/tx/${receipt2.receipt.transactionHash}`)
}