5

Can someone please direct me to a solution to this problem? The hardhat coverage plugin is telling me that my contract can't be deployed to mainnet. But hardhat size-contracts plugin is telling me that my contract size is only 10.9KB. This is far beneath the 24KB threshold imposed by EIP-170. The truffle contract-size plugin tells me that the same contract is 19.78KB. But this is still beneath the threshold.

Should I ignore the hardhat compiler warning? Or is there another tool that I can use to show me the data that hardhat is using to determine that my contract has breached the 24K limit?


When I run npx hardhat size-contracts, I get the following output

 ·-----------------|-------------·
 |  Contract Name  ·  Size (KB)  │
 ··················|··············
 |  Address        ·      0.086  │
 ··················|··············
 |  MYCONTRACT     ·     10.943  │
 ··················|··············
 |  Counters       ·      0.086  │
 ··················|··············
 |  ERC721         ·      4.703  │
 ··················|··············
 |  Strings        ·      0.086  │
 ·-----------------|-------------·

But when I run truffle run contract-size I get the following output

┌──────────────────────────────────────────────────────────────────────┬──────────┐
│ Contract                                                             │ Size     │
├──────────────────────────────────────────────────────────────────────┼──────────┤
│ Address                                                              │ 0.08 KiB │
├──────────────────────────────────────────────────────────────────────┼──────────┤
│ MYCONTRACT                                                           │ 19.78 K… │
├──────────────────────────────────────────────────────────────────────┼──────────┤
│ Other contract 1                                                     │ 0.06 KiB │
├──────────────────────────────────────────────────────────────────────┼──────────┤
│ Other contract 2                                                     │ 7.45 KiB │
├──────────────────────────────────────────────────────────────────────┼──────────┤

When I run the command npx hardhat coverage, I get the following output

Compiling 12 files with 0.8.8
Warning: Contract code size exceeds 24576 bytes (a limit introduced in Spurious Dragon). This contract may not be deployable on mainnet. Consider enabling the optimizer (with a low "runs" value!), turning off revert strings, or using libraries.
  --> contracts/MYCONTRACT.sol:10:1:
   |
10 | contract MYCONTRACT is ERC721, Ownable {
   | ^ (Relevant source part starts here and spans across multiple lines).

2 Answers2

0

You can get contract size in hardhat without any libraries

e.g. test.js in /scripts of hardhat project

const hre = require("hardhat")

async function test() { // Use Hardhats provider (locally) or your own provider const provider = ethers.provider

const bytecode = await provider.getCode(contractAddress)
const size = bytecode.length / 2

}

blockson
  • 47
  • 2
-1

Probably what you're looking for:

npx hardhat compile

I wasted hours chasing my tail because the bad output from npx hardhat coverage doesn't reveal that the warnings after instrumentation bloat are due to the instrumentation.