[SOLVED] Executing Payable Functions from a deployed Contract Address

Hi, So I want to execute a function from a deployed contract address. The contract was deployed to Rinkeby testnet and the address is
0x3104d924a36A55D054544A8bAbFbeb1bd2ea7E5d

For reference, I have been able to execute a read only function that returns fata from the smart Contract. For this, I used both the runContract() method and the the executeFunction() method. And each time, the function was executed properly. However, as you know, the runContract method can only run a function that does returns data from the contract. I.e a function marked as view.
Basically, I want to execute a function that takes one parameter and makes changes to the blockchain (not read values off of it).
So for my payable function, I have tried to use the executeFunction() method. Please find the code below…

//code to run an async function hooked up to a button
async function extraMint(){

const ABI = [{“inputs”:
[{“internalType”:“uint256”,“name”:“amount”,“type”:“uint256”}],“name”:"_extraMint",“outputs”:[],“stateMutability”:“payable”,“type”:“function”}
]

let options = {
contractAddress: 0x3104d924a36A55D054544A8bAbFbeb1bd2ea7E5d",
functionName: “_extraMint”.
abi: ABI,
params: {
amount: 1000 //I tried using a dynamic input from the html where I read the value of an input field and convert it to an integer, it was throwing errors, decided to use a static input to test the process.
}
}
//in some cases, I added a web3 instance here. To be honest, I don’t know what that does.

await Moralis.executeFunction(options)
}

The error generated says something about matching failure. I will attach an image of my console.
Remember, I already used Moralis to execute a function from this same deployed address and it worked smoothly. So I am sure it’s not my syntax or anything local from my PC. Thanks a lot.

here it seems like a type with a string that doesn’t have " at the start for that contract address

I dont think That’s the problem. It is correctly typed on my Code editor. I just wnat to know, can the moralis execute function run payable functions? Cause it seems like it cant.

it should be able to run payable functions fine
here is the documentation: https://docs.moralis.io/moralis-dapp/web3/web3#executefunction

The link @cryptokid shared explains it all.
If you’re trying to pass amount to be paid, it should be

msgValue(optional): Number|String|BN|BigNumber. The value transferred for the transaction in wei.

async function extraMint() {

const ABI = [{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"_extraMint","outputs":[],"stateMutability":"payable","type":"function"}]; // Add ABI of 0xdAC17F958D2ee523a2206206994597C13D831ec7

const extraMintInput = parseInt(document.getElementById("extraMintInput").value)

const options = {

    chain: "kovan",

    address: "0xB507BFBCF557A1EB1de005b55429621E737Bb941",

    function_name: "_extraMint",

    abi: ABI,

    params:{

        amount: "1000"

    },

    msgValue: extraMintInput

};

await Moralis.authenticate()

await Moralis.executeFunction(options);

}

This is what I have and it’s still throwing the same error.

you need to use the exact parameter names as in documentation:

const sendOptions = {
  contractAddress: "0xe...56",
  functionName: "setMessage",
  abi: ABI,
  params: {
    _newMessage: "Hello Moralis",
  },
};

for example, I see function_name in the above code instead of functionName

THANKS A LOT GUYS!!!
I just got it to work. No errors. Gracias.

1 Like