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?
1 Answer
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).
-
In case of fallback function there might be no these 4 bytes of a method id. Jun 5, 2016 at 21:30