0

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?

2

The limit of 2300 only applies for contract-to-contract transactions. Otherwise there is no such strict limit. So all address-to-contract transactions to the fallback function with enough gas limit should succeed.

You can read a bit more details and explanation at Execution of Fallback function with more 2300 gas .

0

Your payable function execution some other operation then it should consume more gas.

If gas used exceed the gas limit the tnx will be error.

  • so you can confirm 2300 gas limit is just to contract-to-contract call and the is no special limitation to just sending eth to contract? – 4ntoine Aug 20 '18 at 8:43
  • I think so because we cannot set gas limit when do contract-to-contract call; otherwise, we can set gas limit for our tnx. People usually to set gas limit high then they send eth to a contract to buy token of an ICO then I think there's no special limitation to just sending eth to contract – Ha ĐANG Aug 20 '18 at 8:54
  • You can set the gas limit when making a contract to contract call – Raghav Sood Aug 20 '18 at 9:55
  • ah okay, so 2300 just is the default one? – Ha ĐANG Aug 20 '18 at 10:00
  • @RaghavSood How to set gas limit when making a contract to contract call? – Ha ĐANG Aug 22 '18 at 11:16

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.