-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartEnergy.sol
50 lines (43 loc) · 1.67 KB
/
SmartEnergy.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
pragma solidity ^0.4.17;
contract SmartEnergy {
uint grid_balance = 0;
mapping (address => uint) balance;
mapping (address => uint) coins;
event energyGenerated(address generated_by);
event energyMoved(address generated_by, uint energy_value);
function pushEnergy(address pusher, uint energy_pushed) returns (bool check){
energyGenerated(pusher); // not sure about this event - should be called by pi based on node values only maybe
balance[pusher] += energy_pushed;
grid_balance += energy_pushed;
coins[pusher] = energy_pushed / 4;
energyMoved(pusher, balance[pusher]);
return true;
}
function requestEnergy(address requestor, uint energy_requested) returns (bool check){
uint final_balance;
if(energy_requested > grid_balance)
throw;
final_balance = balance[requestor] - energy_requested;
if (final_balance >= 0){
grid_balance -= energy_requested;
coins[requestor] -= energy_requested / 4;
balance[requestor] -= energy_requested;
}
else if(final_balance < 0){
final_balance = 0 - final_balance;
if(final_balance < energy_requested)
grid_balance -= energy_requested;
coins[requestor] -= final_balance / 2;
coins[requestor] -= balance[requestor] / 4;
balance[requestor] = 0;
}
energyMoved(requestor, balance[requestor]);
return true;
}
function getBalance(address user) constant returns (uint val) {
return balance[user];
}
function getCoins(address user) constant returns (uint val){
return coins[user];
}
}