-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgroupmanagement.html
371 lines (341 loc) · 14.8 KB
/
groupmanagement.html
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Group Owner Interaction DApp</title>
<!-- Load ethers.js -->
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background: #f7f7f7;
color: #333;
}
.container {
max-width: 600px;
margin: auto;
background: #fff;
padding: 20px;
border-radius: 8px;
border: 1px solid #ccc;
}
h1, h2, h3 {
text-align: center;
}
label {
display: block;
margin-top: 10px;
font-weight: bold;
}
input[type="text"],
input[type="number"],
textarea {
width: 100%;
padding: 8px;
margin-top: 5px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
display: block;
margin: 20px auto 0 auto;
padding: 10px 20px;
background: #5C49E4;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background: #4A39CC;
}
#groupStatus, #memberStatus, #removeStatus, #updateStatus, #status {
text-align: center;
margin-top: 10px;
font-size: 14px;
white-space: pre-wrap;
}
.section {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>Group Owner Interaction DApp</h1>
<!-- Section 1: Load Group Contract -->
<div class="section" id="loadSection">
<h2>Load Group Contract</h2>
<label for="groupAddress">Group Contract Address:</label>
<input type="text" id="groupAddress" placeholder="0x..." />
<button id="loadGroup">Load Group</button>
<div id="groupStatus"></div>
</div>
<!-- Section 2: Display Group Details -->
<div class="section" id="groupDetails" style="display: none;">
<h2>Group Details</h2>
<p><strong>Owner:</strong> <span id="owner"></span></p>
<p><strong>Service:</strong> <span id="service"></span></p>
<p><strong>Mint Handler:</strong> <span id="mintHandler"></span></p>
<p><strong>Redemption Handler:</strong> <span id="redemptionHandler"></span></p>
<p><strong>Minimal Deposit (wei):</strong> <span id="minimalDeposit"></span></p>
<p><strong>Fee Collection:</strong> <span id="feeCollection"></span></p>
<button id="refreshGroup">Refresh Details</button>
</div>
<!-- Section 3: Add Member(s) -->
<div class="section" id="memberSection" style="display: none;">
<h2>Add Member(s)</h2>
<label for="members">Member Address(es) (comma separated):</label>
<input type="text" id="members" placeholder="0x..., 0x..., ..." />
<label for="expiry">Expiry (UNIX timestamp; default: 9999999999):</label>
<input type="number" id="expiry" placeholder="9999999999" />
<button id="addMembersBtn">Add Member(s)</button>
<div id="memberStatus"></div>
</div>
<!-- Section 4: Remove Trust (Remove Member(s)) -->
<div class="section" id="removeSection" style="display: none;">
<h2>Remove Trust (Remove Member(s))</h2>
<label for="removeMembers">Member Address(es) (comma separated):</label>
<input type="text" id="removeMembers" placeholder="0x..., 0x..., ..." />
<button id="removeMembersBtn">Remove Trust</button>
<div id="removeStatus"></div>
</div>
<!-- Section 5: Update Group Admin Settings -->
<div class="section" id="updateSection" style="display: none;">
<h2>Update Group Settings</h2>
<div class="update-field">
<h3>Set Service Address</h3>
<input type="text" id="newService" placeholder="0x..." />
<button id="setService">Update Service</button>
</div>
<div class="update-field">
<h3>Set Mint Handler</h3>
<input type="text" id="newMintHandler" placeholder="0x..." />
<button id="setMintHandler">Update Mint Handler</button>
</div>
<div class="update-field">
<h3>Set Redemption Handler</h3>
<input type="text" id="newRedemptionHandler" placeholder="0x..." />
<button id="setRedemptionHandler">Update Redemption Handler</button>
</div>
<div class="update-field">
<h3>Set Minimal Deposit</h3>
<input type="number" id="newMinimalDeposit" placeholder="Amount in wei" />
<button id="setMinimalDeposit">Update Minimal Deposit</button>
</div>
<div class="update-field">
<h3>Set Fee Collection Address</h3>
<input type="text" id="newFeeCollection" placeholder="0x..." />
<button id="setFeeCollection">Update Fee Collection</button>
</div>
<div id="updateStatus"></div>
</div>
<div id="status"></div>
</div>
<script>
// Global variables for provider, signer, and the group contract instance.
let provider, signer, groupContract;
// ABI for the group contract (includes trustBatch for batch trust/untrust).
const groupABI = [
"function owner() external view returns (address)",
"function service() external view returns (address)",
"function mintHandler() external view returns (address)",
"function redemptionHandler() external view returns (address)",
"function minimalDeposit() external view returns (uint256)",
"function feeCollection() external view returns (address)",
"function setService(address _service) external",
"function setMintHandler(address _mintHandler) external",
"function setRedemptionHandler(address _redemptionHandler) external",
"function setMinimalDeposit(uint256 _minimalDeposit) external",
"function setFeeCollection(address _feeCollection) external",
"function trustBatch(address[] memory _coreMembers, uint96 _expiry) external"
];
// Initialize provider and signer using the injected Ethereum provider.
async function init() {
if (typeof window.ethereum === 'undefined') {
document.getElementById("groupStatus").innerText =
"No Ethereum provider detected. Please install MetaMask (or a compatible wallet).";
return;
}
provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
signer = provider.getSigner();
}
// Load and display group contract details.
async function loadGroupDetails() {
const address = document.getElementById("groupAddress").value.trim();
if (!ethers.utils.isAddress(address)) {
document.getElementById("groupStatus").innerText = "Invalid group contract address.";
return;
}
groupContract = new ethers.Contract(address, groupABI, signer);
try {
const owner = await groupContract.owner();
const service = await groupContract.service();
const mintHandler = await groupContract.mintHandler();
const redemptionHandler = await groupContract.redemptionHandler();
const minimalDeposit = await groupContract.minimalDeposit();
const feeCollection = await groupContract.feeCollection();
document.getElementById("owner").innerText = owner;
document.getElementById("service").innerText = service;
document.getElementById("mintHandler").innerText = mintHandler;
document.getElementById("redemptionHandler").innerText = redemptionHandler;
document.getElementById("minimalDeposit").innerText = minimalDeposit.toString();
document.getElementById("feeCollection").innerText = feeCollection;
document.getElementById("groupStatus").innerText = "Group loaded successfully.";
// Reveal the group details, add-member, remove-member, and update sections.
document.getElementById("groupDetails").style.display = "block";
document.getElementById("memberSection").style.display = "block";
document.getElementById("removeSection").style.display = "block";
document.getElementById("updateSection").style.display = "block";
} catch (error) {
console.error(error);
document.getElementById("groupStatus").innerText = "Error loading group details: " + error.message;
}
}
// Generic function to update a field on the group contract.
async function updateField(fieldName, newValue) {
if (!groupContract) return;
let tx;
try {
switch (fieldName) {
case "service":
tx = await groupContract.setService(newValue);
break;
case "mintHandler":
tx = await groupContract.setMintHandler(newValue);
break;
case "redemptionHandler":
tx = await groupContract.setRedemptionHandler(newValue);
break;
case "minimalDeposit":
tx = await groupContract.setMinimalDeposit(newValue);
break;
case "feeCollection":
tx = await groupContract.setFeeCollection(newValue);
break;
default:
throw new Error("Unknown field");
}
document.getElementById("updateStatus").innerText = `Transaction submitted: ${tx.hash}\nWaiting for confirmation...`;
await tx.wait();
document.getElementById("updateStatus").innerText = `Update successful! Tx hash: ${tx.hash}`;
loadGroupDetails(); // Refresh displayed details.
} catch (error) {
console.error(error);
document.getElementById("updateStatus").innerText = "Error: " + error.message;
}
}
// Function to add one or more members using trustBatch with a non-zero expiry.
async function addMembers() {
if (!groupContract) return;
let inputStr = document.getElementById("members").value.trim();
if (!inputStr) {
alert("Please enter one or more member addresses.");
return;
}
// Split the input by commas and filter for valid addresses.
let addresses = inputStr.split(",")
.map(addr => addr.trim())
.filter(addr => ethers.utils.isAddress(addr));
if (addresses.length === 0) {
document.getElementById("memberStatus").innerText = "No valid member addresses found.";
return;
}
// Get expiry; if missing or invalid, use a default far-future value.
let expiryInput = document.getElementById("expiry").value.trim();
let expiry = (!expiryInput || isNaN(expiryInput)) ? 9999999999 : Number(expiryInput);
try {
const tx = await groupContract.trustBatch(addresses, expiry);
document.getElementById("memberStatus").innerText = `Transaction submitted: ${tx.hash}\nWaiting for confirmation...`;
await tx.wait();
document.getElementById("memberStatus").innerText = `Members added successfully! Tx hash: ${tx.hash}`;
} catch (error) {
console.error(error);
document.getElementById("memberStatus").innerText = "Error: " + error.message;
}
}
// Function to remove trust for one or more members using trustBatch with expiry set to 0.
async function removeMembers() {
if (!groupContract) return;
let inputStr = document.getElementById("removeMembers").value.trim();
if (!inputStr) {
alert("Please enter one or more member addresses to remove trust.");
return;
}
// Split the input by commas and filter for valid addresses.
let addresses = inputStr.split(",")
.map(addr => addr.trim())
.filter(addr => ethers.utils.isAddress(addr));
if (addresses.length === 0) {
document.getElementById("removeStatus").innerText = "No valid member addresses found.";
return;
}
try {
// Set expiry to 0 to remove trust.
const tx = await groupContract.trustBatch(addresses, 0);
document.getElementById("removeStatus").innerText = `Transaction submitted: ${tx.hash}\nWaiting for confirmation...`;
await tx.wait();
document.getElementById("removeStatus").innerText = `Trust removed successfully for provided addresses! Tx hash: ${tx.hash}`;
} catch (error) {
console.error(error);
document.getElementById("removeStatus").innerText = "Error: " + error.message;
}
}
// Set up event listeners once the DOM is loaded.
window.addEventListener("DOMContentLoaded", async () => {
await init();
// Load group and refresh details.
document.getElementById("loadGroup").addEventListener("click", loadGroupDetails);
document.getElementById("refreshGroup").addEventListener("click", loadGroupDetails);
// Update group settings event listeners.
document.getElementById("setService").addEventListener("click", () => {
const newService = document.getElementById("newService").value.trim();
if (ethers.utils.isAddress(newService)) {
updateField("service", newService);
} else {
alert("Invalid service address.");
}
});
document.getElementById("setMintHandler").addEventListener("click", () => {
const newMintHandler = document.getElementById("newMintHandler").value.trim();
if (ethers.utils.isAddress(newMintHandler)) {
updateField("mintHandler", newMintHandler);
} else {
alert("Invalid mint handler address.");
}
});
document.getElementById("setRedemptionHandler").addEventListener("click", () => {
const newRedemptionHandler = document.getElementById("newRedemptionHandler").value.trim();
if (ethers.utils.isAddress(newRedemptionHandler)) {
updateField("redemptionHandler", newRedemptionHandler);
} else {
alert("Invalid redemption handler address.");
}
});
document.getElementById("setMinimalDeposit").addEventListener("click", () => {
const newMinimalDeposit = document.getElementById("newMinimalDeposit").value.trim();
if (newMinimalDeposit && !isNaN(newMinimalDeposit)) {
updateField("minimalDeposit", newMinimalDeposit);
} else {
alert("Invalid minimal deposit value.");
}
});
document.getElementById("setFeeCollection").addEventListener("click", () => {
const newFeeCollection = document.getElementById("newFeeCollection").value.trim();
if (ethers.utils.isAddress(newFeeCollection)) {
updateField("feeCollection", newFeeCollection);
} else {
alert("Invalid fee collection address.");
}
});
// Add member(s) and remove trust event listeners.
document.getElementById("addMembersBtn").addEventListener("click", addMembers);
document.getElementById("removeMembersBtn").addEventListener("click", removeMembers);
});
</script>
</body>
</html>