thanks Carlos but the web3 stuff is just not working at all for me. Now is ethereum.window.
I installed web3 package like @Bhujanga suggested and tried the code but the local host rejects web3, anything with web3.js
I followed a tutorial from Metamask and combined it from a youTube guy and It works but I can only get my account to show but not balance since some of the code is old but I cannot get it read the abi so I can start creating the crypto cats
https://docs.metamask.io/guide/create-dapp.html#project-setup
this is the tutorial but I still need to access the abi
// 1 Basic Action(Part 1)
const forwarderOrigin = 'http://localhost:7545';
const initialize = () => {
//You will start here
//const { ethers } = require("ethers");
//import { ethers } from "ethers";
const onboardButton = document.getElementById('connectButton');
const getAccountsButton = document.getElementById('getAccounts');
const getAccountsResult = document.getElementById('getAccountsResult');
const showBalance = document.getElementById('showbalance')
//Created check function to see if the MetaMask extension is installed
const isMetaMaskInstalled = () => {
//Have to check the ethereum binding on the window object to see if it's installed
const { ethereum } = window;
return Boolean(ethereum && ethereum.isMetaMask);
};
//We create a new MetaMask onboarding object to use in our app
//import MetaMaskOnboarding from '@metamask/onboarding';
/*
import MetaMaskOnboarding from '@metamask/onboarding';
const onboarding = new MetaMaskOnboarding({ forwarderOrigin });
//This will start the onboarding proccess
const onClickInstall = () => {
onboardButton.innerText = 'Onboarding in progress';
onboardButton.disabled = true;
//On this object we have startOnboarding which will start the onboarding process for our end user
onboarding.startOnboarding();
};
*/
const onClickConnect = async () => {
try {
// Will open the MetaMask UI
// You should disable this button while the request is pending!
await ethereum.request({ method: 'eth_requestAccounts' });
} catch (error) {
console.error(error);
}
};
//------Inserted Code------\\
const MetaMaskClientCheck = () => {
//Now we check to see if Metmask is installed
if (!isMetaMaskInstalled()) {
//If it isn't installed we ask the user to click to install it
onboardButton.innerText = 'Click here to install MetaMask!';
//When the button is clicked we call this function
onboardButton.onclick = onClickInstall;
//The button is now disabled
onboardButton.disabled = false;
} else {
//If it is installed we change our button text
onboardButton.innerText = 'Connect';
//When the button is clicked we call this function to connect the users MetaMask Wallet
onboardButton.onclick = onClickConnect;
//The button is now disabled
onboardButton.disabled = false;
}
};
MetaMaskClientCheck();
//------/Inserted Code------\\
// last step, grab data
//Eth_Accounts-getAccountsButton
getAccountsButton.addEventListener('click', async () => {
//we use eth_accounts because it returns a list of addresses owned by us.
const accounts = await ethereum.request({ method: 'eth_accounts' });
//We take the first address in the array of addresses and display it
getAccountsResult.innerHTML = accounts[0] || 'Not able to get accounts';
const balance = await ethereum.request({ method: 'eth_getBalance', params: [accounts, 'latest']});
const read = parseInt[balance] / 10**18; // ten to the 18th power
//console.log((read.toFixed(5)));
showBalance.innerHTML = read.toFixed(5) || 'Not able to get accounts'
});
};
window.addEventListener('DOMContentLoaded', initialize);