-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplitPayment.sol
65 lines (48 loc) · 2.14 KB
/
SplitPayment.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
pragma solidity ^0.5.4;
import 'github.com/OpenZeppelin/openzeppelin-solidity/contracts/ownership/Ownable.sol';
contract SplitPayment is Ownable {
address payable[] public beneficiaries;
event AddedBeneficiary(address beneficiary);
event RemovedBeneficiary(uint256 indexOfBeneficiary, address beneficiary);
event LogPayout(address beneficiary, uint256 amount);
function addBeneficiary(address payable _beneficiary) public onlyOwner {
beneficiaries.push(_beneficiary);
emit AddedBeneficiary(_beneficiary);
}
function bulkAddBeneficiaries(address payable[] memory _beneficiaries) public onlyOwner {
uint256 len = beneficiaries.length;
for (uint256 b = 0; b < len; b++) {
addBeneficiary(_beneficiaries[b]);
}
}
function removeBeneficiary(uint256 indexOfBeneficiary) public onlyOwner {
emit RemovedBeneficiary(indexOfBeneficiary, beneficiaries[indexOfBeneficiary]);
// unless the to be deleted index is not last -> move last one here
if (indexOfBeneficiary < beneficiaries.length - 1) {
beneficiaries[indexOfBeneficiary] = beneficiaries[beneficiaries.length - 1];
}
// if to be deleted index is in range -> decrease length by one
if(indexOfBeneficiary < beneficiaries.length) {
beneficiaries.length--;
}
}
function() external payable {
uint256 len = beneficiaries.length;
uint256 amount = msg.value / len;
for (uint256 b = 0; b < len; b++) {
beneficiaries[b].transfer(amount);
emit LogPayout(beneficiaries[b], amount);
}
}
function payOnce(address payable[] memory _beneficiaries) public payable {
uint256 len = _beneficiaries.length;
uint256 amount = msg.value / len;
for (uint256 b = 0; b < len; b++) {
_beneficiaries[b].transfer(amount);
emit LogPayout(_beneficiaries[b], amount);
}
}
function getNumberOfBeneficiaries() public view returns (uint256 length) {
return beneficiaries.length;
}
}