Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to display contract size in Bytes #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ module.exports = async (config, done) => {
yargs.parse(process.argv.slice(3))

const contractNames = yargs.argv.contracts
const { checkMaxSize, ignoreMocks } = yargs.argv
const { checkMaxSize, ignoreMocks, sizeInBytes } = yargs.argv

if (!isValidCheckMaxSize(checkMaxSize)) {
done(`--checkMaxSize: invalid value ${checkMaxSize}`)
}

const table = new Table({
head: ['Contract'.white.bold, 'Size'.white.bold],
colWidths: [70, 10]
colWidths: [70, sizeInBytes ? 13 : 10]
})

// array of objects of {file: path to file, name: name of the contract}
Expand All @@ -44,11 +44,13 @@ module.exports = async (config, done) => {
done(`Error: deployedBytecode not found in ${contract.file} (it is not a contract json file)`)
}

const byteCodeSize = computeByteCodeSizeInKiB(contractFile.deployedBytecode)
const byteCodeSize = sizeInBytes
? computeByteCodeSizeInBytes(contractFile.deployedBytecode)
: computeByteCodeSizeInKiB(contractFile.deployedBytecode)

table.push([
contract.name,
formatByteCodeSize(byteCodeSize)
formatByteCodeSize(byteCodeSize, sizeInBytes)
])
})

Expand All @@ -73,6 +75,7 @@ function configureArgumentParsing () {
yargs.option('contracts', { describe: 'Only display certain contracts', type: 'array' })
yargs.option('checkMaxSize', { describe: 'Returns an error exit code if a contract is bigger than the optional size in KiB (default: 24). Must be an integer value' })
yargs.option('ignoreMocks', { describe: 'Ignores all contracts which names end with "Mock"', type: 'boolean' })
yargs.option('sizeInBytes', { describe: 'Show contract size in Bytes', type: 'boolean' })

// disable version parameter
yargs.version(false)
Expand All @@ -89,15 +92,19 @@ function isValidCheckMaxSize (checkMaxSize) {
return checkMaxSize === true || !Number.isNaN(checkMaxSize)
}

function computeByteCodeSizeInKiB (byteCode) {
function computeByteCodeSizeInBytes (byteCode) {
// -2 to remove 0x from the beginning of the string
// /2 because one byte consists of two hexadecimal values
return (byteCode.length - 2) / 2
}

function computeByteCodeSizeInKiB (byteCode) {
// /1024 to convert to size from byte to kibibytes
return (byteCode.length - 2) / 2 / 1024
return computeByteCodeSizeInBytes(byteCode) / 1024
}

function formatByteCodeSize (byteCodeSize) {
return `${byteCodeSize.toFixed(2)} KiB`
function formatByteCodeSize (byteCodeSize, sizeInBytes) {
return `${sizeInBytes ? byteCodeSize : byteCodeSize.toFixed(2)} ${sizeInBytes ? "bytes" : "KiB"}`
}

async function checkFile (filePath, done) {
Expand Down