hardhat-contract-sizer
Advanced tools
Comparing version
{ | ||
"name": "hardhat-contract-sizer", | ||
"version": "2.9.0", | ||
"version": "2.10.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Output Solidity contract sizes with Hardhat", |
@@ -11,2 +11,10 @@ const fs = require('fs'); | ||
// see EIPs 170 and 3860 for more information | ||
// https://eips.ethereum.org/EIPS/eip-170 | ||
// https://eips.ethereum.org/EIPS/eip-3860 | ||
const DEPLOYED_SIZE_LIMIT = 24576; | ||
const INIT_SIZE_LIMIT = 49152; | ||
const UNITS = { 'B': 1, 'kB': 1000, 'KiB': 1024 }; | ||
task( | ||
@@ -23,11 +31,8 @@ 'size-contracts', 'Output the size of compiled contracts' | ||
// TODO: avoid hardcoding unit names | ||
if (!['B', 'kB', 'KiB'].includes(config.unit)) { | ||
if (!UNITS[config.unit]) { | ||
throw new HardhatPluginError(`Invalid unit: ${ config.unit }`); | ||
} | ||
const SIZE_LIMIT = 24576; | ||
const formatSize = function (size) { | ||
const divisor = { 'B': 1, 'kB': 1000, 'KiB': 1024 }[config.unit]; | ||
const divisor = UNITS[config.unit]; | ||
return (size / divisor).toFixed(3); | ||
@@ -46,2 +51,3 @@ }; | ||
const previousSizes = {}; | ||
const previousInitSizes = {}; | ||
@@ -52,3 +58,4 @@ if (fs.existsSync(outputPath)) { | ||
JSON.parse(previousOutput).forEach(function (el) { | ||
previousSizes[el.fullName] = el.size; | ||
previousSizes[el.fullName] = el.deploySize; | ||
previousInitSizes[el.fullName] = el.initSize; | ||
}); | ||
@@ -61,7 +68,11 @@ } | ||
const { deployedBytecode } = await hre.artifacts.readArtifact(fullName); | ||
const size = Buffer.from( | ||
const { deployedBytecode, bytecode } = await hre.artifacts.readArtifact(fullName); | ||
const deploySize = Buffer.from( | ||
deployedBytecode.replace(/__\$\w*\$__/g, '0'.repeat(40)).slice(2), | ||
'hex' | ||
).length; | ||
const initSize = Buffer.from( | ||
bytecode.replace(/__\$\w*\$__/g, '0'.repeat(40)).slice(2), | ||
'hex' | ||
).length; | ||
@@ -71,4 +82,6 @@ outputData.push({ | ||
displayName: config.disambiguatePaths ? fullName : fullName.split(':').pop(), | ||
size, | ||
previousSize: previousSizes[fullName] || null, | ||
deploySize, | ||
previousDeploySize: previousSizes[fullName] || null, | ||
initSize, | ||
previousInitSize: previousInitSizes[fullName] || null, | ||
}); | ||
@@ -80,3 +93,3 @@ })); | ||
} else { | ||
outputData.sort((a, b) => a.size - b.size); | ||
outputData.sort((a, b) => a.deploySize - b.deploySize); | ||
} | ||
@@ -125,6 +138,6 @@ | ||
{ | ||
content: chalk.bold(`Size (${config.unit})`), | ||
content: chalk.bold(`Deployed size (${config.unit}) (change)`), | ||
}, | ||
{ | ||
content: chalk.bold(`Change (${config.unit})`), | ||
content: chalk.bold(`Initcode size (${config.unit}) (change)`), | ||
}, | ||
@@ -136,31 +149,52 @@ ]); | ||
for (let item of outputData) { | ||
if (!item.size) { | ||
if (item.deploySize === 0 && item.initSize === 0) { | ||
continue; | ||
} | ||
let size = formatSize(item.size); | ||
let deploySize = formatSize(item.deploySize); | ||
let initSize = formatSize(item.initSize); | ||
if (item.size > SIZE_LIMIT) { | ||
size = chalk.red.bold(size); | ||
if (item.deploySize > DEPLOYED_SIZE_LIMIT || item.initSize > INIT_SIZE_LIMIT) { | ||
oversizedContracts++; | ||
} else if (item.size > SIZE_LIMIT * 0.9) { | ||
size = chalk.yellow.bold(size); | ||
} | ||
let diff = ''; | ||
if (item.deploySize > DEPLOYED_SIZE_LIMIT) { | ||
deploySize = chalk.red.bold(deploySize); | ||
} else if (item.deploySize > DEPLOYED_SIZE_LIMIT * 0.9) { | ||
deploySize = chalk.yellow.bold(deploySize); | ||
} | ||
if (item.previousSize) { | ||
if (item.size < item.previousSize) { | ||
diff = chalk.green(`-${formatSize(item.previousSize - item.size)}`); | ||
} else if (item.size > item.previousSize) { | ||
diff = chalk.red(`+${formatSize(item.size - item.previousSize)}`); | ||
if (item.initSize > INIT_SIZE_LIMIT) { | ||
initSize = chalk.red.bold(initSize); | ||
} else if (item.initSize > INIT_SIZE_LIMIT * 0.9) { | ||
initSize = chalk.yellow.bold(initSize); | ||
} | ||
let deployDiff = ''; | ||
let initDiff = ''; | ||
if (item.previousDeploySize) { | ||
if (item.deploySize < item.previousDeploySize) { | ||
deployDiff = chalk.green(`-${formatSize(item.previousDeploySize - item.deploySize)}`); | ||
} else if (item.deploySize > item.previousDeploySize) { | ||
deployDiff = chalk.red(`+${formatSize(item.deploySize - item.previousDeploySize)}`); | ||
} else { | ||
diff = chalk.yellow(formatSize(0)); | ||
deployDiff = chalk.gray(formatSize(0)); | ||
} | ||
} | ||
if (item.previousInitSize) { | ||
if (item.initSize < item.previousInitSize) { | ||
initDiff = chalk.green(`-${formatSize(item.previousInitSize - item.initSize)}`); | ||
} else if (item.initSize > item.previousInitSize) { | ||
initDiff = chalk.red(`+${formatSize(item.initSize - item.previousInitSize)}`); | ||
} else { | ||
initDiff = chalk.gray(formatSize(0)); | ||
} | ||
} | ||
table.push([ | ||
{ content: item.displayName }, | ||
{ content: size, hAlign: 'right' }, | ||
{ content: diff, hAlign: 'right' }, | ||
{ content: `${deploySize} (${deployDiff})`, hAlign: 'right' }, | ||
{ content: `${initSize} (${initDiff})`, hAlign: 'right' }, | ||
]); | ||
@@ -176,3 +210,3 @@ } | ||
const message = `Warning: ${oversizedContracts} contracts exceed the size limit for mainnet deployment (${formatSize(SIZE_LIMIT)} ${config.unit}).`; | ||
const message = `Warning: ${oversizedContracts} contracts exceed the size limit for mainnet deployment (${formatSize(DEPLOYED_SIZE_LIMIT)} ${config.unit} deployed, ${formatSize(INIT_SIZE_LIMIT)} ${config.unit} init).`; | ||
@@ -179,0 +213,0 @@ if (config.strict) { |
12998
13.46%292
11.45%