forked from Xalava/Daisee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaisee.sol
91 lines (69 loc) · 2.25 KB
/
Daisee.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
contract Daisee {
Usager[] public usagers;
uint public currentTime;
uint prixKwh;
/* Contributeurs */
struct Usager {
address addr;
uint[] conso;
uint[] prod;
uint[] disponible;
string nom;
uint credit;
}
/* Initialisation */
function Daisee() {
currentTime = 0;
}
function () {
bool trouve=false;
for (uint i = 0; i < contributors.length; ++i) {
if(usagers[i].addr == msg.sender){
usagers[i].credit = usagers[i].credit + msg.value;
trouve=true;
}
}
if(trouve){
}else {
throw;
}
}
function sajouterUsager(
string name
) {
usagers[usagers.length++] = Usager({addr: msg.sender, credit: msg.value, nom: name});
for (uint i = 0; i < currentTime; ++i) {
usagers[usagers.length].conso[i]=0;
usagers[usagers.length].prod[i]=0;
usagers[usagers.length].disponible[i]=0;
}
}
function publier(uint consoActuelle, uint prodActuelle) {
prodDiff = prodActuelle - consoActuelle;
address vendeur;
bool trouve;
for (uint i = 0; i < usagers.length; ++i) {
if(usagers[i].addr == msg.sender){
usagers[i].conso[currentTime] = consoActuelle;
usagers[i].prod[currentTime] = prodActuelle;
if(prodDiff>0){
usagers[i].disponible[currentTime] = prodDiff;
} else {
for (uint j = 0; j < usagers.length; ++j) {
if(usagers[j].disponible[currentTime-1]>= prodDiff){
vendeur = usagers[j].addr;
trouve = true;
}
}
if(trouve){
usagers[j].disponible[currentTime-1] -= prodDiff;
usagers[j].credit += prodDiff * prixKwh;
usagers[i].credit -= prodDiff * prixKwh;
}else{
/*Acheter hors reseau ou tenter achat par fractions*/
}
}
}
}
}
}