Skip to content

Instantly share code, notes, and snippets.

@imqdee
Created March 8, 2023 16:44
Show Gist options
  • Save imqdee/c78b89e765e2e3df26d5b8ce51483e48 to your computer and use it in GitHub Desktop.
Save imqdee/c78b89e765e2e3df26d5b8ce51483e48 to your computer and use it in GitHub Desktop.
Just a test that deploy an arbitrary bytecode
import { expect } from "chai";
import { ethers } from "hardhat";
describe("off-chain arbitrary code deployment", function () {
it("test everything goes well", async function () {
// get the signer available in the test environment
const [deployer] = await ethers.getSigners();
// define the bytecode I wanna deploy. After the construction step, only
// this bytecode will be stored as the contract's code
// THIS IS AN EXAMPLE. IN REAL LIFE, THIS MUST BE AN ARGUMENT OF THE SCRIPT
const bytecodeToDeploy = "04006004";
// this is a minimal construction code needed
const constructionCode = "600B5981380380925939F3";
// this is the STOP opcode. It will prepend the bytecode to deploy
// to make sure the contract is not callable
const stopOpCode = "00";
// this it the final bytecode to deploy. It concatenates the construction code,
// the STOP opcode and the bytecode to deploy
const finaleBytecode = `0x${constructionCode}${stopOpCode}${bytecodeToDeploy}`;
// create a empty interface (as our contract won't have any functions)
// use it to generate a factory and deploy the contract that will
// store the finaleBytecode
const emptyInterface = new ethers.utils.Interface([]);
const factory = new ethers.ContractFactory(emptyInterface, finaleBytecode);
// deploy the contract and wait until it is deployed
const bytecodeContract = await factory.connect(deployer).deploy();
await bytecodeContract.deployed();
// get the bytecode stored in the contract
const code = await ethers.provider.getCode(bytecodeContract.address);
// check the code is correct
expect(code).to.equal(`0x00${bytecodeToDeploy}`);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment