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,740 points
1,787 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,350 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

0 votes
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,400 points
4,795 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 25, 2018 in Blockchain by Shashank
• 10,400 points
8,533 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
11,705 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 aryya
• 7,450 points
381 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
1,706 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,237 views
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
• 69,210 points
1,254 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,350 points
4,294 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP