1

I defined a modifier in my contract Auction

modifier onlyOwner() {                                 //Part 2 Task 1
    require (msg.sender == beneficiary);
    _;
}

This modifier should be used in the revealWinners function instead of public. But it's not working and I don't know why.

function revealWinners() onlyOwner returns {                       //Part 2 Task 1
     for (uint id = 0; id < 3; id++) { 
         Item storage currentItem=items[id]; 
         if (currentItem.itemTokens.length != 0){
             uint randomIndex = (block.number / currentItem.itemTokens.length)% currentItem.itemTokens.length; 
             uint winnerId = currentItem.itemTokens[randomIndex];
             winners[id] = winners[winnerId];               //Part 1 Task 6 – 1 line
        }
    }
}

I got the message: Expected token LParen got 'LBrace' (behind returns). Who can assist me?

1

Simply you omitted to specify the return type, if any.

If you do not use it, omit ‘returns’.

function revealWinners() public onlyOwner { 
...

Accordingly to your further comments, you can write the function as above remaining sure that nobody different from the ‘owner’ shall be able to call it.

‘public’ (like ‘internal’, ‘external’, view’, ‘pure’, etc.) is a general attribute of a function, onlyOwner is a ‘modifier’ that is a way to prepend to the function some special code (in your case: ‘require (msg.sender==owner)’.

You can have function being public but using one or more modifier and the behavior shall depends on the specific combination.

In the case you simply want that only owner shall be to call it, declaring it ‘public onlyOwner’ is fine.

  • Compiler provide another message: No visibility specified. Defaulting to public. But public I don't want. I had this before. Only one, the "onlyOwner" should be able to submit the function. – D. Nerge Dec 13 '18 at 14:28
  • Public do not means nothing different that “possibly callable by everyone”. But if someone different from owner shall call it, it will revert immediately because of onlyowner modifier. Leave it public without any worrying. – Rick Park Dec 13 '18 at 14:33
  • Please close the question giving it answered and make different questions for different problems. – Rick Park Dec 13 '18 at 14:34
  • Added a specific explanation in the answer. Have a good work! – Rick Park Dec 13 '18 at 14:48

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.