Skip to content

Commit 32663e2

Browse files
authored
Merge pull request #554 from PolymathNetwork/calc-size
Calculate size script optimized
2 parents 3ca3b9b + ba5c75a commit 32663e2

File tree

3 files changed

+73
-1141
lines changed

3 files changed

+73
-1141
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"babel-preset-stage-3": "6.24.1",
6363
"babel-register": "6.26.0",
6464
"bignumber.js": "5.0.0",
65-
"chalk": "^2.4.1",
65+
"chalk": "^2.4.2",
6666
"coveralls": "^3.0.1",
6767
"ethereumjs-testrpc": "^6.0.3",
6868
"ethers": "^4.0.7",
@@ -90,7 +90,7 @@
9090
"ganache-cli": "^6.2.4",
9191
"mocha-junit-reporter": "^1.18.0",
9292
"prettier": "^1.15.3",
93-
"prettier-plugin-solidity-refactor": "^1.0.0-alpha.10",
93+
"table": "^5.2.3",
9494
"sol-merger": "^0.1.2",
9595
"solidity-coverage": "^0.5.11",
9696
"solidity-docgen": "^0.1.0",

scripts/calculateSize.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
const fs = require("fs");
22
const path = require("path");
3-
var size = new Array();
3+
const exec = require('child_process').execSync;
4+
const chalk = require('chalk');
5+
const { table } = require("table");
46

5-
function readFiles() {
7+
async function readFiles() {
68
if (fs.existsSync("./build/contracts/")) {
7-
let files = fs.readdirSync("./build/contracts/");
8-
return files;
9+
return fs.readdirSync("./build/contracts/");
910
} else {
10-
console.log("Directory doesn't exists");
11+
console.log('Compiling contracts. This may take a while, please wait.');
12+
exec('truffle compile');
13+
return fs.readdirSync("./build/contracts/");
1114
}
1215
}
1316

1417
async function printSize() {
15-
let files = readFiles();
18+
let files = await readFiles();
19+
console.log(`NOTE- Maximum size of contracts allowed to deloyed on the Ethereum mainnet is 24 KB(EIP170)`);
20+
console.log(`---- Size of the contracts ----`);
21+
let dataTable = [['Contracts', 'Size in KB']];
1622
files.forEach(item => {
1723
let content = JSON.parse(fs.readFileSync(`./build/contracts/${item}`).toString()).deployedBytecode;
1824
let sizeInKB = content.toString().length / 2 / 1024;
19-
size.push(sizeInKB);
25+
if (sizeInKB > 24)
26+
dataTable.push([chalk.red(path.basename(item, ".json")),chalk.red(sizeInKB)]);
27+
else if (sizeInKB > 20)
28+
dataTable.push([chalk.yellow(path.basename(item, ".json")),chalk.yellow(sizeInKB)]);
29+
else
30+
dataTable.push([chalk.green(path.basename(item, ".json")),chalk.green(sizeInKB)]);
2031
});
21-
console.log(`NOTE- Maximum size of contracts allowed to deloyed on the Ethereum mainnet is 24 KB(EIP170)`);
22-
console.log(`---- Size of the contracts ----`);
23-
for (let i = 0; i < files.length; i++) {
24-
console.log(`${path.basename(files[i], ".json")} - ${size[i]} KB`);
25-
}
32+
console.log(table(dataTable));
2633
}
2734

2835
printSize();

0 commit comments

Comments
 (0)