Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid auto popup of ensureEthereumChain #193

Closed
wants to merge 11 commits into from
13 changes: 2 additions & 11 deletions components/AppSwitch.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import { VFC, useEffect } from "react";
import { VFC } from "react";
import { css } from "@emotion/react";
import Link from "next/link";
import { Theme } from "@mui/material";
import useSectionUri from "@/hooks/useSectionUri";
import { useMetaMaskExtension } from "@/providers/MetaMaskExtensionProvider";
import { ensureEthereumChain } from "@/utils";
import { useSectionUri } from "@/hooks";

const Switch: VFC = () => {
const section = useSectionUri();
const { extension } = useMetaMaskExtension();

useEffect(() => {
if (!extension) return;

if (section === "bridge") void ensureEthereumChain(extension);
}, [extension, section]);

return (
<nav css={styles.container}>
Expand Down
36 changes: 25 additions & 11 deletions components/BridgeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import BridgeWithdrawAdvanced from "@/components/BridgeWithdrawAdvanced";
import { useDepositRequest, useWithdrawRequest } from "@/hooks";
import { useWalletProvider } from "@/providers/WalletProvider";
import { ETH_CHAIN_ID } from "@/constants";
import { useMetaMaskExtension } from "@/providers/MetaMaskExtensionProvider";
import { ensureEthereumChain } from "@/utils";

interface BridgeFormProps {}

Expand All @@ -18,6 +20,7 @@ const BridgeForm: FC<IntrinsicElements["form"] & BridgeFormProps> = ({
}) => {
const { bridgeAction } = useBridge();
const { connectedChain } = useWalletProvider();
const { extension } = useMetaMaskExtension();

const [buttonLabel, setButtonLabel] = useState<string>("Deposit");

Expand Down Expand Up @@ -61,11 +64,13 @@ const BridgeForm: FC<IntrinsicElements["form"] & BridgeFormProps> = ({
</div>
)}

{!!connectedChain && connectedChain !== "Ethereum" && (
<div css={styles.formNote}>
Please connect to{" "}
{ETH_CHAIN_ID === 1 ? "Ethereum Mainnet" : "Kovan Test Network"} in
MetaMask.
{connectedChain !== "Ethereum" && (
<div
css={styles.formNote(true)}
onClick={() => ensureEthereumChain(extension)}
>
Connect to{" "}
{ETH_CHAIN_ID === 1 ? "Ethereum Mainnet" : "Kovan Test Network"}
</div>
)}
</div>
Expand All @@ -88,10 +93,19 @@ const styles = {
margin: 2em -2.5em 0;
`,

formNote: ({ palette }: Theme) => css`
font-size: 14px;
margin: 1em auto 0;
color: ${palette.grey["800"]};
width: 240px;
`,
formNote:
(isMetaMask?: boolean) =>
({ palette }: Theme) =>
css`
font-size: 14px;
margin: 1em auto 0;
color: ${palette.grey["800"]};
width: 240px;

${isMetaMask && `
cursor: pointer;
font-style: italic;
text-decoration: underline;
`}
`,
};
6 changes: 4 additions & 2 deletions hooks/useBridgeStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ import { useMetaMaskWallet } from "@/providers/MetaMaskWalletProvider";
import { BridgeStatus } from "@/types";
import { fetchBridgeDepositStatus, fetchBridgeWithdrawStatus } from "@/utils";
import { useEffect, useState } from "react";
import { useWalletProvider } from "@/providers/WalletProvider";

export default function useBridgeStatus(): BridgeStatus {
const [bridgeStatus, setBridgeStatus] = useState<BridgeStatus>("Active");
const { api } = useCENNZApi();
const { wallet } = useMetaMaskWallet();
const { bridgeAction } = useBridge();
const { connectedChain } = useWalletProvider();

useEffect(() => {
if (!api || !wallet) return;
if (!api || !wallet || connectedChain !== "Ethereum") return;

if (bridgeAction === "Deposit")
fetchBridgeDepositStatus(api, wallet).then(setBridgeStatus);

if (bridgeAction === "Withdraw")
fetchBridgeWithdrawStatus(api, wallet).then(setBridgeStatus);
}, [bridgeAction, api, wallet]);
}, [bridgeAction, api, wallet, connectedChain]);

return bridgeStatus;
}
9 changes: 6 additions & 3 deletions hooks/useBridgeVerificationFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useBridge } from "@/providers/BridgeProvider";
import { useMetaMaskWallet } from "@/providers/MetaMaskWalletProvider";
import { Balance, getBridgeContract } from "@/utils";
import { useCallback, useEffect, useState } from "react";
import { useWalletProvider } from "@/providers/WalletProvider";

interface BridgeVerificationFeeHook {
verificationFee: Balance;
Expand All @@ -10,10 +11,11 @@ interface BridgeVerificationFeeHook {
}

export default function useBridgeVerificationFee(): BridgeVerificationFeeHook {
const { ethAsset } = useBridge();
const { wallet } = useMetaMaskWallet();
const { connectedChain } = useWalletProvider();
const [loading, setLoading] = useState<boolean>(true);
const [verificationFee, setVerificationFee] = useState<Balance>(null);
const { ethAsset } = useBridge();

const updateVerificationFee = useCallback(async () => {
if (!wallet) return;
Expand All @@ -26,8 +28,9 @@ export default function useBridgeVerificationFee(): BridgeVerificationFeeHook {
}, [wallet, ethAsset]);

useEffect(() => {
updateVerificationFee?.();
}, [updateVerificationFee]);
if (connectedChain !== "Ethereum") return;
void updateVerificationFee?.();
}, [updateVerificationFee, connectedChain]);

return {
verificationFee,
Expand Down
8 changes: 7 additions & 1 deletion providers/MetaMaskWalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
} from "react";
import { ethers } from "ethers";
import { MetaMaskAccount } from "@/types";
import { ensureEthereumChain } from "@/utils";
import { useSectionUri } from "@/hooks";

interface MetaMaskWalletContextType {
connectWallet: (callback?: () => void) => Promise<void>;
Expand All @@ -31,13 +33,17 @@ const MetaMaskWalletProvider: FC<MetaMaskWalletProviderProps> = ({
const [selectedAccount, setSelectedAccount] =
useState<MetaMaskWalletContextType["selectedAccount"]>(null);

const section = useSectionUri();

const connectWallet = useCallback(
async (callback) => {
if (!extension) {
callback?.();
return promptInstallExtension();
}

if (section === "bridge") await ensureEthereumChain(extension);

aidan-starke marked this conversation as resolved.
Show resolved Hide resolved
const accounts = (await extension.request({
method: "eth_requestAccounts",
})) as string[];
Expand All @@ -50,7 +56,7 @@ const MetaMaskWalletProvider: FC<MetaMaskWalletProviderProps> = ({
setSelectedAccount({ address: accounts[0] });
setWallet(new ethers.providers.Web3Provider(extension as any, "any"));
},
[extension, promptInstallExtension]
[extension, promptInstallExtension, section]
);

useEffect(() => {
Expand Down