4

Firstly, I wrote a contract with the source below:

pragma solidity ^0.4.8;

contract HelloWorld {
    address creator; string greeting;

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

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

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

    /**********
     Standard kill() function to recover funds
     **********/
    function kill() {
        if (msg.sender == creator)
            suicide(creator);  // kills this contract and sends remaining funds back to creator
    }
}

And then I compiled it with the online compiler https://ethereum.github.io/browser-solidity . I got the contents from the Web3 deploy box, which is :

var _greeting = /* var of type string here */ ;
var test_sol_helloworldContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_newgreeting","type":"string"}],"name":"setGreeting","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"type":"constructor"}]);
var test_sol_helloworld = test_sol_helloworldContract.new(
_greeting,
   {
     from: web3.eth.accounts[0], 
     data: '0x6060604052341561000c57fe5b6040516104cf3803806104cf833981016040528080518201919050505b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060019080519060200190610080929190610088565b505b5061012d565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100c957805160ff19168380011785556100f7565b828001600101855582156100f7579182015b828111156100f65782518255916020019190600101906100db565b5b5090506101049190610108565b5090565b61012a91905b8082111561012657600081600090555060010161010e565b5090565b90565b6103938061013c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806341c0e1b514610051578063a413686214610063578063cfae3217146100bd575bfe5b341561005957fe5b610061610156565b005b341561006b57fe5b6100bb600480803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506101ea565b005b34156100c557fe5b6100cd610205565b604051808060200182810382528381815181526020019150805190602001908083836000831461011c575b80518252602083111561011c576020820191506020810190506020830392506100f8565b505050905090810190601f1680156101485780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156101e757600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b5b565b80600190805190602001906102009291906102ae565b505b50565b61020d61032e565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102a35780601f10610278576101008083540402835291602001916102a3565b820191906000526020600020905b81548152906001019060200180831161028657829003601f168201915b505050505090505b90565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102ef57805160ff191683800117855561031d565b8280016001018555821561031d579182015b8281111561031c578251825591602001919060010190610301565b5b50905061032a9190610342565b5090565b602060405190810160405280600081525090565b61036491905b80821115610360576000816000905550600101610348565b5090565b905600a165627a7a723058208e2dda784b2ed789c298ab5b4bb42dc4fef4c7acd2585c6638dc0190f9c5c3e20029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
     console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

And then I copied it to the geth console and mined it. I got the message:

Contract mined! address: 0x075ebb29cc7c7d80c4de92077febe53a262524e0 transactionHash: 0xa819e8225eeb7faafd4c2dd239052fba4b2434377c125d31c4e5fb8982fc9228

Then I type into the geth console to get the following result:

> web3.sha3('greet()').substr(0, 10)
"0xcfae3217" 

Then executed the command below(192.168.241.128 is my geth server IP):

curl 192.168.241.128:8545 -X POST --data '{"jsonrpc":"2.0", "method":"eth_call", "params":[{"from": "0x407d73d8a49eeb85d32cf465507dd71d507100c1", "to": "0x075ebb29cc7c7d80c4de92077febe53a262524e0", "data": "0xcfae321700000000000000000000000000000000000000000000000000000000000000000"}, "latest"], "id":1}'

But I got error message:

{"jsonrpc":"2.0","id":1,"error":{"code":-32602,"message":"invalid argument 0: hex string has odd length"}}

How do I get this to work?

BokkyPooBah
  • 40,274
  • 14
  • 123
  • 193
jiebang
  • 993
  • 1
  • 12
  • 18
  • the result is :{"jsonrpc":"2.0","id":1,"result":"0x00000000000000000000000‌​00000000000000000000‌​00000000000000000002‌​00000000000000000000‌​00000000000000000000‌​00000000000000000000‌​0000e68656c6c6f202c6‌​76f6c646d616e0000000‌​00000000000000000000‌​000000000"} But I hope it should return "hello, world‘’, how should I transform the result to a common string such as "hello, world"? Thank you for helping me . – jiebang Apr 05 '17 at 05:57
  • I have set var _greeting = "hello, world" after copy from Web3 deploy box – jiebang Apr 05 '17 at 05:57
  • Welcome to the Ethereum Stack Exchange! A tip: usually when accepting an answer, you would also upvote it by clicking on the triangle that points up. – eth Apr 05 '17 at 06:49
  • switched to brave browser it will work. --------------------------------------- answer source anonymous I tried in chrome , it messesup with the request – vijay Jun 19 '18 at 06:26

1 Answers1

2

Try executing the following line, where I removed an extra 0 character:

curl 192.168.241.128:8545 -X POST --data '{"jsonrpc":"2.0", "method":"eth_call", "params":[{"from": "0x407d73d8a49eeb85d32cf465507dd71d507100c1", "to": "0x075ebb29cc7c7d80c4de92077febe53a262524e0", "data": "0xcfae32170000000000000000000000000000000000000000000000000000000000000000"}, "latest"], "id":1}'

Your original data string was:

0xcfae321700000000000000000000000000000000000000000000000000000000000000000

This can be broken down into the following parts (plus some numbering):

0x
cfae3217
00000000000000000000000000000000000000000000000000000000000000000

12345678901234567890123456789012345678901234567890123456789012345

There is an extra zero in position 65 that caused your error. The string should have 64 hex characters.

BokkyPooBah
  • 40,274
  • 14
  • 123
  • 193
  • 1
    the result is :{"jsonrpc":"2.0","id":1,"result":"0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000e68656c6c6f202c676f6c646d616e000000000000000000000000000000000000"} But I hope it should return "hello, world‘’, how should I transform the result to a common string such as "hello, world"? – jiebang Apr 05 '17 at 04:58
  • 1
    thank you very much also for editing my words above just now. – jiebang Apr 05 '17 at 05:01
  • 1
    I have set var _greeting = "hello, world" after copy from Web3 deploy box. – jiebang Apr 05 '17 at 05:02
  • To transform, see this or this, or search for "toUtf8" in this site. – BokkyPooBah Apr 05 '17 at 06:54
  • Nice to mee you. I have a new question http://ethereum.stackexchange.com/questions/15138/code-32602-messagetoo-many-arguments-want-at-most-1. could you help me? thank you. – jiebang Apr 12 '17 at 07:57