> ## Content Index
> Fetch the complete content index at: https://defiplot.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# Foundry vs. Hardhat
- URL: https://defiplot.com/blog/foundry-vs-hardhat/
- Published: 2024-01-11T10:38:16.000Z
- Updated: 2024-01-11T10:38:16.000Z
- Description: 使用 Hardhat 需要在两种不同的编程语言“Solidity”和“JavaScript”之间不断进行上下文切换。Foundry要更出色一些。
- Author: defiplot
- Tags: tech, #Import 2026-11-24 18:25

智能合约是用 Solidity 编程语言编写的，然后由 Hardhat 以太坊开发环境编译智能合约。使用连接到 Hardhat 网络的“ethers”库测试需求，并使用“Chai”匹配器创建各种单元测试。

我已经使用 Hardhat 来测试和部署我的智能合约超过六个月了。 它要求我学习“javaScript”和“typeScript”的基础知识来部署和测试我的智能合约。

因此，使用 Hardhat 需要在两种不同的编程语言“Solidity”和“JavaScript”之间不断进行上下文切换。

然后Foundry来了......

Foundry 不仅发展迅速，而且很快就被 DeFi 领域的巨头所采用。 但它也有非常详细的文档，比如这本[ Foundry Book](https://book.getfoundry.sh/?ref=defiplot.com)。

花了几个小时后，我弄清楚了如何使用 Foundry 测试“CompoundV2”协议上的其中一个功能。它在各种日志记录、断言和作弊代码功能方面有一个学习曲线。 但很快，我就能把它们拼凑起来。

我的目标是为“cEth”合约提供流动性。 步骤如下：

- 交易合约 `1_000 ether`，通过 `cETH.mint()`提供给 *CompoundV2* 协议
- 执行基本计算以检查汇率和预期铸造的 `cTokens` 数量
- 将收到的 `cTokens` 与上一步中计算的预期金额进行比较。
- 将时间提前 `50_000` 块，以对存入的流动性产生利息。
- 赎回基础 `ETH` 并断言其大于最初提供的金额。

代码片段（非开发人员可以跳到记录的输出消息）：

```
 /***@notice  Supply ETH to the cETH contract, check the cToken balances and redeem.
 */   
 function testSupply_ETH() public{
     console.log("Supplying liquidity into the cETH contract");
        vm.deal(here, 1000 ether); 
        uint256 initialBalETH = address(this).balance;
        emit log_named_uint("Supplied ETH liquidity", 1000 ether);
        emit log_named_uint("Initial cETH balance of this contract:", cETH.balanceOf(here));
        cETH.mint{value: 1000 ether}();
        emit log_named_decimal_uint("cETH balance after calling mint:", cETH.balanceOf(here),0);
        uint256 calcExchangeRate = 1e18*(cETH.getCash() + cETH.totalBorrowsCurrent() - cETH.totalReserves())/cETH.totalSupply();
        uint256 calculatedMinted = (1000 ether *1e18)/calcExchangeRate;
        emit log_named_decimal_uint("Calculated Exchange Rate", calcExchangeRate,0);
        emit log_named_decimal_uint("Calculated minted tokens", calculatedMinted,0);
        emit log_named_decimal_uint("Exchange rate", cETH.exchangeRateCurrent(),(0)); 
       emit log_named_uint("Calculated minted", calculatedMinted);
       uint256 actualMinted_cETH = cETH.balanceOf(here);
       assertEq(calculatedMinted,actualMinted_cETH);
       vm.roll(block.number+50000);
       uint256 currentBalanceMinted  = cETH.balanceOf(here);
       assertEq(cETH.redeem(currentBalanceMinted),0);
       assertEq(cETH.balanceOf(here),0);
       uint256 finalBalETH  = address(this).balance;
        assertGt(finalBalETH,initialBalETH);
      emit log_named_decimal_uint("ETH redeemed after advancing blocks", finalBalETH,18);

    } 
```

输出日志：

```
 Supplying liquidity into the cETH contract
  Supplied ETH liquidity: 1000000000000000000000
  Initial cETH balance of this contract:: 0
  cETH balance after calling mint:: 4979504810940.0
  Calculated Exchange Rate: 200823181815761189523999271.0
  Calculated minted tokens: 4979504810940.0
  Exchange rate: 200823181815761189523999271.0
  Calculated minted: 4979504810940
  ETH redeemed after advancing blocks: 1000.015064602371332482:
```

为了使本文简短，我跳过了所需的接口，也没有解释 Compound 开发人员文档和代码存储库中记录的功能。 我可以根据要求分享我的代码存储库。

> 最后的想法：这是一次有趣的学习体验，Foundry 速度极快且易于使用，我不认为自己会很快回到 Hardhat。

---

原文链接：[Foundry vs Hardhat](https://www.linkedin.com/pulse/foundry-vs-hardhat-sandip-nallani/?ref=defiplot.com)

DefiPlot翻译整理，转载请标明出处