Skip to content

Instantly share code, notes, and snippets.

@Ramarti
Last active June 5, 2018 07:54
Show Gist options
  • Save Ramarti/9c2024d183c5a28c3c7c71bbfd6f35db to your computer and use it in GitHub Desktop.
Save Ramarti/9c2024d183c5a28c3c7c71bbfd6f35db to your computer and use it in GitHub Desktop.
Reputation update hooks
pragma solidity ^0.4.23;
//...
contract EthicHubLending is EthicHubBase, Ownable, Pausable {
//...
// In this moment, when the borrower returns the money, we evaluate how "well" they paid.
function returnBorrowedEth() payable public {
require(state == LendingState.AwaitingReturn);
require(borrowerReturnEthPerFiatRate > 0);
bool projectRepayed = false;
uint excessRepayment = 0;
uint newReturnedEth = 0;
emit onReturnAmount(msg.sender, msg.value);
(newReturnedEth, projectRepayed, excessRepayment) = calculatePaymentGoal(
borrowerReturnAmount(),
returnedEth,
msg.value);
returnedEth = newReturnedEth;
if (projectRepayed == true) {
state = LendingState.ContributionReturned;
emit StateChange(uint(state));
updateReputation();
}
if (excessRepayment > 0) {
msg.sender.transfer(excessRepayment);
}
}
//First we calculate how many days over the pre-established days to return the funds.
//If they are in the default period, the reputations will be decreased, if not incremented.
function updateReputation() internal {
uint defaultDays = getDefaultDays(now);
if (defaultDays > 0) {
ethicHubStorage.setUint(keccak256("lending.defaultDays", this), defaultDays);
reputation.burnReputation();
} else {
uint successesByTier = ethicHubStorage.getUint(keccak256("community.completedProjectsByTier", this, tier)).add(1);
ethicHubStorage.setUint(keccak256("community.completedProjectsByTier", this, tier), successesByTier);
reputation.incrementReputation();
}
}
function getDelayDays(uint date) public view returns(uint) {
uint lendingDaysSeconds = lendingDays * 1 days;
uint defaultTime = fundingEndTime.add(lendingDaysSeconds);
if (date < defaultTime) {
return 0;
} else {
return date.sub(defaultTime).div(60).div(60).div(24);
}
}
//This is to declare the maximum penalty, if funds are not returned after the maximum default days established.
function declareProjectDefault() external onlyOwnerOrLocalNode {
require(state == LendingState.AwaitingReturn);
uint maxDelayDays = ethicHubStorage.getUint(keccak256("lending.maxDelayDays", this));
require(getDelayDays(now) >= maxDelayDays);
ethicHubStorage.setUint(keccak256("lending.delayDays", this), maxDelayDays);
reputation.burnReputation(maxDelayDays);
state = LendingState.Default;
emit StateChange(uint(state));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment