4

How can I fund an EOA inside a test case with Foundry.

Assume I have an NFT contract, want to test the minting functionality and I want to have an EOA that owns enough ether to mint, since users have to send ether to mint it.

Let's say I have

contract NFTCollectionTest is Test {
   ...
   function testMint() public {
      address someRandomUser = vm.addr(1);
      vm.prank(someRandomUser);
      nftCollection.mint{value: 0.01 ether}();
      ...
   }
}

But the test case failed. When I run the foundry test with more verbosity -vvvv I can see that "EvmError: OutOfFund"

2 Answers 2

6

For most such cases you need to find a vm function that does what you want. I recommend you check out this link. In this case it's vm.deal where you pass in the address as well as the amount of ether you want to give this address.

contract NFTCollectionTest is Test {
   ...
   function testMint() public {
      address someRandomUser = vm.addr(1);
      vm.prank(someRandomUser);
      vm.deal(someRandomUser, 1 ether);
      nftCollection.mint{value: 0.01 ether}();
      ...
   }
}
1
  • Why do we need to deal eth to a user when we prank, but if we send eth without pranking, the value is sent? Does the Test contract have unlimited funds?
    – sayandcode
    Commented Sep 18, 2023 at 13:16
0

While you can deal and separately prank, you can use hoax to do this in one step:

hoax(user);
myContract.doSomething();

hoax optionally accepts a second parameter that is the ETH balance to deal to the address. If not used, will default to 2^128 wei

Per docs: https://book.getfoundry.sh/reference/forge-std/hoax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.