Skip to content

How to use etch() to replace an already deployed contract with a version that makes all variables public and/or exposes additional public functions. #1298

Answered by aathan
aathan asked this question in Help
Discussion options

You must be logged in to vote

So in conclusion, what works is:

function testAllPublic() public {
        Vm vm = Vm(HEVM_ADDRESS);
        AllPublic pub = AllPublic(target);
        AllPublic newPub = new AllPublic('foo','bar',otherAddress,0);
	bytes memory replacementCode = address(newPub).code;
        vm.etch(target,replacementCode);
    }

but you'll generally want to make sure the constructor and/or initialization functions of the AllPublic contract are mostly no-ops since they probably interact with other on-chain contracts (such as dex routers etc) that will probably revert.

(By the way, it only makes sense to do this against a mainnet or testnet fork, e.g., via hardhat or truffle/ganache.)

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@onbjerg
Comment options

@aathan
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by aathan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants