3

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.

2 Answers 2

4

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.

5
  • 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, 2019 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. Aug 14, 2019 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. Aug 14, 2019 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, 2019 at 14:51
  • Could you elaborate your answer with examples? Thank you.
    – anonymous
    Jun 15, 2021 at 4:23
0

Using modern web3.js

const bytecode = await web3.eth.getCode(contract)
const creationCode = new web3.eth.Contract([]).deploy({ data: bytecode })._deployData

Your Answer

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

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