Repository navigation
Expand file tree
/
Copy pathwallet.js
More file actions
48 lines (39 loc) · 1.6 KB
/
Copy pathwallet.js
File metadata and controls
48 lines (39 loc) · 1.6 KB
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
import 'dotenv/config';
import { createModularAccountAlchemyClient } from "@alchemy/aa-alchemy";
import { LocalAccountSigner, baseSepolia } from "@alchemy/aa-core";
import { parseEther } from "viem";
import { generatePrivateKey } from "viem/accounts";
const ALCHEMY_API_KEY = process.env.ALCHEMY_API_KEY;
const PRIVATE_KEY = process.env.PRIVATE_KEY || generatePrivateKey();
if (!ALCHEMY_API_KEY) {
throw new Error("ALCHEMY_API_KEY is required in .env file");
}
export async function executeSwap() {
console.log("\nInitializing Smart Account...");
let smartAccountClient;
try {
const signer = LocalAccountSigner.privateKeyToAccountSigner(PRIVATE_KEY);
smartAccountClient = await createModularAccountAlchemyClient({
apiKey: ALCHEMY_API_KEY,
chain: baseSepolia,
signer,
});
} catch (e) {
console.error("Smart Account initialization failed:", e.message);
return;
}
const address = smartAccountClient.getAddress();
console.log("Wallet Address:", address);
console.log("Attempting Transaction...");
try {
const userOpResult = await smartAccountClient.sendUserOperation({
uo: { target: address, data: "0x", value: parseEther("0") },
});
console.log("Waiting for confirmation...");
const txHash = await smartAccountClient.waitForUserOperationTransaction(userOpResult);
console.log("Transaction successful!");
console.log("Hash:", `https://sepolia.basescan.org/tx/${txHash}`);
} catch (error) {
console.error("Transaction failed:", error.message);
}
}