0

我正在分析 Chainrunners 智能合约,所以我继续使用 Etherscan 并复制了经过验证的合约源代码

当我尝试在没有solidity 优化器的情况下进行编译时,我收到了以下警告:

thatguyintech@albert chainrunners % npx hardhat compile
Compiling 5 files with 0.8.4
Warning: Unused local variable.
   --> contracts/ChainRunnersBaseRenderer.sol:232:124:
    |
232 |  ... kenPalettes, uint8 numTokenLayers, string[NUM_LAYERS] memory traitTypes) = getTokenData(_dna);
    |                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


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/ChainRunnersBaseRenderer.sol:48:1:
   |
48 | contract ChainRunnersBaseRenderer is Ownable, ReentrancyGuard {
   | ^ (Relevant source part starts here and spans across multiple lines).

于是我尝试按照Hardhat官方文档开启优化器:https ://hardhat.org/config/

所以这是我的安全帽配置的hardhat.config.js样子:

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: {
    version:  "0.8.4",
    settings: {
      optimizer: {
        enabled: true,
        runs: 2000,
      }
    }
  }
};

所以现在CompilerError当我尝试跑步时,我得到了这个安全帽npx hardhat compile

thatguyintech@albert chainrunners % npx hardhat compile
Compiling 5 files with 0.8.4
CompilerError: Stack too deep when compiling inline assembly: Variable value0 is 3 slot(s) too deep inside the stack.

有谁知道我该如何解决这个问题?从关于安全帽相关线程的几个谷歌搜索来看,似乎打开优化器应该是解决这个问题的方法,所以我很困惑。

这是我在 OpenZeppelin 论坛上找到的一个不适合我的示例:https ://forum.openzeppelin.com/t/stack-to-deep-when-compiling-inline-assembly/11391/11

4

1 回答 1

3

啊,原来 Etherscan 页面中有一个部分显示了确切的可靠性优化器集。(h/t @alcuadadro )

它看起来像这样:

在此处输入图像描述

所以我把它复制到我的hardhat.config.js

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: {
    version:  "0.8.4",
    settings: {
      optimizer: {
        enabled: true,
        runs: 2000,
        details: {
          yul: true,
          yulDetails: {
            stackAllocation: true,
            optimizerSteps: "dhfoDgvulfnTUtnIf"
          }
        }
      }
    },
  },
};

这就是诀窍!

不知道这些yul东西是关于什么的

于 2021-12-10T23:19:15.293 回答