Solidity – How to Fund an Address in Foundry Test

foundrysoliditytesting

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"

Best Answer

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}();
      ...
   }
}
Related Topic