1

I have contract with modifier onlyOwner and below 2 functions that must be implemented by child contract like:

function getBalance(address _address) public onlyOwner returns (uint256);
function setBalance(address _address, uint256 _amount) public onlyOwner;

First like is treated as correct but second one breaks compilation. I do not want this function returns anything meaningful. How to fix it?

Truffle v3.4.11 (core: 3.4.11)
Solidity v0.4.15 (solc-js)

Error:

browser/test.sol:21:86: ParserError: Expected type name
        function setBalance(address _address, uint256 _amount) public onlyOwner;

                                                                      ^
5
  • What is breaking the compilation and what is the error you received upon compilation ?
    – Rangesh
    Nov 13 '17 at 5:36
  • @Rangesh incorrect syntax.
    – Alex G.P.
    Nov 13 '17 at 6:03
  • It seems to be a bug, but modifiers should not be allowed in declarations only when you are defining the function's body.
    – Ismael
    Nov 13 '17 at 9:12
  • @Ismael sounds strange. For instance, I have modifier in partially implemented base class and want all function it declares (and children implements) be protected with exactly this modifier. Modifier specification shows it explicitly and force user to implement function with exactly this modifier.
    – Alex G.P.
    Nov 13 '17 at 10:54
  • @AlexG.P. Do you have a link to the documentation where it is used like you say? I understand that a modifier is a way to append/prepend code to the function body, so if the function has no body then modifiers are superfluous.
    – Ismael
    Nov 15 '17 at 4:44
1

This looks like it is a bug - re-ordering public and onlyOwner of the setBalance function definition in an abstract contract results in the code either compiling or failing to compile:

The following compiles (in Remix):


pragma solidity ^0.4.18;

contract Parent1 {
    address public owner;
    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    function getBalance(address _address) public onlyOwner returns (uint256);
    function setBalance(address _address, uint256 _amount) onlyOwner public;
}

Whereas the following does not compile:


pragma solidity ^0.4.18;

contract Parent2 {
    address public owner;
    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    function getBalance(address _address) public onlyOwner returns (uint256);
    function setBalance(address _address, uint256 _amount) public onlyOwner;
}

Only a slightly different, but relateed note, I agree with @Ismael; that what you are trying to do might not be the best approach / appropriate.

For instance, I'm not sure in which inheritance model you would define the logic for the onlyOwner modifier for a set of functions and not the implementation.

More simply put, you should perhaps think about why the abstract, parent contract (that the child contracts are inheriting from) can't just hold the balance variable provide the implementation for getter and setter functions.

1
  • It is easy: for instance, I have 2 contracts, one is pemanent that stores users balances and must be accessible only for owner and another contract called "facade". Facade cotnract contains business logic and can be deleted and re-created (to fix errors or implement new features) and it knows address of permanent one. I suppose it is clear why some contract must be protected very well.
    – Alex G.P.
    Nov 13 '17 at 17:31

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.