hardhat.config.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /// ENVVAR
  2. // - CI: output gas report to file instead of stdout
  3. // - COVERAGE: enable coverage report
  4. // - ENABLE_GAS_REPORT: enable gas report
  5. // - COMPILE_MODE: production modes enables optimizations (default: development)
  6. // - COMPILE_VERSION: compiler version (default: 0.8.20)
  7. // - COINMARKETCAP: coinmarkercat api key for USD value in gas report
  8. const fs = require('fs');
  9. const path = require('path');
  10. const proc = require('child_process');
  11. const argv = require('yargs/yargs')()
  12. .env('')
  13. .options({
  14. coverage: {
  15. type: 'boolean',
  16. default: false,
  17. },
  18. gas: {
  19. alias: 'enableGasReport',
  20. type: 'boolean',
  21. default: false,
  22. },
  23. gasReport: {
  24. alias: 'enableGasReportPath',
  25. type: 'string',
  26. implies: 'gas',
  27. default: undefined,
  28. },
  29. mode: {
  30. alias: 'compileMode',
  31. type: 'string',
  32. choices: ['production', 'development'],
  33. default: 'development',
  34. },
  35. ir: {
  36. alias: 'enableIR',
  37. type: 'boolean',
  38. default: false,
  39. },
  40. foundry: {
  41. alias: 'hasFoundry',
  42. type: 'boolean',
  43. default: hasFoundry(),
  44. },
  45. compiler: {
  46. alias: 'compileVersion',
  47. type: 'string',
  48. default: '0.8.20',
  49. },
  50. coinmarketcap: {
  51. alias: 'coinmarketcapApiKey',
  52. type: 'string',
  53. },
  54. }).argv;
  55. require('@nomiclabs/hardhat-truffle5'); // deprecated
  56. require('@nomicfoundation/hardhat-toolbox');
  57. require('@nomicfoundation/hardhat-ethers');
  58. require('hardhat-ignore-warnings');
  59. require('hardhat-exposed');
  60. require('solidity-docgen');
  61. argv.foundry && require('@nomicfoundation/hardhat-foundry');
  62. if (argv.foundry && argv.coverage) {
  63. throw Error('Coverage analysis is incompatible with Foundry. Disable with `FOUNDRY=false` in the environment');
  64. }
  65. for (const f of fs.readdirSync(path.join(__dirname, 'hardhat'))) {
  66. require(path.join(__dirname, 'hardhat', f));
  67. }
  68. const withOptimizations = argv.gas || argv.coverage || argv.compileMode === 'production';
  69. /**
  70. * @type import('hardhat/config').HardhatUserConfig
  71. */
  72. module.exports = {
  73. solidity: {
  74. version: argv.compiler,
  75. settings: {
  76. optimizer: {
  77. enabled: withOptimizations,
  78. runs: 200,
  79. },
  80. viaIR: withOptimizations && argv.ir,
  81. outputSelection: { '*': { '*': ['storageLayout'] } },
  82. },
  83. },
  84. warnings: {
  85. 'contracts-exposed/**/*': {
  86. 'code-size': 'off',
  87. 'initcode-size': 'off',
  88. },
  89. '*': {
  90. 'code-size': withOptimizations,
  91. 'unused-param': !argv.coverage, // coverage causes unused-param warnings
  92. default: 'error',
  93. },
  94. },
  95. networks: {
  96. hardhat: {
  97. allowUnlimitedContractSize: !withOptimizations,
  98. },
  99. },
  100. exposed: {
  101. imports: true,
  102. initializers: true,
  103. exclude: ['vendor/**/*'],
  104. },
  105. docgen: require('./docs/config'),
  106. };
  107. if (argv.gas) {
  108. require('hardhat-gas-reporter');
  109. module.exports.gasReporter = {
  110. showMethodSig: true,
  111. currency: 'USD',
  112. outputFile: argv.gasReport,
  113. coinmarketcap: argv.coinmarketcap,
  114. };
  115. }
  116. if (argv.coverage) {
  117. require('solidity-coverage');
  118. module.exports.networks.hardhat.initialBaseFeePerGas = 0;
  119. }
  120. function hasFoundry() {
  121. return proc.spawnSync('forge', ['-V'], { stdio: 'ignore' }).error === undefined;
  122. }