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?
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?
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.
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.
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.
msg.value
. ("value" is not part of msg.data
)
myContract.myMethod(5, {from: eth.accounts[0]})
is usually simpler to pass a data (not value) of 5, than direct eth.sendTransaction
.
myMethod(int8 num)
then num will get the value of 2.
calldata
bytes
See global variables solidity.readthedocs.org/en/latest/miscellaneous.html