7

As per my question here:

I need to compare msg.data (specifically, numbers that are sent with the sendTransaction data object) to a data type of uint256 within my contract.

How is this possible?

6
  • msg.data is of type calldata Feb 5, 2016 at 6:11
  • I am sorry, but that does not answer my question... If someone sends a number between 1-100 to my contract using the data object in sendTransaction, how can I compare it to a uint256 within my contract? Feb 5, 2016 at 6:15
  • Welcome to the SE :) I don't know the answer, but I'm sure someone will come along who does. Just trying to be precise so it's easier for that person. Feb 5, 2016 at 6:17
  • Thank you I do appreciate it. I do have another account but I have lost my logins deets lol. Feb 5, 2016 at 7:05
  • hmm there is no "calldata" type. msg.data type is bytes See global variables solidity.readthedocs.org/en/latest/miscellaneous.html
    – eth
    Feb 5, 2016 at 20:11

3 Answers 3

4

Byte-per-byte comparisons. Conversions needed. reddit link is dead. Why do you need to access msg.data directly?

EDIT: Convert a uint256 from calldata bytes to a proper uint256 in Solidity: https://gist.github.com/anonymous/a734a5d299ffa7b5a834

EDIT: The typing system and posting rules on this site are super weird. Anyways that works. It should always return true.

3
  • Hi and welcome to Ethereum SE, your answer appears to be a comment. Could you add some more information about the conversions? I fixed the link by the way.
    – q9f
    Feb 5, 2016 at 11:39
  • I misunderstood. I thought the only way to send data to a contract was with the data object in the sendTransaction. Why does this contract not send 1 Finney back to me when I send it this transaction, I cannot for the life of me work out why not: random.rand.sendTransaction(1,{from: eth.accounts[0], value: web3.toWei(.0001, 'ether'), to: '0x4abc14efad9a7a84a0a9ef6e1a782c43a9e3f550', gas: 500000, data: web3.fromAscii('message')}) Contract code: gist.github.com/anonymous/e210a052b6b4c86337c2 Great, no paragraph breaks in comments. Feb 5, 2016 at 13:41
  • I upvoted this answer and suggestions for next time: include the actual code itself (since it's not that long) and provide a little more explanation.
    – eth
    Feb 5, 2016 at 19:59
5

The specification for msg.data is the Ethereum Contract ABI.

Here's the code from the answer provided by Andreas:

contract ReadConvertUint256Bytes {

    function equal(uint a) constant returns (bool) {
        uint x = 0;
        for (uint i = 0; i < 32; i++) {
            uint b = uint(msg.data[35 - i]);
            x += b * 256**i;
        }
        return a == x;
    }

}

It starts reading from the byte indexed at 35, because the first 4 bytes (at index 0 to 3) is the Method ID (see ABI above), and everything in Ethereum internally is big-endian.

3

Whilst other answers may provide the tools required to answer this question as verbatim, my problem was solved because I was not aware of other ways to send parameters to contracts on Ethereum. Namely: eth.sendTransaction(2{from: eth.accounts[0], value: web3.toWei(5, 'ether'), to: '0xXXXX', gas: 200000, data: web3.fromAscii('MinedBlock')})

With '2' being the parameter being passed which can then easily be compared inside the contract to other variables. Thanks for the input.

4
  • Since you're passing in value (5 ether), you can check for it with msg.value. ("value" is not part of msg.data)
    – eth
    Feb 5, 2016 at 22:50
  • Also, using Contract Methods like myContract.myMethod(5, {from: eth.accounts[0]}) is usually simpler to pass a data (not value) of 5, than direct eth.sendTransaction.
    – eth
    Feb 5, 2016 at 22:56
  • @eth sorry, I updated my answer, I am passing in a parameter in my answer case - 2. Just to confirm, this is not accessible via msg.value. Correct? Feb 6, 2016 at 2:30
  • Correct, the "2" is not accessible via msg.value. But it is simpler to access the "2": if your function is like myMethod(int8 num) then num will get the value of 2.
    – eth
    Feb 8, 2016 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.