|
| 1 | +import { |
| 2 | + createGelatoSmartWalletClient, |
| 3 | + type GelatoTaskStatus, |
| 4 | + sponsored |
| 5 | +} from "@gelatonetwork/smartwallet"; |
| 6 | +import { custom, type GelatoSmartAccountExtension } from "@gelatonetwork/smartwallet/accounts"; |
| 7 | +import "dotenv/config"; |
| 8 | +import { createPublicClient, createWalletClient, type Hex, http } from "viem"; |
| 9 | +import { entryPoint07Abi, entryPoint07Address } from "viem/account-abstraction"; |
| 10 | +import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; |
| 11 | +import { baseSepolia } from "viem/chains"; |
| 12 | +import { ERC4337Encoding } from "../../../src/_dist/wallet"; |
| 13 | + |
| 14 | +const gelatoApiKey = process.env.GELATO_API_KEY; |
| 15 | + |
| 16 | +if (!gelatoApiKey) { |
| 17 | + throw new Error("GELATO_API_KEY is not set"); |
| 18 | +} |
| 19 | + |
| 20 | +const privateKey = (process.env.PRIVATE_KEY ?? generatePrivateKey()) as Hex; |
| 21 | +const owner = privateKeyToAccount(privateKey); |
| 22 | + |
| 23 | +const publicClient = createPublicClient({ |
| 24 | + chain: baseSepolia, |
| 25 | + transport: http() |
| 26 | +}); |
| 27 | + |
| 28 | +(async () => { |
| 29 | + // Defining an EIP7702 account using as delegation address "0x00000000383e8cBe298514674Ea60Ee1d1de50ac" (Nexus v1.2.0) |
| 30 | + // Using ERC4337 and entry point v0.7 |
| 31 | + const account = await custom< |
| 32 | + typeof entryPoint07Abi, |
| 33 | + "0.7", |
| 34 | + GelatoSmartAccountExtension, |
| 35 | + true |
| 36 | + >({ |
| 37 | + authorization: { |
| 38 | + account: owner, |
| 39 | + address: "0x00000000383e8cBe298514674Ea60Ee1d1de50ac", // Nexus v1.2.0 |
| 40 | + }, |
| 41 | + client: publicClient, |
| 42 | + eip7702: true, |
| 43 | + entryPoint: { |
| 44 | + abi: entryPoint07Abi, |
| 45 | + address: entryPoint07Address, |
| 46 | + version: "0.7", |
| 47 | + }, |
| 48 | + owner, |
| 49 | + scw: { |
| 50 | + encoding: ERC4337Encoding.ERC7579, |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + console.log("Account address:", account.address); |
| 55 | + |
| 56 | + const client = createWalletClient({ |
| 57 | + account, |
| 58 | + chain: baseSepolia, |
| 59 | + transport: http(), |
| 60 | + }); |
| 61 | + |
| 62 | + const swc = await createGelatoSmartWalletClient(client, { |
| 63 | + apiKey: gelatoApiKey, |
| 64 | + }); |
| 65 | + |
| 66 | + const response = await swc.execute({ |
| 67 | + calls: [ |
| 68 | + { |
| 69 | + data: "0xd09de08a", |
| 70 | + to: "0xEEeBe2F778AA186e88dCf2FEb8f8231565769C27", |
| 71 | + value: 0n, |
| 72 | + }, |
| 73 | + ], |
| 74 | + payment: sponsored(), |
| 75 | + }); |
| 76 | + |
| 77 | + console.log(`Your Gelato id is: ${response.id}`); |
| 78 | + console.log("Waiting for transaction to be confirmed..."); |
| 79 | + |
| 80 | + // Listen for events |
| 81 | + response.on("success", (status: GelatoTaskStatus) => { |
| 82 | + console.log(`Transaction successful: ${status.transactionHash}`); |
| 83 | + process.exit(0); |
| 84 | + }); |
| 85 | + response.on("error", (error: Error) => { |
| 86 | + console.error(`Transaction failed: ${error.message}`); |
| 87 | + process.exit(1); |
| 88 | + }); |
| 89 | +})(); |
0 commit comments