0

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?

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

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.

0

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)) ;
}
}) ;

}

  • how to run this? Create an new file named like 4_check_bytecode.js, then runtruffle migrate? – deju Feb 18 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 at 19:36
  • yeah, me too and it works. – deju Feb 20 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.