3

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?

1

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~

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.

  • Did you work out why the Name() function was different to what you expected? – Edmund Edgar Aug 10 '17 at 5:50
  • I was capitalizing... the name function was lowercase – Dino Anastos Aug 10 '17 at 13:08
  • Alternatively to get the function names and their hashes, you can run solc --hashes your_contract.sol – Edmund Edgar Aug 10 '17 at 13:47

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.