-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEthicHubReputation.sol
162 lines (134 loc) · 7.01 KB
/
EthicHubReputation.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
pragma solidity ^0.4.23;
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import '../EthicHubBase.sol';
import './EthicHubReputationInterface.sol';
contract EthicHubReputation is EthicHubBase, EthicHubReputationInterface {
//10 with 2 decilmals
uint constant maxReputation = 1000;
uint constant reputationStep = 100;
//Tier 1 x 20 people
uint constant minProyect = 20;
uint constant public initReputation = 500;
//0.05
uint constant incrLocalNodeMultiplier = 5;
using SafeMath for uint;
event ReputationUpdated(address indexed affected, uint newValue);
/*** Modifiers ************/
/// @dev Only allow access from the latest version of a contract in the Rocket Pool network after deployment
modifier onlyUsersContract() {
require(ethicHubStorage.getAddress(keccak256("contract.name", "users")) == msg.sender);
_;
}
modifier onlyLendingContract() {
require(ethicHubStorage.getAddress(keccak256("contract.address", msg.sender)) == msg.sender);
_;
}
/// @dev constructor
constructor(address _storageAddress) EthicHubBase(_storageAddress) public {
// Version
version = 1;
}
function burnReputation(uint delayDays) external onlyLendingContract {
address lendingContract = msg.sender;
//Get temporal parameters
uint maxDelayDays = ethicHubStorage.getUint(keccak256("lending.maxDelayDays", lendingContract));
require(maxDelayDays != 0);
require(delayDays != 0);
//Affected players
address community = ethicHubStorage.getAddress(keccak256("lending.community", lendingContract));
require(community != address(0));
//Affected local node
address localNode = ethicHubStorage.getAddress(keccak256("lending.localNode", lendingContract));
require(localNode != address(0));
//***** Community
uint previousCommunityReputation = ethicHubStorage.getUint(keccak256("community.reputation", community));
//Calculation and update
uint newCommunityReputation = burnCommunityReputation(delayDays, maxDelayDays, previousCommunityReputation);
ethicHubStorage.setUint(keccak256("community.reputation", community), newCommunityReputation);
emit ReputationUpdated(community, newCommunityReputation);
//***** Local node
uint previousLocalNodeReputation = ethicHubStorage.getUint(keccak256("localNode.reputation", localNode));
uint newLocalNodeReputation = burnLocalNodeReputation(delayDays, maxDelayDays, previousLocalNodeReputation);
ethicHubStorage.setUint(keccak256("localNode.reputation", localNode), newLocalNodeReputation);
emit ReputationUpdated(localNode, newLocalNodeReputation);
}
function incrementReputation(uint completedProjectsByTier) external onlyLendingContract {
address lendingContract = msg.sender;
//Affected players
address community = ethicHubStorage.getAddress(keccak256("lending.community", lendingContract));
require(community != address(0));
//Affected local node
address localNode = ethicHubStorage.getAddress(keccak256("lending.localNode", lendingContract));
require(localNode != address(0));
//Tier
uint projectTier = ethicHubStorage.getUint(keccak256("lending.tier", lendingContract));
require(projectTier > 0);
require(completedProjectsByTier > 0);
//***** Community
uint previousCommunityReputation = ethicHubStorage.getUint(keccak256("community.reputation", community));
//Calculation and update
uint newCommunityReputation = incrementCommunityReputation(previousCommunityReputation, completedProjectsByTier);
ethicHubStorage.setUint(keccak256("community.reputation", community), newCommunityReputation);
emit ReputationUpdated(community, newCommunityReputation);
//***** Local node
uint borrowers = ethicHubStorage.getUint(keccak256("lending.communityMembers", lendingContract));
uint previousLocalNodeReputation = ethicHubStorage.getUint(keccak256("localNode.reputation", localNode));
uint newLocalNodeReputation = incrementLocalNodeReputation(previousLocalNodeReputation, projectTier, borrowers);
ethicHubStorage.setUint(keccak256("localNode.reputation", localNode), newLocalNodeReputation);
emit ReputationUpdated(localNode, newLocalNodeReputation);
}
function incrementCommunityReputation(uint previousReputation, uint completedProjectsByTier) public view returns(uint) {
require(completedProjectsByTier > 0);
uint nextRep = previousReputation.add(reputationStep.div(completedProjectsByTier));
if (nextRep >= maxReputation) {
return maxReputation;
} else {
return nextRep;
}
}
function incrementLocalNodeReputation(uint previousReputation, uint tier, uint borrowers) public view returns(uint) {
require(tier >= 1);
//this should 20 but since it's hardcoded in EthicHubLending, let's be safe.
//TODO store min borrowers in EthicHubStorage
require(borrowers > 0);
uint increment = (tier.mul(borrowers).div(minProyect)).mul(incrLocalNodeMultiplier);
uint nextRep = previousReputation.add(increment);
if (nextRep >= maxReputation) {
return maxReputation;
} else {
return nextRep;
}
}
function burnLocalNodeReputation(uint delayDays, uint maxDelayDays, uint prevReputation) public view returns(uint) {
if (delayDays >= maxDelayDays){
return 0;
}
uint decrement = prevReputation.mul(delayDays).div(maxDelayDays);
if (delayDays < maxDelayDays && decrement < reputationStep) {
return prevReputation.sub(decrement);
} else {
return prevReputation.sub(reputationStep);
}
}
function burnCommunityReputation(uint delayDays, uint maxDelayDays, uint prevReputation) public pure returns(uint) {
if (delayDays < maxDelayDays) {
return prevReputation.sub(prevReputation.mul(delayDays).div(maxDelayDays));
} else {
return 0;
}
}
function initLocalNodeReputation(address localNode) onlyUsersContract external {
require(ethicHubStorage.getUint(keccak256("localNode.reputation", localNode)) == 0);
ethicHubStorage.setUint(keccak256("localNode.reputation", localNode), initReputation);
}
function initCommunityReputation(address community) onlyUsersContract external {
require(ethicHubStorage.getUint(keccak256("comunity.reputation", community)) == 0);
ethicHubStorage.setUint(keccak256("community.reputation", community), initReputation);
}
function getCommunityReputation(address target) public view returns(uint256) {
return ethicHubStorage.getUint(keccak256("community.reputation", target));
}
function getLocalNodeReputation(address target) public view returns(uint256) {
return ethicHubStorage.getUint(keccak256("localNode.reputation", target));
}
}