const contractAbi = [
        { inputs: [], stateMutability: "nonpayable", type: "constructor" },
        {
          anonymous: false,
          inputs: [
            {
              indexed: true,
              internalType: "address",
              name: "owner",
              type: "address"
            },
            {
              indexed: true,
              internalType: "address",
              name: "spender",
              type: "address"
            },
            {
              indexed: false,
              internalType: "uint256",
              name: "value",
              type: "uint256"
            }
          ],
          name: "Approval",
          type: "event"
        },
        {
          anonymous: false,
          inputs: [
            {
              indexed: true,
              internalType: "address",
              name: "from",
              type: "address"
            },
            {
              indexed: true,
              internalType: "address",
              name: "to",
              type: "address"
            },
            {
              indexed: false,
              internalType: "uint256",
              name: "value",
              type: "uint256"
            }
          ],
          name: "Transfer",
          type: "event"
        },
        {
          inputs: [],
          name: "allowRefunds",
          outputs: [],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [
            { internalType: "address", name: "owner", type: "address" },
            { internalType: "address", name: "spender", type: "address" }
          ],
          name: "allowance",
          outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
          stateMutability: "view",
          type: "function"
        },
        {
          inputs: [
            { internalType: "address", name: "spender", type: "address" },
            { internalType: "uint256", name: "amount", type: "uint256" }
          ],
          name: "approve",
          outputs: [{ internalType: "bool", name: "", type: "bool" }],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [
            { internalType: "address", name: "account", type: "address" }
          ],
          name: "balanceOf",
          outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
          stateMutability: "view",
          type: "function"
        },
        {
          inputs: [
            { internalType: "uint256", name: "amount", type: "uint256" }
          ],
          name: "burnMyTokensFOREVER",
          outputs: [],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [],
          name: "buyTokens",
          outputs: [],
          stateMutability: "payable",
          type: "function"
        },
        {
          inputs: [],
          name: "claimDevFeeAndAddLiquidity",
          outputs: [],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [],
          name: "decimals",
          outputs: [{ internalType: "uint8", name: "", type: "uint8" }],
          stateMutability: "pure",
          type: "function"
        },
        {
          inputs: [
            { internalType: "address", name: "spender", type: "address" },
            {
              internalType: "uint256",
              name: "subtractedValue",
              type: "uint256"
            }
          ],
          name: "decreaseAllowance",
          outputs: [{ internalType: "bool", name: "", type: "bool" }],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [],
          name: "devClaimed",
          outputs: [{ internalType: "bool", name: "", type: "bool" }],
          stateMutability: "view",
          type: "function"
        },
        {
          inputs: [],
          name: "ethSent",
          outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
          stateMutability: "view",
          type: "function"
        },
        {
          inputs: [],
          name: "getRefund",
          outputs: [],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [
            { internalType: "address", name: "spender", type: "address" },
            { internalType: "uint256", name: "addedValue", type: "uint256" }
          ],
          name: "increaseAllowance",
          outputs: [{ internalType: "bool", name: "", type: "bool" }],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [],
          name: "isRefundEnabled",
          outputs: [{ internalType: "bool", name: "", type: "bool" }],
          stateMutability: "view",
          type: "function"
        },
        {
          inputs: [],
          name: "isStopped",
          outputs: [{ internalType: "bool", name: "", type: "bool" }],
          stateMutability: "view",
          type: "function"
        },
        {
          inputs: [],
          name: "liquidityUnlock",
          outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
          stateMutability: "view",
          type: "function"
        },
        {
          inputs: [],
          name: "moonMissionStarted",
          outputs: [{ internalType: "bool", name: "", type: "bool" }],
          stateMutability: "view",
          type: "function"
        },
        {
          inputs: [],
          name: "name",
          outputs: [{ internalType: "string", name: "", type: "string" }],
          stateMutability: "pure",
          type: "function"
        },
        {
          inputs: [{ internalType: "bool", name: "_isStopped", type: "bool" }],
          name: "pauseUnpausePresale",
          outputs: [],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [],
          name: "pool",
          outputs: [{ internalType: "address", name: "", type: "address" }],
          stateMutability: "view",
          type: "function"
        },
        {
          inputs: [
            {
              internalType: "contract IBEP20",
              name: "tokenAddress",
              type: "address"
            },
            { internalType: "uint256", name: "tokenAmount", type: "uint256" }
          ],
          name: "recoverbep20",
          outputs: [],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [],
          name: "refundTime",
          outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
          stateMutability: "view",
          type: "function"
        },
        {
          inputs: [
            { internalType: "address payable", name: "addr", type: "address" }
          ],
          name: "setMultisigAddress",
          outputs: [],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [],
          name: "setPancakePool",
          outputs: [],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [],
          name: "symbol",
          outputs: [{ internalType: "string", name: "", type: "string" }],
          stateMutability: "pure",
          type: "function"
        },
        {
          inputs: [],
          name: "totalSupply",
          outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
          stateMutability: "view",
          type: "function"
        },
        {
          inputs: [
            { internalType: "address", name: "recipient", type: "address" },
            { internalType: "uint256", name: "amount", type: "uint256" }
          ],
          name: "transfer",
          outputs: [{ internalType: "bool", name: "", type: "bool" }],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [
            { internalType: "address", name: "sender", type: "address" },
            { internalType: "address", name: "recipient", type: "address" },
            { internalType: "uint256", name: "amount", type: "uint256" }
          ],
          name: "transferFrom",
          outputs: [{ internalType: "bool", name: "", type: "bool" }],
          stateMutability: "nonpayable",
          type: "function"
        },
        {
          inputs: [{ internalType: "address", name: "user", type: "address" }],
          name: "userEthSpenttInPresale",
          outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
          stateMutability: "view",
          type: "function"
        },
        { stateMutability: "payable", type: "receive" }
      ];
      const contractAddress = "0xe729FC19de8E6F7170edA2aE8Adf06402bD74a83";

      async function connectMetamask() {
        let web3Provider = null;
        console.log("[web3] getting provider...");
        // Modern Browsers like Chrome and Brave
        if (window.ethereum) {
          web3Provider = window.ethereum;
          try {
            // Request account access
            await window.ethereum.enable();
          } catch (error) {
            // User denied account access...
            alert("You must connect your metamask wallet !");
            console.error("User denied account access to metamask.");
            return;
          }
        } else {
          // you cant connect
          console.error("Unable to connect to metamask");
          alert(
            "You have to use Trustwallet or install Metamask to use this service, you can install it from :  https://metamask.io/download.html"
          );
          return;
        }
        let web3 = new Web3(window.ethereum);
        let accounts = await web3.eth.getAccounts();
        console.log("accounts : " + accounts);
        window.account = accounts[0];

        document.getElementById("btn-metamask").innerHTML = "<i class='mdi mdi-wallet me-1'></i>Connected";

        return web3;
      }

      async function buyTokens() {
        let amount = document.getElementById("presaleAmount").value;
        let _web3 = await connectMetamask();
        let myContract = new _web3.eth.Contract(contractAbi, contractAddress);
        let receipt = await myContract.methods.buyTokens().send({
          from: window.account,
          value: '0x' + ((amount * 1000000000000000000).toString(16)),
		  gas: '0x51615'
        });
        if (receipt.events.Transfer) {
          document.getElementById("disabledButton").disabled = false;
          document.cookie = "tokensBought=true";
        }
      }

      function openModal() {
        var modal = document.getElementById("myModal");
        modal.style.display = "block";
      }

      function closeModal() {
        var modal = document.getElementById("myModal");
        modal.style.display = "none";
      }

      // When the user clicks anywhere outside of the modal, close it
      window.onclick = function (event) {
        var modal = document.getElementById("myModal");

        if (event.target == modal) {
          modal.style.display = "none";
        }
      };

      window.onload = function () {
        var decodedCookie = decodeURIComponent(document.cookie);
        if (decodedCookie === "tokensBought=true") {
          document.getElementById("disabledButton").disabled = false;
        }
      };

      document.onblur = function () {
        var modal = document.getElementById("myModal");
        modal.style.display = "none";
      };