Modifiers in solidity

0 votes

I'm looking to create a modifier named onlyOwner and assign it to changePrice function. I must make sure the modifier allows function to be executed only if the sender's address matches the address of the owner. The sender's address can be obtained using msg.sender.

I have tried entering this to create the modifier but its not working for me and I'm not sure why. Any help/recommended code would be greatly appreciated!

pragma solidity ^0.4.17;

contract ModifiersTutorial {

address public owner;
uint256 public price = 0;
address public sender=msg.sender;

//
modifier onlyOwner(sender){
if (owner==sender);
}
//

// Use your modifier on the function below
function changePrice(uint256 _price) public onlyOwner {
    price = _price;
}

function ModifiersTutorial () {
    owner = msg.sender; // msg.sender in constructor equals to the address that created the contract
}

}
Oct 1, 2018 in Blockchain by digger
• 26,550 points
59 views

2 answers to this question.

0 votes

Your modifier code is incorrect. You need an underscore to proceed.

modifier onlyOwner(sender){
  if (owner==sender) _; // Note the underscore
}

Also, for security reasons, you really should just use msg.sender instead of passing it in.

modifier onlyOwner() {
  if (owner == msg.sender) _;
}
answered Oct 1, 2018 by slayer
• 29,050 points
0 votes
pragma solidity ^0.4.17;

contract ModifiersTutorial {

    address public owner;
    uint256 public price = 0;

    modifier onlyOwner(){
    if( owner == msg.sender ) _;
    }

    function changePrice(uint256 _price) public onlyOwner{
        price = _price;
    }

    function ModifiersTutorial () {
        owner = msg.sender; 
    }
}
answered Oct 12, 2018 by Pulkit

Related Questions In Blockchain

+1 vote
1 answer

What is the difference between if() and require() statement in solidity??

If() and require() have separate functions and ...READ MORE

answered Apr 18, 2018 in Blockchain by Shashank
• 10,350 points
377 views
+1 vote
1 answer

what is use of msg.sender in solidity code?

msg.sender(address) function indicated the sender of the ...READ MORE

answered Apr 24, 2018 in Blockchain by Shashank
• 10,350 points
403 views
+1 vote
2 answers

How to convert INT to STRING in Solidity?

Look at the following code : function uintToString(uint ...READ MORE

answered Jun 27, 2018 in Blockchain by Christine
• 15,790 points
963 views
0 votes
1 answer

Should I require a specific condition when working with indexes in Solidity?

You are using it correctly. require is intended to ...READ MORE

answered Jul 17, 2018 in Blockchain by ariaholic
• 7,340 points
28 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
140 views
0 votes
1 answer
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 67,480 points
77 views
0 votes
1 answer

Invalid opcode error in a simple Solidity contract and script

You don't need to call .at() if you're using .new(). ...READ MORE

answered Sep 14, 2018 in Blockchain by slayer
• 29,050 points
171 views