0

I want to encode a call for mint() payable with a msg.value of 8 ETH.

My code:

web3.eth.abi.encodeFunctionCall({
    type: 'function',
    name: 'mint',
    payable: true,
    value: web3.utils.toWei("8", "ether")
}, [])

I get TypeError: Cannot read property 'forEach' of undefined

Help?

0

You are missing the inputs field as per the documentation.

So, assuming no parameters :

web3.eth.abi.encodeFunctionCall({
    type: 'function',
    name: 'mint',
    payable: true,
    inputs: [],
    value: web3.utils.toWei("8", "ether")
}, [])
// returns: 0x1249c58b

You can omit the fields : payable and value they are irrelevant to generate the abi encoded function call. So :

web3.eth.abi.encodeFunctionCall({
    type: 'function',
    name: 'mint',
    inputs: [],
}, [])
// returns: 0x1249c58b

Without parameters, this is exactly the same as:

web3.eth.abi.encodeFunctionSignature("mint()")
// returns: 0x1249c58b
2
  • But if I omit the value field, how do I set the msg.value?
    – antoniopgs
    Dec 3 '21 at 8:42
  • The result of encodeFunctionCall is basically what you will use to set the field data of your tx. It seems like you beleive that encodeFunctionCall is used to actually send a valid tx, is that so ?
    – hroussille
    Dec 3 '21 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.