[SOLVED] Calling functions on BSC deployed contract

does it work now for you?

it did not show the total supply in console

and one more question i did not use web3 api for showing data … soo why i used

   await Moralis.Web3API.native.runContractFunction

if you run the code from here on console: [SOLVED] Calling functions on BSC deployed contract
it should work

web3 instance is needed for Moralis.executeFunction

Moralis.Web3API.native.runContractFunction doesn’t use a web3 instance (like a connection to MetaMask), but both will require to have your server url and app id configured

did you see any errors on console, did that function get executed?

i did not get any error … only i get in console is my connected address ,not get totalsupply …this is my code

                <script>
// connect to Moralis server
Moralis.initialize("pfaPMZpSl0COD4qDiZ6ytcu2ePIMaPWI2pSrhTZm");
Moralis.serverURL = "https://4l81fhpvqkit.usemoralis.com:2053/server";

// Login with metamask and get the ethAddress

login = async () => {
  Moralis.Web3.authenticate().then(function (user) {
    console.log(user.get('ethAddress'))
  })
}
async function cptsupply() {
  const web3 = await moralis.enable();
  ERC20_ABI = [
{
  constant: true,
  inputs: [],
  name: "totalSupply",
  outputs: [{ name: "", type: "uint256" }],
  payable: false,
  stateMutability: "view",
  type: "function",
},

];

x = await Moralis.Web3API.native.runContractFunction({
chain: “bsc”,
address: “0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078”,
function_name: “totalSupply”,
abi: ERC20_ABI,

})
console.log(x);

}

I executed now this code in console and it works fine:

  ERC20_ABI = [
{
  constant: true,
  inputs: [],
  name: "totalSupply",
  outputs: [{ name: "", type: "uint256" }],
  payable: false,
  stateMutability: "view",
  type: "function",
},
];

x = await Moralis.Web3API.native.runContractFunction({
chain: "bsc",
address: "0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078",
function_name: "totalSupply",
abi: ERC20_ABI,

})
console.log(x);

=> 10000000000000000000000

where do you call that cptsupply function?

1 Like

await is only valid with async function x = await Moralis.Web3API.native.runContractFunction

for this line i have to make a function cptsupply , can u give me your whole code which shows totalsupply in console …
this is my code

                                      <script>
// connect to Moralis server
Moralis.initialize("pfaPMZpSl0COD4qDiZ6ytcu2ePIMaPWI2pSrhTZm");
Moralis.serverURL = "https://4l81fhpvqkit.usemoralis.com:2053/server";

// Login with metamask and get the ethAddress

login = async () => {
  Moralis.Web3.authenticate().then(function (user) {
    console.log(user.get('ethAddress'))
  })
}
async function cptsupply() {

const web3 = await moralis.enable();
ERC20_ABI = [
{
constant: true,
inputs: [],
name: “totalSupply”,
outputs: [{ name: “”, type: “uint256” }],
payable: false,
stateMutability: “view”,
type: “function”,
},
];

x= await Moralis.Web3API.native.runContractFunction({
chain: “bsc”,
address: “0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078”,
function_name: “totalSupply”,
abi: ERC20_ABI,

})
cptsupply();
console.log(x);
}

try to paste the code from here in your browser console: [SOLVED] Calling functions on BSC deployed contract

it looks like you are trying to call same function recursively there, that is what you want to do?

so for printing the totalsupply in my console
i do console.log(x);
but it is not giving total supply …
and uhh say dat i did not call the function cptsupply so i call it …
and if i dont make async cptsupply function then
x= await Moralis.Web3API.native.runContractFunction
shows error in console without async await did not work …

and i also run the code u give in browsers console

you have to use Moralis.start now, you have an example here: https://github.com/MoralisWeb3/demo-apps/tree/main/moralis-vanilla-boilerplate

can u give pics of ur console

this dont works even my metamask sign in not happening,

i use this before

can u edit my given code and tell me wheres the error

and very thanks sir for helping

in this code what i have to do to print totalsuplly in console…

you copy the html and js files from that github link, and then it should work
after that you can open the browser console and paste the above code

html file

Vanilla Boilerplate Moralis Metamask Login Logout </html

js file

async function login() {

let user = Moralis.User.current();

if (!user) {

  user = await Moralis.authenticate({ signingMessage: "Hello World!" })

    .then(function (user) {

      console.log("logged in user:", user);

      console.log(user.get("ethAddress"));

    })

    .catch(function (error) {

      console(error);

    });

 try {

    user = await Moralis.authenticate({ signingMessage: "Hello World!" })

    console.log(user)

    console.log(user.get('ethAddress'))

 } catch(error) {

   console.log(error)

 }

}

}

i open signup into the metamask there on bscmainnet , my address shows in console , then i copy ur code and paste there , as u say but i get error , and the code which is on github is not working it dont oipen the metamask… my code

      ![WhatsApp Image 2021-11-18 at 17.30.11|299x500](upload://iyJvvIMEkGLHiQ7MbPcW6VkTc10.jpeg) 

// connect to Moralis server

Moralis.initialize("pfaPMZpSl0COD4qDiZ6ytcu2ePIMaPWI2pSrhTZm");

Moralis.serverURL = "https://4l81fhpvqkit.usemoralis.com:2053/server";

// Login with metamask and get the ethAddress

login = async () => {

  Moralis.Web3.authenticate().then(function (user) {

    console.log(user.get('ethAddress'))

  })

}

Did you start a http server?

moralis server is https
and the local host is http from
vscode live server