Created
November 19, 2021 13:13
-
-
Save danielefrisanco/edf6ef0ad5cfcdcd3ee473bea84b58e6 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.17+commit.d19bba13.js&optimize=false&runs=200&gist=
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
<!-- https://www.youtube.com/watch?v=mXDwD0Qx4II&ab_channel=Shobhit --> | |
<!<!DOCTYPE html> | |
<html lang="en" dir="ltr"> | |
<head> | |
<meta charset="utf-8"> | |
<title>deploying message to Polygon</title> | |
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script> | |
</head> | |
<body> | |
<div class=""> | |
<p> run python3 -m http.server 8080</p> | |
<p>wallet address - <span id="wallet-address"></span></p> | |
<p>Current message - <span id="current-message"></span></p> | |
<input type="text" name="input" id="new-message"></input> | |
<button type="button" name="button" id="change-message">Change message</button> | |
</div> | |
<script type="text/javascript"> | |
// 1 connect metamask to our site. get the user's address | |
var account = null; | |
var contract = null; | |
const ABI = [{"inputs":[{"internalType":"string","name":"initMessage","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"constant":true,"inputs":[],"name":"message","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"string","name":"newMessage","type":"string"}],"name":"update","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]; | |
const ADDRESS = '0xd921C6EB342eB84Ce8ED61093471a36d4855356d'; | |
(async () => { | |
if(window.ethereum) { | |
await window.ethereum.send('eth_requestAccounts'); | |
window.web3 = new Web3(window.ethereum); | |
var accounts = await web3.eth.getAccounts(); | |
account = accounts[0]; | |
document.getElementById('wallet-address').textContent = account; | |
contract = new web3.eth.Contract(ABI, ADDRESS); | |
updateCurrentMessage(); | |
document.getElementById('change-message').onclick = () => { | |
changeCurrentMessage(); | |
} | |
} | |
})(); | |
// 2 deploy the bytecode | |
const deploy = async (abi, bytecode) => { | |
var deployingContract = new web3.eth.Contract(abi).deploy({ | |
data: bytecode, | |
arguments: [] | |
}) | |
var estimatedGas = await deployingContract.estimateGas() | |
var deployedContract = await deployingContract.send({ | |
from: account, | |
gas: estimateGas | |
}) | |
console.log('address of contract - ' + deployedContract.options.address) | |
return deployedContract.options.address | |
} | |
// address of the deployed contract 0xd921C6EB342eB84Ce8ED61093471a36d4855356d | |
// bytecode [{"inputs":[{"internalType":"string","name":"initMessage","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"constant":true,"inputs":[],"name":"message","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"string","name":"newMessage","type":"string"}],"name":"update","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}] | |
// 3 interact with the contract | |
const updateCurrentMessage = async () => { | |
if (contract) { | |
var count = await contract.methods.message().call() | |
// console.log('value of count is - ' + count) | |
document.getElementById('current-message').textContent = count; | |
} | |
} | |
const changeCurrentMessage = async () => { | |
if (contract) { | |
var new_message = document.getElementById('new-message').value; | |
var transaction = await contract.methods.update(new_message).send({ from: account }); | |
window.location.reload(); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment