pragma solidity 0.7.5;
contract Destroyable{
address payable owner;
constructor(){
owner = msg.sender;
}
modifier onlyOwner(){
require(msg.sender == owner);
_;
}
function destroy() internal onlyOwner{
selfdestruct(owner);
}
}
Hey @William
Seems like you have imported Destroyable
both in BankContract
and Ownable
.
Because BankContract
imports Ownable
it is like you are importing Destroyable
twice.
If Ownable
imports Destroyable
, your bank should only import Ownable
.
Cheers,
Dani
//SPDX-License-Identifier: GPL-3.0
//this is file destroyable.sol
pragma solidity ^0.7.5;
contract destroyable{
address payable owner;
constructor(){
owner = msg.sender;
}
modifier only_Owner{
require(msg.sender == owner);
_;
}
function destroy_contract() internal only_owner{
selfdestruct(owner);
}
}
//------------------------------------------
//SPDX-License-Identifier: GPL-3.0
//file inherits_destroyable.sol
pragma solidity ^0.7.5;
import “./destroyable.sol”;
contract waiting_to_be_destroyed is destroyable{
function summon_the_end() public{ //can this function return anything if the result will be destruction?
destroy_contract();
}
}
pragma solidity 0.7.5;
contract Ownable {
address public owner;
modifier onlyOwner {
require(msg.sender == owner);
_; // means run the function
}
constructor (){
owner = msg.sender;
}
}
pragma solidity 0.7.5;
import “./Ownable.sol”;
contract Destroyable is Ownable{
function close() public onlyOwner { //onlyOwner is custom modifier
selfdestruct(msg.sender); // `owner` is the owners address
}
}
Im a bit confused with the solution of the Destroyable.
in this function:
function destroy() public onlyOwner {
address payable receiver = msg.sender;
selfdestruct(receiver);
}
why we cant use the state variable owner declared in te contract Ownable?
Hey @zero0_cero
If your contract imports Ownable and if the variable owner
is public or internal you can use it in your function destroy. Keep in mind that selfdestruct
wants a payable address as parameter.
Cheers,
Dani
thats what I was suposed but when using the owner variable in the selfdestruct, I get an error
“Invalid type for argument in function call. Invalid implicit conversion from address to address payable requested”
this is my code:
pragma solidity 0.7.5;
contract Ownable {
address internal owner;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
constructor (){
owner = msg.sender;
}
}
pragma solidity 0.7.5;
import "./ownable.sol";
import "./Destructor.sol";
contract Destroyable is Ownable,Destructor {
uint value;
function setValue(uint _value) public {
value = _value;
}
function getValue() public view returns(uint){
return value;
}
}
pragma solidity 0.7.5;
import "./ownable.sol";
contract Destructor is Ownable {
function wipeOut() public onlyOwner {
//address payable _owner = msg.sender;
selfdestruct(owner);
}
}
it is something wrong there?
Hey @zero0_cero
The issue is that you are not passing a payable address to selfdistruct
.
function wipeOut() public onlyOwner {
//address payable _owner = msg.sender;
selfdestruct(owner);
}
You can:
selfdestruct(payable(msg.sender));
or
address payable _owner = msg.sender;
selfdestruct(_owner);
Cheers,
Dani
Im doing it in the constructor in the contract Ownable.
constructor (){
owner = msg.sender;
}
Here the state variable is equal to msg.sender…
I have some missunderstanding i guess.
Why this doesnt work?
Whatever variable you are using, you just need to cast it to payable.
If you want to use owner
do payable(owner)
when you call selfdestruct
and will work
ok.
So I have to cast it again to payable, right?
pragma solidity 0.7.5;
contract Destroyable {
address payable internal owner;
constructor(){
owner = msg.sender;
}
modifier onlyOwner{
require(msg.sender == owner);
_;
}
function nukeItBaby() public onlyOwner {
selfdestruct(owner);
}
}
@filip I wrote the code before looking at the pushed solution, thus I put everything in the Destroyable contract. I would like to ask a couple of things:
- Does it makes sense to user the
internal
modifier for the owner variable? - I tried to invoke the function in Remix after deploying, but the contract was not deleted, like pressing the trash bin button. Is there something wrong with the implementation or is this behavior normal?
Thanks!
pragma solidity 0.7.5;
contract Ownable {
address public owner;
modifier onlyOwner(){
require(msg.sender == owner);
_;
}
constructor() public {
owner = msg.sender;
}
}
contract Destroyable is Ownable {
function destroy() public onlyOwner {
selfdestruct(owner);
}
}
This is my Destroyable Contract:
import "./Ownable.sol";
contract Destroyable is Ownable {
function endIt() public onlyOwner {
address payable owner;
owner = msg.sender;
selfdestruct(owner);
}
}
This is how I implemented it into the Bank Contract:
import "./Ownable.sol";
import "./Destroyable.sol";
contract Bank is Ownable, Destroyable {
I just realized it would have been easier to implement this function in the same contract as Ownable
Destroyable.sol
pragma solidity 0.7.5;
import "./Ownable.sol";
contract Destroyable is Ownable{
function removeContract() public onlyOwner {
selfdestruct(owner);
}
}
Bank.sol
pragma solidity 0.7.5;
import "./Destroyable.sol";
import "./Ownable.sol";
contract Bank is Ownable, Destroyable{
Hi @karips
- Does it makes sense to user the
internal
modifier for the owner variable?
Modifiers do not require to set visibility.
I tried to invoke the function in Remix after deploying, but the contract was not deleted, like pressing the trash bin button. Is there something wrong with the implementation or is this behavior normal?
selftdistruct() does not delete the contract therefore that is the normal behaviour.
Cheers,
Dani
I basically copied the close()
function in the blog post, and extended the Ownable
contract to inherit the onlyOwner
modifier.
pragma solidity 0.7.5;
import "./Ownable.sol";
contract Destroyable is Ownable {
function destroy() public onlyOwner {
selfdestruct(owner);
}
}