-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
69 lines (59 loc) · 2.37 KB
/
index.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
import 'dotenv/config'
import { createModularAccountAlchemyClient } from '@alchemy/aa-alchemy'
import {
Address,
LocalAccountSigner,
polygonMumbai,
type Hex,
} from '@alchemy/aa-core'
const chain = polygonMumbai
// The private key of your EOA that will be the signer to connect with the Modular Account
// Our recommendation is to store the private key in an environment variable
const privateKey = `0x${process.env.PRIVATE_KEY}` as Hex
const alchemyKey = process.env.ALCHEMY_KEY
const vitalikAddress = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045' as Address
const targetedAddress =
(process.env.TARGETED_ADDRESS as Address) || vitalikAddress
const alchemyPolicyId = process.env.ALCHEMY_POLICY_ID as string | undefined
if (!alchemyKey) {
throw new Error('Need Alchemy Key.')
}
if (!privateKey) {
throw new Error('Need a private key.')
}
if (!alchemyPolicyId) {
throw new Error('Need a gas policy id.')
}
const signer = LocalAccountSigner.privateKeyToAccountSigner(privateKey)
//TODO: We could call an Create Policy endpoint if we need to dynamically create it.
// https://docs.alchemy.com/reference/create-policy
// Create a smart account client to send user operations from your smart account
const client = await createModularAccountAlchemyClient({
// get your Alchemy API key at https://dashboard.alchemy.com
apiKey: alchemyKey,
chain,
signer,
gasManagerConfig: {
policyId: alchemyPolicyId,
},
})
;(async () => {
// Fund your account address with ETH to send for the user operations
// (e.g. Get Sepolia ETH at https://sepoliafaucet.com)
console.log('Smart Account Address: ', client.getAddress()) // Log the smart account address
console.log(`From ${client.getAddress()} to ${targetedAddress}`)
// Send a user operation from your smart account to targetedAddress (custom adress or Vitalik's adress) that does nothing
const { hash: uoHash } = await client.sendUserOperation({
uo: {
target: targetedAddress, // The desired target contract address
data: '0x', // The desired call data
value: 0n, // (Optional) value to send the target contract address
},
})
console.log('UserOperation Hash: ', uoHash) // Log the user operation hash
// Wait for the user operation to be mined
const txHash = await client.waitForUserOperationTransaction({
hash: uoHash,
})
console.log('Transaction Hash: ', txHash) // Log the transaction hash
})()