[SOLVED] Calling functions on BSC deployed contract

Hey I m also going through same problem I call the totalSupply function from my bsc contract for that I have to apply execute or run contract?
And I want to show the total supply in my console

both Moralis.executeFunction and runContractFunction should work
an example:

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

x = await Moralis.Web3API.native.runContractFunction({
    chain: "avalanche",
    address: "0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7",
    function_name: "totalSupply",
    abi: ERC20_ABI,
  })

thats my main js file , i want to read totalsupply from my contract which is on mainnet bsc…
and print it on my console ,

login = async () => {

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

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

  })

}

async function cptsupply() {

  const web3 = await moralis.enable();

  const options = {

    chain: "bsc",

    address: "0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078",

    function_name: "totalSupply",

    abi: window.abi,

  };

  const allowance = await Moralis.runContractFunction(options);

  console.log(allowance);

}

you can look at the above example that I gave, where you replace chain and address and it should work fine

its right , bcz i dont change the abi too

ogin = 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);

}

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