-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
100 lines (78 loc) · 3.34 KB
/
main.ts
File metadata and controls
100 lines (78 loc) · 3.34 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
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
//===== ====== //
// import { ethers } from 'ethers';
// import { Presets, Client } from 'userop';
// import * as dotenv from 'dotenv';
// dotenv.config();
// const config = {
// rpcUrl: process.env.RPC_URL,
// paymasterUrl: process.env.PAYMASTER_RPC_URL,
// provider_data: process.env.PROVIDER
// }
// const rpcUrl = config.rpcUrl;
// const paymasterUrl = config.paymasterUrl;
// let provider: ethers.providers.JsonRpcProvider | undefined;
// if (config.provider_data) {
// provider = new ethers.providers.JsonRpcProvider(config.provider_data);
// } else {
// console.error('Provider data is undefined.');
// }
import { ethers } from 'ethers';
import { Presets, Client } from 'userop';
// require('dotenv').config();
import * as dotenv from 'dotenv';
dotenv.config();
// const config = {
// rpcUrl: process.env.RPC_URL,
// paymasterUrl: process.env.PAYMASTER_RPC_URL,
// provider_data: process.env.PROVIDER
// }
// const rpcUrl: string | undefined = process.env.RPC_URL;
// const paymasterUrl: string | undefined = process.env.PAYMASTER_RPC_URL;
const rpcUrl= "https://api.stackup.sh/v1/node/c713cd6b5ac9196b897e0e69c5be6951f3f18b3ca763fd7cd346310b9ddfdd3e";
const paymasterUrl = "https://api.stackup.sh/v1/paymaster/c713cd6b5ac9196b897e0e69c5be6951f3f18b3ca763fd7cd346310b9ddfdd3e";
const provider = new ethers.providers.JsonRpcProvider("https://sepolia.infura.io/v3/9a8614ca9a11412aaab9734cdde0cd29");
async function main() {
const paymasterContext = { type: 'payg' };
const paymasterMiddleware = Presets.Middleware.verifyingPaymaster(
paymasterUrl,
paymasterContext
);
const opts =
paymasterUrl.toString() === ''
? {}
: {
paymasterMiddleware: paymasterMiddleware,
};
// Initialize the account
const signingKey ="0x";
// console.log(signingKey);
const signer = new ethers.Wallet(signingKey);
var builder = await Presets.Builder.SimpleAccount.init(signer, rpcUrl, opts);
const address = builder.getSender();
console.log(`Account address: ${address}`);
// Create the call data
const to = address; // Receiving address, in this case we will send it to ourselves
const token = "0x3938Ac4C25454F65F62292b748a105d70bA5D71f"; // Address of the ERC-20 token
const value = "0"; // Amount of the ERC-20 token to transfer
// Read the ERC-20 token contract
const ERC20_ABI = require("./erc20Abi.json"); // ERC-20 ABI in json format
const erc20 = new ethers.Contract(token, ERC20_ABI, provider);
const decimals = await Promise.all([erc20.decimals()]);
const amount = ethers.utils.parseUnits(value, decimals);
// Encode the calls
const callTo = [token, token];
const callData = [erc20.interface.encodeFunctionData("approve", [to, amount]),
erc20.interface.encodeFunctionData("transfer", [to, amount])]
// Send the User Operation to the ERC-4337 mempool
const client = await Client.init(rpcUrl);
const res = await client.sendUserOperation(builder.executeBatch(callTo, callData), {
onBuild: (op) => console.log('Signed UserOperation:', op),
});
// Return receipt
console.log(`UserOpHash: ${res.userOpHash}`);
console.log('Waiting for transaction...');
const ev = await res.wait();
console.log(`Transaction hash: ${ev?.transactionHash ?? null}`);
console.log(`View here: https://jiffyscan.xyz/userOpHash/${res.userOpHash}`);
}
main().catch((err) => console.error('Error:', err));