0

I have a raffle contract that required a custom ERC20 token as an entrance fee. In Raffle contract I have the enterRaffle function:

   function enterRaffle() public payable {
        uint256 walletBalance = i_raffleToken.balanceOf(msg.sender);
        console.log("msg.value: ", msg.value);
        console.log("walletBalance: ", walletBalance); // this is 0 and don t know how to initliaze with some dummy tokens. vm.deal() works only for ether
        console.log("i_entranceFee: ", i_entranceFee);

        if (walletBalance < i_entranceFee) {
            revert Raffle__SendMoreToEnterRaffle();
        }
        ... other code
    }

My test file looks like this:

 ... imports

contract RaffleTest is StdCheats, Test {
    /* Errors */
    event RequestedRaffleWinner(uint256 indexed requestId);
    event RaffleEnter(address indexed player);
    event WinnerPicked(address indexed player);

    Raffle public raffle;
    HelperConfig public helperConfig;

    uint64 subscriptionId;
    bytes32 gasLane;
    uint256 automationUpdateInterval;
    uint256 raffleEntranceFee;
    uint32 callbackGasLimit;
    address vrfCoordinatorV2;

    address public PLAYER = makeAddr("player");
    uint256 public constant STARTING_USER_BALANCE = 10 ether;

    function setUp() external {
        DeployRaffle deployer = new DeployRaffle();
        (raffle, helperConfig) = deployer.run();
        vm.deal(PLAYER, STARTING_USER_BALANCE); // how do I do this for a custom token

        (
            ,
            gasLane,
            automationUpdateInterval,
            raffleEntranceFee,
            callbackGasLimit,
            vrfCoordinatorV2, // link
            // deployerKey
            ,
            ,

        ) = helperConfig.activeNetworkConfig();
    }

    /////////////////////////
    // fulfillRandomWords //
    ////////////////////////

    modifier raffleEntered() {
        vm.prank(PLAYER);
        raffle.enterRaffle{value: raffleEntranceFee}();
        vm.warp(block.timestamp + automationUpdateInterval + 1);
        vm.roll(block.number + 1);
        _;
    }

   // function that fails test because if the raffleEntered modifier
    function testFulfillRandomWordsCanOnlyBeCalledAfterPerformUpkeep()
        public
        raffleEntered
        onlyOnDeployedContracts
    {
        // Arrange
        // Act / Assert
        vm.expectRevert("nonexistent request");
        // vm.mockCall could be used here...
        VRFCoordinatorV2Mock(vrfCoordinatorV2).fulfillRandomWords(
            0,
            address(raffle)
        );

        vm.expectRevert("nonexistent request");

        VRFCoordinatorV2Mock(vrfCoordinatorV2).fulfillRandomWords(
            1,
            address(raffle)
        );
    }

How do I do vm.deal(PLAYER, STARTING_USER_BALANCE) for a custom ERC20 token ?

2

1 Answer 1

1

To deal a custom erc20 token in Foundry tests, here is the right way to do it :

deal(address(_weth), seller, 10 ether);

as seen in the foundry book:

foundry - deal

https://book.getfoundry.sh/cheatcodes/deal?highlight=deal#deal

here is one of my test that works fine in my setUp() using it :

deal working fine in setUp()

You can use to that to increase a balance and you can also decrease. You just need to set it to the new balance you want. It will declare as balance the amount you state and replace the previous one with it (not add or substract).

Or you can just mint some tokens from an address that has the necessary rights (by using vm.prank() if needs to) and then send it to the address you want to use for your test.

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.