-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-en.html
More file actions
146 lines (125 loc) · 6.72 KB
/
admin-en.html
File metadata and controls
146 lines (125 loc) · 6.72 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSA Admin Panel</title>
<script src="https://cdn.jsdelivr.net/npm/ethers@5.7.2/dist/ethers.umd.min.js"></script>
<style>
body { font-family: sans-serif; max-width: 700px; margin: 2em auto; padding: 1em; border: 1px solid #ccc; border-radius: 8px; }
button { font-size: 1em; padding: 0.5em 1em; cursor: pointer; border-radius: 4px; border: 1px solid #007bff; background-color: #007bff; color: white; margin-top: 10px; }
button:disabled { background-color: #ccc; border-color: #ccc; cursor: not-allowed; }
input { font-size: 1em; padding: 0.5em; width: 95%; margin-bottom: 10px; }
label { display: block; margin-bottom: 5px; font-weight: bold; }
#status { margin-top: 1em; padding: 0.5em; background-color: #f0f0f0; border-radius: 4px; word-break: break-all; }
.error-message { color: #d9534f; }
.success-message { color: #5cb85c; }
</style>
</head>
<body>
<h1>CSA (Community Supported Agriculture) Admin Panel</h1>
<p>Use this page to manage your contract and create new seasons.</p>
<button id="connectButton">Connect as Administrator</button>
<hr>
<div id="adminPanel" style="display:none;">
<h2>Create New Season</h2>
<form id="seasonForm">
<div>
<label for="seasonName">Season Name:</label>
<input type="text" id="seasonName" required placeholder="E.g., Winter Harvest 2024">
</div>
<div>
<label for="seasonPrice">Share Price (in AVAX):</label>
<input type="text" id="seasonPrice" required placeholder="E.g., 1.5">
</div>
<div>
<label for="seasonCapacity">Capacity (Number of Members):</label>
<input type="number" id="seasonCapacity" required placeholder="E.g., 100">
</div>
<div>
<label for="seasonDuration">Duration (in weeks):</label>
<input type="number" id="seasonDuration" required placeholder="E.g., 12">
</div>
<button type="submit" id="createSeasonButton">Create Season</button>
</form>
</div>
<div id="accessDenied" style="display:none;">
<h2 class="error-message">Access Denied</h2>
<p>The connected wallet is not the owner of this contract. Only the administrator can access this page.</p>
</div>
<div id="status">Please connect your wallet to continue...</div>
<script>
const connectButton = document.getElementById('connectButton');
const statusDiv = document.getElementById('status');
const adminPanel = document.getElementById('adminPanel');
const accessDenied = document.getElementById('accessDenied');
const seasonForm = document.getElementById('seasonForm');
const createSeasonButton = document.getElementById('createSeasonButton');
// Form inputs
const seasonNameInput = document.getElementById('seasonName');
const seasonPriceInput = document.getElementById('seasonPrice');
const seasonCapacityInput = document.getElementById('seasonCapacity');
const seasonDurationInput = document.getElementById('seasonDuration');
const contractAddress = "0x1c96D0701B1bd9c3CC24fAcca3A4d68ED8Bfb1AF";
const contractABI = [
"function createNewSeason(string name, uint256 price, uint256 capacity, uint256 durationInWeeks)",
"function owner() view returns (address)"
];
let provider, signer, contract;
connectButton.addEventListener('click', async () => {
if (typeof window.ethereum !== 'undefined') {
try {
// Request connection to MetaMask
await window.ethereum.request({ method: 'eth_requestAccounts' });
provider = new ethers.providers.Web3Provider(window.ethereum);
signer = provider.getSigner();
contract = new ethers.Contract(contractAddress, contractABI, signer);
const userAddress = await signer.getAddress();
const contractOwner = await contract.owner();
connectButton.style.display = 'none';
// SECURITY LOGIC
if (userAddress.toLowerCase() === contractOwner.toLowerCase()) {
statusDiv.textContent = `Administrator connected: ${userAddress.substring(0, 6)}...`;
adminPanel.style.display = 'block';
} else {
statusDiv.textContent = `Connected wallet is not the admin: ${userAddress.substring(0, 6)}...`;
accessDenied.style.display = 'block';
}
} catch (error) {
statusDiv.textContent = `Error connecting: ${error.message}`;
}
} else {
statusDiv.textContent = "MetaMask not found!";
}
});
seasonForm.addEventListener('submit', async (event) => {
// Prevents the default page reload
event.preventDefault();
const name = seasonNameInput.value;
const price = seasonPriceInput.value;
const capacity = seasonCapacityInput.value;
const duration = seasonDurationInput.value;
if (!name || !price || !capacity || !duration) {
statusDiv.textContent = "Please fill out all fields.";
return;
}
try {
// Converts the price from AVAX to Wei
const priceInWei = ethers.utils.parseEther(price);
createSeasonButton.disabled = true;
statusDiv.innerHTML = `<span class="success-message">Sending transaction... Please confirm in your wallet.</span>`;
const tx = await contract.createNewSeason(name, priceInWei, capacity, duration);
statusDiv.textContent = `Transaction sent! Awaiting mining... Hash: ${tx.hash}`;
await tx.wait();
statusDiv.innerHTML = `<span class="success-message">Success! New season "${name}" has been created!</span>`;
createSeasonButton.disabled = false;
seasonForm.reset(); // Clears the form
} catch (error) {
console.error(error);
statusDiv.innerHTML = `<span class="error-message">Error creating season: ${error.reason || error.message}</span>`;
createSeasonButton.disabled = false;
}
});
</script>
</body>
</html>