I'm trying to understand what I can do in fallback payable (I have read the docs and I know what I can't do for sure).
Here is my contract:
pragma solidity ^0.4.22;
contract SmartMsgData {
// events
event onDeposit(int128 id, address person, uint amount);
function bytesToBytes16(bytes b, uint offset) private pure returns (bytes16) {
bytes16 out;
for (uint i = 0; i < 16; i++) {
out |= bytes16(b[offset + i] & 0xFF) >> (i * 8);
}
return out;
}
function () payable external {
require(msg.value > 0);
require(msg.data.length == 16); // 16 bytes = int128
// TODO: add checking for msg.value if i have enough gas
int128 id = int128(bytesToBytes16(msg.data, 0));
address person = msg.sender;
uint amount = msg.value;
// fire event
emit onDeposit(id, person, amount);
}
}
I've sent some amount from one address to contract address with data provided on myetherwallet.com with metamask.
Here is succeeded transaction. You can see gas used is 27826. However, in the docs it's written that fallback payable function can rely on 2300 of gas only:
In the worst case, the fallback function can only rely on 2300 gas being available
How did the transaction succeed if gas used exceeds the limit? Will it work in 100% of cases or depends on anything (miner, etc)?
Ideally, I'd check msg.value
to be valid (within some range) so I'm trying to figure out if I have enough gas to read struct from storage and check it. How can I measure gas requirement beforehand as Remix evaluates loops incorrectly and measures gas requirement for bytesToBytes16(..)
as infinite
?