0

when calling the withdraw function , where does the value of (address(this).balance) go ?


contract Fallback {

 mapping(address => uint) public contributions;
 address public owner;

 constructor() {
   owner = msg.sender;
   contributions[msg.sender] = 1000 * (1 ether);
 }

 modifier onlyOwner {
       require(
           msg.sender == owner,
           "caller is not the owner"
       );
       _;
   }

 function contribute() public payable {
   require(msg.value < 0.001 ether);
   contributions[msg.sender] += msg.value;
   if(contributions[msg.sender] > contributions[owner]) {
     owner = msg.sender;
   }
 }

 function getContribution() public view returns (uint) {
   return contributions[msg.sender];
 }

 function withdraw() public onlyOwner {
   payable(owner).transfer(address(this).balance);
 }

 receive() external payable {
   require(msg.value > 0 && contributions[msg.sender] > 0);
   owner = msg.sender;
 }
}
1
  • payable(owner).transfer(address(this).balance); initiates a transfer of address(this).balance eth to payable(owner). owner is set in the constructor as the msg.sender. Commented Feb 9, 2023 at 1:31

2 Answers 2

0

The function is sending the balance of the contract to the owner address. If we look at the constructor:

 constructor() {
   owner = msg.sender;
   contributions[msg.sender] = 1000 * (1 ether);
 }

We can notice that the owner is set to msg.sender. In other words, the eth is transferred to the person who have minted the contract.

0

the value is being redirected to the address that minted the contract.

 constructor() {
   **owner = msg.sender;**
   contributions[msg.sender] = 1000 * (1 ether);
 }

the constructor is a function automatically executed at the creation of the contract, this is how you get to define the "owner" public variable.

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.