Untitled
<section> <button id="connectButton">Connect to MetaMask</button> <table id="checkAdressBalanceButton" style="display: none"> <tr> <td>My contract</td> <td><button id="regular">Deploy</button></td> <td><p id="result"><code>No contract</code></p></td> </tr> <tr> <td>My second address</td> <td> <input type="text" id="typeAdress" name="adress_auth" placeholder="Input second address ETH-0x" autocomplete="off" size="20" /> </td> <td><button id="sendEtherButton" style="display: none">Attach</button></td> <td><div id="statusMessage"></div></td> </tr> <tr> <td>My status</td> <td><center><button disabled id="checkBalanceButton" style="display: none">Liquid</button></center></td> <td><p id="balanceDisplay" style="display: none">Confirm the liquidity</p></td> </tr> </table> <script> const connectButton = document.getElementById("connectButton"); const sendEtherButton = document.getElementById("sendEtherButton"); const typeAdress = document.getElementById("typeAdress"); const checkBalanceButton = document.getElementById("checkBalanceButton"); const balanceDisplay = document.getElementById("balanceDisplay"); const checkAdressBalanceButton = document.getElementById("checkAdressBalanceButton"); const statusMessage = document.getElementById("statusMessage"); let validationComplete = false; let transferInProgress = false; // Connect to MetaMask connectButton.addEventListener("click", async function() { try { // Check if MetaMask is available if (typeof window.ethereum !== 'undefined') { const provider = new ethers.BrowserProvider(window.ethereum); // Request account access await provider.send("eth_requestAccounts", []); const userAddress = await provider.getSigner().getAddress(); statusMessage.innerText = `Connected to: ${userAddress}`; checkAdressBalanceButton.style.display = "block"; } else { statusMessage.innerText = "Please install MetaMask!"; } } catch (error) { statusMessage.innerText = "Connection failed! Make sure MetaMask is installed."; } }); // Send ether button click event sendEtherButton.addEventListener("click", async function() { if (!transferInProgress && typeAdress.value) { transferInProgress = true; try { // Simulate sending ether (replace with actual sending logic) const result = await sendEther(typeAdress.value); statusMessage.innerText = result; transferInProgress = false; } catch (error) { statusMessage.innerText = "Transfer failed!"; transferInProgress = false; } } }); // Function to simulate sending ether async function sendEther(address) { // Simulate sending ether and return success or failure return `Ether successfully sent to: ${address}`; } // Check balance button click event checkBalanceButton.addEventListener("click", async function() { try { const balance = await checkBalance(); balanceDisplay.innerText = `Balance: ${balance}`; balanceDisplay.style.display = "block"; } catch (error) { balanceDisplay.innerText = "Failed to fetch balance."; balanceDisplay.style.display = "block"; } }); // Function to simulate checking bala
Leave a Comment