1

For example I want to redeploy/clone a contract to another address but don't have the original source code, so I only have the runtime bytecode stored on the blockchain. If I try to deploy the runtime bytecode directly, it will result in a failure. So is there any way to construct valid deployable bytecode from runtime bytecode, so that the exact same contract code can be deployed/cloned to another address?

I'm not looking to deploy a forwarder proxy, I know how to do that in both solidity assembly and vyper, what I need is to deploy the exact same runtime bytecode to a new address without the original source code. Thanks.

3

Sure. To deploy smart contract one needs to publish a transaction with empty to address and with data containing contract initialization code concatenated with values of constructor parameters. Initialization code usually consists of constructor byte code and contract's byte code to be deployed. So, deployed byte code does not contain constructor and constructor parameters, that's why you cannot use it to redeploy the contract.

Hopefully, blockchain stores not only deployed byte code of the smart contract, but also the original transaction used to deploy it (I assume that contract was deployed directly from externally owned address, rather that by another smart contract). So what you need to do is:

  1. Find transaction used to deploy smart contract you want to redeploy
  2. Extract data from it
  3. Change values of constructor parameters in the tail of the extracted data
  4. Deploy resulting byte code

Let me know if you need more details.

  • that's not what I'm trying to accomplish, since your method requires finding the exact transaction by which the contract is created, and AFAIK that information is not stored on the contract address, which means one needs to depend on some third-party block explorer like Etherscan.io or indexing the blockchain from the first block one by one until the transaction can be found. Also I'm not looking to change the constructor arguments, just redeploying the exact same runtime bytecode, so I'm wondering if I can construct some deployable bytecode from the runtime bytecode stored at certain address. – hellopeach Aug 13 at 8:38
  • Function getCode of Web3js accepts optional block number parameter, so you may use binary search to quickly find what block the contract was deployed at. Then you will need to scan all transactions in this block to find out what transaction deployed the smart contract. – Mikhail Vladimirov Aug 14 at 11:54
  • You may convert deployed byte code into initialization code, and thus may redeploy the same code. Though, such redeployed smart contract may behave differently from the original one. This is because n general, smart contract behavior depends not only on its byte code, but also on storage content. Constructor of original smart contract might populate storage with some initial data, which redeployed contract will miss. – Mikhail Vladimirov Aug 14 at 11:58
  • @ Mikhail Vladimirov, so how to convert deployed bytecode into initialization code? That's exactly I'm looking to do... – hellopeach Aug 14 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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