Skip to content

Prepare Hardhat 3 and Solidstate release #25

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

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,44 @@

Output Solidity contract sizes with Hardhat.

> Versions of this plugin prior to `3.0.0` were released as `hardhat-contract-sizer`, outside of the `@solidstate` namespace.

> Versions of this plugin prior to `2.0.0` were released as `buidler-contract-sizer`.

## Installation

```bash
npm install --save-dev hardhat-contract-sizer
npm install --save-dev @solidstate/hardhat-contract-sizer
# or
yarn add --dev hardhat-contract-sizer
yarn add --dev @solidstate/hardhat-contract-sizer
```

## Usage

Load plugin in Hardhat config:

```javascript
require('hardhat-contract-sizer');
require('@solidstate/hardhat-contract-sizer');
```

Add configuration under the `contractSizer` key:

| option | description | default |
| ------------------- | --------------------------------------------------------------------------------------------------------------------------- | ------- |
| `alphaSort` | whether to sort results table alphabetically (default sort is by contract size) | `false` |
| `runOnCompile` | whether to output contract sizes automatically after compilation | `false` |
| `disambiguatePaths` | whether to output the full path to the compilation artifact (relative to the Hardhat root directory) | `false` |
| `strict` | whether to throw an error if any contracts exceed the size limit (may cause compatibility issues with `solidity-coverage`) | `false` |
| `only` | `Array` of `String` matchers used to select included contracts, defaults to all contracts if `length` is 0 | `[]` |
| `except` | `Array` of `String` matchers used to exclude contracts | `[]` |
| `outputFile` | file path to write contract size report | `null` |
| `unit` | unit of measurement for the size of contracts, which can be expressed in 'B' (bytes), 'kB' (kilobytes) or 'KiB' (kibibytes) | `KiB` |
| option | description | default |
| -------------- | --------------------------------------------------------------------------------------------------------------------------- | ------- |
| `alphaSort` | whether to sort results table alphabetically (default sort is by contract size) | `false` |
| `runOnCompile` | whether to output contract sizes automatically after compilation | `false` |
| `flat` | whether to hide the full path to the compilation artifact and output only the contract name | `false` |
| `strict` | whether to throw an error if any contracts exceed the size limit (may cause compatibility issues with `solidity-coverage`) | `false` |
| `only` | `Array` of `String` matchers used to select included contracts, defaults to all contracts if `length` is 0 | `[]` |
| `except` | `Array` of `String` matchers used to exclude contracts | `[]` |
| `outputFile` | file path to write contract size report | `null` |
| `unit` | unit of measurement for the size of contracts, which can be expressed in 'B' (bytes), 'kB' (kilobytes) or 'KiB' (kibibytes) | `KiB` |

```javascript
contractSizer: {
alphaSort: true,
disambiguatePaths: false,
runOnCompile: true,
flat: true,
strict: true,
only: [':ERC20$'],
}
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hardhat-contract-sizer",
"version": "2.10.0",
"name": "@solidstate/hardhat-contract-sizer",
"version": "3.0.0",
"license": "MIT",
"description": "Output Solidity contract sizes with Hardhat",
"keywords": [
Expand All @@ -15,7 +15,7 @@
"wow",
"bytecode"
],
"repository": "https://github.com/ItsNickBarry/hardhat-contract-sizer",
"repository": "github:solidstate-network/hardhat-contract-sizer",
"author": "Nick Barry",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down Expand Up @@ -47,5 +47,8 @@
"chalk": "^4.0.0",
"cli-table3": "^0.6.0",
"strip-ansi": "^6.0.0"
},
"publishConfig": {
"access": "public"
}
}
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ declare module 'hardhat/types/config' {
interface HardhatUserConfig {
contractSizer?: {
alphaSort?: boolean;
disambiguatePaths?: boolean;
runOnCompile?: boolean;
flat?: boolean;
strict?: boolean;
only?: string[];
except?: string[];
Expand All @@ -20,8 +20,8 @@ declare module 'hardhat/types/config' {
interface HardhatConfig {
contractSizer: {
alphaSort: boolean;
disambiguatePaths: boolean;
runOnCompile: boolean;
flat: boolean;
strict: boolean;
only: string[];
except: string[];
Expand All @@ -35,8 +35,8 @@ extendConfig((config, userConfig) => {
config.contractSizer = Object.assign(
{
alphaSort: false,
disambiguatePaths: false,
runOnCompile: false,
flat: false,
strict: false,
only: [],
except: [],
Expand Down
19 changes: 16 additions & 3 deletions src/tasks/size_contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from 'fs';
import { TASK_COMPILE } from 'hardhat/builtin-tasks/task-names';
import { task } from 'hardhat/config';
import { HardhatPluginError } from 'hardhat/plugins';
import { parseFullyQualifiedName } from 'hardhat/utils/contract-names';
import path from 'path';
import stripAnsi from 'strip-ansi';

Expand Down Expand Up @@ -89,9 +90,9 @@ task('size-contracts', 'Output the size of compiled contracts')

outputData.push({
fullName,
displayName: config.disambiguatePaths
? fullName
: fullName.split(':').pop() ?? '',
displayName: config.flat
? parseFullyQualifiedName(fullName).contractName
: fullName,
deploySize,
previousDeploySize: previousSizes[fullName],
initSize,
Expand All @@ -100,6 +101,18 @@ task('size-contracts', 'Output the size of compiled contracts')
}),
);

outputData.reduce((acc, { displayName }) => {
if (acc.has(displayName)) {
throw new HardhatPluginError(
pluginName,
`ambiguous contract name: ${displayName}`,
);
}

acc.add(displayName);
return acc;
}, new Set());

if (config.alphaSort) {
outputData.sort((a, b) =>
a.displayName.toUpperCase() > b.displayName.toUpperCase() ? 1 : -1,
Expand Down