[SOLVED] Display BSC Contract Info

I also want to show the balance of users native token
So

const chainOptions = { chain: ‘bsc’, address: “0xaF88…96eE1” }

In this line which address should be given ?

I think that the address of the wallet for which you want to get the native balance should be there

i put the same code but changing token address and wallet address but balance did not shows in my console …

async function usercptBalance(){

const chainOptions = { chain: 'bsc' ,address:"0x5974c357769D77a04CD87AeE3e49e5bf719d1CFe" }

const balances = await Moralis.Web3API.account.getTokenBalances(chainOptions);

const tokenAddress =  "0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078";

const tokenBalance = balances.find((token) => token.token_address === tokenAddress);

console.log(tokenBalance.balance);

return tokenBalance.balance;

} https://bscscan.com/token/0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078?a=0x88a393f2aff8d8e0dd7bb4add82cd8ebac439b9e
my contract
is there any way to call balanceof function of my contract ?

when i m doing through console browser it shows balance but when i m doing through my code its nothing shows there why it is ? due to moralis.start ?

this returns the balances of ERC20 tokens, but not also the native balance
for native balance you have to use getNativeBalance: https://docs.moralis.io/moralis-server/web3-sdk/account#getnativebalance

if that code works in your browser console, it should work in your main js code too

yes it is working on my console for both my cpt token and bnb balance but it does not working on my code…
what u think i have to do moralis.intiliaze in the starting?

if it works in your browser console, it means that it should work in your code too,
but latest Moralis SDK syntax doesn’t use initialize any more, only Moralis.start

an example: https://github.com/MoralisWeb3/demo-apps/tree/main/moralis-vanilla-boilerplate

i try many times but it dont show on my console

<script>
    /* Moralis init code */
    const serverUrl = "https://4l81fhpvqkit.usemoralis.com:2053/server";
    const appId = "pfaPMZpSl0COD4qDiZ6ytcu2ePIMaPWI2pSrhTZm";
    Moralis.start({ serverUrl, appId });

    async function login() {
      let currentuser = Moralis.User.current();
      if (!currentuser) {
        currentuser = await Moralis.authenticate({ signingMessage: "welcome to crypto planet " })

      }
      console.log("logged in user:", currentuser);
      console.log(currentuser.get('ethAddress'))
    }

    async function logOut() {
      await Moralis.User.logOut();
      console.log("logged out");
    }
    async function userBNB(user) {


      // get BSC native balance for a given address

      const optionsBNB = { chain: "bsc" };

      const balance = await Moralis.Web3API.account.getNativeBalance(optionsBNB);

      console.log(balance)

    }

    async function usercptBalance() {
      const chainOptions = { chain: 'bsc' }
      const balances = await Moralis.Web3API.account.getTokenBalances(chainOptions);
      const tokenAddress = "0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078";
      const tokenBalance = balances.find((token) => token.token_address === tokenAddress);
      console.log(tokenBalance.balance);
      return tokenBalance.balance;
    }

    document.getElementById("btn-login").onclick = login;
    document.getElementById("btn-logout").onclick = logOut;ype or paste code here

did you call this function somewhere?

noo i did not call this function

if you don’t call it, then it is not going to be executed, and if it is not going to be executed, then you will not see anything on console

after the function i add two lines for calling but it is showing error `

 async function usercptBalance() {
      const chainOptions = { chain: 'bsc' }
      const balances = await Moralis.Web3API.account.getTokenBalances(chainOptions);
      const tokenAddress = "0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078";
      const tokenBalance = balances.find((token) => token.token_address === tokenAddress);
      console.log(tokenBalance.balance);
      return tokenBalance.balance;
    }
        
       let a= usercptBalance();
       console.log (a);

`

`

this works only if you are authenticated, if you are not authenticated then you have to specify the address parameter

yeah thats the thing i m asking if i give address it is showing balance but if not giving address it shows error … 404 as u see ina bove images ,

is any way to solve this problem

if you add await Moralis.enableWeb3() before calling that web3api function, it helps?

yesss it is working when i m doing await.moralis.enable.web3 before calling webapi afunction
const balance = await Moralis.Web3API.account.getNativeBalance(optionsBNB);
before this line . but the problem is enable.web3 enables the metamask pop up automatically when i reload the site on browsers and if i accept the sign in request
then its shows balance … but when i m rejecting the request ,and click on login button and sign in the metamask then balance not shown ,only address is showing dat time …

so it is not happening in sequential manner
i m trying to resolve it with if else but if sir u have solution …plzz let me know

hey sir , im calling the function getnumberofdividenttokenholders from my smart contract and i m getting some array not exact number
https://bscscan.com/address/0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078#code

  bsc20_ABI = [
      {
        constant: true,
        inputs: [],
        name: "getNumberOfDividendTokenHolders",
        outputs: [
          {
            internalType: "uint256",
            name: "",
            type: "uint256"
          }
        ],
        stateMutability: "view",
        type: "function"
      },
    ];
    async function divtokenholders() {
      y = await Moralis.Web3API.native.runContractFunction({
        chain: "bsc",
        address: "0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078",
        function_name: "getNumberOfDividendTokenHolders",
        abi: bsc20_ABI,

      })
      console.log(y);
    }


1 Like

It looks like I get a number

but its not giving number sir, can u check it