9

I'm experimenting with Solidity Browser

Here is my contract code:

contract Test {

    mapping (address => uint256) weismap;

    function bet(uint vote) payable returns (uint256 weis) {
        if (msg.value==0) throw;
        weismap[msg.sender]= msg.value;
        return  weismap[msg.sender];
    }

    function test()  returns (uint myVote)  {
           return 1;
    }
}

If I set as transaction value 1

I set as transaction value at 1

,create the contract and call the function bet with 1 as parameter

the function is run successfully.

If after that, I call the test() function I get

VM Exception: invalid JUMP

Any idea, about what's wrong?

2
  • When you put 1 in the browser, it is understood as 1 ether, not 1 wei. Sep 12, 2016 at 14:21
  • Yes, I know that but I don't understand what it has to do with error. Besides msg.value is in wei. Are you sure that is relevant?
    – Gyonder
    Sep 12, 2016 at 14:22

1 Answer 1

5

You need to add the constant keyword to the test() function like:

pragma solidity ^0.4.0;
contract Test {
    mapping (address => uint256) weismap;

    function bet(uint vote) payable returns (uint256 weis) {
        if (msg.value==0) 
            throw;
        weismap[msg.sender]= msg.value;
        return  weismap[msg.sender];
    }

    function test() constant returns (uint myVote)  {
        return 1;
    }
}

Calling bet(...) with the value 2. Note that I'm sending 2 ethers as well with the transaction:

enter image description here

Now I call test() AND I've left the value 2. This call to test() fails as the constant function is being called as a transaction, not a read of the values off the blockchain:

enter image description here

If I call test() AND I've set value to blank, this call to test() succeeds as the constant function is being called as a read of the values off the blockchain:

enter image description here

3
  • Sorry, but it fails. Have you tried it out yourself?Does it work?
    – Gyonder
    Sep 12, 2016 at 15:01
  • Yup. Tried it in browser-solidity with local geth 1.4.11-stable. Answer updated with more details. Sep 12, 2016 at 15:24
  • Yes but you didn't call bet. Could you try this please? First you set the transaction value. Then call bet with let's say 2. Then test. Does it work? Thanks
    – Gyonder
    Sep 12, 2016 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.