1

I have deployed the following contract without any problem. When I try and run the function addFunds it seems to execute but the variable funds is not changed. Also if I enter the number 1 as a parameter it says sending 1 ether, but I thought 1 ether was 1 plus 18 zeros? Regardless of what number I use funds remains at 0.

pragma solidity ^0.4.18;

contract mortal{

    address public owner;

    function mortal() public {

        owner = msg.sender;
    }

    modifier onlyOwner{

        if(msg.sender == owner) { 
            revert();
        }else{
            _;
        }
    }

    function kill() onlyOwner public {

        selfdestruct(owner);
    }
}

contract Borrower is mortal {
    string  public borrowerName;
    uint256 public funds;

    function Borrower(string _name) payable public {
        borrowerName     = _name;
    }

    function addFunds() onlyOwner payable public {
        // Incoming funds
        funds += msg.value;
    }
} // Borrower

What am I doing wrong?

3
  • Are you using Remix or Truffle/JS to interact with the contract? If JS, please post the code. – Adam Kipnis Jan 24 '18 at 5:34
  • I am using remix to verify my code but then deployed using the Ethereum wallet UI. I could not figure out how to connect my account to Remix (yet). – Philip Rutovitz Jan 24 '18 at 5:36
  • Can you show us the code of the mortal contract? – willjgriff Jan 25 '18 at 9:51
1

I'm not sure how the Ethereum Wallet works but you can experiment in Remix using the JavaScript VM without needing to use the Ethereum Wallet and your code should work. At least it does when I've tested it.

To do this in remix select Run -> Environment -> Javascript VM. This is a temporary Ethereum blockchain that lives in your browser.

Then in the same tab click 'Create'. This deploys your Borrower contract to the temporary Ethereum blockchain.

Then change the 'Value' field to the amount you want to send when you call addFunds(). Consider changing 'wei' to 'ether' in the drop down. Then call addFunds() by clicking on the 'addFunds' button.

Then you can check the funds were updated correctly by calling the funds() function with the 'funds' button.

You can also use Remix to deploy contracts to live testnets or the main chain. You need MetaMask installed and unlocked and to select Run -> Environment -> Injected Web3.

Edit: Check the gif below, it all seems to work for me. Note that public variables in Solidity don't automatically create setters, they only create getters. I've also commented out the bits I don't have.

enter image description here

4
  • Thanks. I was able to run Borrower in Remix as you said both with Java VM and Injected Web3. However, I still have 2 problems, 1 is that I do not know how to add the variable borrowerName - it does not ask me for the name and I don't know how to send it. The second is when I try to run addFunds, I get the error "The constructor should be payable if you send value" but it is defined as payable. I would appreciate any help you could give me. – Philip Rutovitz Jan 25 '18 at 6:03
  • See the above edit and maybe add the code to the mortal contract to your original question. – willjgriff Jan 25 '18 at 9:52
  • Here is the mortal code: contract mortal{ address public owner; function mortal() public { owner = msg.sender; } modifier onlyOwner{ if(msg.sender == owner) { revert(); }else{ _; } } function kill() onlyOwner public { selfdestruct(owner); } } – Philip Rutovitz Jan 25 '18 at 18:45
  • Willjgriff, thanks for your GIF, I was able to get it to work by taking out the onlyOwner modifier. So that is where the problem is. Any ideas? – Philip Rutovitz Jan 25 '18 at 18:53
1

Ok, so thanks to all of you I now have a complete answer. The problem was actually with Mortal. I had "if(msg.sender == owner) { revert();}" instead of "if(msg.sender != owner) { revert();}"

Also, I did not realize that I needed to put the value for the Borrower Name in quotes. Now it works and the code looks like this:

pragma solidity ^0.4.18;

    contract mortal{

        address public owner;

        function mortal() public {

            owner = msg.sender;
        }

        modifier onlyOwner{

            if(msg.sender != owner) { 
                revert();
            }else{
                _;
            }
        }

        function kill() onlyOwner public {

            selfdestruct(owner);
        }
    }


contract Borrower is mortal {
    string  public borrowerName;
    uint256 public funds;

    function Borrower(string _name) payable public {
        borrowerName     = _name;
    }

    function addFunds() payable onlyOwner public {
        // Incoming funds
        funds += msg.value;
    }
} // Borrower

Thanks all!

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.