Giter Site home page Giter Site logo

itsnickbarry / hardhat-contract-sizer Goto Github PK

View Code? Open in Web Editor NEW
72.0 4.0 13.0 121 KB

Output Ethereum contract sizes with Hardhat ๐Ÿ“

License: MIT License

JavaScript 100.00%
buidler abi solidity solc smart-contracts ethereum blockchain wow bytecode hardhat

hardhat-contract-sizer's Introduction

Hardhat Contract Sizer

Output Solidity contract sizes with Hardhat.

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

Installation

npm install --save-dev hardhat-contract-sizer
# or
yarn add --dev hardhat-contract-sizer

Usage

Load plugin in Hardhat config:

require("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
contractSizer: {
  alphaSort: true,
  disambiguatePaths: false,
  runOnCompile: true,
  strict: true,
  only: [':ERC20$'],
}

Run the included Hardhat task to output compiled contract sizes:

npx hardhat size-contracts
# or
yarn run hardhat size-contracts

By default, the hardhat compile task is run before sizing contracts. This behavior can be disabled with the --no-compile flag:

npx hardhat size-contracts --no-compile
# or
yarn run hardhat size-contracts --no-compile

hardhat-contract-sizer's People

Contributors

6str avatar cumaozavci avatar itsnickbarry avatar matteocelani avatar phated avatar ryanrhall avatar thearhaam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

hardhat-contract-sizer's Issues

Contract size issue

Getting contract size issue as while compiling the size of contract is 13.631 (kiB) but while compiling through Remix Got contract size limit excedded.
I tried with both enabling and disabling optimizer. Previously it worked fine.

Contracts that use libraries are not sized accurately

Hi @ItsNickBarry! Love this plugin

When sizing a contract whose bytecode includes library address placeholders, e.g. __$....$__, it seems like the size measurement is incorrect - it only returns size up to the first placeholder entry.

A repro based on how length is measured here is:

const a = "aaaa__$aaaa$__aaaa";
const b = "bbbbbbbbbbbbbbbbbb";

a_bufferLength = Buffer.from(a, 'hex').length;  // Issue w/ non-hex chars?
b_bufferLength = Buffer.from(b, 'hex').length;

a_stringLength = a.length;
b_stringLength = b.length;

console.log(`a_bufferLength: ${a_bufferLength}`);
console.log(`b_bufferLength: ${b_bufferLength}`);
console.log(`a_stringLength: ${a_stringLength}`);
console.log(`b_stringLength: ${b_stringLength}`);

> a_bufferLength: 2  // Short
> b_bufferLength: 9  // OK
> a_stringLength: 18
> b_stringLength: 18

Testing - Include a hardhat test project

Not sure what process is being followed for testing at the moment but I suggest including a typescript hardhat test project, that should improve the development and testing experience.

Seeing red for contract size less than 24.576kb

hardhat-contract-sizer is telling me is that my contract is 24.1kb and is producing the warning message:
Warning: 1 contracts exceed the size limit for mainnet deployment.

My understanding is that contracts can be as much as 24.675kb.

Is this a bug in hardhat-contract-sizer or is there some other legitimate reason I'm seeing Warning: 1 contracts exceed the size limit for mainnet deployment. ?

Inconsistency between hardhat sizer & manual computation

Hey, thanks for this cool tool :)

I am having the following issue. Hardhat sizer shows that my contract is over the limit in color even though the size in number is ok.

image

Moreover, when I manually check the size of the contract, it's considerably larger.

cat artifacts/contracts/Loadout.sol/Loadout.json | jq -r '.deployedBytecode' | wc -c
   49863

Any ideas?

Support for NPM?

If this works with NPM as well, could you add that to readme and how to install?

Sometimes Sizer/Compiler will give me different compile sizes in Test v's Coverage - hardhat deploy project

Hi there,

I'm using this plug to help me along with some ethereum development and I'm seeing different contract sizing values when I run the tasks in my hardhat project and I wondered what I was doing wrong?

Testing locally I will run some tests against hardhat using the test command:

"test": "HARDHAT_DEPLOY_FIXTURE=true HARDHAT_COMPILE=true mocha --recursive test",

And my hardhat.config.ts is:

import 'dotenv/config';
import {HardhatUserConfig} from 'hardhat/types';
import 'hardhat-deploy';
import '@nomiclabs/hardhat-ethers';
import 'hardhat-gas-reporter';
import '@typechain/hardhat';
import 'solidity-coverage';
import {node_url, accounts} from './utils/network';
import 'hardhat-tracer';
import 'hardhat-contract-sizer';

// While waiting for hardhat PR: https://github.com/nomiclabs/hardhat/pull/1542
if (process.env.HARDHAT_FORK) {
  process.env['HARDHAT_DEPLOY_FORK'] = process.env.HARDHAT_FORK;
}

const config: HardhatUserConfig = {
  solidity: {
    version: '0.8.7',
    settings: {
      optimizer: {
        runs: 200, // Default 200
        enabled: !process.env.DEBUG,
      },
    },
  },
  namedAccounts: {
    deployer: 0,
    minter: 1,
    gameWallet: 2,
    buybackWallet: 3,
    player1: 4,
    player2: 5,
    player3: 6,
    player4: 7,
  },
  networks: {
    hardhat: {
      initialBaseFeePerGas: 0, // to fix : https://github.com/sc-forks/solidity-coverage/issues/652, see https://github.com/sc-forks/solidity-coverage/issues/652#issuecomment-896330136
      // process.env.HARDHAT_FORK will specify the network that the fork is made from.
      // this line ensure the use of the corresponding accounts
      accounts: accounts(process.env.HARDHAT_FORK),
      forking: process.env.HARDHAT_FORK
        ? {
            // TODO once PR merged : network: process.env.HARDHAT_FORK,
            url: node_url(process.env.HARDHAT_FORK),
            blockNumber: process.env.HARDHAT_FORK_NUMBER
              ? parseInt(process.env.HARDHAT_FORK_NUMBER)
              : undefined,
          }
        : undefined,
    },
    localhost: {
      url: node_url('localhost'),
      accounts: accounts(),
    },
   
  },
  paths: {
    sources: 'src',
  },
  gasReporter: {
    currency: 'USD',
    gasPrice: 100,
    enabled: process.env.REPORT_GAS ? true : false,
    coinmarketcap: process.env.COINMARKETCAP_API_KEY,
    maxMethodDiff: 10,
  },
  typechain: {
    outDir: 'typechain',
    target: 'ethers-v5',
  },
  mocha: {
    timeout: 10_000,
  },
  contractSizer: {
    alphaSort: true,
    disambiguatePaths: true,
    runOnCompile: true,
    strict: true,
  },
};

export default config;

So when I run a test my contracts are sized like this:

Nothing to compile
No need to generate any newer typings.
 ยท---------------------------------------------------------------------------------------------------------|-------------ยท
 |  Contract Name                                                                                          ยท  Size (Kb)  โ”‚
 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
 |  @openzeppelin/contracts/token/ERC1155/ERC1155.sol:ERC1155                                              ยท       4.95  โ”‚
 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
 |  @openzeppelin/contracts/utils/Address.sol:Address                                                      ยท       0.08  โ”‚
 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
 |  @openzeppelin/contracts/utils/Counters.sol:Counters                                                    ยท       0.08  โ”‚
 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
 |  @openzeppelin/contracts/utils/Strings.sol:Strings                                                      ยท       0.08  โ”‚
 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
 |  @openzeppelin/contracts/utils/structs/EnumerableSet.sol:EnumerableSet                                  ยท       0.08  โ”‚
 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
 |  src/test/TestPackNFT.sol:TestPackNFT                                                                   ยท      10.64  โ”‚
 ยท---------------------------------------------------------------------------------------------------------|-------------ยท
โœจ  Done in 3.15s.

but if I do run the coverage task:

 "coverage": "HARDHAT_DEPLOY_FIXTURE=true hardhat coverage",

I get the following:

Network Info
============
> HardhatEVM: v2.6.4
> network:    hardhat

Nothing to compile
No need to generate any newer typings.
 ยท---------------------------------------------------------------------------------------------------------|-------------ยท
 |  Contract Name                                                                                          ยท  Size (Kb)  โ”‚
 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
 |  @openzeppelin/contracts/token/ERC1155/ERC1155.sol:ERC1155                                              ยท       9.42  โ”‚
 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
 |  @openzeppelin/contracts/utils/Address.sol:Address                                                      ยท       0.08  โ”‚
 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
 |  @openzeppelin/contracts/utils/Counters.sol:Counters                                                    ยท       0.08  โ”‚
 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
 |  @openzeppelin/contracts/utils/Strings.sol:Strings                                                      ยท       0.08  โ”‚
 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
 |  @openzeppelin/contracts/utils/structs/EnumerableSet.sol:EnumerableSet                                  ยท       0.08  โ”‚
 ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท|ยทยทยทยทยทยทยทยทยทยทยทยทยทยท
 |  src/test/TestPackNFT.sol:TestPackNFT                                                                   ยท      23.24  โ”‚
 ยท---------------------------------------------------------------------------------------------------------|-------------ยท

Thats almost double the size value so I'm not sure what I'm doing wrong or does it do something else when running tests locally?

Sorry for the n00b question.

Disable `strict` option when `hardhat-coverage` is in use

The hardhat-coverage plugin greatly increases the size of contracts, and therefore cannot be used if the strict option is enabled. Should check whether that plugin is running, or perhaps rely on the allowUnlimitedContractSize option.

Uncompiled Internal Functions

Overview

We use the contract sizer to analyze our contract size a lot. An issue we find is if internal functions are not been called in the contract, they will not be compiled into the contract, so it's hard to analyze the size of internal functions. This case does happen if I have a contract A with some internal functions, and then contract B inherits A and calls the internal functions.

Proposed Solution

Show how many internal functions that are igonre when compiling. I'm not sure whether it's doable. It's just an idea. Hope someone can propose better ideas or guidance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.