mirror of
https://github.com/rjNemo/epicNFT
synced 2026-06-12 12:46:39 +00:00
conect client to wallet
This commit is contained in:
parent
db7eaded75
commit
e222193365
3 changed files with 51 additions and 5 deletions
|
|
@ -1,13 +1,46 @@
|
||||||
import "./App.css";
|
import "./App.css";
|
||||||
|
import {} from "ethers";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { withEth } from "./lib/eth";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const [currentAccount, setCurrentAccount] = useState("");
|
||||||
|
|
||||||
|
const checkIfWalletConnected = withEth(async (ethereum) => {
|
||||||
|
const accounts = await ethereum.request({ method: "eth_accounts" });
|
||||||
|
if (accounts.length !== 0) {
|
||||||
|
console.log(`Found authorized account ${accounts[0]}`);
|
||||||
|
setCurrentAccount(accounts[0]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const connectWallet = withEth(async (ethereum) => {
|
||||||
|
const accounts = await ethereum.request({ method: "eth_requestAccounts" });
|
||||||
|
console.log(`connected to ${accounts[0]}`);
|
||||||
|
setCurrentAccount(accounts[0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
const mintNFT = withEth(async (ethereum) => {});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
checkIfWalletConnected();
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
<div className="container">
|
<div className="container">
|
||||||
<div className="header-container">
|
<div className="header-container">
|
||||||
<p className="header gradient-text">EpicNFTs Collection</p>
|
<p className="header gradient-text">EpicNFTs Collection</p>
|
||||||
<p className="sub-text">Each unique. Each beautiful. Discover your NFT today.</p>
|
<p className="sub-text">Each unique. Each beautiful. Discover your NFT today.</p>
|
||||||
<button className="cta-button connect-wallet-button">Connect with Wallet</button>
|
{currentAccount ? (
|
||||||
|
<button onClick={mintNFT} className="cta-button mint-button">
|
||||||
|
Mint your EpicNFT
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button className="cta-button connect-wallet-button" onClick={connectWallet}>
|
||||||
|
Connect with Wallet
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,10 @@ import ReactDOM from "react-dom";
|
||||||
import reportWebVitals from "./reportWebVitals";
|
import reportWebVitals from "./reportWebVitals";
|
||||||
import App from "./App";
|
import App from "./App";
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
import { MantineProvider } from "@mantine/core";
|
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<MantineProvider>
|
<App />
|
||||||
<App />
|
|
||||||
</MantineProvider>
|
|
||||||
</React.StrictMode>,
|
</React.StrictMode>,
|
||||||
document.getElementById("root")
|
document.getElementById("root")
|
||||||
);
|
);
|
||||||
|
|
|
||||||
16
client/src/lib/eth.js
Normal file
16
client/src/lib/eth.js
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
const getEth = () => {
|
||||||
|
const { ethereum } = window;
|
||||||
|
if (!ethereum) {
|
||||||
|
alert("Wallet not detected. Make sure you have Metamask extension installed and activated.");
|
||||||
|
}
|
||||||
|
return ethereum;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const withEth = (f) => async (props) => {
|
||||||
|
try {
|
||||||
|
const ethereum = getEth();
|
||||||
|
await f(ethereum, props);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue