AnswerBun.com

Using "owner = msg.sender" in a non-constructor function or in a declaration statement?

Will it be a problem to use :

 owner = msg.sender;

statement in a non-constructor function?

What if instead of writing the above code in a function, we initialize the owner at the time of declaration like this in a state variable:

address private owner = msg.sender;

Some body please guide me.

Zulfi.

Ethereum Asked by zak100 on January 2, 2021

3 Answers

3 Answers

Based on the clarification in the comments to smarx's answer.

It depends what you mean by "problem". Consider this function.

function changeOwner() public {
  owner = msg.sender;
}

That allows anyone to seize the owner role, assuming owner is pivotal to access control somewhere else. That's not a problem in the sense that it works. That is a problem in the sense that anyone can do it and that's usually not desirable.

Consider this variant:

function changeOwner(address newOwner) public {
  require(msg.sender = owner);
  owner = newOwner;
}

The require() strictly limits this to the owner, so the sender has to be the owner in order to relinquish ownership. It doesn't make sense to relinquish to itself, so another address (the newOwner) is passed in. A production-ready implementation of this common pattern would check that the new address is valid to prevent accidents.

It may be helpful to remember that owner is not special in any way. It's merely a variable of type address given meaning by other aspects of the contract such as conditionals that oversee permission.

Hope it helps.

Answered by Rob Hitchens on January 2, 2021

owner = msg.sender; statement in a non-constructor function?

If you write owner = msg.sender; in a non-constructor function then 1) You have to pay more gas amount(approx 1.5x more) as compared to we deploy this statement using the constructor.

2) You need to call a function to set owner = msg.sender when you are using the non-constructor function.

What if instead of writing the above code in a function, we initialize the owner at the time of declaration like this in a state variable:

There is no problem with initializing owner at the time of declaration but it changes the meaning of state variable for solidity compiler. let me explain you:

Private:

When you declare owner as a private state variable like this address private owner = msg.sender; then solidity compiler treat owner variable as variable and for getting this variable value you need to define another function like getOwnerAddress().

Public:

If you set owner as public like this address public owner = msg.sender; then solidity compiler treat owner variable as functionowner() and this will also return the address value of owner there is no need to define a separate function for getting value.

For more understanding, you can see a difference in abi of contract details.

Answered by Mahesh Rajput on January 2, 2021

Statements aren't allowed outside of functions, but initializations are.

So your second snippet (address private owner = msg.sender;) is good.

Answered by user19510 on January 2, 2021

Add your own answers!

Related Questions

Nethereum C# FilterInput.GetAllChanges always empty

1  Asked on February 12, 2021 by exaltedoil

       

Testing Gas Token on testnet failing transaction

0  Asked on February 9, 2021 by marr

   

I am unable to install web3 on my mac

1  Asked on February 5, 2021 by saiteja-kuruva

 

Out of stack DUP2 1/1024 when adding string to parameter

0  Asked on February 1, 2021 by koukotsu

   

How to find contract’s address?

6  Asked on January 31, 2021 by user3280964

   

Import error in Truffle Pet Shop tutorial

1  Asked on January 29, 2021 by xpain

   

Timeout w/ Mocha on deploying a contract

2  Asked on January 26, 2021 by tulun

   

How to send ERC20 token to smart contract in constructor

1  Asked on January 24, 2021 by user938363

 

Deploy bytecode

1  Asked on January 19, 2021 by william-entriken

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.