-
Notifications
You must be signed in to change notification settings - Fork 4
/
Althea.sol
144 lines (121 loc) · 4.1 KB
/
Althea.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
pragma solidity ^0.4.24;
import "@aragon/os/contracts/apps/AragonApp.sol";
import "@aragon/os/contracts/lib/math/SafeMath.sol";
import "@aragon/os/contracts/common/EtherTokenConstant.sol";
import "@aragon/apps-vault/contracts/Vault.sol";
contract Althea is EtherTokenConstant, AragonApp {
using SafeMath for uint;
event NewMember(
address indexed ethAddress,
bytes16 ipAddress,
bytes16 nickname
);
event MemberRemoved(
address indexed ethAddress,
bytes16 ipAddress,
bytes16 nickname
);
event NewBill(address payer, address collector);
event BillUpdated(address payer);
bytes32 constant public MANAGER = keccak256("MANAGER");
bytes32 constant public DELETE = keccak256("DELETE");
bytes32 constant public ADD = keccak256("ADD");
struct Bill {
uint balance;
uint perBlock;
uint lastUpdated;
}
struct User {
address ethAddr;
bytes16 nick;
}
uint public perBlockFee;
Vault public vault;
bytes16[] public subnetSubscribers;
mapping(bytes16 => User) public userMapping;
mapping(address => Bill) public billMapping;
function initialize(Vault _vault) external onlyInit {
perBlockFee = 10000;
vault = _vault;
initialized();
}
function addMember(address _ethAddr, bytes16 _ip, bytes16 _nick)
external
auth(ADD)
{
require(userMapping[_ip].ethAddr== address(0), "Member already exists");
userMapping[_ip] = User(_ethAddr, _nick);
subnetSubscribers.push(_ip);
NewMember(_ethAddr, _ip, _nick);
}
function deleteMember(bytes16 _ip) external auth(DELETE) {
MemberRemoved(userMapping[_ip].ethAddr, _ip, userMapping[_ip].nick);
delete userMapping[_ip];
for (uint i = 0; i < subnetSubscribers.length; i++) {
if (_ip == subnetSubscribers[i]) {
subnetSubscribers[i] = subnetSubscribers[subnetSubscribers.length-1];
subnetSubscribers.length--;
}
}
}
function getMember(bytes16 _ip) external view returns(address addr) {
addr = userMapping[_ip].ethAddr;
}
function setPerBlockFee(uint _newFee) external auth(MANAGER) {
perBlockFee = _newFee;
}
function getCountOfSubscribers() external view returns (uint) {
return subnetSubscribers.length;
}
function addBill() public payable {
require(msg.value > perBlockFee, "Message value not enough");
if (billMapping[msg.sender].lastUpdated == 0) {
billMapping[msg.sender] = Bill(msg.value, perBlockFee, block.number);
emit NewBill(msg.sender, vault);
} else {
billMapping[msg.sender].balance = billMapping[msg.sender].balance.add(msg.value);
emit BillUpdated(msg.sender);
}
}
function collectBills() external {
uint transferValue = 0;
for (uint i = 0; i < subnetSubscribers.length; i++) {
transferValue = transferValue.add(
processBills(userMapping[subnetSubscribers[i]].ethAddr)
);
}
require(transferValue != 0, "Not enough value to send to vault");
vault.deposit.value(transferValue)(ETH, transferValue);
}
function payMyBills() public {
uint amount = processBills(msg.sender);
vault.deposit.value(amount)(ETH, amount);
}
function withdrawFromBill() external {
payMyBills();
uint amount = billMapping[msg.sender].balance;
require(amount > 0, "Amount to payout is no more than zero, reverting");
delete billMapping[msg.sender];
address(msg.sender).transfer(amount);
emit BillUpdated(msg.sender);
}
function processBills(address _subscriber) internal returns(uint) {
uint transferValue;
Bill memory bill = billMapping[_subscriber];
uint amountOwed = block.number.sub(bill.lastUpdated).mul(bill.perBlock);
if (amountOwed <= bill.balance) {
billMapping[_subscriber].balance= bill.balance.sub(amountOwed);
transferValue = amountOwed;
} else {
transferValue = bill.balance;
billMapping[_subscriber].balance = 0;
}
billMapping[_subscriber].lastUpdated = block.number;
emit BillUpdated(_subscriber);
return transferValue;
}
// leave a space between `function` and `(` or else the parser won't work
function () external payable isInitialized {
addBill();
}
}