0

Learning ethereum, I am hoping this is an easy one. I wrote a simple test contract

contract Mini{
uint balance;

// constructor
// runs once upon creation
function Mini(){
    balance = 1000;
}

function deposit(uint _value) returns(uint _newValue){
    balance += _value;
    return balance;
}

function benchmark() returns(uint _newValue){
    return 4545;
}}

When I invoke "benchmark"

    var result = myContractInstance.benchmark(function(err,res){
        console.log(res);
    });

The response is an address (e.g. 0xd1d618bcb179db8b020358710138d1a008f4f39e). I can see the transaction in etherscan. Question, how do I see the response value?

0

benchmark is not a constant function so it will return a transaction id for you.

if you need to return a value or read a value from block chain you can use constant function

function benchmark() public constant returns(uint _newValue){ return 4545; }}

  • Thanks, that is helpful. However, in the case where you want the result of the function call, the one identified by the transaction. How do you see it? – beblocking Feb 7 '18 at 13:12
  • I just verified I can call the constant function and get the expect return value, 4545. Question, how do I get the response from the "deposit" function? What do I do with the address? – beblocking Feb 8 '18 at 5:53
  • While the return value is available to other contracts, it is not possible for an external observer to get the return value of a state-changing transaction. You can use the returned receipt to wait for the transaction to be mined, then a few options: Inspect the transaction log, inspect another function, replay the transaction (that's how Remix does it). – Rob Hitchens Feb 5 '19 at 15:07
0

I got it to work. I was missing "constant" and "view". Here is the final contract:

contract Mini{
    uint balance;

    // constructor
    function Mini(){
        balance = 1000;
    }

    function deposit(uint _value) public returns(uint _newValue){
        balance += _value;
        return balance;
    }

    function withdraw(uint _value) view  returns(uint _newValue){
        balance -= _value;
        return balance;
    }

    function benchmark() public  constant returns(uint _newValue){
        return 4545;
    }

    function getBalance() public constant returns(uint _newValue){
       return balance;
    } 
}

I was then able call the functions as follows:

var MyContract = web3.eth.contract(abi);

var myContractInstance = MyContract.at('YourContractAddress');


myContractInstance.benchmark(function(err,res){
    console.log(res);
});

myContractInstance.deposit(200,function(err,res){
    console.log(res);
});

myContractInstance.withdraw(50,function(err,res){
    console.log(res);
});

myContractInstance.getBalance(function(err,res){
    console.log(res);
});

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.