0

Hi i have came out my code but i am not sure i am anywhere near

what i am trying to achieve is

  1. Getting the balance of token of Token eg(BNB)
  2. Sending token after connected to metamask to another wallet for purchase eg. To another wallet address
  3. Which i have this busd token contract which i always get 0 where i have 5 busd in my metamask

any advise would be appreciated!

<html>
   <head>
      <title>Connect to crypto wallet</title>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/web3/1.7.4-rc.1/web3.min.js"></script>
 </head>
<body>
<script>
/* To connect using MetaMask */
document.addEventListener("DOMContentLoaded", function(){
    if (window.ethereum) {
        connect();
    } else {

        //show connect button
    }

});


async function connect() {
  if (window.ethereum) {

     await window.ethereum.request({ method: "eth_requestAccounts" });

     // var Web3 = require('web3');
     const web3 = new Web3('https://bsc-dataseed1.binance.org:443');

     //window.web3 = new Web3(window.ethereum);
     const account = web3.eth.accounts;

     //Get the current MetaMask selected/active wallet
     const walletAddress = account.givenProvider.selectedAddress;
     
     console.log(`Wallet: ${walletAddress}`);
     //alert(walletAddress);

     const bnbToken = "0xB8c77482e45F1F44dE1745F52C74426C631bDD52";
     const ibgTokenContract = "0x5c46c55A699A6359E451B2c99344138420c87261";
     const busdTokenContract = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56";

     web3.eth.getBalance(busdTokenContract).then((value) =>{
        console.log('Busd '+value);   
     });


  } else {
   console.log("No wallet");
  }
}
</script>
<input type="button" value="Connect Wallet" onclick="connect();">
</body>
</html>

1 Answer 1

0
 web3.eth.getBalance(busdTokenContract) 

Here you're trying to get the BNB balance of address 0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56. If you want the BUSD balance of another address, you need to instantiate the BUSD contract somewhere in your code, then call its balanceOf() function. Reading https://web3js.readthedocs.io/en/v1.7.4/ might help.

1
  • what i mean is i need to get my balance instead of the token balance of (busd) Jul 27 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.