0

Im learning how Blockchains work and created a private Blockchain to test some things. I deployed the following contract:

contract Greeter         
{
    address creator;     
    string greeting;     

    function Greeter(string _greeting) public   
    {
        creator = msg.sender;
        greeting = _greeting;
    }

    function greet() constant returns (string)          
    {
        return greeting;
    }

    function getBlockNumber() constant returns (uint) 
    {                                                   
        return block.number;
    }

    function setGreeting(string _newgreeting) 
    {
        greeting = _newgreeting;
    }


    function kill()
    { 
        if (msg.sender == creator)  
            suicide(creator);       
    }

}

Now I tried different methods to call the setter function I got the instance with using contract address and abi and called

instance.setGreeting.sendTransaction(...)

This one worked and with instance.greet() I got the new greeting. But then I tried an example from this page https://github.com/ethereumbook/ethereumbook/blob/develop/transactions.asciidoc#invocation and called

web3.eth.sendTransaction({from: eth.coinbase, to: contract_address, data: "0x4136862e000000000000000000000000000000000000000000004d794e65774772656574"})

where the data is created of the first 4 bytes of the Keccak256 hash of the prototype function setGreeting(string) and the String "MyNewGreet" encoded to Hex and padded to 32 bytes. The transaction is mined but instance.greet() still return the old greeting

1 Answer 1

1

It looks like you got the function selector wrong. It should be 0xa4136862. (You're missing the first character, and you included an extra one on the end instead.)

The string is also incorrectly encoded. It should be a 32-byte location of the data (0x20) followed by a 32-byte length (0x0a), followed by the actual data, right-padded to be a multiple of 32 bytes. See https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#argument-encoding for the full details.

All together, this is the correct ABI encoding of your function call:

0xa41368620000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a4d794e6577477265657400000000000000000000000000000000000000000000

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.