You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
trouble in connecting react to ganache
this is the code a button is used to call the functions connectWalletHandler() and getCurrentVal():
//code
let contractAddress = '0xafa1f7A195F176c0f0BEd30b2c4D0441F3C417C0';
const [connButtonText, setConnButtonText] = useState('Connect Wallet');
const [defaultAccount, setDefaultAccount] = useState(null);
const [provider, setProvider] = useState(null);
const [signer, setSigner] = useState(null);
const [contract, setContract] = useState(null);
const connectWalletHandler = (e) => {
e.preventDefault()
if (window.ethereum) {
window.ethereum.request({ method: 'eth_requestAccounts'})
.then(result => {
accountChangedHandler(result[0]);
setConnButtonText('Wallet Connected');
})
.catch(error => {
});
} else {
console.log('Need to install MetaMask');
}
}
const accountChangedHandler = (newAccount) => {
setDefaultAccount(newAccount);
updateEthers();
}
const updateEthers = async () => {
console.log('getting provider');
let tempProvider = new ethers.BrowserProvider(window.ethereum);
setProvider(tempProvider);
console.log('getting signer');
let tempSigner = await tempProvider.getSigner();
setSigner(tempSigner);
console.log('getting contract');
let tempContract = new ethers.Contract(contractAddress, case_abi, tempSigner);
setContract(tempContract);
}
const getCurrentVal = async (e) => {
e.preventDefault()
let val = await contract.checkAccess_p('0x1696f5D2E78c7B0ecD51d7060fB249F98C4320B8');
console.log(val);
}
Beta Was this translation helpful? Give feedback.
All reactions