2

I am trying to send string parameter value to a solidity method but i am not able to get the return value. this is my solidity code :

contract Entity{
    event Entities(string name);
    struct Entity{
        string name;
    }
    uint32 public entityId;
    mapping(uint32 => Entity) entities;
    function insert(string name) returns (uint32 returnId){
         returnId = ++entityId;
         entities[returnId] = Entity(name);
         Entities(name);
         return returnId;
    }
}

and this is how I am trying to interact with it through JSON RPC API

curl --data '{"jsonrpc":"2.0","method": "eth_call", "params": [{"from": "0xec1466c6fa38de0506e14fe8e5b51e95d3f80b11","to":"0xbd46d45c440ae02a76938043af18cc75ac569e4b","data":"0xb1498e29456e746974792042000000000000000000000000000000000000000000000000"}], "id": 6}'

I am getting 0x as result.

1

Your method changes the state of the contract (block chain) meaning that you can't get any values since by the time the call returns the transaction (calling the method) may not have been mined (there is no computed id to return).

You will get the hash of the transaction and must wait until the code is executed (mined). Only then can you get the id by calling a method that does only a read operation over the contract.

You also need to assign a cost in gas to the transaction for the call since you are asking a miner to run it for you. Use the geth IPC since it's far less cumbersome than throwing JSON objects at the client:

entityContract.insert("my_string", {from:eth.accounts[0],gas:100000000});

where entityContract is a contract instance created from its ABI and address.

  • In case of sending a transaction it has to mined if i am not wrong but here i am just calling the method which basically should have returned some value but that is not happening – Abhishek Ranjan Aug 5 '16 at 7:45
  • You are changing the state of the contract in your method by incrementing entityId. – Sebi Aug 5 '16 at 7:47
  • this will happen internally basically but since i am not sending any transaction the state won't change and it will revert back to original i suppose – Abhishek Ranjan Aug 5 '16 at 7:49
  • In your contract remove returnId from uint32 returnId. No, you are still doing computation on the block chain so someone has to pay for those clock cycles even if the result isn't stored in the block chain (you have to pay for both storage and instructions executed). – Sebi Aug 5 '16 at 7:52
  • The pament what you are mentioning i think blockchain automatically does that if you not mention in the code – Abhishek Ranjan Aug 5 '16 at 8:16

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.