Foundry作弊码第五部分:使用vm.deal即时设置ETH

本文介绍了Foundry框架中的vm.deal作弊码,它可以直接为测试账户提供ETH或代币,无需水龙头或转账,从而方便模拟富裕用户、支付gas或测试payable流程。

高级 Foundry Cheatcodes 系列:第五部分 - Cheatcode vm.deal,资金账户

使用 Foundry 的 vm.deal 立即用 ETH 或 tokens 资助测试账户。模拟存款、支付 gas 或测试 payable 的流程,无需任何设置。

Advanced Foundry Cheatcodes Series: Part 5 - Cheatcode vm.deal, Funding Accounts

Foundry

介绍

在第四部分学习了如何弯曲时间后,凭空造出一些以太币也是很公平的。本章展示了 vm.deal 如何立即充值任何地址,无需 faucet,无需转账,因此你可以模拟富有的用户,支付 gas 或测试 payable 的流程,而零摩擦。

vm.deal 的作用

在测试中,你通常需要一个账户拥有 ETH(例如,支付 gas 或进行 payable 调用)。vm.deal(address who, uint256 newBalance) cheatcode 简单地将 who 的 Ether 余额设置为 newBalance。例如:

vm.deal(address(0xbabe), 100 ether);

From now on, 0xbabe has 100 ETH to play with, even if it was just a random address. 从现在开始,0xbabe 有 100 ETH 可以使用,即使它只是一个随机地址。

Using vm.deal

vm.deal is especially useful in tests where you need to pay gas or test payable functions.

Paying Gas

The default msg.sender for test calls is the test contract itself, which starts with zero Ether. To send transactions, you must first fund the test contract. As a contrived example: vm.deal 在需要支付 gas 或测试 payable 函数的测试中特别有用。

支付 Gas

测试调用的默认 msg.sender 是测试合约本身,它以零 Ether 开始。要发送交易,你必须首先资助测试合约。作为一个虚构的例子:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Script.sol";

contract Example is Script {
    function run() external {
        // Fund our contract so it can pay gas
        // 为我们的合约提供资金,以便它可以支付 gas
        vm.deal(address(this), 1 ether);

        // Now we can deploy contracts
        // 现在我们可以部署合约
        new ExampleConsumer();
    }
}

contract ExampleConsumer {
    constructor() payable {}
}

Without vm.deal, the transaction would revert due to insufficient funds. 如果没有 vm.deal,交易会因为资金不足而回滚。

Testing Payable Functions

Here’s a more interesting example where vm.deal lets us simulate users calling the deposit function with different amounts. 这是一个更有趣的例子,其中 vm.deal 让我们模拟用户使用不同金额调用 deposit 函数。

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";

contract Foo {
    uint256 public total;

    function deposit() external payable {
        total += msg.value;
    }
}

contract DealTest is Test {
    Foo foo;

    function setUp() public {
        foo = new Foo();
    }

    function testDeposit() public {
        // Fund two addresses, bob with 2 ether, alice with 1 ether
        // 为两个地址提供资金,bob 提供 2 ether,alice 提供 1 ether
        vm.deal(address(bob), 2 ether);
        vm.deal(address(alice), 1 ether);

        // Bob calls deposit
        // Bob 调用 deposit
        vm.prank(address(bob));
        foo.deposit{value: 2 ether}();
        assertEq(foo.total(), 2 ether);

        // Alice calls deposit
        // Alice 调用 deposit
        vm.prank(address(alice));
        foo.deposit{value: 1 ether}();
        assertEq(foo.total(), 3 ether);
    }
}

We use vm.prank to set msg.sender to Bob and Alice respectively, so vm.deal lets us simulate them calling the deposit function with different amounts. 我们使用 vm.prankmsg.sender 分别设置为 Bob 和 Alice,因此 vm.deal 让我们模拟他们使用不同金额调用 deposit 函数。

Minting Tokens

If you want to mint tokens easily, use the vm.mint cheatcode instead. 如果你想轻松地凭空生成 tokens,请改用 vm.mint cheatcode。

Conclusion

Now, you can fund any account at will with vm.deal, allowing you to simulate rich users, pay gas, or test payable flows with zero setup. 现在,你可以随意使用 vm.deal 为任何账户提供资金,从而使你可以模拟富有的用户,支付 gas 或测试 payable 的流程,而无需任何设置。

In Part 6, we’ll look at the vm.label cheatcode, which lets you label addresses in your tests, making them much easier to read. 在第 6 部分中,我们将研究 vm.label cheatcode,它允许你在测试中标记地址,使其更易于阅读。

  • 原文链接: threesigma.xyz/blog/foun...
  • 登链社区 AI 助手,为大家转译优秀英文文章,如有翻译不通的地方,还请包涵~
点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
Three Sigma
Three Sigma
Three Sigma is a blockchain engineering and auditing firm focused on improving Web3 by working closely with projects in the space.