Practice Sending Ether: Practice using .call

Storing Owner

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

contract Contract {
	address public owner;

	constructor() {
		owner = msg.sender;
	}
}

Receive Ether

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

contract Contract {
	address public owner;

	constructor() {
		owner = msg.sender;
	}

	receive() external payable {
		
	}
}

Tip Owner

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

contract Contract {
	address public owner;

	constructor() {
		owner = msg.sender;
	}

	receive() external payable {
		
	}

	function tip() public payable {
		(bool success, ) = owner.call{ value: msg.value }("");
		require(success);
	}
}

Charity

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

contract Contract {
	address public owner;
	address public charity;

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

	receive() external payable {
		
	}

	function donate() public {
		(bool success, ) = charity.call{ value: address(this).balance }("");
		require(success);
	}

	function tip() public payable {
		(bool success, ) = owner.call{ value: msg.value }("");
		require(success);
	}
}

Self Destruct

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

contract Contract {
	address public owner;
	address public charity;

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

	receive() external payable {
		
	}

	function donate() public {
		selfdestruct(payable(charity));
	}

	function tip() public payable {
		(bool success, ) = owner.call{ value: msg.value }("");
		require(success);
	}
}

Learn Solidity. Alchemy University.
Accessed December 30, 2024
https://university.alchemy.com/overview/solidity