Suppose I deployed a smart contract on the Ethereum blockchain...and later added some storage data to the contract. I want to know the current contract size in KB programmatically from my web3 backend possibly using web3 js and ethers js. How can I achieve that?

  • you want to know the amount of storage used? the size of the contract and the amount of storage aren't really related so you can't pull that info in as one blob. If you know the storage layout you could calculate the storage in use, but if you have mappings you'd need to know all the keys as there'd be no way to enumerate them natively
    – sola24
    Commented Jul 19, 2022 at 19:55
  • The 24k storage limit does it only refers to the size of the contract or the amount of storage or both?
    – Tahlil
    Commented Jul 20, 2022 at 0:57
  • just the contract's bytecode size. you can get the bytecode with web3 or ethers using getCode, but you wouldn't have been able to deploy a contract if it busts the limit
    – sola24
    Commented Jul 20, 2022 at 8:03
  • Okay, so what is the limit of storage that can be used and how would I find out how much storage is already used?
    – Tahlil
    Commented Jul 20, 2022 at 8:07
  • 1
    the common analogy is that the contract's storage is a huge array of memory slots. Each slot being 32-bytes wide, and there are 2 ** 256 slots available, which is huge. You don't need to worry about running out of storage, but gas costs would be a consideration
    – sola24
    Commented Jul 20, 2022 at 8:28


Your Answer

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

Browse other questions tagged or ask your own question.