Socket
Socket
Sign inDemoInstall

truffle-contract-size

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

truffle-contract-size - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

2

.eslintrc.js

@@ -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))
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc