[SOLVED] Calling functions on BSC deployed contract

on ā€œSign in with Metamaskā€. I think itā€™s because Iā€™m already signed in. But for signed in users, theyā€™re supposed to see the game.

If it helps at all, the Youtube video you showed me, at the timestamp 11:59 is where Iā€™m stuck.

In case you need the video link again:

You can open the console to see if it says anything there.

Thanks. It turns out that the console.log(user) was supposed to be console.log(currentUser). I didnā€™t make any changes to it because I thought whatever I copied from the Github should be the right code to use. Next time I know! Thank you again.

yep, now I also found that this was the problem :slight_smile:

Okay so I followed through all the videos, now I am back to building the frontend dapp to call a function of my contract. I saw in the Moralis docs and also another post in this forum that @Yoomoo recommended to use the Moralis.execute code.

Hereā€™s the code I found in the docs:

const ABI = [
  {
    constant: true,
    inputs: [
      {
        internalType: "address",
        name: "owner",
        type: "address"
      },
      {
        internalType: "address",
        name: "spender",
        type: "address"
      }
    ],
    name: "allowance",
    outputs: [
      {
        internalType: "uint256",
        name: "",
        type: "uint256"
      }
    ],
    payable: false,
    stateMutability: "view",
    type: "function"
  }
];

const options = {
  contractAddress: "0xe...56",
  functionName: "allowance",
  abi: ABI,
  params: {
    owner: "0x2...45",
    spender: "0x3...49"
  },
};

const allowance = await Moralis.executeFunction(options);

Since I have the abi of the contract code in another js file from when I followed the Serverless dapp videos, I modified the codes a little. Please take a look:

const claimRewards = {
    contractAddress: "0xb48D1488266F636dB97597875dE305Bb981EC5a5",
    functionName: "claimRewards",
    abi: window.abi,
    params: {
      // I don't know what to set for params because the function is claimRewards.
     // It simply calls the contract to send the rewards to the connected wallet.


    }
  };
  
  const allowance = await Moralis.executeFunction(claimRewards);

Iā€™d appreciate any help I can get with this!

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