-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathaula08.sol
127 lines (102 loc) · 4.04 KB
/
aula08.sol
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "./aula07.sol";
interface ICaixa {
function myBalance() external view returns (uint256);
}
contract Caixa {
// Payable address can receive Ether
address payable public owner;
event Track(string indexed _function, address sender, uint value, bytes data);
// Payable constructor can receive Ether
constructor() payable {
owner = payable(msg.sender);
}
// Function to deposit Ether into this contract.
// Call this function along with some Ether.
// The balance of this contract will be automatically updated.
function deposit() public payable {
emit Track("deposit()", msg.sender, msg.value, "");
}
// Call this function along with some Ether.
// The function will throw an error since this function is not payable.
function notPayable() public {}
// Function to withdraw all Ether from this contract.
function withdraw() public {
require(msg.sender == owner, "only owner can withdraw");
// get the amount of Ether stored in this contract
uint amount = address(this).balance;
// send all Ether to owner
// Owner can receive Ether since the address of owner is payable
(bool success, ) = owner.call{value: amount}("");
require(success, "Failed to send Ether");
}
// Function to transfer Ether from this contract to address from input
function transfer(address payable _to, uint _amount) public {
require(msg.sender == owner, "only owner can withdraw");
// Note that "to" is declared as payable
(bool success, ) = _to.call{value: _amount}("");
require(success, "Failed to send Ether");
}
receive() external payable {
(bool success, ) = msg.sender.call{value: msg.value}("");
require(success, "it was not sent to the owner");
emit Track("receive()", msg.sender, msg.value, "");
}
function myBalance() external view returns (uint256) {
return address(this).balance;
}
}
contract NoCash {
event Track(string indexed _function, address sender, uint value, bytes data);
fallback() external payable {
emit Track("fallback()", msg.sender, msg.value, msg.data);
}
}
contract Autorizador {
ICaixa public caixa;
IERC20 public token;
constructor() {
caixa = ICaixa(0xD7170F4cE3be4707Ee34c15De8057775414db5Ee);
token = IERC20(0x8eE5B68e89d86f5662d02200cD0FF7baa8065067);
}
function estaAutorizado(address _conta) external view returns (bool) {
return caixa.myBalance()>0 && token.balanceOf(_conta)>0;
}
function estouAutorizado() external view returns (bool) {
return token.balanceOf(address(this))>0;
}
}
contract ClienteBanco {
string public cpf;
IERC20 public token;
address public chaveUm;
address public chaveDois;
uint8 public autorizacoes;
event AutorizacaoDada(address chave, uint data);
constructor(string memory _cpf) {
cpf = _cpf;
chaveUm = msg.sender;
chaveDois = address(0x8e287B1F206eF762D460598bdE1A9C22db6b6382);
token = IERC20(0x8eE5B68e89d86f5662d02200cD0FF7baa8065067);
}
function saldoCliente() external view returns (bool) {
return token.balanceOf(address(this))>0;
}
function autorizo() external returns (bool) {
require(msg.sender == chaveUm || msg.sender == chaveDois, "somente o banco pode fazer essa operacao");
autorizacoes++;
emit AutorizacaoDada(msg.sender, block.timestamp);
return true;
}
function saqueTotal(address _to) external returns (bool) {
require(msg.sender == chaveUm || msg.sender == chaveDois, "somente o banco pode fazer essa operacao");
if (token.balanceOf(address(this)) > 1000) {
require(autorizacoes == 2, "nao possue autorizacoes necessarias");
}
bool success = token.transfer(_to, token.balanceOf(address(this)));
require(success, "houve falha no saque");
autorizacoes = 0;
return success;
}
}