Skip to content

Commit

Permalink
#34
Browse files Browse the repository at this point in the history
  • Loading branch information
enkogu committed Dec 18, 2018
1 parent 1197e62 commit 18aaa4e
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 16 deletions.
30 changes: 14 additions & 16 deletions contracts/ether/WeiExpense.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ contract WeiExpense is IWeiReceiver, IDestination, Ownable {
}

modifier zeroIfNoNeed() {
uint out;
if(!isNeedsMoney()) {
out = 0;
} else {
if(isNeedsMoney()) {
_;
}
}
Expand Down Expand Up @@ -97,7 +94,8 @@ contract WeiExpense is IWeiReceiver, IDestination, Ownable {
totalWeiReceived += msg.value;
isMoneyReceived = true;

if(getTotalWeiNeeded(msg.value)==0) {

if((getTotalWeiNeeded(msg.value)==0) || (isPeriodic)) {
momentReceived = block.timestamp;
balanceOnMomentReceived = totalWeiReceived;
}
Expand All @@ -111,10 +109,9 @@ contract WeiExpense is IWeiReceiver, IDestination, Ownable {
return totalWeiNeed;
}

event minNeedEvent(uint need, uint _cf_div_minWeiAmount);
function getTotalWeiNeeded(uint _currentFlow)public view zeroIfNoNeed returns(uint need) {
if(0!=partsPerMillion) {
need = (getDebtMultiplier()*(partsPerMillion * _currentFlow)) / 1000000;
need = ((getDebtMultiplier()*(partsPerMillion * _currentFlow)) / 1000000);

}else if(getDebtMultiplier()*totalWeiNeed > totalWeiReceived) {
need = getDebtMultiplier()*totalWeiNeed - totalWeiReceived;
Expand All @@ -136,14 +133,11 @@ contract WeiExpense is IWeiReceiver, IDestination, Ownable {
}
}

function getMinWeiNeeded(uint _currentFlow) public view zeroIfNoNeed returns(uint need) {
if( !isNeedsMoney()
|| (0!=partsPerMillion)
|| ((minWeiAmount==0)&&(totalWeiNeed>0))
) {
return 0;
}
return getTotalWeiNeeded(_currentFlow);
function getMinWeiNeeded(uint _currentFlow) public zeroIfNoNeed view returns(uint need) {
if( !((minWeiAmount==0) && (totalWeiNeed>0))
&& !(partsPerMillion > 0) ) {
need = getTotalWeiNeeded(_currentFlow);
}
}

function getMomentReceived()public view returns(uint) {
Expand All @@ -152,7 +146,11 @@ contract WeiExpense is IWeiReceiver, IDestination, Ownable {

function getDebtMultiplier()public view returns(uint) {
if((isPeriodic)&&(!isSlidingAmount)&&((block.timestamp - momentReceived) / (periodHours * 3600 * 1000) >=1)) {
return (balanceOnMomentReceived/totalWeiNeed) + 1;
if(0!=partsPerMillion) {
return 1;
} else {
return (balanceOnMomentReceived/totalWeiNeed) + 1;
}
} else if((isPeriodic)&&(isSlidingAmount)) {
return 1 + ((block.timestamp - momentCreated) / (periodHours * 3600 * 1000));
}else {
Expand Down
86 changes: 86 additions & 0 deletions test/moneyflow.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ async function passHours (hours) {
}, function (err) { if (err) console.log('err:', err); });
}


async function checkMinNeed (targets, flowArr, needArr) {
for(var i=0; i<targets.length; i++) {
assert.equal((await targets[i].getMinWeiNeeded(flowArr[i]*1e14)).toNumber() / 1e14, needArr[i]);
}
}

async function checkTotalNeed (targets, flowArr, needArr) {
for(var i=0; i<targets.length; i++) {
assert.equal((await targets[i].getTotalWeiNeeded(flowArr[i]*1e14)).toNumber() / 1e14, needArr[i]);
}
}

async function checkIsNeed (targets, needArr) {
for(var i=0; i<targets.length; i++) {
assert.equal((await targets[i].isNeedsMoney()), needArr[i]);
}
}

function KECCAK256 (x) {
return web3.sha3(x);
}
Expand Down Expand Up @@ -776,4 +795,71 @@ contract('Moneyflow', (accounts) => {
await struct.AllOutpults.processFunds(1000000*money, { value: 1000*money, from: creator }).should.be.rejectedWith('revert');
await struct.AllOutpults.processFunds(1000*money, { value: 1000000*money, from: creator }).should.be.rejectedWith('revert');
});

it('should process money with WeiSplitter + 3 WeiRelativeExpenseWithPeriod', async () => {
// create WeiSplitter
var splitter = await WeiSplitter.new();

var rel1 = await WeiRelativeExpenseWithPeriod.new(100000, 24, { from: creator, gasPrice: 0 });
var rel2 = await WeiRelativeExpenseWithPeriod.new(250000, 24, { from: creator, gasPrice: 0 });
var rel3 = await WeiRelativeExpenseWithPeriod.new(370000, 48, { from: creator, gasPrice: 0 });

// // add 3 rel expense outputs to the splitter
await splitter.addChild(rel1.address);
await splitter.addChild(rel2.address);
await splitter.addChild(rel3.address);

var targets = [splitter, rel1, rel2, rel3];
var flowArr = [1000, 1000, 1000, 1000];

await checkMinNeed(targets, flowArr, [0, 0, 0, 0]);
await checkTotalNeed(targets, flowArr, [720, 100, 250, 370]);
await checkIsNeed(targets, [true, true, true, true]);

// now send some money to the revenue endpoint
await splitter.processFunds(1000*money, {value:720*money, from: creator});

assert.equal((await web3.eth.getBalance(rel1.address)).toNumber(), 100*money);
assert.equal((await web3.eth.getBalance(rel2.address)).toNumber(), 250*money);
assert.equal((await web3.eth.getBalance(rel3.address)).toNumber(), 370*money);

await checkMinNeed(targets, flowArr, [0, 0, 0, 0]);
await checkTotalNeed(targets, flowArr, [0, 0, 0, 0]);
await checkIsNeed(targets, [false, false, false, false]);

await passHours(24);

await checkMinNeed(targets, flowArr, [0, 0, 0, 0]);
await checkTotalNeed(targets, flowArr, [350, 100, 250, 0]);
await checkIsNeed(targets, [true, true, true, false]);

await passHours(24);

await checkMinNeed(targets, flowArr, [0, 0, 0, 0]);
await checkTotalNeed(targets, flowArr, [720, 100, 250, 370]);
await checkIsNeed(targets, [true, true, true, true]);

await splitter.processFunds(1000*money, { value: 720*money, from: creator });

await checkMinNeed(targets, flowArr, [0, 0, 0, 0]);
await checkTotalNeed(targets, flowArr, [0, 0, 0, 0]);
await checkIsNeed(targets, [false, false, false, false]);

// money should end up in the outputs
assert.equal((await web3.eth.getBalance(rel1.address)).toNumber(), 200*money);
assert.equal((await web3.eth.getBalance(rel2.address)).toNumber(), 500*money);
assert.equal((await web3.eth.getBalance(rel3.address)).toNumber(), 740*money);

await passHours(24);

await checkMinNeed(targets, flowArr, [0, 0, 0, 0]);
await checkTotalNeed(targets, flowArr, [350, 100, 250, 0]);
await checkIsNeed(targets, [true, true, true, false]);

await splitter.processFunds(1000*money, { value: 350*money, from: creator });

await checkMinNeed(targets, flowArr, [0, 0, 0, 0]);
await checkTotalNeed(targets, flowArr, [0, 0, 0, 0]);
await checkIsNeed(targets, [false, false, false, false]);
});
});

0 comments on commit 18aaa4e

Please sign in to comment.