0
pragma solidity ^0.8.4;

contract Contract {
    address payable public owner;
    address payable public charity;

    constructor(address payable _charity) {
        owner = payable(msg.sender);
        charity = _charity;
    }

    receive() external payable { }

    function donate() public {
        charity.transfer(address(this).balance);
    }

    function tip() public payable {
        owner.transfer(msg.value);
    }
}

msg.sender is the person deploys the contract

charity is charity

address(this) is contract address

When I use donate function "msg.value" sent to charity from owner ? But I use address(this).balance in this function. I dont use owner.

I dont understand what is happened when I use tip function ?

1
  • 1
    When you call donate() it sends from contract balance into charity address. When you call tip() you have to set some ETH to be sent to contract, that amount of ETH is msg.vlue and it will be sent to owner address so if you use owner wallet to call tip() you do not actually do anything Nov 11, 2021 at 13:36

1 Answer 1

1

The donate() function sends all the ETH of the contract to the address specified in the charity var.

The tip() function requires you to send ETH with your transaction. This is then transferred to the address specified in the owner var.

So if you are calling tip() from the owner address you are creating a tx which sends ETH to yourself.

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.