0

This question already has an answer here:

enter image description here

Please explain how can i use delegatecall or callcode. Thanks !

marked as duplicate by Ismael, Roman Frolov, flygoing, Achala Dissanayake, MSwezey Feb 21 '18 at 8:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

You might be asking this because your goal is to delete a smart contract that didn't initially have a call to selfdestruct(). If this is the case, you can't achieve your goal.

The note says that a contract can still execute selfdestruct() if delegatecall or callcode to another contract is executed. That is to say, another contract is already referenced from your contract, and that contract can call the method, even though it hasn't been referenced in the original contract.

You can't just delete any arbitrary contract on the network once it's been published, i.e., after deployment, without already designing this in. If anyone could do that, it would undermine the basis for all smart contract behavior. First, if a non-owner could call a non-existent selfdestruct(), they could remove the contract. Obviously, this isn't possible, but still a thought exercise. Second, if an owner could arbitrarily blow up any contract and recover funds, then the owner could effectively "revoke" all smart contracts that they entered into, making them null and void.

Let's say you had a contract that held community funds and decided where those funds were based on some internal logic. When you notice that the funds will probably go to someone you don't like, you can't just choose to blow up the contract because you made it. Everyone agreed on the internal logic of the smart contract, and that logic never gave you an "out" that you could use. Just like a real life contract, you can't terminate the contract without a termination clause built in.

1

You have to delegate the call to another contract (hence the name delegatecall()).

The following would allow B to self-destruct by calling into A:

pragma solidity ^0.4.2;        
contract A {
    address owner;
    function A() {
        owner = msg.sender;
    }
    function destroy() {
        selfdestruct(owner);
    }
}

contract B{ 
    function call(address a) {
        a.delegatecall(bytes4(sha3("destroy()"))); 
    }
}
0

By simply calling selfdestruct(<address>) in contract.

function destroyContract() public onlyOwner{
        selfdestruct(owner);
}

But be careful selfdestruct() method.

Check below link

How does the delegatecall method work to call to another contract's method?

  • Where is your call to delegatecall()...? :-) – Richard Horrocks Feb 16 '18 at 7:39

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