-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (78 loc) · 4.29 KB
/
app.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const contractAddress = '0xdF72AB181C97FA0979F1F22c3894f6D1356DE0B8'; // Replace with your deployed contract address
const contractABI = [
// Paste your contract ABI here
// Example:
// { "constant": true, "inputs": [], "name": "name", "outputs": [{ "name": "", "type": "string" }], "payable": false, "stateMutability": "view", "type": "function" },
// { "constant": true, "inputs": [], "name": "symbol", "outputs": [{ "name": "", "type": "string" }], "payable": false, "stateMutability": "view", "type": "function" },
// { "constant": false, "inputs": [], "name": "mint", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function" },
// { "constant": true, "inputs": [{ "name": "", "type": "uint256" }], "name": "_tokenURIs", "outputs": [{ "name": "", "type": "string" }], "payable": false, "stateMutability": "view", "type": "function" },
// { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" },
// { "constant": true, "inputs": [], "name": "owner", "outputs": [{ "name": "", "type": "address" }], "payable": false, "stateMutability": "view", "type": "function" },
// { "constant": true, "inputs": [{ "name": "", "type": "address" }], "name": "_balances", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" },
// { "constant": false, "inputs": [], "name": "withdraw", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" },
// { "constant": true, "inputs": [], "name": "MAX_SUPPLY", "outputs": [{ "name": "", "type": "uint256" }], "payable": false, "stateMutability": "view", "type": "function" }
];
let web3;
let contract;
let accounts;
async function connectWallet() {
if (window.ethereum) {
web3 = new Web3(window.ethereum);
try {
accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
contract = new web3.eth.Contract(contractABI, contractAddress);
document.getElementById('connectButton').innerText = 'Wallet Connected';
document.getElementById('mintSection').style.display = 'block';
document.getElementById('adminSection').style.display = 'block';
} catch (error) {
console.error('User denied account access', error);
}
} else {
console.error('No Ethereum provider detected');
}
}
async function mintToken() {
try {
await contract.methods.mint().send({
from: accounts[0],
value: web3.utils.toWei('0.0420', 'ether'),
gas: '300000',
});
document.getElementById('feedback').innerText = 'NFT minted successfully!';
} catch (error) {
console.error('Error minting NFT', error);
document.getElementById('feedback').innerText = 'Error minting NFT';
}
}
async function withdrawFunds() {
try {
await contract.methods.withdraw().send({
from: accounts[0],
gas: '300000',
});
document.getElementById('feedback').innerText = 'Funds withdrawn successfully!';
} catch (error) {
console.error('Error withdrawing funds', error);
document.getElementById('feedback').innerText = 'Error withdrawing funds';
}
}
async function loadContractDetails() {
try {
const name = await contract.methods.name().call();
const symbol = await contract.methods.symbol().call();
const maxSupply = await contract.methods.MAX_SUPPLY().call();
const totalSupply = await contract.methods.totalSupply().call();
console.log('Contract Name:', name);
console.log('Contract Symbol:', symbol);
console.log('Max Supply:', maxSupply);
console.log('Current Supply:', totalSupply);
} catch (error) {
console.error('Error loading contract details', error);
}
}
document.getElementById('connectButton').addEventListener('click', connectWallet);
document.getElementById('mintButton').addEventListener('click', mintToken);
document.getElementById('withdrawButton').addEventListener('click', withdrawFunds);
// Additional functions can be added for more contract interactions
// Call the function to load initial contract details
loadContractDetails();