Created
July 15, 2022 10:50
-
-
Save DevloperHS/f3b6f13f838f05e2feec4e3f31ef8241 to your computer and use it in GitHub Desktop.
UI file for interacting with smart contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<!--Adding Meta Data For Browser--> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="stylesheet" href="main.css"> | |
<title>Mood Dairy</title> | |
</head> | |
<body> | |
<div> | |
<h1>Mood Dairy</h1> | |
<p>Here you can SET or GET mood:</p> | |
<!--Create input form to enter mood--> | |
<label for="mood">Input Your Mood:</label> <br /> | |
<input type="text" id="mood" /> | |
<!--Adding buttons to set and get mood--> | |
<button onclick="getMood()">Get Mood</button> | |
<button onclick="setMood()">Set Mood</button> | |
</div> | |
<!--Adding functionality to interact with smart contract - MoodDairy--> | |
<!--Importing ether.js library--> | |
<script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js" type="application/javascript"></script> | |
</script> | |
<!--Declaring Contract Address, ABI, ETH Account Connect - Request and asynchnous fn --> | |
<script> | |
// Add provider - ropsten | |
const provider = new ethers.providers.Web3Provider( | |
window.ethereum, | |
"ropsten" | |
); | |
//Contract Address & ABI | |
const MoodContractAddress = "0xb74C9881649672333d5418eAf337C9A177eB3F97"; | |
const MoodContractABI = [ | |
{ | |
"inputs": [ | |
{ | |
"internalType": "string", | |
"name": "_mood", | |
"type": "string" | |
} | |
], | |
"name": "setMood", | |
"outputs": [], | |
"stateMutability": "nonpayable", | |
"type": "function" | |
}, | |
{ | |
"inputs": [], | |
"name": "getMood", | |
"outputs": [ | |
{ | |
"internalType": "string", | |
"name": "", | |
"type": "string" | |
} | |
], | |
"stateMutability": "view", | |
"type": "function" | |
} | |
]; | |
//Declare constant | |
let MoodContract; | |
let signer; | |
//Adding wallet popup support | |
provider.send("eth_requestAccounts", []).then(() => { | |
provider.listAccounts().then(function (accounts) { | |
signer = provider.getSigner(accounts[0]); | |
MoodContract = new ethers.Contract( | |
MoodContractAddress, | |
MoodContractABI, | |
signer | |
); | |
}); | |
}); | |
// async fn to get mood | |
async function getMood() { | |
const getMoodPromise = MoodContract.getMood(); | |
const Mood = await getMoodPromise; | |
console.log(Mood); | |
} | |
// async fn to set mood | |
async function setMood() { | |
const mood = document.getElementById("mood").value; | |
const setMoodPromise = MoodContract.setMood(mood); | |
await setMoodPromise; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment