1

I've written code to deploy contracts programatically. However when I set the same settings as remix ("gas price, gas limit, etc). The transaction mines but I get the error:

Warning! Error encountered during contract execution [out of gas]

Now, If I set the gasLimit to like 6m as opposed to the 3M gas suggested by Remix, The contract deploys successfully but at roughly 70% more of the fees than deploying on Remix.

Here's the code I'm using for the contract:

const input = {
    language: "Solidity",
    sources: {
      "TestContract.sol": {
        content: sourceCode,
      },
    },
    settings: {
      outputSelection: { "*": { "*": ["*"] } },
    },
  };

solc.loadRemoteVersion(compilerVersion, async (err, solcSnapshot) => {
    if (err) {
      console.log("inside error: ");
      console.log("error compiling with solc: ", e);
    } else {
      const { contracts } = JSON.parse(
        solcSnapshot.compile(JSON.stringify(input))
      );
      const contract = contracts["TestContract.sol"][<name of contract>];
      const { abi } = contract;
      const bytecode = contract.evm.bytecode.object;
      const { signer, provider } = getSigner({ pk });
      const factory = new ethers.ContractFactory(abi, bytecode, signer);
      const deployContract = await factory.deploy();
      const receipt = await deployContract.deployTransaction.wait();
    }
})

I feel like maybe it has to do with the bytecode, but I'm not sure why that would differ from the same exact contract being compiled in Remix (Code is exactly the same).

1 Answer 1

2

In remix the optimizer is enabled per default, in solc library not. To use the optimizer:

const input = {
    language: "Solidity",
    sources: {
      "TestContract.sol": {
        content: sourceCode,
      },
    },
    settings: {
      outputSelection: { "*": { "*": ["*"] } },
      optimizer: {
          enabled: true,
          runs: 200   
      }
    },
  };

More info at - https://docs.soliditylang.org/en/v0.8.21/using-the-compiler.html

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.