Excellent analysis, @dAnijboned!
Hi @Thaddeus19,
Interesting choice of name â count
â for the local variable which temporarily holds the ownerâs address as a payable address. What was your reason for chosing this name? Do you think it will be clear to other developers what itâs purpose is?
By the way, what amendments did you make to contract HelloWorld
in terms of its inheritance?
Nice analysis, @zzzsoren
Also, check this post out â it explains how you can make an additional adjustment in terms of the inheritance. I think youâll find it interesting.
Nice solution, @ZAR
By the way, what amendments did you make to contract HelloWorld
in terms of its inheritance?
Great analysis, @rbrownlow
So, just to confirm, Iâve understood that your HelloWorld
contract now startsâŚ
âŚand youâve removed the code that explicity inherits contract Ownable
, because HelloWorld
now indirectly inherits Ownable
via Destroyable
. Is that right?
Yes sir! That is exactly what I did. The code works the exact same for me. I tried it both ways!
Yes, it will⌠but your solution is the more efficient one. It may well save on some gas as well (due to less code) but Iâd need to check that⌠or maybe you could?
Thank you
So the HelloWorld contract implicitly inherits Ownable contract, by inheriting the Destroyable contract which inherits the Ownable contract.
import "./ownable.sol";
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(owner);
}
}
Hi @Jamie_Frame,
Your solution is correct, but it involves making an additional change to contract Ownable. Do you know what it is and why?
And what amendments did you make to contract HelloWorld
in terms of its inheritance?
Hi Jon,
Owner needs to be a payable address so that any funds associated with the detroyed contract can be sent to the owner address. HelloWorld needs to inherit from the Ownable and Destroyable contracts.
contract HelloWorld is Ownable, Destroyable{...}
Created new contract Destoyable.sol:
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(msg.sender);
}
}
I gave up in the end but I think it almost worked.
-----destroyable.sol--------
import â./2_Owner.solâ; -> built in owner.sol provided by remix.
pragma solidity >=0.4.22 <0.7.0;
contract Destroyable is Owner
{
function Destroy() public isOwner()
{
selfdestruct(msg.sender);
}
}
-----helloworld.sol--------
import â./2_Owner.solâ; -> built in owner.sol provided by remix.
import â./destroyable.solâ;
contract HelloWorld is Owner, Destroyable
âŚ
function Kill() public isOwner
{
Destroy();//inherited from destroyable contract
}
contract Ownable{
// this contract contains the âonlyOwnerâ modifier and the instantiated âownerâ variable
}
âŚ
new file
âŚ
import â./Ownable.solâ;
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(owner);
}
}
âŚ
new file
âŚ
import â./Ownable.solâ; // Is this needed, given that Destroyable is Ownable?
import â./Destroyable.solâ;
contract HelloWorld is Destroyable {
}
Hi @Jamie_Frame,
Yes
⌠also, check this post out â it explains how you can make an additional adjustment in terms of the inheritance. I think youâll find it interesting.
Hi @sahowe1,
What was your reason for using the Owner contract provided by Remix? You should use the one that we built during the lesson:
You could use the template provided by Remix, but you would have to make some modifications. This may be one of the reasons why you couldnât get yours to work.
Also, there is no need to add a function to HelloWorld which calls your Destroy function in Destroyable. This is duplication, as the Destroy function can already be called from Remix due to it being inherited and its public visibility. If you remove this additional function youâve added in HelloWorld, and then deploy HelloWorld, youâll see that this is the case. This is inheritance in action.
Yes I was getting a type cast error for address - I think one was payable and the other not. Thanks for the comments - all taken on boardâŚ
Hi @FrankB,
You are absolutely rightâŚwe no longer need to import Ownable.sol.
This is also correct.
As Destroyable inherits Ownable we can remove the code from HelloWorld that explicity inherits Ownable. This is because HelloWorld now inherits from Destroyable, and so also indirectly inherits Ownable via Destroyable.
Wow!..thatâs a mouthfull
Just one other observationâŚ
This solution is correct, but it involves making an additional change to contract Ownable. Do you know what it is and why? We donât need to make any changes to Ownable if we useâmsg.sender
âinstead ofâowner
,âbut usingâowner
âis perfectly valid, as long as Ownable is modified accordingly.
In the Owner contract provided by Remix the address owner
has private visibility, whereas we want to have public (or at least internal) visibility. It is also a non-payable address.
By using msg.sender
instead of owner
in your Destroy function in Destroyable, you should have avoided any problems associated with this and that shouldnât have thrown an error. But I imagine the error you got was in HelloWorld, because there we need access to owner
(which we wonât have if itâs marked private in Owner).
I hope that makes sense, and fits in with what youâve been getting.