Uniswap – Why vm.startPrank Doesn’t Change msg.sender in Foundry

forgefoundrytestinguniswap

While I'm performing the testing in foundry. I'm facing an issue of vm.startPrank doesn't change the msg.sender.

CheatCodes cheats = CheatCodes(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
address userAcct = address(0xA6466D12A42B4496CD8ce61343aF392A8d7Bd871);
IERC20 UMT = IERC20(0xca861e289f04cB9C67fd6b87ca7EAFa59192f164); 

//setting up BSC mainnet fork
function setUp() public { 
        cheats.createSelectFork("bsc");
    }

//Performing the swap and startprank

 function testSwap() public {
        vm.startPrank(userAcct,userAcct);
        console.log("Message.Sender", msg.sender);
        console.log("This address", address(this));
        console.log("TX origin",tx.origin);

        uint256 userBalance = UMT.balanceOf(userAcct);
        console.log(userBalance);
        vm.stopPrank();
    }


As mentioned into the Foundry docs vm.startPrank will change the msg.sender but when I'm console logging it I'm still getting the default address.

Output


Message.Sender 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
  This address 0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496  
  TX origin 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38     
  4590405000000000000000

Traces:
  [19408] SwapTest::testSwap()
    ├─ [0] console::log(Fork Completed) [staticcall]
    │   └─ ← ()
    ├─ [0] VM::startPrank(0xA6466D12A42B4496CD8ce61343aF392A8d7Bd871, 0xA6466D12A42B4496CD8ce61343aF392A8d7Bd871)
    │   └─ ← ()
    ├─ [0] console::log(Message.Sender, DefaultSender: [0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38]) [staticcall]
    │   └─ ← ()
    ├─ [0] console::log(This address, SwapTest: [0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496]) [staticcall]
    │   └─ ← ()
    ├─ [0] console::log(TX origin, DefaultSender: [0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38]) [staticcall]
    │   └─ ← ()
    ├─ [2909] 0xca861e289f04cB9C67fd6b87ca7EAFa59192f164::balanceOf(0xA6466D12A42B4496CD8ce61343aF392A8d7Bd871) [staticcall]
    │   └─ ← 0x0000000000000000000000000000000000000000000000f8d8aaa27df1008000
    ├─ [0] console::f5b1bba9(0000000000000000000000000000000000000000000000f8d8aaa27df1008000) [staticcall]
    │   └─ ← ()
    ├─ [0] VM::stopPrank()
    │   └─ ← ()
    └─ ← ()


Best Answer

Doing it this way would be a more accurate test:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract address_test {
    function retSender() external view returns(address, address) {
        return (msg.sender, tx.origin);
    }
}
CheatCodes cheats = CheatCodes(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
address userAcct = address(0xA6466D12A42B4496CD8ce61343aF392A8d7Bd871);
IERC20 UMT = IERC20(0xca861e289f04cB9C67fd6b87ca7EAFa59192f164); 

//setting up BSC mainnet fork
function setUp() public { 
        cheats.createSelectFork("bsc");
    }

//Performing the swap and startprank

 function testSwap() public {
        vm.startPrank(userAcct, tx.origin);
        console.log("Message.Sender", msg.sender);
        console.log("This address", address(this));
        console.log("TX origin",tx.origin);
        address addr;
        address origin;
        (addr, origin) = membership.retSender();
        console.log("In contract Message.Sender", addr);
        console.log("In contract TX origin", origin);
        vm.stopPrank();
    }

In this case, I received the expected sender's address upon fulfillment of the contract

[PASS] testSwap() (gas: 17789)
Logs:
  Message.Sender 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
  This address 0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496
  TX origin 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
  In contract Message.Sender 0xA6466D12A42B4496CD8ce61343aF392A8d7Bd871 <== 
  In contract TX origin 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38 <==