Skip to content

Commit 52bb8f7

Browse files
committed
init commit 2
1 parent 59f00d8 commit 52bb8f7

File tree

16 files changed

+557
-58
lines changed

16 files changed

+557
-58
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"solidity.formatter": "forge"
3+
}

foundry.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ src = "src"
33
out = "out"
44
libs = ["lib"]
55

6+
mappings = ["@openzeppelin/=lib/openzeppelin-contracts"]
7+
68
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options

lib/openzeppelin-contracts

script/Counter.s.sol

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/BankV1.sol

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.0;
3+
4+
contract BankV1 {
5+
mapping(address account => bool activated) private activatedAccounts;
6+
mapping(address account => uint256 password) private accountsPasswords;
7+
mapping(address account => uint256 balance) private accountsBalances;
8+
9+
/// @notice Create new Account in the Bank
10+
/// @param password Account password that will be used in authentication when withdrawing
11+
function createAccount(uint256 password) external {
12+
activatedAccounts[msg.sender] = true;
13+
accountsPasswords[msg.sender] = password;
14+
}
15+
16+
/// @notice adding provided account balance by the value of sent Ether
17+
/// @dev Allows anyone to deposit ether to any
18+
/// @param account The account receiving the deposited Ether
19+
function depositEther(address account) external payable {
20+
require(activatedAccounts[account], "Account is not activated yet");
21+
require(msg.value > 0, "No Ether sent");
22+
23+
accountsBalances[account] += msg.value;
24+
}
25+
26+
/// @notice Withdrawing ether from a given account and send him to the recipent
27+
/// @dev the sender provide a valid password for the account to withdraw from it
28+
/// @param account The account to withdraw ether from
29+
/// @param password The password for that account
30+
/// @param amount The amount to withdraw
31+
/// @param recipent The receiver of ether
32+
function withdrawEther(address account, uint256 password, uint256 amount, address recipent) external {
33+
require(activatedAccounts[account], "Account is not activated yet");
34+
require(accountsPasswords[account] == password, "Account is not activated yet");
35+
require(accountsBalances[account] >= amount, "Amount to withdraw exceeds balance");
36+
37+
accountsBalances[account] -= amount;
38+
(bool success,) = recipent.call{value: amount}("");
39+
require(success, "Withdrawal Failed");
40+
}
41+
}

src/BankV2.sol

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.0;
3+
4+
contract BankV2 {
5+
mapping(address account => uint256 balance) private accountsBalances;
6+
7+
/// @notice Adding sender balance by the amount of ether sent
8+
function depositEther() external payable {
9+
require(msg.value > 0, "No Ether sent");
10+
accountsBalances[msg.sender] += msg.value;
11+
}
12+
13+
/// @notice withdraw All ether hold by the caller and send them to him
14+
/// @dev reverts if there is no ether hold by the caller
15+
function withdrawEther() external {
16+
uint256 withdrawalAmount = accountsBalances[msg.sender];
17+
require(withdrawalAmount > 0, "No ether to withdraw");
18+
19+
(bool success,) = msg.sender.call{value: withdrawalAmount}("");
20+
require(success, "Withdrawal Failed");
21+
22+
accountsBalances[msg.sender] = 0;
23+
}
24+
}

src/BankV3.sol

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.0;
3+
4+
import {IERC20} from "forge-std/interfaces/IERC20.sol";
5+
6+
contract BankV3 {
7+
mapping(address account => uint256 balance) private accountsBalances;
8+
9+
address public immutable USDT;
10+
11+
constructor(address usdt) {
12+
USDT = usdt;
13+
}
14+
15+
/// @notice Adding sender balance by the amount of USD
16+
/// @param amount The amount to deposit to the caller
17+
function depositUSD(uint256 amount) external payable {
18+
require(amount > 0, "No USD sent");
19+
bool success = IERC20(USDT).transferFrom(msg.sender, address(this), amount);
20+
require(success, "Transfering Failed");
21+
22+
accountsBalances[msg.sender] += amount;
23+
}
24+
25+
/// @notice withdraw the amount of USD send the caller and send them to receipent
26+
/// @param amount the amount of USD to withdraw
27+
/// @param receipent The receiver that we will take the withdrawn USD
28+
function withdrawUSD(uint256 amount, address receipent) external {
29+
uint256 withdrawalAmount = accountsBalances[msg.sender];
30+
require(withdrawalAmount >= amount, "Amount to withdraw exceeds balance");
31+
32+
bool success = IERC20(USDT).transfer(receipent, amount);
33+
require(success, "Transfering Failed");
34+
accountsBalances[msg.sender] -= amount;
35+
}
36+
37+
/// @notice withdraw USD with calling a Fallcak function
38+
/// @param amount the amount of USD to withdraw
39+
/// @param receipent The receiver that we will take the withdrawn USD
40+
/// @param data The Fallback function signature we will call on receipent
41+
function withdrawFallbackUSD(uint256 amount, address receipent, bytes memory data) external {
42+
uint256 withdrawalAmount = accountsBalances[msg.sender];
43+
require(withdrawalAmount >= amount, "Amount to withdraw exceeds balance");
44+
45+
bool success = IERC20(USDT).transfer(receipent, amount);
46+
require(success, "Transfering Failed");
47+
accountsBalances[msg.sender] -= amount;
48+
49+
(bool fallbackSuccess,) = receipent.call(data);
50+
require(fallbackSuccess, "Fallback Function reverted");
51+
}
52+
}

src/BankV4.sol

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.0;
3+
4+
import {IERC20} from "forge-std/interfaces/IERC20.sol";
5+
import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";
6+
7+
contract BankV4 is AccessControl {
8+
mapping(address account => uint256 balance) public accountsBalances;
9+
10+
bytes32 public constant BLACKLISTED_WITHDRAW = keccak256("BLACKLISTED_WITHDRAW");
11+
12+
address public immutable USDT;
13+
14+
constructor(address usdt, address admin) {
15+
USDT = usdt;
16+
_grantRole(DEFAULT_ADMIN_ROLE, admin);
17+
}
18+
19+
/// @notice Adding sender balance by the amount of USD sent
20+
function depositUSD(uint256 amount) external {
21+
require(amount > 0, "No USD sent");
22+
bool success = IERC20(USDT).transferFrom(msg.sender, address(this), amount);
23+
require(success, "Transfering Failed");
24+
25+
accountsBalances[msg.sender] += amount;
26+
}
27+
28+
/// @notice withdraw the amount of USD send the caller and send them to receipent
29+
/// @dev reverts if the caller is BLACKLISTED
30+
/// @param amount the amount of USD to withdraw
31+
/// @param receipent The receiver that we will take the withdrawn USD
32+
function withdrawUSD(uint256 amount, address receipent) external {
33+
require(!hasRole(BLACKLISTED_WITHDRAW, msg.sender), "Account is BlackListed");
34+
35+
uint256 withdrawalAmount = accountsBalances[msg.sender];
36+
require(withdrawalAmount >= amount, "Amount to withdraw exceeds balance");
37+
38+
bool success = IERC20(USDT).transfer(receipent, amount);
39+
require(success, "Transfering Failed");
40+
accountsBalances[msg.sender] -= amount;
41+
}
42+
43+
/// @notice Blacklist a given account and prevent him from withdrawing
44+
/// @param account The account to add to blacklist
45+
function blackListAccount(address account) external onlyRole(DEFAULT_ADMIN_ROLE) {
46+
bool success = _grantRole(BLACKLISTED_WITHDRAW, account);
47+
require(success, "Account was already Black Listed");
48+
}
49+
50+
/// @notice unBlacklist a given account and prevent him from withdrawing
51+
/// @param account The account to remove from blacklist
52+
function unBlackListAccount(address account) external onlyRole(DEFAULT_ADMIN_ROLE) {
53+
bool success = _revokeRole(BLACKLISTED_WITHDRAW, account);
54+
require(success, "Account was not Black Listed");
55+
}
56+
57+
/// @notice take the amount of USD hold by a given blacklisted account
58+
/// @param account The blacklisted account to take his balance
59+
/// @param receipent The receiver of the USD
60+
function drainBlackListedBalance(address account, address receipent) external onlyRole(DEFAULT_ADMIN_ROLE) {
61+
require(hasRole(BLACKLISTED_WITHDRAW, account), "Accound is not BlackListed");
62+
63+
uint256 amount = accountsBalances[account];
64+
bool success = IERC20(USDT).transfer(receipent, amount);
65+
require(success, "Transfering Failed");
66+
accountsBalances[msg.sender] = 0;
67+
}
68+
}

src/BankV5.sol

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.0;
3+
4+
import {IERC20} from "forge-std/interfaces/IERC20.sol";
5+
6+
contract BankV5 {
7+
mapping(address account => uint256 balance) private accountsBalances;
8+
9+
address public immutable WETH;
10+
11+
constructor(address weth) {
12+
WETH = weth;
13+
}
14+
15+
/// @notice Adding sender balance by the amount of WETH sent
16+
/// @param amount the amount of deposited WETH
17+
function depositWETH(uint256 amount) external payable {
18+
require(amount > 0, "No amount sent");
19+
bool success = IERC20(WETH).transferFrom(msg.sender, address(this), amount);
20+
require(success, "Transfering Failed");
21+
22+
accountsBalances[msg.sender] += amount;
23+
}
24+
25+
/// @notice withdraw the amount of WETH send the caller and send them to receipent
26+
/// @param amount the amount of WETH to withdraw
27+
/// @param receipent The receiver that we will take the withdrawn WETH
28+
function withdrawWETH(uint256 amount, address receipent) external {
29+
uint256 withdrawalAmount = accountsBalances[msg.sender];
30+
require(withdrawalAmount >= amount, "Amount to withdraw exceeds balance");
31+
32+
bool success = IERC20(WETH).transferFrom(address(this), receipent, amount);
33+
require(success, "Transfering Failed");
34+
accountsBalances[msg.sender] -= amount;
35+
}
36+
}

src/Counter.sol

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)