-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW4day1.sol
More file actions
55 lines (42 loc) · 1.61 KB
/
W4day1.sol
File metadata and controls
55 lines (42 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
contract EthGas{
address payable public recipient = payable(msg.sender);//must be marke payable for an address to be able to receive eth
error EthSendFailed();
//transfer-Now discouraged
/*function Transfer(uint256 amount) public payable {
payable(msg.sender).transfer(amount);
}*/
//send-Adds complexity without solving gas issues in Transfer
/*function Success(uint256 amount) public payable{
bool success = payable(msg.sender).send(amount);
require(success, EthSendFailed());
}*/
//call-Recommended
function CAll(uint256 Amount) public payable {
(bool success, ) = payable(msg.sender).call{value:Amount}("");
require(success, EthSendFailed());
}
function GetBalance() public view returns(uint256) {
return address(this).balance;
}
receive() external payable{}
fallback() external payable{}
}
/*Write a withdraw function using call
Add require checks
Update state before sending ETH
Test sending ETH to a contract with receive function*/
contract WithdrawalFunc{
error InsufficientBalance();
mapping(address => uint256) public balances;
function Deposit(uint256 amount)public payable {
balances[msg.sender] += amount;
}
function Withdraw(uint256 amount) public payable {
require(amount <= msg.value, InsufficientBalance());
balances[msg.sender] -= amount;
(bool approved, ) = payable (msg.sender).call{value: amount}("");
require(approved, InsufficientBalance());
}
}