Nice solution, @Dragutin
For this to work, what changes did you also have to make to HelloWorld in terms of inheritance?
Nice solution, @Dragutin
For this to work, what changes did you also have to make to HelloWorld in terms of inheritance?
Nice solution @Obi
Also, take a look at this post â it explains how you can make an additional adjustment to HelloWorld in terms of the inheritance. I think youâll find it interesting
I made owner address payable.
Well corrected @Marlic
What changes did you also have to make to HelloWorld in terms of inheritance?
In fact, your first version using âselfdestruct(owner)
â also works as long as you make owner
payable in contract Ownable:
contract Ownable {
// change
address public owner;
// to
address payable public owner;
....
}
What changes did you also have to make to HelloWorld in terms of inheritance?
Hi Jon,
HelloWorld needs to inherit only Destroyable since Ownable is inherited by Destroyable.
import â./Ownable.solâ;
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function destroy() public onlyOwner {
address payable receiver = msg.sender;
selfdestruct(receiver);
}
}
pragma solidity 0.5.12;
contract Destroyable{
address public owner;
modifier onlyOwner(){
require(msg.sender==owner);
_;
// continue execution
}
constructor()public{
owner=msg.sender;
}
function close() public onlyOwner { //onlyOwner is custom modifier
selfdestruct(msg.sender); // `owner` is the owners address
}
}
````import"./Ownable.sol";
import"./Destroyable.sol";
pragma solidity 0.5.12;
contract HelloWorld is Ownable,Destroyable{...}
// Destroyable.sol
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function destroy() public onlyOwner {
address payable receiver = msg.sender;
selfdestruct(receiver);
}
}
// HelloWorld.sol
import "./Ownable.sol";
import "./Destroyable.sol";
pragma solidity 0.5.12;
contract HelloWorld is Ownable, Destroyable {
// ....
}
Hi @scornkrypto,
A well-coded Destroyable contract
What changes did you also have to make to HelloWorld in terms of inheritance?
Hi @Alexiaa,
The following code, which youâve included in your Destroyable function, would be better left in the separate Ownable function, because it is dealing with different functionality. You should also include the constructor, which you currently have commented out (although Iâm not sure why?..)
Destroyable would then inherit Ownable as follows:
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable { ... }
Also, take a look at this post â it explains how you can make an additional adjustment to HelloWorld in terms of the inheritance. I think youâll find it interesting
Nice solution @franzmoro
Also, take a look at this post â it explains how you can make an additional adjustment to HelloWorld in terms of the inheritance. I think youâll find it interesting
// created new contract named destroyable
contract Destroyable is Ownable {
function destroy() public onlyOwner {
address payable receiver = msg.sender;
selfdestruct(receiver);
}
}
//ADDED NEW CONTRACT TO PARENT
import './Ownable
import './destroyable
Hi @NLUGNER,
Your Destroyable contract is correct â you just need to also import the file Ownable.sol:
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable {...}
Iâm not really sure what you mean by this though. You should end up with multi-level inheritance where HelloWorld inherits Destroyable, and Destroyable inherits Ownable. So the other part of your solution should be:
import "./Destroyable.sol";
pragma solidity 0.5.12;
contract HelloWorld is Destroyable {
In HelloWorld you donât need to include code that explicity inherits Ownable. This is because Destroyable already inherits Ownable, and so by inheriting from Destroyable, HelloWorld also indirectly inherits Ownable via Destroyable.
First I created a new contract in a new file - Destroyable.sol
It inherits from ownable contract to use onlyowner functionality. It contains only one function - selfdestruct.
Self destruct will transfer all the contracts balance to a specified address. I had to cast the owner address into a payable address before it could send the funds.
import â./Ownable.solâ;
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function destruct() public onlyOwner{
selfdestruct(address(uint160(owner)));
}
}
Finally I imported Destroyable.sol into HelloWorld.sol and added it as a second base class for HelloWorld contract to inherit from.
import â./Ownable.solâ;
import â./Destroyable.solâ;
pragma solidity 0.5.12;
contract HelloWorld is Ownable, Destroyable {
âŚ
}
import"./Destroyable.sol";
pragma solidity 0.5.12:
contract HelloWorld is Destroyable{....
}
pragma solidity 0.5.12;
import './iot_ownable.sol';
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(owner);
}
}
Uses the ownable contract which is
pragma solidity 0.5.12;
contract Ownable {
address payable public owner;
modifier onlyOwner(){
require (msg.sender == owner);
_; }
constructor() public{
owner = msg.sender; }
}
I used the following hierarchy for inheritance:
Ownable => Destroyable => HelloWorld
Since the HelloWorld contract inherits all features and functions from Ownable via inheriting from Destroyable there is no need to import âOwnable.solâ or inherit Ownable directly in âHelloWorld.solâ.
Destroying the contract will result in a zero address for the owner, deletion of data of a person created and the balance will be 0. Furthermore the functions of the HelloWorld contract for example createPerson will no longer work.
âOwnable.solâ
pragma solidity 0.5.12;
contract Ownable{
address payable public owner;
modifier onlyOwner(){
require(msg.sender == owner);
_; //Continue execution
}
constructor() public{
owner = msg.sender;
}
}
âDestroyable.solâ
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable{
function close() public onlyOwner { //onlyOwner is custom modifier
selfdestruct(owner); // `owner` is the owners address
}
}
âHelloWorld.solâ (only the first few lines of code)
import "./Destroyable.sol";
pragma solidity 0.5.12;
contract HelloWorld is Destroyable {
struct Person {
string name;
uint age;
uint height;
bool senior;
}
event personCreated(string name, bool senior);
event personDeleted(string name, bool senior, address DeletedBy);
function getContractBalance() public view onlyOwner returns(uint256){
return address(this).balance;
}
function withdrawAll () public onlyOwner {
msg.sender.transfer(getContractBalance());
assert(getContractBalance()==0);
}
...
i canât really get it, how does it work when we do not use it in the main contract. How does it know which contract should be destructed?
Hi @acey,
Nice solution, and good analysis
Also, take a look at this post â it explains how you can make an additional adjustment to HelloWorld in terms of the inheritance. I think youâll find it interesting
How would this affect your diagram of the inheritance relationships between the contracts? What type of inheritance would we have?