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?

  • When you put 1 in the browser, it is understood as 1 ether, not 1 wei. – Xavier Leprêtre B9lab Sep 12 '16 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 '16 at 14:22
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

  • Sorry, but it fails. Have you tried it out yourself?Does it work? – Gyonder Sep 12 '16 at 15:01
  • Yup. Tried it in browser-solidity with local geth 1.4.11-stable. Answer updated with more details. – The Officious BokkyPooBah Sep 12 '16 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 '16 at 15:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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