//---------------------------connect wallet------------------------------\
window.userWalletAddress = null
const loginButton = document.getElementById('connectWallet')
const userWallet = document.getElementById('userWallet')
function toggleButton() {
if (!window.ethereum) {
loginButton.innerText = 'MetaMask is not installed'
return false
}
loginButton.addEventListener('click', loginWithMetaMask)
}
async function loginWithMetaMask() {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' })
.catch((e) => {
console.error(e.message)
return
})
if (!accounts) { return }
window.userWalletAddress = accounts[0]
userWallet.innerText = window.userWalletAddress
loginButton.innerText = 'Sign out of MetaMask'
loginButton.removeEventListener('click', loginWithMetaMask)
setTimeout(() => {
loginButton.addEventListener('click', signOutOfMetaMask)
}, 200)
}
function signOutOfMetaMask() {
window.userWalletAddress = null
userWallet.innerText = ''
loginButton.innerText = 'Sign in with MetaMask'
loginButton.removeEventListener('click', signOutOfMetaMask)
setTimeout(() => {
loginButton.addEventListener('click', loginWithMetaMask)
}, 200)
}
window.addEventListener('DOMContentLoaded', () => {
toggleButton()
});
// ---------------------------------------------------------\
const testnet = ' https://data-seed-prebsc-1-s1.binance.org:8545/';
const walletAddress = document.getElementById('userWallet').innerText;
const web3 = new Web3(new Web3.providers.HttpProvider(testnet));
var balance = web3.eth.getBalance(walletAddress); //Will give value in.
balance = web3.toDecimal(balance);
ok so i’m trying to get the users current smart chain balance to show in the console for now but when i try to grab the data of the wallet address
(which shows in an element with an ID of userWallet after they connect)
but when i try to grab the elements inner text it shows as undefined in the terminal
but if i put an actual address without trying to grab but rather as a string it works correctly.
I honestly think i should get better at javascript before diving into this web3 stuff but i’m stubborn and want this to work lol.
also if anyone wants to help with a little project of mine let me know and ill see how i can contact you (i don’t know if its against the rules to post my telegram here yet)