TransWikia.com

Way to fetch function name in fallback function with Solidity

Ethereum Asked by Bob5421 on January 29, 2021

I am writing a fallback function this way:

fallback() external {        
     (param1, param2) = abi.decode(msg.data[4:], (uint256, uint256));
}

I know this is not a good practice but let’s suppose i call an "azerty" function on my contract with 2 uint256. And of course, azerty function does not exists. So fallback function will be called.

I have 2 questions inside this fallback function:

  • How can i get the function name (azerty) ?
  • Is there a way to get the arguments types and count ? (in my case, i know i have 2 uint256 but what will happen if i call azerty with a single uint argument ?

Thanks

One Answer

It is not possible to recover the function name from the transaction alone since only 4 bytes from the function identifier are include. Those 4 bytes are also accessible through msg.sig.

It should be possible to store a mapping in the contract so they can be recovered from the identifier: mapping (bytes4 => string) public signatures. Store them signatures[bytes4(0x70a08231)] = "balanceOf(address)", using it signatures[msg.sig].

Recent solidity version validate a function has enough data and revert if they have less than expected.

Using fallback skips any validation but abi.decode fails when there's not enough data to decode all of its parameters.

Answered by Ismael on January 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP