Lesson 7: Understanding interactions #296
-
I'm currently doing the last part of this lesson where I'm making the interaction test files but I don't quite get what's going on here
contract InteractionsTest is Test {
FundMe fundMe;
address USER = makeAddr("user");
uint256 constant SEND_VALUE = 0.1 ether;
uint256 constant STARTING_BALANCE = 10 ether;
uint256 constant GAS_PRICE = 1;
function setUp() external {
DeployFundMe deploy = new DeployFundMe();
fundMe = deploy.run();
vm.deal(USER, STARTING_BALANCE);
}
function testUserCanFundInteractions() public {
FundFundMe fundFundMe = new FundFundMe();
fundFundMe.fundFundMe(address(fundMe));
WithdrawFundMe withdrawFundMe = new WithdrawFundMe();
withdrawFundMe.withdrawFundMe(address(fundMe));
assert(address(fundMe).balance == 0);
}
}
contract FundFundMe is Script {
uint256 constant SEND_VALUE = 0.1 ether;
function fundFundMe(address mostRecentlyDeployed) public {
vm.startBroadcast();
FundMe(payable(mostRecentlyDeployed)).fund{value: SEND_VALUE}();
vm.stopBroadcast();
}
function run() external {
address mostRecentlyDeployed = DevOpsTools.get_most_recent_deployment(
"FundMe",
block.chainid
);
fundFundMe(mostRecentlyDeployed);
}
}
contract WithdrawFundMe is Script {
function withdrawFundMe(address mostRecentlyDeployed) public {
vm.startBroadcast();
FundMe(payable(mostRecentlyDeployed)).withdraw();
vm.stopBroadcast();
}
function run() external {
address mostRecentlyDeployed = DevOpsTools.get_most_recent_deployment(
"FundMe",
block.chainid
);
withdrawFundMe(mostRecentlyDeployed);
}
} In And if that's the case, why am I calling the Also, I'm having some trouble understanding what's exactly going on on this line: Am I calling the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hello @NotNotRama
Yes, you're creating a new instance of The line
If you check at the top of |
Beta Was this translation helpful? Give feedback.
-
Hello! Great question. And fantastic formatting on your question :) We don't call FundFundMe fundFundMe = new FundFundMe(); This is where solidity scripting can be a bit confusing. In order for us to run the fundFundMe.fundFundMe(address(fundMe)); We call
However, in our tests, we just pass the FundMe(payable(mostRecentlyDeployed)).fund{value: SEND_VALUE}(); This is the line that actually calls the FundMe(payable(mostRecentlyDeployed)) We are saying: Then: .fund{value: SEND_VALUE}(); This is saying "call the Does that make sense? |
Beta Was this translation helpful? Give feedback.
So
run
is only called when we call the script from the command line, in our tests here, we actually never call it!^ This is the command that will call run. In our tests we don't bother.
On your other question, this is where the "magic" of forge scripting happens. Whatever the private key is being used to run the test sends the value. In forge test, it magically creates some fake accounts for you that have value in them to start.