Skip to content

bug(cheatcodes): Facing issues with startPrank where address that have a balance do not work. #10302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 tasks done
blmalone opened this issue Apr 15, 2025 · 1 comment · Fixed by #10304
Closed
2 tasks done
Assignees
Labels
A-cheatcodes Area: cheatcodes T-bug Type: bug

Comments

@blmalone
Copy link

blmalone commented Apr 15, 2025

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge Version: 1.0.0-nightly Commit SHA: 7a7ad4e

What version of Foundryup are you on?

No response

What command(s) is the bug in?

The startPrank cheatcode vm.startPrank(address msgSender, bool delegateCall)

Operating System

macOS (Apple Silicon)

Describe the bug

This bug is related to a previous issue I opened here. I'm seeing some weird behavior with startPrank. When I use any address that contains a balance, the delegatecall fails. Please see the test cases below.

Steps To Reproduce:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;

import {Test} from "forge-std/Test.sol";

contract DelegateCallTest is Test {
    
    function testDelegateFails() external {
        vm.createSelectFork("sepolia");
        A a = new A();
        vm.startPrank(0x0fe884546476dDd290eC46318785046ef68a0BA9, true);
        (bool success,) = address(a).delegatecall(abi.encodeWithSelector(A.foo.selector));
        vm.stopPrank();
        // This is the weird behavior - success should be true but it isn't.
        assertFalse(success, "Delegate call should fail");
    }

    function testDelegatePassesWhenBalanceSetToZero() external {
        vm.createSelectFork("sepolia");
        A a = new A();
        vm.startPrank(0x0fe884546476dDd290eC46318785046ef68a0BA9, true);
        vm.deal(0x0fe884546476dDd290eC46318785046ef68a0BA9, 0 ether);
        (bool success,) = address(a).delegatecall(abi.encodeWithSelector(A.foo.selector));
        vm.stopPrank();
        // It starts working when the balance of the msg.sender is set to 0.
        assertTrue(success, "Delegate call should succeed");
    }

    function testDelegateCallSucceeds() external {
        vm.createSelectFork("sepolia");
        A a = new A();
        vm.startPrank(0xd363339eE47775888Df411A163c586a8BdEA9dbf, true);
        (bool success,) = address(a).delegatecall(abi.encodeWithSelector(A.foo.selector));
        vm.stopPrank();
        assertTrue(success, "Delegate call should succeed");
    }

    function testDelegateFailsWhenBalanceGtZero() external {
        vm.createSelectFork("sepolia");
        A a = new A();
        vm.startPrank(0xd363339eE47775888Df411A163c586a8BdEA9dbf, true);
        vm.deal(0xd363339eE47775888Df411A163c586a8BdEA9dbf, 1 ether);
        (bool success,) = address(a).delegatecall(abi.encodeWithSelector(A.foo.selector));
        vm.stopPrank();
        // It fails when the balance of the msg.sender is greater than 0.
        assertFalse(success, "Delegate call should fail");
    }
}

contract A {
    function foo() public pure returns (bool) {
        return true;
    }
}

Command to run:

forge test DelegateCallTest -vvvv

Logs:

[PASS] testDelegateFails() (gas: 214590)
Traces:
  [214590] DelegateCallTest::testDelegateFails()
    ├─ [0] VM::createSelectFork("sepolia")
    │   └─ ← [Return] 0
    ├─ [177020] → new A@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
    │   └─ ← [Return] 884 bytes of code
    ├─ [0] VM::startPrank(0x0fe884546476dDd290eC46318785046ef68a0BA9, true)
    │   └─ ← [Return]
    ├─ [45] A::foo() [delegatecall]
    │   └─ ← [Revert] EvmError: Revert
    ├─ [0] VM::stopPrank()
    │   └─ ← [Return]
    └─ ← [Stop]
@blmalone blmalone added T-bug Type: bug T-needs-triage Type: this issue needs to be labelled labels Apr 15, 2025
@blmalone
Copy link
Author

blmalone commented Apr 15, 2025

cc: @grandizzy - I hit this weird issue today. Some addresses don't work with vm.startPrank(address msgSender, bool delegateCall). I haven't been able to figure out why some addresses work and some don't.

Update: I realized if I have a balance at msg.sender, it breaks.

@github-project-automation github-project-automation bot moved this to Todo in Foundry Apr 15, 2025
@blmalone blmalone changed the title bug(cheatcodes): Facing issues with startPrank where certain addresses don't work. bug(cheatcodes): Facing issues with startPrank where address that have a balance do not work. Apr 15, 2025
@grandizzy grandizzy self-assigned this Apr 15, 2025
@grandizzy grandizzy moved this from Todo to Ready For Review in Foundry Apr 15, 2025
@zerosnacks zerosnacks added A-cheatcodes Area: cheatcodes and removed T-needs-triage Type: this issue needs to be labelled labels Apr 15, 2025
@github-project-automation github-project-automation bot moved this from Ready For Review to Done in Foundry Apr 15, 2025
@grandizzy grandizzy moved this from Done to Completed in Foundry Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes T-bug Type: bug
Projects
Status: Completed
Development

Successfully merging a pull request may close this issue.

3 participants