2

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?

1 Answer 1

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
    Commented Dec 3, 2021 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
    Commented Dec 3, 2021 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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