Call Write Function of Contract

Hi again :slight_smile:

i am almost done with all my coding, and it works great… moralis really is awesome. I have one last problem before i am finally done…

I need to call the Claim Function of an bep20 Contract. On Bscscan if i click the claimbutton, i need to pay some gas, and the rewards are sent to me… but on moralis if i call the claim function , nothing happens… no metamask popup or anything… as i already use some other functions of the contract, i know my calls are working… but them all read functions… this is the first write function. what do i have to do?

can you please share me an example or help me?

greetings!

Hi,
You’ll have to use .send function instead of .call in order to make a transaction.

yeah just figured it out, and had to add from address xD Thank you very much <3

Hey @Hydracoder

Also you can use our new SDK feature Moralis.executeFunction() . It makes calls to contracts easier :man_factory_worker:

It automatically detects is your contract method .call() or .send()

ohh thats nice to know! thank you for that info!

1 Like

You are welcome!

Happy BUIDLing :man_factory_worker:

yeah i also want to know how to call the write function , as i done all the read function in my contract , but i want to call claim function in my contract … heres my code which i m running on browers console and getting error that
https://bscscan.com/address/0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078#code

“only read functions are allowed”

 bsc40_abi = [
      {

        constant: true,

        inputs: [],
        name: "claim",
        outputs: [],
        stateMutability: "nonpayable",
        type: "function"


      },

    ];

    a = await Moralis.Web3API.native.runcontractfunction({
      chain: "bsc",
      address: "0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078",
      function_name: "claim",
      abi: bsc40_abi,
      params: {
        owner: "0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078",
        spender: "0x5974c357769D77a04CD87AeE3e49e5bf719d1CFe"
      }

    })

    console.log(a)

You have to use Moralis.executeFunction for write functions

u mean await Moralis.Web3API.native.executefunction ?

in params owner address i have to give contract address?
and in spender current address which is logged in through metamask , i have to give that address ?

No, I mean Moralis.executeFunction, it will require a web3 connection to sign the transaction.

This error is coming … when I m calling

bsc40_abi = [
      {

        constant: true,

        inputs: [],
        name: "claim",
        outputs: [],
        stateMutability: "nonpayable",
        type: "function"


      },

    ];

    a = await Moralis.executeFunction({
      chain: "bsc",
      address: "0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078",
      function_name: "claim",
      abi: bsc40_abi,
      params: {
        owner: "0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078",
        spender: "0x5974c357769D77a04CD87AeE3e49e5bf719d1CFe",
      }

    })

    console.log(a)

Any error u see here ?

I don’t know exactly what is wrong, I’m not at computer to test now

which address i have to give for owner ?

okh if u have time sir then check it …

a version that starts MetaMask:

a = await Moralis.executeFunction({
      contractAddress: "0x28e4f32fa7e842ad0a6e530bb6b4ec03b36d5078",
      functionName: "claim",
      abi: bsc40_abi,
      params: { }
    })

https://docs.moralis.io/moralis-server/web3/web3#executefunction