-
I need to spelunk the storage and behavior of an existing deployed contract. I know about ForgeStd but accessing its storage via those functions is cumbersome, and it occurred to me that an approach I could take would be to deploy new contract code to the old contract address, where I've made everything public and/or added additional utility/testing functions to explore the old contract's storage variables and information model. I'm having trouble with that. I'm probably forgetting something fundamental about the way contract code is stored on-chain, and/or there are some clarifications that could be made to the cheatcode documentation. For example, I know the bytecode stored on-chain is what results from calling a contract's constructor, and that the constructor code itself is not part of the deployed bytecode of a contract. Therefore, it is not exactly clear how to go from getCode to etch to call of the prior contract address while having access to new functions. Obviously the replacing contract has exactly the same storage variable declarations in exactly the same order. Neither version of the replacementCode works. The test reverts calling the pub.name() function (the ERC20 one) even though it's declared and implemented the same way in both the original contract code and the AllPublic replacement code.
test code:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I checked the signature Ok, that was the problem. The constructor of the new contract was reverting. I can also report that If you could reinforce my understanding as to why that is, that would be awesome. |
Beta Was this translation helpful? Give feedback.
-
So in conclusion, what works is:
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.) |
Beta Was this translation helpful? Give feedback.
So in conclusion, what works is:
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.)