Great!
Asâcontract Destroyableâinheritsâcontract Ownable ,âyou can also streamline things even more by removing the code fromâcontract HelloWorldâthat explicity inheritsâcontract Ownable. This is becauseâHelloWorldâalready inherits fromâDestroyable ,âand so now indirectly inheritsâOwnableâviaâDestroyable .
What Iâm trying to say is that inâcontract HelloWorldâyou can just have:
import "./Destroyable.sol";
pragma solidity 0.5.12;
contract HelloWorld is Destroyable {...}
In case you didnât know, before you type in your code to post it here in the forum, click on the </> icon in the menu at the top of the text editor. You will see 2 sets of 3 back ticks.
```
```
If you now input your code between these, you will end up with nicely formatted code, which is then also easier for you to organise with the correct spacing and indentation etc. You should end up with something like:
import "./Inheritance.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(msg.sender);
}
}
By the way, what amendments did you make to âcontract HelloWorldâ in terms of its inheritance of âcontract Destroyable ?
Also, if you amendâ contract Destroyable âso that it inherits from âcontract Ownable ,âyou wonât need to repeat the owner variable, the modifier or the constructor, as these will be inherited from Ownable. However, you would then need to make an amendment to EITHER your destroy function, OR the owner variable in contract Ownable. See if you can work out what it would be and whyâŚ
Thank you.
I did not work with the original HelloWorld contract that was used in the videos.
I just created a generic contract that I could use with other contracts that I had in remix and that didnât have the Ownable function.
But youâre right, if I wanted to do a real separation of concerns here, the right thing to do would be to have the constructor and modifier within a contract Ownable.sol, and then just the call to selfDestruct () in the contract Destructible.sol.
import "./Ownable.sol";
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(owner);
}
}
HelloWorld.sol
import "./Destroyable.sol";
contract HelloWorld is Destroyable {
[...]
I made Destroyable inherit from Ownable, because to be Destroyable, a contract needs some safety so that only the owner can destroy it. So in the end if you want a contract that is Destroyable, you also get the âOwnableâ properties and functions.
I also had to fix some error because âownerâ had to be a payable address so:
Hi @filip (hello to anybody else too, who wants to answer )
Iâve seen in your solution:
import "./Ownable.sol";
import "./Destroyable.sol";
contract HelloWorld is Ownable, Destroyable {
in my solution I did simply:
import "./Destroyable.sol";
contract HelloWorld is Destroyable {
In HelloWorld I didnât import Ownable, because that is imported already in Destroyable. Also I didnât specify is Ownable, because Destroyable is already ownable.
Why did you do that redundancy? Do you agree that itâs not necessary? I tested my solution and of course it works, so I assume itâs ok
And I like the idea of the additional setNewOwner Function
You should removeâreturns(address)âandâreturn ownerâbecause this function is a setter not a getter, and these additional pieces of code donât actually do anything anyway. Contract Ownable automatically generates a getter for owner which is a state variable and has public visibility, and so is inherited by Hello World, and so this enables us to get the ownerâs address from there.
Have a go at making these changes and youâll see what I mean.
You are exactly right in your observation. There is actually some discussion about this a bit further up in this threadâŚ
I think maybe in the solution code, contract HelloWorld was left inheriting Ownable (as well as Destroyable) from the previous lecture. It isnât an error, but your inheritance composition is the more efficient, and everyone agrees with that.