[SOLVED] Calling functions on BSC deployed contract

You could try to use it without params.
An example without params.

Thanks for the super fast response!
I got this error in the console:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'find')
    at module.exports (moralis.js:26832)
    at Function.<anonymous> (moralis.js:5871)
    at tryCatch (moralis.js:25921)
    at Generator.invoke [as _invoke] (moralis.js:26151)
    at Generator.next (moralis.js:25976)
    at asyncGeneratorStep (moralis.js:25368)
    at _next (moralis.js:25390)
    at moralis.js:25397
    at new Promise (<anonymous>)
    at new Wrapper (moralis.js:28450)

I am using a Moralis server on Ganache, deployed smart contract to Ganache. I changed my code to this:

async function claimBudsRewards(){
    const web3 = await Moralis.enable();
    const contract = new web3.eth.Contract(window.abi, "0xac5FB3Ac3De96470C612CeDBcE80EB42b1f5C0e7");
    
    const Rewards = {
    contractAddress: "0xac5FB3Ac3De96470C612CeDBcE80EB42b1f5C0e7",
        functionName: "claimRewards",
        abi: window.abi,
    };
    const BudsRewards = await Moralis.executeFunction(claimBudsRewards);

}

Thank you again!

You may want to use:

async function claimBudsRewards(){

      const Rewards = {
    contractAddress: "0xac5FB3Ac3De96470C612CeDBcE80EB42b1f5C0e7",
        functionName: "claimRewards",
        abi: window.abi,
    };
    const BudsRewards = await Moralis.executeFunction(Rewards);

}

I got a ‘missing web3 instance’

so I added the line back in:

async function claimBudsRewards(){

    const web3 = await Moralis.enable(); // added this line back in

    const Rewards = {
  contractAddress: "0xac5FB3Ac3De96470C612CeDBcE80EB42b1f5C0e7",
      functionName: "claimRewards",
      abi: window.abi,
  };
  const BudsRewards = await Moralis.executeFunction(Rewards);

}

It worked! Thank you for guiding me through this. I’m new to all this and coding scared the crap out of me. But thanks to you guys, I’m actually enjoying this quite a bit now. Can’t wait to come back and show y’all the results!

1 Like

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