使用 Hardhat 通过 Uniswap 流动性配置测试代币

Abr*_*m P 3 testing solidity smartcontracts hardhat

我正在尝试分叉 Safemoon(或者实际上是 NotSafeMoon),并将其用作学习智能合约开发的工具。(我拥有大量您可能称之为“Web 2.0”的开发经验)。

假设我的构造函数中有类似的内容:

constructor () {
        _rOwned[_msgSender()] = _rTotal;
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);       // binance PANCAKE V2
        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()).createPair(address(this), _uniswapV2Router.WETH());
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,npx hardhat test出现以下失败:

Compilation finished successfully


  TestToken contract
    Deployment
      1) "before each" hook for "Has the right name"


  0 passing (807ms)
  1 failing

  1) TestToken contract
       "before each" hook for "Has the right name":
     Error: Transaction reverted: function call to a non-contract account
Run Code Online (Sandbox Code Playgroud)

现在,这确实很有意义,毕竟我正在尝试调用 Pancakeswap v2 路由器合约。我如何绕过这个限制?有没有办法将路由器的合约地址作为环境变量注入?我可以使用 UniswapRouter 的模拟构造函数吗?一般来说,这种事情是如何通过智能合约开发以保持可测试的方式完成的(以及如何测试)?

Pet*_*jda 6

安全帽测试将合约部署到安全帽本地网络(默认情况下)。这个本地网络只有很少的预资助和解锁账户,但没有部署智能合约。包括 PancakeSwap v2 路由器(0x10ED43...)。


您可以创建一个从生产 BSC 分叉的新安全帽网络,而不是部署和配置路由器合约的本地副本及其所有依赖项。

https://hardhat.org/guides/mainnet-forking.html

这将运行一个具有可用路由器合约的本地网络,但您的操作只会影响本地网络(而不是主网)。

  • Hardhat 建议使用 Alchemy,但目前不支持 BSC 主网。 (4认同)