Created
May 25, 2023 06:01
-
-
Save Srutip04/3d6573bd079afc65775c48507722e361 to your computer and use it in GitHub Desktop.
Web3 Boilerplate
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
import React, { useState, useEffect } from "react"; | |
import getWeb3 from "./getWeb3"; | |
import SimpleStorage from "./contracts/SimpleStorage.json"; | |
const App = () =>{ | |
const [state, setState] = useState({ | |
web3: null, | |
contract: null, | |
}); | |
const [data, setData] = useState("nil"); | |
const [address, setAddress] = useState(null); | |
useEffect(() => { | |
const init = async () => { | |
try { | |
const web3 = await getWeb3(); | |
const networkId = await web3.eth.net.getId(); | |
const deployedNetwork = SimpleStorage.networks[networkId]; | |
console.log("Contract Address:", deployedNetwork.address); | |
const instance = new web3.eth.Contract( | |
SimpleStorage.abi, | |
deployedNetwork && deployedNetwork.address | |
); | |
setAddress(deployedNetwork.address) | |
setState({ web3, contract: instance }); | |
} catch (error) { | |
alert("Falied to load web3 or contract."); | |
console.log(error); | |
} | |
}; | |
init(); | |
}, []); | |
useEffect(() => { | |
const { contract } = state; | |
async function readData() { | |
const data = await contract.methods.getter().call(); | |
setData(data); | |
} | |
contract && readData(); | |
}, [state]); | |
async function writeData() { | |
const { contract } = state; | |
const data = document.querySelector("#value").value; | |
await contract.methods | |
.setter(data) | |
.send({ from: account }); | |
window.location.reload(); | |
} | |
} |
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
import Web3 from "web3"; | |
const getWeb3 = () => | |
new Promise((resolve, reject) => { | |
// Wait for loading completion to avoid race conditions with web3 injection timing. | |
window.addEventListener("load", async () => { | |
// Modern dapp browsers... | |
if (window.ethereum) { | |
const web3 = new Web3(window.ethereum); | |
try { | |
// Request account access if needed | |
await window.ethereum.enable(); | |
// Accounts now exposed | |
resolve(web3); | |
} catch (error) { | |
reject(error); | |
} | |
} | |
// Legacy dapp browsers... | |
else if (window.web3) { | |
// Use Mist/MetaMask's provider. | |
const web3 = window.web3; | |
console.log("Injected web3 detected."); | |
resolve(web3); | |
} | |
// Fallback to localhost; use dev console port by default... | |
else { | |
const provider = new Web3.providers.HttpProvider( | |
"http://127.0.0.1:8545" | |
); | |
const web3 = new Web3(provider); | |
console.log("No web3 instance injected, using Local web3."); | |
resolve(web3); | |
} | |
}); | |
}); | |
export default getWeb3; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment