6

I have a fallback (unnamed) function in my contract and I know that the last parameter (or first, does not matter) passed is always an uint. Is it possible to somehow access it though the msg.data?

eth
  • 85,679
  • 53
  • 285
  • 406
md2312
  • 193
  • 6

1 Answers1

4

Yes, all parameters can be accessed from msg.data (EVM term is calldata).

The calldata is ABI encoded: What is an ABI and why is it needed to interact with contracts?

Extracting the first parameter is simpler than others. For the first parameter, skip the first 4 bytes (Method ID), and the next 32 bytes is the uint (left-padded) you are looking for.

If the parameter was the last, extracting it would be more involved especially if some of the parameters are arrays, strings, or bytes.


Note: There's a case where the fallback function gets a very limited amount of gas, and in that case you might not be able to do what you'd like with the uint (like storing it).

eth
  • 85,679
  • 53
  • 285
  • 406