0

Web3.js documentation says here that the methods of a smart contract are available through:

  1. The name: myContract.methods.myMethod
  2. The name with parameters: myContract.methods['myMethod(uint256)']
  3. The signature: myContract.methods['0x58cf5f10']

But how can I access the contract's fallback function, which has no name (and no parameters)?

Will the 2nd option work if I use myContract.methods['()']?

Is there a way to make the 1st option work?

Thank you!

2

Fallback functions are executed if a contract is called and no other function matches the specified function identifier, or if no data is supplied

so you can use the following:

send Transaction to the contract with the msg.value = 0 and it will automatically invoke the fallback function.

OR

call the contract using myContract.methods and provide the wrong or random function name or signature and it will execute the fallback function as it couldn't find the function with the same signature in the contract. (not tested)

Update (Tested on expanse network)

Tested the second option and it didn't work. As using random name in myContract.methods will return undefined because when web3js tries to find the function name in abi it returns empty. The reason being

enter image description here

other function in the abi have the name attribute and fallback doesn't have one enter image description here

After that created a raw transaction with the data being empty and sent that transaction to the network which invoked the fallback function as expected.

enter image description here

And on trace_transaction it says reverted which the fallback function does in this particular case. enter image description here

  • The first option does not answer my question ("how to use myContract.methods?"). In addition, the msg.value = 0 is irrelevant, since I actually do want to send funds to the fallback function (which is payable). The second option - I sincerely doubt that it would work. I was going to ask if you've tried it, but it seems that you haven't ("not tested"). – goodvibration Jan 15 at 6:22
  • 1
    give me a minute, i'll hack something together to test the second option – Hamza Ahmed Jan 15 at 6:25
  • ok, tested it using myContract.methods["random"] and it gives undefined as it couldn't find any function with the same name in abi. Then tried creating a raw transaction with empty data value and it worked. In conclusion you cannot call fallback function using myContract.methods. – Hamza Ahmed Jan 15 at 7:03
  • Thank you. I know that I can invoke a contract's callback function using the contract address in the to field, and an empty string in the data field. However, I have a general function which executes transactions, and this function relies on the user passing the transaction itself (not to and data). So I was hoping to understand how this function - as is - can support invoking the callback function. – goodvibration Jan 15 at 7:57

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.