Skip to content

Commit

Permalink
chore: Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wryonik committed Sep 17, 2024
1 parent 662d84a commit 373747f
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 427 deletions.
3 changes: 0 additions & 3 deletions src/components/GuardianSetup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { useAppContext } from "../context/AppContextHook";
import { safeEmailRecoveryModule } from "../../contracts.base-sepolia.json";
import {
genAccountCode,
getRequestGuardianCommand,
templateIdx,
} from "../utils/email";
import { readContract } from "wagmi/actions";
Expand Down Expand Up @@ -190,8 +189,6 @@ const GuardianSetup = () => {

console.debug("recovery configured");

// const command = getRequestGuardianCommand(address);

const command = await readContract(config, {
abi: safeEmailRecoveryModuleAbi,
address: safeEmailRecoveryModule as `0x${string}`,
Expand Down
94 changes: 0 additions & 94 deletions src/components/PerformRecovery.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/RequestedRecoveries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useReadContract } from "wagmi";
import infoIcon from "../assets/infoIcon.svg";

import { relayer } from "../services/relayer";
import { getRequestsRecoveryCommand, getRequestsRecoveryCommandForSafe13, templateIdx } from "../utils/email";
import { templateIdx } from "../utils/email";
import { safeEmailRecoveryModule } from "../../contracts.base-sepolia.json";
import { StepsContext } from "../App";
import toast from "react-hot-toast";
Expand Down
94 changes: 26 additions & 68 deletions src/components/burnerWallet/GuardianSetup.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { useCallback, useContext, useEffect, useMemo, useState } from "react";
import { ConnectKitButton } from "connectkit";
import { useCallback, useContext, useEffect, useState } from "react";
import { Button } from "../Button";
import { useAccount, useReadContract, useWriteContract } from "wagmi";
import { useWriteContract } from "wagmi";
import infoIcon from "../../assets/infoIcon.svg";
import { useAppContext } from "../../context/AppContextHook";

import { abi as universalEmailRecoveryModuleAbi } from "../../abi/UniversalEmailRecoveryModule.json";
import { universalEmailRecoveryModule } from "../../../contracts.base-sepolia.json";
import {
genAccountCode,
getRequestGuardianCommand,
templateIdx,
} from "../../utils/email";
import { genAccountCode, templateIdx } from "../../utils/email";
import { readContract } from "wagmi/actions";
import { config } from "../../providers/config";
import { relayer } from "../../services/relayer";
Expand Down Expand Up @@ -41,8 +36,6 @@ import {
TextField,
Typography,
} from "@mui/material";
import { useTheme } from "@mui/material";
import Loader from "../Loader";
import { useGetSafeAccountAddress } from "../../utils/useGetSafeAccountAddress";

const TIME_UNITS = {
Expand Down Expand Up @@ -77,14 +70,11 @@ const isValidEmail = (email: string) => {
const GuardianSetup = () => {
const address = useGetSafeAccountAddress();
const { writeContractAsync } = useWriteContract();
const [account, setAccount] = useState<string>(null);
const [accountCreationError, setAccountCreationError] = useState(false);

const { guardianEmail, setGuardianEmail, accountCode, setAccountCode } =
useAppContext();
const stepsContext = useContext(StepsContext);

const [isAccountInitialized, setIsAccountInitialized] = useState(false);
const [isAccountInitializedLoading, setIsAccountInitializedLoading] =

Check failure on line 78 in src/components/burnerWallet/GuardianSetup.tsx

View workflow job for this annotation

GitHub Actions / eslint

'isAccountInitializedLoading' is assigned a value but never used

Check failure on line 78 in src/components/burnerWallet/GuardianSetup.tsx

View workflow job for this annotation

GitHub Actions / eslint

'isAccountInitializedLoading' is assigned a value but never used
useState(false);
const [loading, setLoading] = useState(false);
Expand All @@ -109,8 +99,6 @@ const GuardianSetup = () => {

let interval: NodeJS.Timeout;

const isMobile = window.innerWidth < 768;

const checkIfRecoveryIsConfigured = async () => {
if (!address) {
return;
Expand All @@ -125,7 +113,6 @@ const GuardianSetup = () => {

console.log(getGuardianConfig);

// TODO: add polling for this
if (
getGuardianConfig.acceptedWeight === getGuardianConfig.threshold &&
getGuardianConfig.threshold !== 0n
Expand Down Expand Up @@ -173,10 +160,6 @@ const GuardianSetup = () => {

const acctCode = await genAccountCode();

// if (localStorage.getItem("accountCode")) {
// return;
// }

localStorage.setItem("accountCode", acctCode);
await setAccountCode(accountCode);

Expand All @@ -193,7 +176,6 @@ const GuardianSetup = () => {
});

const burnerWalletAddress = await run(client, safeAccount, guardianAddr);
setAccount(burnerWalletAddress);
localStorage.setItem(
"burnerWalletConfig",
JSON.stringify({ burnerWalletAddress })
Expand All @@ -202,7 +184,6 @@ const GuardianSetup = () => {
} catch (error) {
console.log(error);
toast.error(`Something went wrong. Err: ${error.shortMessage}`);
setAccountCreationError(error);
} finally {
setIsBurnerWalletCreating(false);
}
Expand Down Expand Up @@ -239,33 +220,42 @@ const GuardianSetup = () => {
throw new Error("guardian email not set");
}

if(!localStorageAccountCode) {
toast.error("Seomthing went wrong, please restart the flow")
console.error("Invalid account code")
}

try {
setLoading(true);
toast(
"Please check your email",
{
icon: <img src={infoIcon} />,
style: {
background: "white",
},
}
);
toast("Please check your email", {
icon: <img src={infoIcon} />,
style: {
background: "white",
},
});

const subject = await readContract(config, {
abi: universalEmailRecoveryModuleAbi,
address: universalEmailRecoveryModule as `0x${string}`,
functionName: "acceptanceCommandTemplates",
args: [],
});
console.log(subject, "command");

const subject = getRequestGuardianCommand(address);
const { requestId } = await relayer.acceptanceRequest(
universalEmailRecoveryModule as `0x${string}`,
guardianEmail,
localStorageAccountCode,
templateIdx,
subject
subject[0].join().replaceAll(",", " ").replace("{ethAddr}", address)
);

// Setting up interval for polling
interval = setInterval(() => {

Check warning on line 254 in src/components/burnerWallet/GuardianSetup.tsx

View workflow job for this annotation

GitHub Actions / eslint

Assignments to the 'interval' variable from inside React Hook useCallback will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useCallback

Check warning on line 254 in src/components/burnerWallet/GuardianSetup.tsx

View workflow job for this annotation

GitHub Actions / eslint

Assignments to the 'interval' variable from inside React Hook useCallback will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useCallback
checkIfRecoveryIsConfigured();
}, 5000); // Adjust the interval time (in milliseconds) as needed
} catch (err) {
console.log(err);
console.error(err);
toast.error(
err?.shortMessage ?? "Something went wrong, please try again."
);
Expand Down Expand Up @@ -350,38 +340,6 @@ const GuardianSetup = () => {
</Select>
</Grid>
</Grid>

{/* <Box
display="flex"
justifyContent="space-between"
alignItems="center"
sx={{ marginRight: "35px" }}
>
<Typography variant="body1" sx={{ marginRight: '25px', textAlign:'left' }}>Recovery Expiry (hours)</Typography>
<InputNumber
type="number"
min={1}
value={recoveryExpiry}
onChange={(e) =>
setRecoveryExpiry(
parseInt((e.target as HTMLInputElement).value)
)
}
title='Recovery Expiry (hours)'
message='This is the expiry delay that...'
/>
</Box> */}

{/* <Grid
item
container
gap="1rem"
justifyContent={"space-between"}
alignItems={"center"}
>
<Typography variant="body1">Connected Wallet:</Typography>
<ConnectKitButton />
</Grid> */}
</Grid>
<Grid item sx={{ borderRight: { md: "1px solid #EBEBEB" } }} />

Expand Down Expand Up @@ -421,7 +379,7 @@ const GuardianSetup = () => {
></Box>
{isWalletPresent ? (
<Button
disabled={!guardianEmail || isAccountInitialized}
disabled={!guardianEmail}
loading={loading}
onClick={configureRecoveryAndRequestGuardian}
filled={true}
Expand All @@ -430,7 +388,7 @@ const GuardianSetup = () => {
</Button>
) : (
<Button
disabled={!guardianEmail || isAccountInitialized}
disabled={!guardianEmail}
loading={isBurnerWalletCreating}
onClick={connectWallet}
filled={true}
Expand Down
Loading

0 comments on commit 373747f

Please sign in to comment.