-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat.sol
More file actions
277 lines (232 loc) · 8.23 KB
/
flat.sol
File metadata and controls
277 lines (232 loc) · 8.23 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Sources flattened with hardhat v2.22.10 https://hardhat.org
// SPDX-License-Identifier: MIT AND UNLICENSED
// File @openzeppelin/contracts/token/ERC20/IERC20.sol@v5.0.2
// Original license: SPDX_License_Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the value of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the value of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 value) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the
* allowance mechanism. `value` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
// File contracts/MultiSig.sol
// Original license: SPDX_License_Identifier: UNLICENSED
pragma solidity ^0.8.24;
// Uncomment this line to use console.log
// import "hardhat/console.sol";
contract MultiSig {
address[] public signers;
uint8 public qorum;
uint8 public noValidSigners;
uint8 public trxId;
struct Transaction{
uint8 id;
address recipient;
uint256 amount;
address sender;
address tokenAddress;
uint256 transactionTime;
uint8 numberOfApproval;
bool isCompleted;
address[] _signee;
}
struct QorumUpdate{
uint8 proposedQorum;
uint8 numberOfApproval;
bool isCompleted;
address[] _signee;
}
mapping(uint8=>QorumUpdate) public qorumUpdate;
mapping (address=>bool) isValidSigners;
mapping(address=>mapping(uint256=> bool)) hasSigned;
mapping(address=>mapping(uint256=>bool)) hasSignedQorum;
mapping(uint8=>Transaction) public transactions;
uint8 public qorumId;
constructor(uint8 _qorum, address[] memory _signers){
signers = _signers;
for(uint8 i=0; i<_signers.length; i++){
address validSigner = _signers[i];
if(validSigner==address(0)){
revert ZeroAddressDetected();
}
require(!isValidSigners[validSigner], "Signer already exist");
isValidSigners[_signers[i]]= true;
}
noValidSigners =uint8(_signers.length);
if(!isValidSigners[msg.sender]){
isValidSigners[msg.sender] = true;
noValidSigners += 1;
}
if(_qorum<=0 || _qorum>noValidSigners){
revert NotAValidQorum();
}
qorum =_qorum;
}
error ZeroAddressDetected();
error InvalidTransaction();
error TransactionCompleted();
error InsufficientBalance();
error CannotSignTransactionTwice();
error NotAValidSigner();
error NotAValidQorum();
function transfer(address _recipient, uint256 _amount, address _tokenAddress) external {
if(_recipient==address(0)){
revert ZeroAddressDetected();
}
if(!isValidSigners[msg.sender]){
revert NotAValidSigner();
}
if(_amount<=0 || _amount>IERC20(_tokenAddress).balanceOf(address(this))){
revert InsufficientBalance();
}
uint8 _trxid = trxId +1;
Transaction storage _transaction = transactions[_trxid];
_transaction.amount=_amount;
_transaction.tokenAddress =_tokenAddress;
_transaction.sender = msg.sender;
_transaction.recipient=_recipient;
_transaction.id = _trxid;
_transaction.transactionTime = block.timestamp;
_transaction.numberOfApproval +=1;
_transaction._signee.push(msg.sender);
hasSigned[msg.sender][_trxid] =true;
trxId +=1;
}
function approveTransaction(uint8 _trxId) external{
if(_trxId==0){
revert InvalidTransaction();
}
Transaction storage _transaction = transactions[_trxId];
require(IERC20(_transaction.tokenAddress).balanceOf(address(this))>=_transaction.amount, "Insufficient Funds");
if(_transaction.isCompleted){
revert TransactionCompleted();
}
if(hasSigned[msg.sender][_trxId]){
revert CannotSignTransactionTwice();
}
_transaction.numberOfApproval +=1;
_transaction._signee.push(msg.sender);
if(_transaction.numberOfApproval==qorum){
_transaction.isCompleted =true;
IERC20(_transaction.tokenAddress).transfer(_transaction.recipient, _transaction.amount);
}
}
function withdraw(uint256 _amount, address _tokenAddress) external {
if(msg.sender==address(0)){
revert ZeroAddressDetected();
}
if (_amount == 0 || _amount > IERC20(_tokenAddress).balanceOf(address(this))) {
revert InsufficientBalance();
}
uint8 _trxid = trxId +1;
Transaction storage _transaction = transactions[_trxid];
_transaction.amount=_amount;
_transaction.tokenAddress =_tokenAddress;
_transaction.sender = address(this);
_transaction.recipient = msg.sender;
_transaction.transactionTime = block.timestamp;
_transaction.numberOfApproval +=1;
_transaction._signee.push(msg.sender);
hasSigned[msg.sender][_trxid] =true;
trxId +=1;
}
function updateQorum(uint8 newQorum) external {
if(!isValidSigners[msg.sender]){
revert NotAValidSigner();
}
if(newQorum<=0 || newQorum>noValidSigners){
revert NotAValidQorum();
}
uint8 qId = qorumId+1;
QorumUpdate storage _qorumUpdate = qorumUpdate[qId];
_qorumUpdate.proposedQorum = newQorum;
_qorumUpdate.numberOfApproval +=1;
_qorumUpdate._signee.push(msg.sender);
hasSignedQorum[msg.sender][qId]= true;
qorumId+=1;
}
function approveQorumUpdate(uint8 _qorumId) external {
if (_qorumId == 0) {
revert InvalidTransaction();
}
if(!isValidSigners[msg.sender]){
revert NotAValidSigner();
}
QorumUpdate storage _qorumUpdate = qorumUpdate[_qorumId];
if (_qorumUpdate.isCompleted) {
revert TransactionCompleted();
}
if(hasSignedQorum[msg.sender][_qorumId]){
revert CannotSignTransactionTwice();
}
_qorumUpdate.numberOfApproval+=1;
_qorumUpdate._signee.push(msg.sender);
hasSignedQorum[msg.sender][_qorumId]= true;
if(_qorumUpdate.numberOfApproval>=qorum){
_qorumUpdate.isCompleted=true;
qorum=_qorumUpdate.proposedQorum;
}
}
}