Skip to content

Commit

Permalink
Merge pull request #357 from Thetta/dev-enkogu-sept
Browse files Browse the repository at this point in the history
Dev enkogu sept
  • Loading branch information
AnthonyAkentiev authored Oct 8, 2018
2 parents 59c1b05 + 191cf26 commit 189481d
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 110 deletions.
91 changes: 45 additions & 46 deletions contracts/moneyflow/MoneflowTable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import "zeppelin-solidity/contracts/ownership/Ownable.sol";


contract MoneyflowTable is IWeiReceiver, Ownable {
uint public elementsCount = 0;
enum ElementTypes {
uint public nodesCount = 0;
enum NodeTypes {
AbsoluteExpense,
RelativeExpense,
TopdownSplitter,
UnsortedSplitter
}
event ElementAdded(uint _eId, ElementTypes _eType);
event NodeAdded(uint _eId, NodeTypes _eType);

mapping(uint=>ElementTypes) elementsType;
mapping(uint=>NodeTypes) nodesType;
mapping(uint=>Expense) expenses;
mapping(uint=>Splitter) splitters;

Expand All @@ -45,17 +45,17 @@ contract MoneyflowTable is IWeiReceiver, Ownable {
uint[] outputs;
}

// -------------------- INTERNAL IWEIRECEIVER FUNCTIONS -------------------- for elements
// -------------------- INTERNAL IWEIRECEIVER FUNCTIONS -------------------- for nodes

function _getPartsPerMillion(uint _eId) internal view returns(uint) {
if(ElementTypes.RelativeExpense == elementsType[_eId]) {
if(NodeTypes.RelativeExpense == nodesType[_eId]) {
return expenses[_eId].neededPpm;
}else {
return 0;
}
}

function isElementNeedsMoney(uint _id)view external returns(bool) {
function isNodeNeedsMoney(uint _id)view external returns(bool) {
return _isNeedsMoney(_id);
}

Expand Down Expand Up @@ -104,7 +104,7 @@ contract MoneyflowTable is IWeiReceiver, Ownable {
uint needed = _getTotalWeiNeeded(splitters[_eId].outputs[i], currentFlow);
_processFunds(splitters[_eId].outputs[i], currentFlow, needed);

if(ElementTypes.TopdownSplitter == elementsType[_eId]) {
if(NodeTypes.TopdownSplitter == nodesType[_eId]) {
if(currentFlow >= needed) {
currentFlow = currentFlow - needed;
}else {
Expand All @@ -122,15 +122,15 @@ contract MoneyflowTable is IWeiReceiver, Ownable {
expenses[_eId].isMoneyReceived = true;
}

function getMinWeiNeededForElement(uint _eId) external view returns(uint) {
function getMinWeiNeededForNode(uint _eId) external view returns(uint) {
return _getMinWeiNeeded(_eId);
}

function _getMinWeiNeeded(uint _eId) internal view returns(uint) {
if((splitters[_eId].isOpen) && (ElementTypes.TopdownSplitter == elementsType[_eId])) {
if((splitters[_eId].isOpen) && (NodeTypes.TopdownSplitter == nodesType[_eId])) {
return _getMinWeiNeededTopdownSplitter(_eId);

}else if((splitters[_eId].isOpen) && (ElementTypes.UnsortedSplitter == elementsType[_eId])) {
}else if((splitters[_eId].isOpen) && (NodeTypes.UnsortedSplitter == nodesType[_eId])) {
return _getMinWeiNeededUnsortedSplitter(_eId);

}else if(expenses[_eId].isOpen && _isNeedsMoney(_eId)) {
Expand All @@ -146,7 +146,7 @@ contract MoneyflowTable is IWeiReceiver, Ownable {
uint partsPerMillionReverseSum = 1000000;

for(uint i=0; i<splitters[_eId].outputs.length; ++i) {
if(ElementTypes.RelativeExpense == elementsType[splitters[_eId].outputs[i]]) {
if(NodeTypes.RelativeExpense == nodesType[splitters[_eId].outputs[i]]) {
partsPerMillionReverseSum -= expenses[splitters[_eId].outputs[i]].neededPpm;
}else {
absSum += _getMinWeiNeeded(splitters[_eId].outputs[i]);
Expand All @@ -163,7 +163,7 @@ contract MoneyflowTable is IWeiReceiver, Ownable {
function _getMinWeiNeededTopdownSplitter(uint _eId) internal view returns(uint) {
uint out = 0;
for(uint j=splitters[_eId].outputs.length; j>0; --j) {
if(ElementTypes.RelativeExpense == elementsType[splitters[_eId].outputs[j-1]]) {
if(NodeTypes.RelativeExpense == nodesType[splitters[_eId].outputs[j-1]]) {
out = 1000000 * out / expenses[splitters[_eId].outputs[j-1]].neededPpm;
}else {
out += _getMinWeiNeeded(splitters[_eId].outputs[j-1]);
Expand All @@ -173,7 +173,7 @@ contract MoneyflowTable is IWeiReceiver, Ownable {
}

function _getMinWeiNeededExpense(uint _eId) internal view returns(uint) {
if(!_isNeedsMoney(_eId) || (ElementTypes.RelativeExpense == elementsType[_eId])) {
if(!_isNeedsMoney(_eId) || (NodeTypes.RelativeExpense == nodesType[_eId])) {
return 0;
}
return _getDebtMultiplier(_eId)*expenses[_eId].neededAmount;
Expand All @@ -187,7 +187,7 @@ contract MoneyflowTable is IWeiReceiver, Ownable {
}
}

function getTotalWeiNeededForElement(uint _eId, uint _currentFlow) external view returns(uint) {
function getTotalWeiNeededForNode(uint _eId, uint _currentFlow) external view returns(uint) {
return _getTotalWeiNeeded(_eId, _currentFlow);
}

Expand All @@ -208,7 +208,7 @@ contract MoneyflowTable is IWeiReceiver, Ownable {
uint needed = _getTotalWeiNeeded(splitters[_eId].outputs[i], currentFlow);
total = total + needed;

if(ElementTypes.TopdownSplitter==elementsType[_eId]) { // this should be reduced because next child can get only '_inputWei minus what prev. child got'
if(NodeTypes.TopdownSplitter==nodesType[_eId]) { // this should be reduced because next child can get only '_inputWei minus what prev. child got'
if(currentFlow>needed) {
currentFlow-=needed;
}else {
Expand All @@ -224,14 +224,14 @@ contract MoneyflowTable is IWeiReceiver, Ownable {
return 0;
}

if(ElementTypes.RelativeExpense==elementsType[_eId]) {
if(NodeTypes.RelativeExpense==nodesType[_eId]) {
return (_getDebtMultiplier(_eId)*(expenses[_eId].neededPpm * _currentFlow)) / 1000000;
}else {
return _getMinWeiNeeded(_eId);
}
}

function getElementBalance(uint _eId)public view returns(uint) {
function getNodeBalance(uint _eId)public view returns(uint) {
return expenses[_eId].balance;
}

Expand Down Expand Up @@ -264,45 +264,45 @@ contract MoneyflowTable is IWeiReceiver, Ownable {
// -------------------- public SCHEME FUNCTIONS --------------------

function addAbsoluteExpense(uint _neededAmount, bool _isPeriodic, bool _isAccumulateDebt, uint _periodHours, IWeiReceiver _output)public onlyOwner returns(uint) {
expenses[elementsCount] = Expense(
expenses[nodesCount] = Expense(
_neededAmount, 0,
_periodHours, _isPeriodic, _isAccumulateDebt, _output,
0, false, true, 0
);
elementsType[elementsCount] = ElementTypes.AbsoluteExpense;
emit ElementAdded(elementsCount, ElementTypes.AbsoluteExpense);
elementsCount += 1;
return elementsCount-1;
nodesType[nodesCount] = NodeTypes.AbsoluteExpense;
emit NodeAdded(nodesCount, NodeTypes.AbsoluteExpense);
nodesCount += 1;
return nodesCount-1;
}

function addRelativeExpense(uint _neededPpm, bool _isPeriodic, bool _isAccumulateDebt, uint _periodHours, IWeiReceiver _output)public onlyOwner returns(uint) {
expenses[elementsCount] = Expense(
expenses[nodesCount] = Expense(
0, _neededPpm,
_periodHours, _isPeriodic, _isAccumulateDebt, _output,
0, false, true, 0
);
elementsType[elementsCount] = ElementTypes.RelativeExpense;
emit ElementAdded(elementsCount, ElementTypes.RelativeExpense);
elementsCount += 1;
return elementsCount-1;
nodesType[nodesCount] = NodeTypes.RelativeExpense;
emit NodeAdded(nodesCount, NodeTypes.RelativeExpense);
nodesCount += 1;
return nodesCount-1;
}

function addTopdownSplitter()public onlyOwner returns(uint) {
uint[] memory emptyOutputs;
splitters[elementsCount] = Splitter(true, emptyOutputs);
elementsType[elementsCount] = ElementTypes.TopdownSplitter;
emit ElementAdded(elementsCount, ElementTypes.TopdownSplitter);
elementsCount += 1;
return elementsCount-1;
splitters[nodesCount] = Splitter(true, emptyOutputs);
nodesType[nodesCount] = NodeTypes.TopdownSplitter;
emit NodeAdded(nodesCount, NodeTypes.TopdownSplitter);
nodesCount += 1;
return nodesCount-1;
}

function addUnsortedSplitter()public onlyOwner returns(uint) {
uint[] memory emptyOutputs;
splitters[elementsCount] = Splitter(true, emptyOutputs);
elementsType[elementsCount] = ElementTypes.UnsortedSplitter;
emit ElementAdded(elementsCount, ElementTypes.UnsortedSplitter);
elementsCount += 1;
return elementsCount-1;
splitters[nodesCount] = Splitter(true, emptyOutputs);
nodesType[nodesCount] = NodeTypes.UnsortedSplitter;
emit NodeAdded(nodesCount, NodeTypes.UnsortedSplitter);
nodesCount += 1;
return nodesCount-1;
}

function addChild(uint _splitterId, uint _childId)public onlyOwner returns(uint) {
Expand All @@ -314,24 +314,24 @@ contract MoneyflowTable is IWeiReceiver, Ownable {


function _isExpense(uint _eId) internal returns(bool) {
if((ElementTypes.AbsoluteExpense==elementsType[_eId])||
(ElementTypes.RelativeExpense==elementsType[_eId])) {
if((NodeTypes.AbsoluteExpense==nodesType[_eId])||
(NodeTypes.RelativeExpense==nodesType[_eId])) {
return true;
}else {
return false;
}
}

function _isSplitter(uint _eId) internal returns(bool) {
if((ElementTypes.UnsortedSplitter==elementsType[_eId])||
(ElementTypes.TopdownSplitter==elementsType[_eId])) {
if((NodeTypes.UnsortedSplitter==nodesType[_eId])||
(NodeTypes.TopdownSplitter==nodesType[_eId])) {
return true;
}else {
return false;
}
}

function openElement(uint _eId) public onlyOwner {
function openNode(uint _eId) public onlyOwner {
if(_isExpense(_eId)) {
expenses[_eId].isOpen = true;

Expand All @@ -343,7 +343,7 @@ contract MoneyflowTable is IWeiReceiver, Ownable {
}
}

function closeElement(uint _eId)public onlyOwner {
function closeNode(uint _eId)public onlyOwner {
if(_isExpense(_eId)) {
expenses[_eId].isOpen = false;

Expand Down Expand Up @@ -379,8 +379,7 @@ contract MoneyflowTable is IWeiReceiver, Ownable {
return splitters[_eId].outputs[_index];
}

function withdrawFundsFromElement(uint _eId)public onlyOwner {
// require(_isExpense(_eId));
function flushFromNode(uint _eId)public onlyOwner {
expenses[_eId].output.processFunds.value(expenses[_eId].balance)(expenses[_eId].balance);
expenses[_eId].balance = 0;
}
Expand Down
17 changes: 14 additions & 3 deletions contracts/moneyflow/ether/WeiFund.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ contract WeiFund is IWeiReceiver, IDestination, Ownable {
require(!((_isAccumulateDebt)&&(_periodHours==0)));
require(!(!(_isPeriodic)&&(_periodHours!=0)));
require(!((_isAccumulateDebt)&&(!_isPeriodic)));
require(_neededWei!=0);
require(!((_neededWei==0)&&(_isPeriodic)));
neededWei = _neededWei;
isPeriodic = _isPeriodic;
isAccumulateDebt = _isAccumulateDebt;
Expand All @@ -60,9 +60,11 @@ contract WeiFund is IWeiReceiver, IDestination, Ownable {
function processFunds(uint _currentFlow) public payable {
// emit consoleUint('_getDebtMultiplier', _getDebtMultiplier());
require(isNeedsMoney());

if(neededWei!=0) {
require(totalWeiReceived+msg.value<=getDebtMultiplier()*neededWei); // protect from extra money
}

require(totalWeiReceived+msg.value<=getDebtMultiplier()*neededWei); // protect from extra money
// require(msg.value==_currentFlow);
totalWeiReceived += msg.value;
if(getTotalWeiNeeded(msg.value)==0) {
momentReceived = block.timestamp;
Expand All @@ -72,6 +74,11 @@ contract WeiFund is IWeiReceiver, IDestination, Ownable {

function getTotalWeiNeeded(uint _inputWei)public view returns(uint) {
uint need;

if(neededWei==0) {
return _inputWei;
}

if(getDebtMultiplier()*neededWei > totalWeiReceived) {
need = getDebtMultiplier()*neededWei - totalWeiReceived;
}else {
Expand All @@ -94,6 +101,10 @@ contract WeiFund is IWeiReceiver, IDestination, Ownable {
}

function isNeedsMoney()public view returns(bool) {
if(neededWei==0) {
return true;
}

return getDebtMultiplier()*neededWei > totalWeiReceived;
}

Expand Down
14 changes: 14 additions & 0 deletions contracts/moneyflow/ether/WeiFundWithPeriod.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pragma solidity ^0.4.24;
import "./WeiFund.sol";

/**
* @title WeiFundWithPeriod
* @dev WeiFundWithPeriod is a WeiFund that every period need _neededWei. If not collected enough – amount will not slide
* This is a terminal item, that has no children.
*/


contract WeiFundWithPeriod is WeiFund {
constructor(uint _neededWei, uint _periodHours) public WeiFund(_neededWei, true, false, _periodHours) {
}
}
15 changes: 15 additions & 0 deletions contracts/moneyflow/ether/WeiFundWithPeriodSliding.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pragma solidity ^0.4.24;
import "./WeiFund.sol";

/**
* @title WeiFundWithPeriod
* @dev WeiFundWithPeriod is a WeiFund that every period need _neededWei. If not collected enough – amount will slide
* This is a terminal item, that has no children.
*/



contract WeiFundWithPeriodSliding is WeiFund {
constructor(uint _neededWei, uint _periodHours) public WeiFund(_neededWei, true, true, _periodHours) {
}
}
14 changes: 14 additions & 0 deletions contracts/moneyflow/ether/WeiInfiniteFund.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pragma solidity ^0.4.24;
import "./WeiFund.sol";

/**
* @title WeiInfiniteFund
* @dev WeiInfiniteFund is a WeiFund that needs money always
* This is a terminal item, that has no children.
*/


contract WeiInfiniteFund is WeiFund {
constructor() public WeiFund(0, false, false, 0) {
}
}
15 changes: 15 additions & 0 deletions contracts/moneyflow/ether/WeiUncappedFund.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pragma solidity ^0.4.24;
import "./WeiFund.sol";

/**
* @title WeiUncappedFund
* @dev WeiUncappedFund is a WeiFund that will keep receiving ANY amount of ETH forever
* This is a terminal item, that has no children.
*/



contract WeiUncappedFund is WeiFund {
constructor(uint _neededWei) public WeiFund(_neededWei, false, false, 0) {
}
}
Loading

0 comments on commit 189481d

Please sign in to comment.