0

I am a japanese student. I am studying ethereum.

pragma solidity ^ 0.4.18;
contract Owned {
    address public owner; // contract owner

    // / modifier for access check
    modifier onlyOwner () {
        require (msg.sender == owner);
        _;
    }

    /// Set owner
    function owned () public {
        owner = msg.sender;
    }

    /// Change owner
    function changeOwner (address _newOwner) public onlyOwner {
        owner = _newOwner;
    }
}

contract Mortal is Owned {
        // / Function to destroy contract
    function kill () public onlyOwner {
        selfdestruct (owner);
    }
}

contract MortalSample is Mortal {
    string public someState;

    function () public payable {
    }

    function MotalSample () public {
        owned;

        someState = "initial";
    }
}

I made it as described above, but Owner's name is 0x0000 ... and someState is not even displayed. I am in trouble because I do not know what is wrong. I would appreciate it if someone tells me.

  • I am not good at English.so I will use the translation of google. – user50160 Jan 24 at 7:37
  • I made a contract as above.but If you look at the contract screen, the contract owner will not be displayed correctly at the owner's point. And somestate is not displayed even. – user50160 Jan 24 at 7:42
  • I'm in trouble. What should I do? – user50160 Jan 24 at 7:43
  • Please edit your question to include this information. I can't help you myself, but I can provide guidance on how to get help from people here. – Glorfindel Jan 24 at 7:43
  • Something changed into English automatically, but did you do it? – user50160 Jan 24 at 7:55
1

I think you are using Remix in order to display the contract creation and deployment to your students. If not, please do!

http://remix.ethereum.org

You shall be able to have evidence of some mistyped keywords.

Said that, your variable owner is declared, but it is not initialized. That is it contains the default value, i.e. “0”.

You need to assign to it the proper value.

For instance you can call once the function “owned” you declared. Using remix this means to click once on the button related to the function itself.

After a while in remix, you should be able to have more control on your code and to discover other peculiarities.

Just an advice: do not start with a so “heavy” use of inheritance. Use a simple contract to start and, after that, add new stuff progressively.

Have a good work!

(PS: see here Is a constructor needed to set a contract owner? for infos on standard way to perform the owner setting)

0

Initially, your owner is 0x0000000.. because you have not instantiated it, but once you call owned from one of the accounts, you can see the result in owner variable is updated. And further you can only call changeOwner from the owner account. If you want it to be done from the very beginning, that is, as soon as the contract is deployed, you can use a constructor that way when you deploy your contract, the account used to deploy can be set as owner.

  • Do I need to changeowner? I chageowner from select function, but it did not change. – user50160 Jan 24 at 8:12
0

Your code have a bug.

In solc v0.4.xx the constructor is a function with the same name than the contract. In your case it should be Owned but is is named owned so the constructor will never initialize variable owner.

It is better if you explicitely use construtor insted:

contract Owned {
    address public owner; // contract owner

    // ...

    /// Set owner
    constructor() public {
        owner = msg.sender;
    }

    // ..
}

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.