From b7936a66d5a3fc8cc05f3932d2b26613ba7a3b37 Mon Sep 17 00:00:00 2001 From: Spencer Schoeneman Date: Thu, 7 Dec 2023 10:20:15 -0600 Subject: [PATCH 1/5] get wallet address and display in Header --- .../dash-wind/containers/Header.tsx | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/packages/nextjs/components/dash-wind/containers/Header.tsx b/packages/nextjs/components/dash-wind/containers/Header.tsx index d80d64f..e324276 100644 --- a/packages/nextjs/components/dash-wind/containers/Header.tsx +++ b/packages/nextjs/components/dash-wind/containers/Header.tsx @@ -5,6 +5,8 @@ import { useRouter } from "next/router"; import { openRightDrawer } from "../features/common/rightDrawerSlice"; import { RIGHT_DRAWER_TYPES } from "../utils/globalConstantUtil"; import { themeChange } from "theme-change"; +import { Address, createWalletClient, custom } from "viem"; +import { polygonMumbai } from "viem/chains"; import Bars3Icon from "@heroicons/react/24/outline/Bars3Icon"; import BellIcon from "@heroicons/react/24/outline/BellIcon"; import MoonIcon from "@heroicons/react/24/outline/MoonIcon"; @@ -13,7 +15,7 @@ import { setIsConnected } from "~~/auth/authSlice"; import { web3auth } from "~~/auth/web3auth"; // import UserIcon from "@heroicons/react/24/outline/UserIcon"; import { MyState, useMyDispatch, useMySelector } from "~~/components/dash-wind/app/store"; -import { Address } from "~~/components/web-3-crew/Address"; +import { Address as AddressDisplay } from "~~/components/web-3-crew/Address"; function Header() { const dispatch = useMyDispatch(); @@ -21,8 +23,19 @@ function Header() { const [currentTheme, setCurrentTheme] = useState( typeof window !== "undefined" ? localStorage.getItem("theme") : null, ); + const [address, setAddress] = useState
(null); const router = useRouter(); + useEffect(() => { + async function getAddress() { + const address = await getAccounts(); + if (address) { + setAddress(address); + } + } + getAddress(); + }, []); + useEffect(() => { themeChange(false); if (currentTheme === null) { @@ -35,6 +48,22 @@ function Header() { // 👆 false parameter is required for react project }, [currentTheme]); + async function getAccounts() { + if (!web3auth.provider) { + console.log("from login - getAccounts: provider not defined"); + return; + } + const client = createWalletClient({ + // account: privateKeyToAccount('0x...'); // from viem + chain: polygonMumbai, + transport: custom(web3auth.provider), + }); + + // Get user's public address + const [address] = await client.getAddresses(); + return address as Address; + } + // Opening right sidebar for notification const openNotification = () => { dispatch(openRightDrawer({ header: "Notifications", bodyType: RIGHT_DRAWER_TYPES.NOTIFICATION })); @@ -99,12 +128,7 @@ function Header() {