Advertisement
0xtris

Untitled

Feb 26th, 2022
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Next js
  2. import Image from "next/image";
  3. import { useEffect } from "react";
  4. // Redux
  5. import { useDispatch } from "react-redux";
  6. import {
  7.     setConnectPopup,
  8.     setProvider,
  9.     setSigner,
  10.     setChainId,
  11.     setAddress,
  12.     resetWeb3,
  13. } from "../../redux/actions/actions";
  14. // React icon
  15. import { AiOutlineClose } from "react-icons/ai";
  16. // Web3
  17. import WalletConnectProvider from "@walletconnect/web3-provider";
  18. import { providers, ethers } from "ethers";
  19.  
  20. const tailwind = {
  21.     providerContainer:
  22.         "flex flex-col items-center mx-5 hover:bg-zinc-200 px-5 py-10 cursor-pointer duration-200",
  23. };
  24.  
  25. const ConnectPopup = () => {
  26.     const dispatch = useDispatch();
  27.  
  28.     // Connection to metamask =============================
  29.     async function connectMetamask() {
  30.         if (window.ethereum) {
  31.             try {
  32.                 window.ethereum
  33.                     .request({ method: "eth_requestAccounts" })
  34.                     .then((result) => {
  35.                         updateMetamask(result[0]);
  36.                     });
  37.             } catch (error) {
  38.                 console.log(error);
  39.             }
  40.         } else {
  41.             alert("Please install Metamask Wallet: metamask.io");
  42.         }
  43.     }
  44.  
  45.     async function updateMetamask() {
  46.         const provider = new ethers.providers.Web3Provider(window.ethereum);
  47.         const signer = provider.getSigner();
  48.         const address = await signer.getAddress();
  49.         const network = await provider.getNetwork();
  50.         // ======
  51.         dispatch(setProvider(provider));
  52.         dispatch(setSigner(signer));
  53.         dispatch(setAddress(address));
  54.         dispatch(setChainId(network.chainId));
  55.         dispatch(setConnectPopup(false));
  56.     }
  57.  
  58.     // Connection with Wallet Connect and Infura section =============================
  59.     const infuraId = "c324248cc5e14a859dcc4ea1a25b0880";
  60.     const WCprovider = new WalletConnectProvider({
  61.         rpc: {
  62.             137: `https://polygon-mainnet.infura.io/v3/${infuraId}`,
  63.         },
  64.     });
  65.  
  66.     async function connectWalletConnect() {
  67.         try {
  68.             dispatch(setConnectPopup(false));
  69.             await WCprovider.enable();
  70.             const web3Provider = new providers.Web3Provider(WCprovider);
  71.             const signer = web3Provider.getSigner();
  72.             const address = await signer.getAddress();
  73.             const network = await web3Provider.getNetwork();
  74.             // ===============
  75.             dispatch(setProvider(web3Provider));
  76.             dispatch(setSigner(signer));
  77.             dispatch(setAddress(address));
  78.             dispatch(setChainId(network.chainId));
  79.         } catch (error) {
  80.             console.log(error);
  81.         }
  82.     }
  83.  
  84.     // Wallet events =====================================
  85.     useEffect(() => {
  86.         window.ethereum.on("chainChanged", (chainId) => {
  87.             if (chainId === "0x89") {
  88.                 dispatch(setChainId(137));
  89.             } else {
  90.                 dispatch(setChainId(chainId));
  91.             }
  92.         });
  93.  
  94.         window.ethereum.on("accountsChanged", (accounts) => {
  95.             dispatch(resetWeb3());
  96.             connectMetamask();
  97.         });
  98.     });
  99.  
  100.     return (
  101.         <div
  102.             className="fixed w-full flex items-center justify-center min-h-screen z-[2000]"
  103.             style={{
  104.                 backgroundColor: "rgba(0, 0, 0, 0.9)",
  105.             }}
  106.         >
  107.             <div className="bg-white rounded-xl p-10 flex flex-col lg:flex-row text-black relative mx-5">
  108.                 <div
  109.                     className={tailwind.providerContainer}
  110.                     onClick={connectMetamask}
  111.                 >
  112.                     <Image
  113.                         src="/img/metamask-fox.svg"
  114.                         alt="logo metamask"
  115.                         height={75}
  116.                         width={75}
  117.                     />
  118.                     <h3 className="text-2xl font-bold mt-3">Metamask</h3>
  119.                     <p className="text-center">
  120.                         Connectez votre wallet Metamask
  121.                     </p>
  122.                 </div>
  123.                 <div
  124.                     className={tailwind.providerContainer}
  125.                     onClick={connectWalletConnect}
  126.                 >
  127.                     <Image
  128.                         src="/img/wallet-connect.png"
  129.                         alt="logo metamask"
  130.                         height={75}
  131.                         width={75}
  132.                     />
  133.                     <h3 className="text-2xl font-bold mt-3">WalletConnect</h3>
  134.                     <p className="text-center">
  135.                         Une centaine de wallets disponibles
  136.                     </p>
  137.                 </div>
  138.                 <AiOutlineClose
  139.                     className="absolute text-2xl top-5 right-5 cursor-pointer"
  140.                     onClick={() => dispatch(setConnectPopup(false))}
  141.                 />
  142.             </div>
  143.         </div>
  144.     );
  145. };
  146.  
  147. export default ConnectPopup;
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement