Is there a way to deploy pure bytecode with hardhat or remix?
Thanks
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!
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
These two resources discuss how to deploy bytecode using Remix (might be dated):
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()
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.