-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitz.sol
More file actions
70 lines (53 loc) · 1.99 KB
/
splitz.sol
File metadata and controls
70 lines (53 loc) · 1.99 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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Splitz {
// stores list of recipients and their ids
address payable [] public recipients;
event TransferReceived(address _from, uint _amount);
event newSplit(address payable [] recipients, address owner);
event AddedRecipient(address recipientAdd);
event RemovedRecipient(address recipientRemove);
address payable public splitOwner;
// maps address to array id
mapping(address => uint) public addressToUint;
modifier onlyOwner {
require(msg.sender == splitOwner);
_;
}
receive() payable external {
uint256 share = msg.value / recipients.length;
for (uint i=0; i< recipients.length; i++){
recipients[i].transfer(share);
}
emit TransferReceived(msg.sender, msg.value);
}
function createSplit (address payable _owner, address payable [] memory newRecipients) external {
for(uint i=0; i<newRecipients.length; i++) {
recipients.push(newRecipients[i]);
}
// sets splitOwner
splitOwner = _owner;
emit newSplit(newRecipients, _owner);
}
// add recipient with address
function addRecipient( address payable newRecipient) onlyOwner external {
recipients.push(newRecipient);
emit AddedRecipient(newRecipient);
}
// remove recipient with address
function removeRecipient(address payable recipient) onlyOwner external {
uint index = addressToUint[recipient];
require(index < recipients.length, "index out of bound");
for (uint i = index; i < recipients.length - 1; i++) {
recipients[i] = recipients[i + 1];
}
recipients.pop();
emit RemovedRecipient(recipient);
}
function splitRecipients() public view returns (address payable [] memory) {
return recipients;
}
function splitRecipientCount() public view returns (uint) {
return recipients.length;
}
}