10

I'm having a hard time understand the difference between msg.owner and msg.sender. Can't find anything in the documentation that explicitly explains the difference. Suppose I create and deploy a contract. Will I be the 'owner'? Can these variables ever be the same?

This is confusing me because of line 118 in this contract. It looks like its refunding the sender the eth he sends but thats not what it does.

1
  • 2
    from where did you get this msg.owner?
    – Badr Bellaj
    Commented Jul 12, 2017 at 1:23

2 Answers 2

23

When you deploy the contract msg.sender is the owner of the contract. If you have a variable defined in your contract by the name of "owner", you can assign it with the value(address) of msg.sender.

address owner = msg.sender;

So now owner variable will always have the address of the person who initially deployed the contract, means the owner of the contract.

Now coming to line 118 of the contract you shared -

owner.transfer(msg.value);

Here, if the fallback function is called then msg.value would be transferred at owner's address.

In your contract, "owner" variable is defined at line 9.

Also, there is nothing like msg.owner in solidity as per documentation.

Hope this helps.

3
  • 1
    If the owner is the person who deployed the contract then how do you access the person that actually sent a tx to the contract? In line 142 it says that the balances of the sender is increased, doesn't that mean the balance of the owner?
    – ninesalt
    Commented Jul 12, 2017 at 11:36
  • 1
    I'm not in front of a computer at the moment so can't check the code but you can access the person's address who sent the transaction by msg.sender
    – Sanchit
    Commented Jul 12, 2017 at 11:51
  • 2
    msg.sender is the one who is sending transaction, it can be the owner or can be some other user well. It's just that for reference we save the value of msg.sender in owner variable for the first time when the contract is deployed. Here in line 142, the value is being transferred to msg.sender's account, be it the owner or any other user.
    – Sanchit
    Commented Jul 12, 2017 at 12:11
13

A contract's msg.sender is the address currently interacting with the contract. Be it a human or another contract.

So if a human is interacting with a contract, msg.sender is the address of the person. And if another contract (B) is interacting with the contract, the contract (B)'s address becomes msg.sender.

The owner of a contract is the address that deployed the contract to the blockchain, that is, the first msg.sender to interact with the contract. And the fact of that address being the one that deployed the contract will never change, meaning the owner will remain the owner forever except a change occurs via a valid contract function, for example transferOwnership(newOwner).
The owner address is not called msg.owner but it can be saved in a public variable that other contracts can read in the future.

1
  • By the first paragraph and the third one we can conclude that the owner of the smart contract is the one who interacts with him. This means that the owner is not the one who programmed it but anyone who sends data via it. Commented May 1, 2018 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.