I want to define a large array, hardcoded into the smart contract. I know there's not currently a way to define a constant array in a smart contract and there has to be a getter function implemented, as e.g.

function get_S() public pure returns (uint256[] memory) {
  uint256[285] memory Sf = [
  uint256[] memory Sd = new uint256[](285);
  for (uint256 i = 0; i < 285; i++) {
    Sd[i] = Sf[i];
  return Sd;

But this makes contract deployment produce

Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (reason="Transaction reverted: trying to deploy a contract whose code is too large", method="estimateGas"

I can solve this by making the contract store my constants in the constructor (so I have to pass them to the constructor at deployment), but I'd like it better if that was not necessary. Is there a way of solving this nicely?

1 Answer 1


You can define a constant array as a static array with fixed length and initializing it. Anyways, according to EIP-170, the proposal specifies that the deployment of a contract whose bytecode is more than 24 kb should fail. You may need to downsize your code.




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.