generated from lakshh07/Web3-Starter-Kit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
useAccountAbstraction.ts
107 lines (91 loc) · 2.86 KB
/
useAccountAbstraction.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
import { ethersProvider, ethersSigner } from "@/utils/rainbowConfig";
import { ethers } from "ethers";
import spaceAbi from "@/contracts/ABI/Space.json";
import {
IHybridPaymaster,
PaymasterMode,
SponsorUserOperationDto,
} from "@biconomy/paymaster";
import toast from "react-hot-toast";
import { BiconomySmartAccountV2 } from "@biconomy/account";
import spaceContractAddress from "@/utils/contractAddress";
type useAccountAbstractionProps = {
setDisabled?: React.Dispatch<React.SetStateAction<boolean>>;
setLoading?: React.Dispatch<React.SetStateAction<boolean>>;
transactionData: ethers.PopulatedTransaction;
smartAccount: BiconomySmartAccountV2 | undefined;
};
export const contract = new ethers.Contract(
spaceContractAddress,
spaceAbi,
ethersProvider
);
export const signerContract = new ethers.Contract(
spaceContractAddress,
spaceAbi,
ethersSigner
);
const useAccountAbstraction = () => {
const accountAbstraction = async ({
setDisabled,
setLoading,
transactionData,
smartAccount,
}: useAccountAbstractionProps) => {
const toastId = toast.loading("Transaction logging...");
try {
setLoading && setLoading(true);
setDisabled && setDisabled(true);
const minTx = transactionData;
console.log(minTx.data);
const tx1 = {
to: spaceContractAddress,
data: minTx.data,
};
let userOp = await smartAccount?.buildUserOp([tx1]);
console.log({ userOp });
const biconomyPaymaster = smartAccount?.paymaster as IHybridPaymaster<
SponsorUserOperationDto
>;
let paymasterServiceData: SponsorUserOperationDto = {
mode: PaymasterMode.SPONSORED,
smartAccountInfo: {
name: "BICONOMY",
version: "2.0.0",
},
};
if (userOp) {
toast.loading("Transaction sent to paymaster...", {
id: toastId,
});
const paymasterAndDataResponse = await biconomyPaymaster.getPaymasterAndData(
userOp,
paymasterServiceData
);
userOp.paymasterAndData = paymasterAndDataResponse.paymasterAndData;
const userOpResponse = await smartAccount?.sendUserOp(userOp);
toast.loading("Paying gas fees...", {
id: toastId,
});
console.log("userOpHash", userOpResponse);
if (userOpResponse) {
const { receipt } = await userOpResponse.wait(1);
toast.success(
`Transaction successfull with txHash ${receipt.transactionHash}`,
{
id: toastId,
duration: 3000,
}
);
setLoading && setLoading(false);
setDisabled && setDisabled(false);
console.log("txHash", receipt.transactionHash);
}
}
} catch (err) {
console.error(err);
}
};
return { accountAbstraction };
};
export default useAccountAbstraction;