truffle-contract-size
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -19,2 +19,2 @@ module.exports = { | ||
} | ||
}; | ||
} |
{ | ||
"name": "truffle-contract-size", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Displays the size of a truffle contracts in kilobytes", | ||
@@ -29,3 +29,4 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"cli-table": "^0.3.1" | ||
"cli-table": "^0.3.1", | ||
"yargs": "^15.3.1" | ||
}, | ||
@@ -32,0 +33,0 @@ "devDependencies": { |
# truffle-contract-size | ||
[![npm](https://img.shields.io/npm/v/truffle-contract-size.svg)](https://www.npmjs.com/package/truffle-contract-size) | ||
This [Truffle](https://www.trufflesuite.com/docs/truffle/overview) plugin displays the contract size of all or a selection of your smart contracts in kilobytes. | ||
@@ -32,6 +34,30 @@ | ||
To show only certain contracts one or more contract names can be given as arguments | ||
To show only certain contracts one or more contract names can be given as arguments to the contracts option: | ||
```bash | ||
truffle run contract-size ExampleContract1 ExampleContract2 | ||
truffle run contract-size --contracts ExampleContract1 ExampleContract2 | ||
``` | ||
### Check maximum contract sizes | ||
The plugin can be used to check that the smart contracts aren't bigger than the allowed maximum contract size of the Ethereum Mainnet (24 kb). For example this can be used, to make a CI/CD pipeline fail, if a contract is bigger than allowed. | ||
```bash | ||
truffle run contract-size --checkMaxSize | ||
``` | ||
If another size limit than the default one should be checked, it can be given as argument to the option. For example to set the maximum to 48 kb the following command can be used: | ||
```bash | ||
truffle run contract-size --checkMaxSize 48 | ||
``` | ||
If one or more of the contracts are bigger than the maximum size, an error message will de displayed, and the exit status will be 1. | ||
### Ignore mocks | ||
Mock contracts are used to improve the testing of smart contracts. As they are only used during testing and will not be deployed, it can be useful to ignore when calculating the contract sizes. When the option is used, all contract which names are ending with `Mock` will be ignored. This can especially be useful in combination with the `--checkMaxSize` option. | ||
```bash | ||
truffle run contract-size --ignoreMocks | ||
``` |
@@ -8,3 +8,6 @@ const fs = require('fs') | ||
const Table = require('cli-table') | ||
const yargs = require('yargs') | ||
const DEFAULT_MAX_CONTRACT_SIZE_IN_KB = 24 | ||
/** | ||
@@ -17,2 +20,12 @@ * Outputs the size of a given smart contract | ||
module.exports = async (config, done) => { | ||
configureArgumentParsing() | ||
yargs.parse(process.argv.slice(3)) | ||
const contractNames = yargs.argv.contracts | ||
const { checkMaxSize, ignoreMocks } = yargs.argv | ||
if (!isValidCheckMaxSize(checkMaxSize)) { | ||
done(`--checkMaxSize: invalid value ${checkMaxSize}`) | ||
} | ||
const table = new Table({ | ||
@@ -24,3 +37,3 @@ head: ['Contract'.white.bold, 'Size'.white.bold], | ||
// array of objects of {file: path to file, name: name of the contract} | ||
const contracts = await getContracts(config, done) | ||
const contracts = await getContracts(config, contractNames, ignoreMocks, done) | ||
@@ -48,5 +61,35 @@ const contractPromises = contracts.map(async (contract) => { | ||
if (checkMaxSize) { | ||
const maxSize = checkMaxSize === true ? DEFAULT_MAX_CONTRACT_SIZE_IN_KB : checkMaxSize | ||
table.forEach(row => { | ||
if (Number.parseFloat(row[1]) > maxSize) { | ||
done(`Contract ${row[0]} is bigger than ${maxSize} kb`) | ||
} | ||
}) | ||
} | ||
done() | ||
} | ||
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 kb (default: 24). Must be an integer value' }) | ||
yargs.option('ignoreMocks', { describe: 'Ignores all contracts which names end with "Mock"', type: 'boolean' }) | ||
// disable version parameter | ||
yargs.version(false) | ||
// puts help parameter at the end of the parameter list | ||
yargs.help() | ||
} | ||
function isValidCheckMaxSize (checkMaxSize) { | ||
if (checkMaxSize === undefined) { | ||
return true | ||
} | ||
return checkMaxSize === true || !Number.isNaN(checkMaxSize) | ||
} | ||
function computeByteCodeSizeInKb (byteCode) { | ||
@@ -77,8 +120,7 @@ // -2 to remove 0x from the beginning of the string | ||
async function getContracts (config, done) { | ||
async function getContracts (config, contractNames, ignoreMocks, done) { | ||
const contractsBuildDirectory = config.contracts_build_directory | ||
let contractNames = getContractNamesFromCommandArguments(config) | ||
if (contractNames.length === 0) { | ||
contractNames = await getAllContractNames(contractsBuildDirectory, done) | ||
if (contractNames === undefined || contractNames.length === 0) { | ||
contractNames = await getAllContractNames(contractsBuildDirectory, ignoreMocks, done) | ||
} | ||
@@ -94,8 +136,3 @@ | ||
function getContractNamesFromCommandArguments (config) { | ||
// the first item is the command name, the following ones are the contract names | ||
return config._.slice(1) | ||
} | ||
async function getAllContractNames (contractsBuildDirectory, done) { | ||
async function getAllContractNames (contractsBuildDirectory, ignoreMocks, done) { | ||
let files | ||
@@ -109,6 +146,16 @@ | ||
const contractsFiles = files.filter(file => file.endsWith('.json')) | ||
const contractsFiles = files.filter(file => { | ||
if (!file.endsWith('.json')) { | ||
return false | ||
} | ||
if (ignoreMocks && file.endsWith('Mock.json')) { | ||
return false | ||
} | ||
return true | ||
}) | ||
// -5 because that's the length of the string '.json' | ||
return contractsFiles.map(contractFile => contractFile.slice(0, -5)) | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19089
141
63
2
+ Addedyargs@^15.3.1
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedcamelcase@5.3.1(transitive)
+ Addedcliui@6.0.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedfind-up@4.1.0(transitive)
+ Addedget-caller-file@2.0.5(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedlocate-path@5.0.0(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-locate@4.1.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpath-exists@4.0.0(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@2.0.0(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedwhich-module@2.0.1(transitive)
+ Addedwrap-ansi@6.2.0(transitive)
+ Addedy18n@4.0.3(transitive)
+ Addedyargs@15.4.1(transitive)
+ Addedyargs-parser@18.1.3(transitive)