-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiSigWallet.sol
123 lines (102 loc) · 3.27 KB
/
MultiSigWallet.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract MultiSigWallet {
event Deposit(address indexed sender, uint amount);
event Submit(uint indexed txId);
event Approve(address indexed owner, uint indexed txId);
event Revoke(address indexed owner, uint indexed txId);
event Execute(uint indexed txId);
struct Transtaction {
address to;
uint value;
bytes data;
bool executed;
}
address[] public owners;
mapping(address => bool) public isOwner;
uint public required;
Transtaction[] public transactions;
mapping(uint => mapping(address => bool)) public approved;
modifier onlyOwner() {
require(isOwner[msg.sender], "is not owner");
_;
}
modifier txExist(uint _txId) {
require(_txId < transactions.length, "tx doesn't exist");
_;
}
modifier notApproved(uint _txId) {
require(!approved[_txId][msg.sender], "tx already approved");
_;
}
modifier notExecuted(uint _txId) {
require(!transactions[_txId].executed, "tx already executed");
_;
}
constructor(address[] memory _owners, uint _required) {
require(_owners.length > 0, "You need to add at least one owner");
require(
_required > 0 && _required <= _owners.length,
"Invalid required number of owners"
);
for (uint i; i < _owners.length; i++) {
address owner = _owners[i];
require(owner != address(0), "invalid owner");
require(!isOwner[owner], "owner is not unique");
isOwner[owner] = true;
owners.push(owner);
}
}
receive() external payable {
emit Deposit(msg.sender, msg.value);
}
function submit(address _to, uint _value, bytes calldata _data)
external
onlyOwner
{
transactions.push(Transtaction({
to: _to,
value: _value,
data: _data,
executed: false
}));
emit Submit(transactions.length -1);
}
function approve(uint _txId)
external
onlyOwner
txExist(_txId)
notApproved(_txId)
notExecuted(_txId)
{
approved[_txId][msg.sender] = true;
emit Approve(msg.sender, _txId);
}
function _getApprovalCount(uint _txId) private view returns (uint count) {
for (uint i; i < owners.length; i++) {
if (approved[_txId][owners[i]]) {
count++;
}
}
}
function execute(uint _txId) external txExist(_txId) notExecuted(_txId) {
require(_getApprovalCount(_txId) >= required, "approvals < required");
Transtaction storage transaction = transactions[_txId];
transaction.executed = true;
(bool success, ) = transaction.to.call{value: transaction.value}(
transaction.data
);
require(success, "transaction failed");
emit Execute(_txId);
}
function revoke(uint _txId)
external
notExecuted(_txId)
onlyOwner
notExecuted(_txId)
{
require(approved[_txId][msg.sender], "you didn't approved the tx");
approved[_txId][msg.sender] = false;
emit Revoke(msg.sender, _txId);
}
}