From 9f73ee59ec90a6153a4715cd518f002620cf84d1 Mon Sep 17 00:00:00 2001 From: GuiBibeau Date: Wed, 5 Oct 2022 11:27:57 -0400 Subject: [PATCH] step 2 --- README.md | 2 +- components/Wallet.tsx | 32 +++++++++++++++++++++++++++++--- hooks/useMetamask.tsx | 16 +++++++++------- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c3698ec..b43cb8a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Branches will be created with stages for the rest of the implementation You are presented with a Next.js application that has a basic layout and a Context Provider. Your goal is to learn how to use the Metamask API to make this an application that users can interact with. -Few notes about the initial setup ans constraints: +Few notes about the initial setup and constraints: - You do not need any additional dependencies. - The context provider is already setup so that any react code under [components](./components/) and [pages](./pages/) can access the context using a hook. diff --git a/components/Wallet.tsx b/components/Wallet.tsx index d163d50..0e50004 100644 --- a/components/Wallet.tsx +++ b/components/Wallet.tsx @@ -6,12 +6,13 @@ import { Loading } from "./Loading"; export default function Wallet() { const { dispatch, - state: { status, isMetamaskInstalled }, + state: { status, isMetamaskInstalled, wallet, balance }, } = useMetamask(); const showInstallMetamask = status !== "pageNotLoaded" && !isMetamaskInstalled; - const showConnectButton = status !== "pageNotLoaded" && isMetamaskInstalled; + const showConnectButton = + status !== "pageNotLoaded" && isMetamaskInstalled && !wallet; const handleConnect = async () => { dispatch({ type: "loading" }); @@ -20,7 +21,12 @@ export default function Wallet() { }); if (accounts.length > 0) { - dispatch({ type: "connect", wallet: accounts[0] }); + const balance = await window.ethereum!.request({ + method: "eth_getBalance", + params: [accounts[0], "latest"], + }); + + dispatch({ type: "connect", wallet: accounts[0], balance }); } }; @@ -40,6 +46,26 @@ export default function Wallet() { {" "} in order to learn how to use the Metamask API.

+ + {wallet && ( +
+
+
+
+
+

+ Address: {wallet} +

+

+ Balance: {balance} +

+
+
+
+
+
+ )} + {showConnectButton && (