Solidity Transactions – How to Know the Hex Code to Call a Specific Contract Function

abicontract-invocationsoliditytransactions

For instance, on the BAT ico site, you can see the crowdsale contract address, the ABI and also a string to use in the data part of your transaction if you don't want to call the contract function using the ABI.

The data to put in the transaction is 0xb4427263 that corresponds to the createTokens() function.

How did they manage to get this 0xb4427263 hex value ?
Did they have to make a transaction using the ABI first then get the data from the transaction or is there a direct way to get the value without sending a transaction ?

Best Answer

For this specific example you take the method signature createToken() and compute its SHA3 hash 0xb4427263722d66ea37c3d76cba9c56b0f1a7d6e049effc1f9f75567547fd47eb, then take the first 4 bytes (8 hex characters) of it and you have 0xb4427263. Since there are no arguments to pass, that's all of the data.

web3.sha3("createTokens()").substr(0,10)

The full specifications for this encoding can be found at https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI