Skip to content

Instantly share code, notes, and snippets.

@jcmartinezdev
Last active January 5, 2023 19:16
Show Gist options
  • Select an option

  • Save jcmartinezdev/45bb7bb39fea940cd45a5e88759f58a5 to your computer and use it in GitHub Desktop.

Select an option

Save jcmartinezdev/45bb7bb39fea940cd45a5e88759f58a5 to your computer and use it in GitHub Desktop.

Revisions

  1. jcmartinezdev renamed this gist Nov 6, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. jcmartinezdev revised this gist Nov 6, 2020. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    <head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>My Website</title>
    <title>Web 3 Demo</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>

    <script src='node_modules/web3/dist/web3.min.js'></script>
    @@ -13,9 +13,9 @@
    <body>

    Web 3 Demo
    <br />
    <br >
    <button onclick="printCoolNumber();">Print Cool Number</button>
    <button onclick="changeCoolNumber();">Print Cool Number</button>
    <button onclick="changeCoolNumber();">Change Cool Number</button>
    <br /><br />
    Status: <span id="status">Loading...</span>

    @@ -55,29 +55,29 @@
    "stateMutability": "nonpayable",
    "type": "function"
    }
    ], '0xa7BbEA124501fb05471cd53Daa6e9c49F06AE8D0');
    }

    async function getCurrentAccount() {
    const accounts = await window.web3.eth.getAccounts();
    return accounts[0];
    ], '0x5F4a8C71AFB0c01BA741106d418E78888607Ee63');
    }

    async function printCoolNumber() {
    updateStatus(`fetching Cool Nunber...`);
    updateStatus('fetching Cool Number...');
    const coolNumber = await window.contract.methods.coolNumber().call();
    updateStatus(`coolNumber: ${coolNumber}`);
    }

    async function getCurrentAccount() {
    const accounts = await window.web3.eth.getAccounts();
    return accounts[0];r
    }

    async function changeCoolNumber() {
    const value = Math.floor(Math.random() * 100);
    updateStatus(`Updating coolNumber with ${value}...`);
    updateStatus(`Updating coolNumber with ${value}`);
    const account = await getCurrentAccount();
    const coolNumber = await window.contract.methods.setCoolNumber(value).send({from: account});
    const coolNumber = await window.contract.methods.setCoolNumber(value).send({ from: account });
    updateStatus('Updated.');
    }

    async function run() {
    async function load() {
    await loadWeb3();
    window.contract = await loadContract();
    updateStatus('Ready!');
    @@ -89,7 +89,7 @@
    console.log(status);
    }

    run();
    load();
    </script>
    </body>

  3. jcmartinezdev created this gist Nov 6, 2020.
    96 changes: 96 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    <!DOCTYPE html>
    <html>

    <head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>My Website</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>

    <script src='node_modules/web3/dist/web3.min.js'></script>
    </head>

    <body>

    Web 3 Demo
    <br />
    <button onclick="printCoolNumber();">Print Cool Number</button>
    <button onclick="changeCoolNumber();">Print Cool Number</button>
    <br /><br />
    Status: <span id="status">Loading...</span>

    <script type="text/javascript">
    async function loadWeb3() {
    if (window.ethereum) {
    window.web3 = new Web3(window.ethereum);
    window.ethereum.enable();
    }
    }

    async function loadContract() {
    return await new window.web3.eth.Contract([
    {
    "inputs": [],
    "name": "coolNumber",
    "outputs": [
    {
    "internalType": "uint256",
    "name": "",
    "type": "uint256"
    }
    ],
    "stateMutability": "view",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "uint256",
    "name": "_coolNumber",
    "type": "uint256"
    }
    ],
    "name": "setCoolNumber",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
    }
    ], '0xa7BbEA124501fb05471cd53Daa6e9c49F06AE8D0');
    }

    async function getCurrentAccount() {
    const accounts = await window.web3.eth.getAccounts();
    return accounts[0];
    }

    async function printCoolNumber() {
    updateStatus(`fetching Cool Nunber...`);
    const coolNumber = await window.contract.methods.coolNumber().call();
    updateStatus(`coolNumber: ${coolNumber}`);
    }

    async function changeCoolNumber() {
    const value = Math.floor(Math.random() * 100);
    updateStatus(`Updating coolNumber with ${value}...`);
    const account = await getCurrentAccount();
    const coolNumber = await window.contract.methods.setCoolNumber(value).send({from: account});
    updateStatus('Updated.');
    }

    async function run() {
    await loadWeb3();
    window.contract = await loadContract();
    updateStatus('Ready!');
    }

    function updateStatus(status) {
    const statusEl = document.getElementById('status');
    statusEl.innerHTML = status;
    console.log(status);
    }

    run();
    </script>
    </body>

    </html>
    9 changes: 9 additions & 0 deletions smart_contract.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    pragma solidity ^0.6.6;

    contract CoolNumberContract {
    uint public coolNumber = 10;

    function setCoolNumber(uint _coolNumber) public {
    coolNumber = _coolNumber;
    }
    }