Created
August 3, 2020 15:58
-
-
Save MicrowaveDev/110799f0765b0da5c4809e3f314f7fa7 to your computer and use it in GitHub Desktop.
Get all compiled smart contracts sizes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const Table = require('cli-table'); | |
const table = new Table({ | |
head: ['Contract', 'Size (bytes)'], | |
colWidths: [50, 15] | |
}); | |
const testFolder = './build/contracts/'; | |
const contracts = []; | |
fs.readdirSync(testFolder).forEach(file => { | |
contracts.push([file.substring(0, file.length - 5), getSize(file)]); | |
}); | |
contracts.sort(function(a, b) { | |
return a[1] - b[1]; | |
}); | |
contracts.forEach(value => { | |
table.push(value); | |
}); | |
console.log(table.toString()); | |
console.log('\n Size cap is about', 24577, '\n'); | |
/** | |
* Get contract size in bytes | |
* | |
* @param contract | |
* @returns {number} | |
*/ | |
function getSize(contract) { | |
let abi; | |
try { | |
abi = JSON.parse(fs.readFileSync(`build/contracts/${contract}`)); | |
return Buffer.byteLength(abi.deployedBytecode, 'utf8') / 2; | |
} catch (e) { | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment