vm.prank( ) cannot understand . #164
-
Can someone explain the use of vm.prank with an example. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Before speaking about In foundry everything is a conctract -- scripts, tests, libraries, imports, etc..., due to this reason Foundry has set up a serie of default addresses that acts as the When we execute a test, is not our private key that is being used, in fact is a key from Foundry. function testConsoleCaller() public view {
console.log(msg.sender); // is not you!
} This behaviour could be problematic depending on the test, for example the function testCanWithdrawTheFund() public {
fundMe.withdraw(); // the test fails because the default address that execute the file is not the owner
} And for cases like this one function testCanWithdrawTheFund() public {
vm.prank(USER); // your key will be used to withdraw the funds
fundMe.withdraw();
}
function testCanFundAndWithdraw() public {
vm.prank(USER); // your key will be used to fund the contract
fundMe.fund{value: 1 ether}(); // prank only works in this transaction and then it's over
fundMe.withdraw(); // the `msg.sender` is again the key from Foundry
} If you want to bundle transactions use function testCanWithdrawTheFund() public {
vm.startPrank(USER); // your key will be used for all the next transactions until you call `stopPrank`
fundMe.fund{value: 1 ether}();
fundMe.withdraw();
vm.stopPrank();
} This is a recopilation of the behaviours I have noticed from |
Beta Was this translation helpful? Give feedback.
-
thanks man! |
Beta Was this translation helpful? Give feedback.
Before speaking about
prank
, let me explain a little bit about what I've learned about Foundry.In foundry everything is a conctract -- scripts, tests, libraries, imports, etc..., due to this reason Foundry has set up a serie of default addresses that acts as the
msg.sender
to execute these files and the functions in them.When we execute a test, is not our private key that is being used, in fact is a key from Foundry.
This behaviour could be problematic depending on the test, for example the
withdraw
function from theFundMe
contract has a modifier that only allows the owner to withdraw the funds, bu…