1

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 = [
    7511745149465107256748700652201246547602992235352608707588321460060273774987,
    1781874611967874592137274483616240894881315449294815307306613366069350853425,
    9676220459425127104563807626505378474104527268335041816433595157913150665495,
    ...
    9131299761947733513298312097611845208338517739621853568979632113419485819303
  ];
  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

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.

See:

https://eips.ethereum.org/EIPS/eip-170

https://ethereum.org/en/developers/tutorials/downsizing-contracts-to-fight-the-contract-size-limit/

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.