2

I've been trying to deploy a contract with ethers.js using the artifacts produced by hardhat compile, but it fails on factory creation with an "invalid bytecode" error, because the json bytecode key in the json file from artfacts/MyContract.sol/MyContract.json' contains non-hex characters which cause ethers.js ContractFactory constructor to throw an invalid bytecode error.

Examing what's actually in the json, most of it looks like a hex string, but sometime I am seeing as section that looks like this: 3__$1ca363c46bd7f7114c4ccedbc882b31c3a$__9163: a section of code that starts with __$ and ending with $__. (which I suspect is an address, but not certain).

Is there some way to exact from the results of hardhat compile pure bytecode that doesn't have these embbeded non-hex characters and can be used to deploy a contract with ethers.js directly?

Additional note: I think these codes have do with libraries used by the smart contracts in question. But how to get the merged bytecode from the contracts actually deployed which includes the necessary bytecode to call the libraries isn't clear to me.

1

1 Answer 1

0

Checkout library linking in the solidity docs. https://docs.soliditylang.org/en/latest/using-the-compiler.html#library-linking

The variable that you are seeing are placeholders for library addresses that your contract uses. Typically when you deploy contracts with hardhat you specify the addresses of the libraries for the deploy factory. Your bytecode is simply not linked.

const ContractFactory = await ethers.getContractFactory("ContractFactory", {
    libraries: {
        lib1: "0x1234567890123456789012345678901234567890",
        lib2: "0xabCD567890123456789012345678901234567890",
        etc
    },
});


const contractFactory = await ContractFactory.deploy(
    args1, 
    args2,
    etc
)

await curvefactoryv2.deployed()
1
  • Yeah, that's the way I do it hardhat environment, but unfortunately the "getContractFactory("name", {libraries {[libraries]} syntax doesn't work with ether.js unless you're using the special version of ethers that is modified by hardhat. What I'm trying to do is deploy outside a hardhat environment using just ethers js, where my choices are only new ContractFactory or ContractFactory.fromSolidity, and I can't figure out how to get either one to work with the json produced by hardhat.
    – GGizmos
    Commented Nov 21, 2022 at 4:57

Your Answer

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

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