3

Hey can someone help me figure out why this function returns 0, when I call it? The function doesn't throw any errors when I send a transaction to it, but the Ether isn't sent and the variables aren't set.

    function payout() returns (uint) { 
       if (projectPaid) {return 3; }
       projectPaid = true;
       if (!msg.sender.send(amountRaised)) { 
        projectPaid = false;
        return 4; 
      }
    return 5; 
    }

Here is the web3/truffle call

  var projectToFund = document.getElementById("payout").value;
  var thisProject = Project.at(projectToFund); 
  thisProject.payout.call({from: account, gas:200000}).then(function(_result) { 
    console.log(_result.valueOf());
  • 1
    Have you solved this? Can you please shed some light on what was the actual problem? – max taldykin Feb 28 '17 at 15:47
  • 1
    So it seemed that I was setting the variable with both uint and uint256 (at different points of the application). It seemed to get fixed when I changed every variable to uint256. – ethereal Mar 3 '17 at 17:41
3

it won't compile with return 5; outside the function.

So let's consider:

function payout() returns (uint) { 

       if (projectPaid) {return 3; }
       projectPaid = true;
       if (!msg.sender.send(amountRaised)) { 
        projectPaid = false;
        return 4; 
       }


 // return 5; 

if projectPaid ??? No.

if send() failed ??? No.

Nothing returned if the send worked, so 0.

Simple function:

    bool projectPaid;
    uint amountRaised;

    function payout() returns(bool success) {
        if(projectPaid) return false;
        projectPaid = true;
        if(!msg.sender.send(amountRaised)) {
            projectPaid = false;
            return false;
        }
        return true;
    }

Hope it helps.

  • Hey sorry Rob, I forgot to add the closing semicolon, I edited the question to match what I have in solidity. – ethereal Feb 24 '17 at 16:57
  • So when I comment out every line and just try to return a uint like 5, it returns 0, when I try to return true, it returns false. – ethereal Feb 24 '17 at 19:14
  • Sounds like you're not waiting for the transaction to be mined. Have a look at this code I've used with success for a nice pattern. gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6 – Rob Hitchens Feb 24 '17 at 22:11
  • Ok, I'm actually making a call() and then trying the transaction, but should I be waiting for the call() to be mined as well? Do you know how to parse the transaction receipt when it's mined? – ethereal Feb 25 '17 at 18:26
  • call() does not issue a transaction, so you don't need to wait for mining. – max taldykin Feb 25 '17 at 20:09
3

I guess you are using web3.js to send transaction calling your function. This does not support return values.

You can get return value only when calling this function from another contract or calling it as ctr.payout.call() (note that in the latter case, the function can't change contract's state).

Please see these questions for more information:

It is possible to use events to track return values.


Here is test contract and example geth session:

x.sol

pragma solidity ^0.4.0;

contract Test {
    bool public projectPaid = false;
    uint public amountRaised = 0;

    function payout() returns (uint) { 
        if (projectPaid) return 3;
        projectPaid = true;
        if (!msg.sender.send(amountRaised)) { 
            projectPaid = false;
            return 4; 
        }
        return 5; 
    }
}

geth console

var x_sol_testContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"payout","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"amountRaised","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"projectPaid","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"}]);
var x_sol_test = x_sol_testContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x60606040526000805460ff19168155600155341561001957fe5b5b61014d806100296000396000f300606060405263ffffffff60e060020a60003504166363bd1d4a81146100375780637b3e5e7b14610059578063c2e39a731461007b575bfe5b341561003f57fe5b61004761009f565b60408051918252519081900360200190f35b341561006157fe5b610047610112565b60408051918252519081900360200190f35b341561008357fe5b61008b610118565b604080519115158252519081900360200190f35b6000805460ff16156100b35750600361010f565b6000805460ff1916600190811782555460405173ffffffffffffffffffffffffffffffffffffffff33169282156108fc02929190818181858888f19350505050151561010b57506000805460ff19169055600461010f565b5060055b90565b60015481565b60005460ff16815600a165627a7a723058205acabc04fdd5e478a5e40215426abb624ec48fea0ef750e4e6a0f14cb328dbe90029', 
     gas: '4700000'
   })

 x_sol_test.payout.call()

Try to run this and see if it returns 5. I my case it does.

  • I'm not sure what you mean by web3 doesn't support return values. Web3 is how front-ends get data from contract functions. – Rob Hitchens Feb 23 '17 at 20:30
  • Sorry, I mean that it is not possible to get return value when sending a transaction from web3. Updated my answer. – max taldykin Feb 23 '17 at 21:18
  • Ok I am indeed trying to use call() and it is returning 0...I added the web3 call – ethereal Feb 24 '17 at 16:58
  • 1
    I added example geth console session. Check that projectToFund contains valid address (or maybe it points to some outdated contract). – max taldykin Feb 24 '17 at 17:17

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.