1

I try to call my Solidity function:

function getVehicleDetails(string _vin) returns (string _vln, uint _year, string _make, string _model, string _colour) {
    Vehicle vehicle = vehicles[_vin];
    if (vehicle.year == 0)
        throw;

     _vln = vehicle.vln;
    _year = vehicle.year;
    _make = vehicle.make;
    _model = vehicle.model;
    _colour = vehicle.colour;
}

Here's the geth command:

instance.getVehicleDetails("vin_101",{from:web3.eth.accounts[1]})

returns "0x6cf98ab5b77bfa0397ee6eaa046f85a133c5a3ef636c59a56b4308dc73fb20fd"

i.e. it keeps returning a tx receipt!

Here's how I create a vehicle:

function setVehicleDetails(string _vin, string _vln, uint _year, string _make, string _model, string _colour) owneronly {
    Vehicle vehicle = vehicles[_vin];
    if (vehicle.year == 0)
        throw;
    vehicle.vln = _vln;
    vehicle.year = _year;
    vehicle.make = _make;
    vehicle.model = _model;
    vehicle.colour = _colour;
}

I call this in geth with:

instance.registerVehicle("vin_101","vln_101",2004,"ferrari","convertible","black",{from:web3.eth.accounts[0],gas:4000000})

What am I doing wrong?

All I want to do is read the return values from getVehicleDetails and it just refuses to!

I'm on testrpc.

2

As this is a non payable function. Always call these functions with call keyword and not just the function name. Only calls will return values.

Format if the call: Contract.getVehicleDetails.call(params)

0

The functions in solidity always return transaction hash. You can use constant functions to return values. The constant functions can not change the state of contract.

If you need to change state of contract in a function and aslo want to return some value, Events are best possible solution AFAIK.

You can use events to get values from a non-constant function.

  • I understand that. Thank you for clarifying. Because someone else wrote the Solidity, I was unable to add the constant keyword. So, I used "call" instead. instance.getVehicleDetails.call("vin_101",{from:web3.eth.accounts[0]}) I'm figuring this out the hard way!!! – Eamorr May 27 '17 at 16:13
  • Using call won't help you. Call is similar to send just it doesn't create a transaction and publish it to the blockchain. There is no difference in the way call and send invoke the function. – Prashant Prabhakar Singh May 27 '17 at 16:18
  • I disagree. On the blockchain (as opposed to in-browser Solidity compilers such as browser-solidity), there is a definite difference between using "call" and not using it. Without "call", no return params are returned - just a tx receipt. With "call", you get the parameters returned. One thing you could do is just use the "constant" keyword in your Solidity function. If you're using browser-solidity, you don't have to worry about this nuance. Though I found this very confusing - code worked fine in browser-solidity, but not on a real blockchain... – Eamorr May 28 '17 at 9:44
0

You have to use constant function for returning any values from the solidity function. Constant function comes with a limitation that you can not change the state of contract(can not change the value of any state variable) in the constant function.

Here is the example of constant function and calling constant function from JS.

contract MyNumber{

    uint public num1 = 1;

    function getNumber() constant returns (uint){
        return num1 ;
    }

}

To call this function use call() instead of sendTransaction() here is the example

var contract = web3.eth.contract(abi).at(contractAddress);    
contract.getNumber.call();

again if you want to get the values from the function which change the state of contract you can use events and event call back For more on using events and event callback see the code in this QUESTION

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.