7

In solidity, the bytecode for functions is taking the web3.sha3 value of a function name and taking the first 4 bytes of the resulting hash in the format of 0x00000000

So if you have a function called totalsupply() the hash would be 0x18160ddd

I have the understanding that public variables have getters automatically created.

However, when trying to determine the bytecode for a public variable such as 'Name'. I would assume the function created is 'Name()' and thus the bytecode is 0x8052474d.

However, it doesnt seem to be working when I try to call a function using that bytecode.

Any thoughts as to what I am doing wrong?

3 Answers 3

2

For a public variable x, i will automatically generate method x(), so you can access this variable the way call method. I give a simple demo, as follows:

pragma solidity ^0.4.2;

contract test{

    uint public age;

    function test(){
        age = 10;
    }

    function callData() returns (string){
        bool result = this.delegatecall(bytes4(sha3("age()")));
        if (result) {
            return "success";
        }
        return "failed";
    }

}

Hope it helps~

2

From the example in the Solidity documentation, we can see that:

0xcdcd77c0: the Method ID. This is derived as the first 4 bytes of the Keccak hash of the ASCII form of the signature baz(uint32,bool)

We can verify the hash using this online keccak 256 hash tool by inputing "baz(uint32,bool)":

https://emn178.github.io/online-tools/keccak_256.html

Also, you can get the contract ABI from etherscan, and paste it into this tool: https://abi.hashex.org/#

which provides then the hashes for your functions. It seems you need to enter at least the first parameter.

0

On solidity browser the bytecode for all functions ina contract is given. No need to break your head... Discovered it moments after posting this.

4
  • Did you work out why the Name() function was different to what you expected? Aug 10, 2017 at 5:50
  • I was capitalizing... the name function was lowercase Aug 10, 2017 at 13:08
  • Alternatively to get the function names and their hashes, you can run solc --hashes your_contract.sol Aug 10, 2017 at 13:47
  • I think you mean Remix, under the Solidity Compiler -> Compilation Details -> Function Hashes Sep 16, 2022 at 17:55

Your Answer

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

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