12

I want to measure the size of my compiled contract using solc.

I can dump the binary using --bin or --bin-runtime and then count how many hexadecimal digits it contains.

Is there an easier way to do it?

6 Answers 6

20

Foundry

You can get a report of the contract byte sizes by adding the --sizes flag when building the contracts:

forge build --sizes

Hardhat

There is this plugin called hardhat-contract-sizer:

$ yarn add --dev hardhat-contract-sizer

You load it in the Hardhat config file:

require('hardhat-contract-sizer');
// OR
import "hardhat-contract-sizer";

And then you run it:

$ yarn hardhat size-contracts
2
  • Is possible to install in Remix web?
    – JTCon
    May 8, 2022 at 18:01
  • 1
    @JTConsulta no, you can't use Hardhat in Remix. Check out my template for an easy way to bootstrap a Hardhat template. May 9, 2022 at 8:35
5

This approach takes more leg-work, but works:

  1. Open your compiled ABI (build/contracts/YourContract.json) and grab the value in deployedBytecode. It'll be easier if you use an editor (like Sublime) that lets you collapse JSON values, that you way don't need to scroll past the abi value.
  2. Copy+paste that value into a new file, save it with a hex extension (e.g. bytecode.hex).
  3. From your terminal, run ls -l bytecode.hex to get the detailed file info which includes the size in bytes.

EDIT: Just saw the detail that you're already dumping the binary from solc. You can always copy-paste that into a file. Real time-saver here is using ls -l rather than counting characters.

1

If you want to do it through smart-contracts on-chain check out the extcodesize opcode that is available when using solidity assembly.

1

You can use jq to filter the ABI JSON and then wc to count the number of bytes for deployed bytecode:

$ cat artifacts/Greeter.json | jq -r '.deployedBytecode' | wc -c
1555
1

I'm not sure this kosher, but:

(1) I'm using truffle (so built-in solc)

(2) I wrote a script to compute sizes from compiled contracts. It's pretty trivial. Not sure if it's accurate, but hey, it seems to do a good job of helping me predict when my contracts aren't gonna deploy.

var fs = require('fs') ;

module.exports = async function(cb) {

    const LIMIT = 1*1024 ;

function sizes (name) {
var abi = artifacts.require(name) ;
var size = (abi.bytecode.length / 2) - 1 ;
var deployedSize = (abi.deployedBytecode.length / 2) - 1 ;
return {name, size, deployedSize} ;
}

function fmt(obj) {
return `${ obj.name } ${ obj.size } ${ obj.deployedSize }` ;
}

var l = fs.readdirSync("build/contracts") ;
l.forEach(function (f) {
var name = f.replace(/.json/, '') ;
var sz = sizes(name) ;
if (sz.size >= LIMIT || sz.deployedSize >= LIMIT) {
    console.log(fmt(sz)) ;
}
}) ;

}

3
  • how to run this? Create an new file named like 4_check_bytecode.js, then runtruffle migrate?
    – deju
    Feb 18, 2019 at 9:14
  • It's been nearly a year, so I've forgotten details. Also, a while after this I gave up on truffle and switched to straight JS. In any case, I think I put this code in a file in scripts/ and used "truffle run ..." to run it.
    – Chet
    Feb 19, 2019 at 19:36
  • yeah, me too and it works.
    – deju
    Feb 20, 2019 at 6:04
-1

You can get contract size in hardhat

e.g. test.js in /scripts of hardhat project

const hre = require("hardhat")

async function test() {
    // Use Hardhats provider (locally) or your own provider
    const provider = ethers.provider
        
    const bytecode = await provider.getCode(contractAddress)
    const size = bytecode.length / 2
}
1
  • 1
    Please, don't copy & paste the same answer in multiple questions. If the questions are duplicates please flag them.
    – Ismael
    Feb 29 at 2:28

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.