-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.jsx
More file actions
72 lines (63 loc) · 1.99 KB
/
Copy pathApp.jsx
File metadata and controls
72 lines (63 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { useState } from 'react'
import { ethers } from 'ethers'
import { CONTRACT_ADDRESS, CONTRACT_ABI } from './constants'
import './App.css'
function App() {
const [account, setAccount] = useState(null)
const [status, setStatus] = useState('')
const [isMinting, setIsMinting] = useState(false)
const connectWallet = async () => {
if (window.ethereum) {
try {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' })
setAccount(accounts[0])
setStatus('Wallet connected')
} catch (error) {
setStatus('Error connecting wallet: ' + error.message)
}
} else {
setStatus('Please install MetaMask!')
}
}
const mintNFT = async () => {
if (!account) {
setStatus('Please connect your wallet first')
return
}
try {
setIsMinting(true)
setStatus('Minting...')
const provider = new ethers.BrowserProvider(window.ethereum)
const signer = await provider.getSigner()
const contract = new ethers.Contract(CONTRACT_ADDRESS, CONTRACT_ABI, signer)
const tx = await contract.mint()
setStatus('Transaction sent. Waiting for confirmation...')
await tx.wait()
setStatus('NFT Minted successfully!')
} catch (error) {
console.error(error)
setStatus('Minting failed: ' + (error.reason || error.message))
} finally {
setIsMinting(false)
}
}
return (
<div className="container">
<h1>NFT Minter</h1>
<div className="card">
{!account ? (
<button onClick={connectWallet}>Connect Wallet</button>
) : (
<div className="action-area">
<p className="account">Connected: {account.slice(0, 6)}...{account.slice(-4)}</p>
<button onClick={mintNFT} disabled={isMinting}>
{isMinting ? 'Minting...' : 'Mint NFT'}
</button>
</div>
)}
{status && <p className="status">{status}</p>}
</div>
</div>
)
}
export default App