-1

I have the following SC containing the protected selfdestruct:

// SPDX-License-Identifier: MIT
pragma solidity ^0.5.1;
contract Discontinue {
   address payable owner ;
   constructor () public {
      owner = msg . sender ;
   }
   function Terminate (address payable addr) public {
      if ( msg.sender == owner ){
         selfdestruct ( addr );
      }
   }
}

I think Remix should not warn on protected selfdestruct because selfdestruct is helpful to get rid of a useless and buggy SC. I have attached the image.remix(static analysis) warning on protected selfdestruct

Zulfi.

1 Answer 1

0

First of all your code has so many typos. In the constructor, there should be == not a single one and then its msg.sender not msg . sender. Your code should look like this:

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.1;
contract Discontinue {
   address payable owner ;

   constructor () {
      owner == msg.sender ;
   }

   function Terminate (address payable addr) public {
      if ( msg.sender == owner ){
         selfdestruct (addr);
      }
   }
}

Now, this compiled successfully for me. Another thing is, You are using a very old compiler which is full of bugs in Remix. I prefer the ^0.7.x version. Or If you want only ^0.5.1 I would advise you to code on VS Code, Maybe you will not get these errors there.

Hope this will help you :)

(Edit) Actually, if I try to compile this code with 0.5.1 It tells me to put contructors' visibility, when I put public with the constructor, it gives me another error saying, public should not be there. And one thing more if I comment out the constructor and add the owner hardcoded like this address payable owner = msg.sender ; it compiles successfully.


// SPDX-License-Identifier: MIT
pragma solidity ^0.5.1;
contract Discontinue {
   address payable owner = msg.sender ;

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

   function Terminate (address payable addr) public {
      if ( msg.sender == owner ){
         selfdestruct (addr);
      }
   }
}

This compiled successfully.

5
  • That worked bro or not? Lemme know pls :)
    – DevABDee
    Commented Jul 21, 2022 at 13:14
  • Welcome. Sorry why are you initializing outside the constructor, what's the purpose of a constructor?
    – zak100
    Commented Jul 22, 2022 at 2:52
  • As I said, If I compile the code with the constructor with ^0.7.x compiler. It complies successfully but, if I try to compile this code with 0.5.1 It tells me to put contructors' visibility, when I put public with the constructor, it gives me another error saying, public should not be there. And one thing more if I comment out the constructor and add the owner hardcoded like this address payable owner = msg.sender ; it compiles successfully.
    – DevABDee
    Commented Jul 22, 2022 at 3:58
  • So basically If you want the constructor you have to use ^0.7.x solidity but if you want to go with the ^0.5.x solidity, then for me it's creating some more errors. Try the above code with a different compiler yourself. AND also, I think your problem (which you initially posted) is solved. selfdestruct was giving you error because your code had some typos. I fixed it, and now selfdestruct is working. So you shouldmark this answer as correct, and if you are facing another issue then please open a new issue. Thanks :)
    – DevABDee
    Commented Jul 22, 2022 at 4:02
  • Sorry, my question was not about compilation.
    – zak100
    Commented Oct 3, 2022 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.