macbudkowski

Untitled

Sep 27th, 2022
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.96 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.    
  4.     <head>
  5.         <meta charset="UTF-8" />
  6.         <meta http-equiv="X-UA-Compatible" content="IE Edge" />
  7.         <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  8.         <title>My First dApp</title>
  9.         <style>
  10.             body {
  11.               text-align: center;
  12.               font-family: Arial, Helvetica, sans-serif;
  13.             }
  14.          
  15.             div {
  16.               width: 20%;
  17.               margin: 0 auto;
  18.               display: flex;
  19.               flex-direction: column;
  20.             }
  21.          
  22.             button {
  23.               width: 100%;
  24.               margin: 10px 0px 5px 0px;
  25.             }
  26.           </style>
  27.     </head>
  28.     <body>
  29.         <div>
  30.         <h1>This is my first dApp ever!</h1>
  31.         <p>Here we can set or get the mood:</p>
  32.         <label for="mood">Input Mood:</label> <br />
  33.         <input type="text" id="mood" />
  34.         <button onclick="getMood()">Get Mood</button>
  35.         <button onclick="setMood()">Set Mood</button>
  36.       </div>
  37.  
  38.       <script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js" type="application/javascript"></script>
  39.    
  40.       <script>
  41.         const provider = new ethers.providers.Web3Provider(
  42.             window.ethereum,
  43.             "goerli"
  44.             );
  45.  
  46.       const MoodContractAddress = "0x26D9333A425a0CbE3Da57F0A1FC0F713911F60F7";
  47.       const MoodContractABI = [
  48.         {
  49.             "inputs": [
  50.                 {
  51.                     "internalType": "string",
  52.                     "name": "_mood",
  53.                     "type": "string"
  54.                 }
  55.             ],
  56.             "name": "setMood",
  57.             "outputs": [],
  58.             "stateMutability": "nonpayable",
  59.             "type": "function"
  60.         },
  61.         {
  62.             "inputs": [],
  63.             "name": "getMood",
  64.             "outputs": [
  65.                 {
  66.                     "internalType": "string",
  67.                     "name": "",
  68.                     "type": "string"
  69.                 }
  70.             ],
  71.             "stateMutability": "view",
  72.             "type": "function"
  73.         }
  74.     ];
  75.  
  76.       let MoodContract;
  77.       let signer;
  78.      
  79.       provider.send("eth_requestAccounts", []).then(() => {
  80.         provider.listAccounts().then((accounts) => {
  81.             signer = provider.getSigner(accounts[0]);
  82.             MoodContract = new ethers.Contract(
  83.                 MoodContractAddress,
  84.                 MoodContractABI,
  85.                 signer
  86.             );
  87.         });
  88.     });
  89.  
  90.         async function getMood() {
  91.             const getMoodPromise = MoodContract.getmood();
  92.             const Mood = await getMoodPromise;
  93.             console.log(Mood);
  94.         }
  95.  
  96.         async function setMood() {
  97.             const mood = document.getElementById("mood").value;
  98.             const setMoodPromise = MoodContract.setMood(mood);
  99.             await setMoodPromise;
  100.         }
  101.      
  102.     </script>
  103.     </body>
  104. </html>
Advertisement
Add Comment
Please, Sign In to add comment