Skip to content

Instantly share code, notes, and snippets.

@Violet-Bora-Lee
Created March 30, 2023 08:05
Show Gist options
  • Save Violet-Bora-Lee/a92f4f212d6bfa98b9f9dc5a012fcd24 to your computer and use it in GitHub Desktop.
Save Violet-Bora-Lee/a92f4f212d6bfa98b9f9dc5a012fcd24 to your computer and use it in GitHub Desktop.
LW3DAO-Freshmen-1
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My First dapp</title>
<script
src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js"
type="application/javascript"
></script>
<script>
const MoodContractAddress = '0x2BF470ba845Af3f3a8DfDD9524427e7eAC789798'; // 자기가 배포한 컨트랙트 주소(이더스캔에서 확인)
const MoodContractABI = [
{
inputs: [],
name: 'getMood',
outputs: [
{
internalType: 'string',
name: '',
type: 'string',
},
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
internalType: 'string',
name: '_mood',
type: 'string',
},
],
name: 'setMood',
outputs: [],
stateMutability: 'nonpayable',
type: 'function',
},
];
const provider = new ethers.providers.Web3Provider(window.ethereum, 'goerli');
let signer;
let MoodContract;
provider.send('eth_requestAccounts', []).then((accounts) => {
// console.log("accounts--->", accounts);
provider.listAccounts().then((accounts) => {
signer = provider.getSigner(accounts[0]);
MoodContract = new ethers.Contract(MoodContractAddress, MoodContractABI, signer);
});
});
async function getMood() {
const getMoodPromise = MoodContract.getMood();
const Mood = await getMoodPromise;
document.getElementById("showMood").innerText = `Your mood: ${Mood}`;
console.log(Mood);
}
async function setMood() {
const mood = document.getElementById("mood").value;
const setMoodPromise = MoodContract.setMood(mood);
await setMoodPromise;
}
</script>
</head>
<body>
<div>
<h1>This is my dApp!</h1>
<p>Here we can set or get the mood:</p>
<label for="mood">Input Mood:</label><br />
<input type="text" id="mood" /><br />
<button onclick="getMood()">Get Mood</button><br />
<button onclick="setMood()">Set Mood</button>
<p id="showMood"></p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment