-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiSig.sol
90 lines (75 loc) · 2.53 KB
/
MultiSig.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract MultiSig {
address[] public owners;
uint256 public required;
struct Transaction{
address dst_tnx;
uint256 value;
bool executed;
bytes data;
}
mapping(uint => Transaction) public transactions;
mapping(uint => mapping(address => bool)) public confirmations;
mapping(uint => uint) no_of_confirmation;
uint public transactionCount;
constructor(address[] memory _owners, uint256 _required){
require(_owners.length != 0, "Owners array is empty");
require(_required != 0, "required number is zero");
require(_required <= _owners.length, "required confirmation is more than owners");
owners = _owners;
required = _required;
}
receive() external payable{
}
function addTransaction(address _dst_tnx, uint256 _value, bytes memory _data) internal returns(uint256 tnx_id)
{
Transaction memory tnx = Transaction(_dst_tnx, _value, false, _data);
transactions[transactionCount] = tnx;
transactionCount ++;
return transactionCount - 1;
}
function confirmTransaction(uint256 _id) public ownerOnly {
confirmations[_id][msg.sender] = true;
no_of_confirmation[_id] = no_of_confirmation[_id] + 1;
if(no_of_confirmation[_id] == required)
{
executeTransaction(_id);
}
}
function getConfirmationsCount(uint _id) public view returns(uint256 no_of_confirm)
{
return no_of_confirmation[_id];
}
function isConfirmed(uint tnx_id) public view returns(bool confirm)
{
if (no_of_confirmation[tnx_id] < required)
{
return false;
}
return true;
}
function submitTransaction(address _dst_tnx, uint _value, bytes memory _data) external{
uint tnx_id = addTransaction(_dst_tnx,_value, _data);
confirmTransaction(tnx_id);
}
function executeTransaction(uint tnx_id) public{
require(isConfirmed(tnx_id));
Transaction storage tnx = transactions[tnx_id];
(bool success, ) = tnx.dst_tnx.call{value: tnx.value}(tnx.data);
require(success, "Failed to execute transaction");
tnx.executed = true;
}
modifier ownerOnly(){
bool owner;
for(uint i = 0; i < owners.length; i++)
{
if(owners[i] == msg.sender)
{
owner = true;
}
}
require(owner, "caller is not owner");
_;
}
}