2

Let's say we have Ethereum smart contract with fallback function:

event onDeposit(int128 indexed key, address from, uint amount);
... 
function () payable public {
  require(msg.value > 0);

  int128 key = ... // how to extract it from msg.data? (0x123)
  address from = msg.sender;
  uint amount = msg.value;

  // fire event
  emit onDeposit(key, from, amount);
}

and taking into account key is entered in myetherwallet.com like that:

enter image description here

Is it correct that i should skip 4 first bytes in msg.data (function address) and get the next 4 bytes (int128 size)? How can i do it (slice bytes and cast them as int128)?

1

Is it correct that i should skip 4 first bytes in msg.data (function address)

No, because no function selector is being included in the data.

and get the next 4 bytes (int128 size)?

An int128 is 16 bytes wide (128 bits).

I believe just doing int128(msg.data) will work, assuming the data is properly formatted (16 bytes wide, left-padded with zeros).

  • oh, yes, 16x8=128. When function selector is included and not? – 4ntoine Aug 18 '18 at 23:05
  • 1
    It's up to the client what they include in the data field, but here you seem to be typing it in by hand. So unless you've encoded a function selector, there isn't one. – smarx Aug 18 '18 at 23:06
  • so it's just any bytes array without any structure/markup? – 4ntoine Aug 18 '18 at 23:08
  • 1
    Yeah, the structure (ABI encoding) is done by the client (e.g. web3.js) and then parsed by the smart contract. – smarx Aug 18 '18 at 23:10
  • thanks. it's not related question, but can i set msg.data with Remix? I did not find how to do it – 4ntoine Aug 18 '18 at 23:12

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.