2

Is there a way to deploy pure bytecode with hardhat or remix?

Thanks

4 Answers 4

3
+50

Yes, this is possible.

Given that you have the bytecode and the ABI, you can create a script like this at scripts/deploy.js:

const hre = require("hardhat");
const fs = require("fs")
const path = require("path")

async function main() {
  
  var abi = require('/path/to/your/abi.json');
  var bytecode = require('/path/to/your/bytecode.json');

  const Contract = await hre.ethers.getContractFactory(abi, bytecode);
  const deployed = await Contract.deploy("test string");

  await deployed.deployed();

  console.log(
    `🚢 done. Contract deployed to ${deployed.address}!`
  );
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Where your abi.json contains the ABI (standard JSON format). The bytecode.json is in the following form from using solc (under say another script ./compile.sh):

const output = JSON.parse(solc.compile(JSON.stringify(input)));
const bytecode = output.contracts['Contract.sol'].Contract.evm.bytecode.object;

Where input is obtained from something like (where yourCodeFile.sol is the name of your Solidity file, the one you are compiling):

const thePath = path.resolve(__dirname, '../', 'contracts', 'yourCodeFile.sol');
const source = fs.readFileSync(thePath, 'utf-8');

var input = {
    language: 'Solidity',
    sources: {
        'yourCodeFile.sol' : {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': [ "*" ]
            }
        }
    }
};

Then you export that to the JSON file. And replace Contract with your relevant name of course.

I used the above, e.g., to deploy pure Yul code via Hardhat.

Also note that, with this method, you can put whatever bytecode you want in the /path/to/your/bytecode.json file and pass to Hardhat!

2
  • 1
    No way to do so without the abi? Thanks
    – Kuly14
    Commented Oct 12, 2022 at 10:54
  • 1
    Yes you can, the ABI is just a convenience. You don't have to follow it. But if you use the ABI like above via hre.ethers... then when calling the contract deployed, ethers will check that any function you call is in the ABI and throw an error if not. But if you just call the contract outside of ethers (which you can do without the ABI) then you don't need it. So in short: Just put a random ABI there. Commented Oct 12, 2022 at 13:28
1

These two resources discuss how to deploy bytecode using Remix (might be dated):

https://medium.com/sofocle-technologies/deploying-contract-using-bytecode-myetherwallet-and-remix-10f643a82d40

https://github.com/Sekin/ethereum-bytecode-deployment

1
  • I saw both resources, the second one should work fine, but I would like to know if it's possible to do so through hardhat. If there is some native function that introduces this functionality. Thanks tho!
    – Kuly14
    Commented Oct 11, 2022 at 21:29
1

Using ethers you can deploy a contract just from its bytecode using sendTransaction:

const [wallet] = await ethers.getSigners()
const txReceipt = await wallet.sendTransaction({ data: bytecode })
await txReceipt.wait()
0

This solution did not work for me--at least on Polygon Mumbai. The tx succeeds but no contract or bytecode is displayed, and there's an error stating that the contract doesn't have an address.

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