From 18aaa4e9438ebc29fd6b82768060ec5e8fe20245 Mon Sep 17 00:00:00 2001 From: enkogu Date: Tue, 18 Dec 2018 14:16:02 +0700 Subject: [PATCH] #34 --- contracts/ether/WeiExpense.sol | 30 ++++++------ test/moneyflow.tests.js | 86 ++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 16 deletions(-) diff --git a/contracts/ether/WeiExpense.sol b/contracts/ether/WeiExpense.sol index 6e9a29d..c8c35bc 100644 --- a/contracts/ether/WeiExpense.sol +++ b/contracts/ether/WeiExpense.sol @@ -41,10 +41,7 @@ contract WeiExpense is IWeiReceiver, IDestination, Ownable { } modifier zeroIfNoNeed() { - uint out; - if(!isNeedsMoney()) { - out = 0; - } else { + if(isNeedsMoney()) { _; } } @@ -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; } @@ -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; @@ -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) { @@ -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 { diff --git a/test/moneyflow.tests.js b/test/moneyflow.tests.js index 496eff3..4523a94 100644 --- a/test/moneyflow.tests.js +++ b/test/moneyflow.tests.js @@ -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 { 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]); + }); }); \ No newline at end of file