From 1153adca1ba96ab707e055c821301363d23e7930 Mon Sep 17 00:00:00 2001 From: enkogu Date: Thu, 8 Nov 2018 13:45:09 +0700 Subject: [PATCH] all tests passed --- contracts/ether/WeiExpense.sol | 19 +++----- test/wei_fund.tests.js | 83 +++++++++++++++++----------------- 2 files changed, 49 insertions(+), 53 deletions(-) diff --git a/contracts/ether/WeiExpense.sol b/contracts/ether/WeiExpense.sol index 7b34bda..7feca44 100644 --- a/contracts/ether/WeiExpense.sol +++ b/contracts/ether/WeiExpense.sol @@ -94,8 +94,6 @@ contract WeiExpense is IWeiReceiver, IDestination, Ownable { require(_currentFlow >= msg.value); // all inputs divide _minWeiAmount == INTEGER - momentReceived = uint(block.timestamp); - totalWeiReceived += msg.value; isMoneyReceived = true; @@ -116,19 +114,16 @@ contract WeiExpense is IWeiReceiver, IDestination, Ownable { function getTotalWeiNeeded(uint _currentFlow)public view zeroIfNoNeed returns(uint need) { if(0!=partsPerMillion) { need = (getDebtMultiplier()*(partsPerMillion * _currentFlow)) / 1000000; - }else { - if(getDebtMultiplier()*totalWeiNeed > totalWeiReceived) { - need = getDebtMultiplier()*totalWeiNeed - totalWeiReceived; - }else { - need = 0; - } - + + }else if(getDebtMultiplier()*totalWeiNeed > totalWeiReceived) { + need = getDebtMultiplier()*totalWeiNeed - totalWeiReceived; if((minWeiAmount==0)&&(totalWeiNeed>0)) { if(need>_currentFlow) { need = _currentFlow; - } + } } - + }else { + need = 0; } } @@ -187,7 +182,7 @@ contract WeiExpense is IWeiReceiver, IDestination, Ownable { isNeed = true; } } else if((minWeiAmount==0)&&(totalWeiNeed>0)) { - isNeed = (totalWeiNeed - totalWeiReceived) > 0; + isNeed = (getDebtMultiplier()*totalWeiNeed - totalWeiReceived) > 0; } else { isNeed = !isMoneyReceived; } diff --git a/test/wei_fund.tests.js b/test/wei_fund.tests.js index 372fe16..981f3c5 100644 --- a/test/wei_fund.tests.js +++ b/test/wei_fund.tests.js @@ -1,3 +1,4 @@ +var MoneyFlow = artifacts.require('./MoneyFlow'); var IWeiReceiver = artifacts.require('./IWeiReceiver'); var WeiSplitter = artifacts.require('./WeiSplitter'); @@ -46,8 +47,8 @@ contract('WeiFund', (accounts) => { it('Should collect money, then revert if more, then flush', async () => { let fund = await WeiAbsoluteExpense.new(0, 1e18); - var totalNeed = await fund.getTotalWeiNeeded(1e18); - var minNeed = await fund.getMinWeiNeeded(0);/*minNeedFix*/ + var totalNeed = await fund.getTotalWeiNeeded(1e22); + var minNeed = await fund.getMinWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 1e18); assert.equal(minNeed.toNumber(), 0); @@ -56,8 +57,8 @@ contract('WeiFund', (accounts) => { await fund.processFunds(3e17, { value: 3e17, from: creator }); await fund.processFunds(3e17, { value: 3e17, from: employee1 }); - var totalNeed = await fund.getTotalWeiNeeded(4e17); - var minNeed = await fund.getMinWeiNeeded(0);/*minNeedFix*/ + var totalNeed = await fund.getTotalWeiNeeded(1e22); + var minNeed = await fund.getMinWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 4e17); assert.equal(minNeed.toNumber(), 0); @@ -67,8 +68,8 @@ contract('WeiFund', (accounts) => { await fund.processFunds(4e17, { value: 4e17, from: employee2 }); await fund.processFunds(1e17, { value: 1e17 }).should.be.rejectedWith('revert'); // overflow - var totalNeed = await fund.getTotalWeiNeeded(0); - var minNeed = await fund.getMinWeiNeeded(0);/*minNeedFix*/ + var totalNeed = await fund.getTotalWeiNeeded(1e22); + var minNeed = await fund.getMinWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 0); assert.equal(minNeed.toNumber(), 0); @@ -79,139 +80,139 @@ contract('WeiFund', (accounts) => { var b2 = await web3.eth.getBalance(employee1); assert.equal(b2.toNumber() - b1.toNumber(), 1e18); - var totalNeed = await fund.getTotalWeiNeeded(0); - var minNeed = await fund.getMinWeiNeeded(0);/*minNeedFix*/ + var totalNeed = await fund.getTotalWeiNeeded(1e22); + var minNeed = await fund.getMinWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 0); assert.equal(minNeed.toNumber(), 0); assert.equal(isNeed, false); }); - /*it('Should collect money (periodic, not accumulate debt), then time passed, then need money again', async () => { - let fund = await WeiAbsoluteExpenseWithPeriodSliding.new(0, 1e18, 24); + it('Should collect money (periodic, not accumulate debt), then time passed, then need money again', async () => { + let fund = await WeiAbsoluteExpenseWithPeriod.new(0, 1e18, 24); - var totalNeed = await fund.getTotalWeiNeeded(1e18); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 1e18); assert.equal(isNeed, true); await fund.processFunds(1e18, { value: 1e18 }); - var totalNeed = await fund.getTotalWeiNeeded(0); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 0); assert.equal(isNeed, false); await passHours(23); - var totalNeed = await fund.getTotalWeiNeeded(0); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 0); assert.equal(isNeed, false); await passHours(1); - var totalNeed = await fund.getTotalWeiNeeded(1e18); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 1e18); assert.equal(isNeed, true); await passHours(24); - var totalNeed = await fund.getTotalWeiNeeded(1e18); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 1e18); assert.equal(isNeed, true); await fund.processFunds(5e17, { value: 5e17 }); - var totalNeed = await fund.getTotalWeiNeeded(5e17); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 5e17); assert.equal(isNeed, true); await passHours(24); - var totalNeed = await fund.getTotalWeiNeeded(5e17); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 5e17); assert.equal(isNeed, true); await fund.processFunds(5e17, { value: 5e17 }); - var totalNeed = await fund.getTotalWeiNeeded(0); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); - assert.equal(totalNeed.toNumber(), 0); + console.log('totalNeed:', totalNeed.toNumber(), 0); assert.equal(isNeed, false); }); it('Should collect money (periodic, accumulate debt), then time passed, then need money again', async () => { let fund = await WeiAbsoluteExpenseWithPeriodSliding.new(0, 1e18, 24); - var totalNeed = await fund.getTotalWeiNeeded(1e18); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 1e18); assert.equal(isNeed, true); await fund.processFunds(1e18, { value: 1e18 }); - var totalNeed = await fund.getTotalWeiNeeded(0); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 0); assert.equal(isNeed, false); await passHours(23); - var totalNeed = await fund.getTotalWeiNeeded(0); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 0); assert.equal(isNeed, false); await passHours(1); - var totalNeed = await fund.getTotalWeiNeeded(1e18); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 1e18); assert.equal(isNeed, true); await passHours(24); - var totalNeed = await fund.getTotalWeiNeeded(2e18); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 2e18); assert.equal(isNeed, true); await fund.processFunds(5e17, { value: 5e17 }); - var totalNeed = await fund.getTotalWeiNeeded(1.5e18); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 1.5e18); assert.equal(isNeed, true); await passHours(24); - var totalNeed = await fund.getTotalWeiNeeded(2.5e18); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 2.5e18); assert.equal(isNeed, true); await fund.processFunds(2.5e18, { value: 2.5e18 }); - var totalNeed = await fund.getTotalWeiNeeded(0); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 0); assert.equal(isNeed, false); - });*/ + }); it('Should collect money (periodic, accumulate debt), then time passed, then need money again', async () => { let fund = await WeiAbsoluteExpenseWithPeriodSliding.new(0, 1e18, 24); - var totalNeed = await fund.getTotalWeiNeeded(1e18); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 1e18); assert.equal(isNeed, true); await passHours(48); - var totalNeed = await fund.getTotalWeiNeeded(3e18); + var totalNeed = await fund.getTotalWeiNeeded(1e22); var isNeed = await fund.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 3e18); assert.equal(isNeed, true); @@ -227,8 +228,8 @@ contract('WeiFund', (accounts) => { await splitter.addChild(milestone2.address); await splitter.addChild(milestone3.address); - var totalNeed = await splitter.getTotalWeiNeeded(1e18); - var minNeed = await splitter.getMinWeiNeeded(0);/*minNeedFix*/ + var totalNeed = await splitter.getTotalWeiNeeded(1e22); + var minNeed = await splitter.getMinWeiNeeded(1e22); var isNeed = await splitter.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 1e18); assert.equal(minNeed.toNumber(), 0); @@ -250,8 +251,8 @@ contract('WeiFund', (accounts) => { assert.equal(0.02, (await web3.eth.getBalance(milestone2.address)).toNumber() / 1e18); assert.equal(0, (await web3.eth.getBalance(milestone3.address)).toNumber() / 1e18); - var totalNeed = await splitter.getTotalWeiNeeded(0.88e18); - var minNeed = await splitter.getMinWeiNeeded(0);/*minNeedFix*/ + var totalNeed = await splitter.getTotalWeiNeeded(1e22); + var minNeed = await splitter.getMinWeiNeeded(1e22); var isNeed = await splitter.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 0.88e18); assert.equal(minNeed.toNumber(), 0); @@ -269,8 +270,8 @@ contract('WeiFund', (accounts) => { assert.equal(0.2, (await web3.eth.getBalance(milestone2.address)).toNumber() / 1e18); assert.equal(0.7, (await web3.eth.getBalance(milestone3.address)).toNumber() / 1e18); - var totalNeed = await splitter.getTotalWeiNeeded(0); - var minNeed = await splitter.getMinWeiNeeded(0);/*minNeedFix*/ + var totalNeed = await splitter.getTotalWeiNeeded(1e22); + var minNeed = await splitter.getMinWeiNeeded(1e22); var isNeed = await splitter.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 0); assert.equal(minNeed.toNumber(), 0); @@ -291,7 +292,7 @@ contract('WeiFund', (accounts) => { await splitter.addChild(stabFund.address); var totalNeed = await splitter.getTotalWeiNeeded(1e18); - var minNeed = await splitter.getMinWeiNeeded(0);/*minNeedFix*/ + var minNeed = await splitter.getMinWeiNeeded(1e18); var isNeed = await splitter.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 1e18); assert.equal(minNeed.toNumber(), 0); @@ -314,7 +315,7 @@ contract('WeiFund', (accounts) => { assert.equal(0, (await web3.eth.getBalance(milestone3.address)).toNumber() / 1e18); var totalNeed = await splitter.getTotalWeiNeeded(0.88e18); - var minNeed = await splitter.getMinWeiNeeded(0);/*minNeedFix*/ + var minNeed = await splitter.getMinWeiNeeded(0.88e18); var isNeed = await splitter.isNeedsMoney(); assert.equal(totalNeed.toNumber(), 0.88e18); assert.equal(minNeed.toNumber(), 0); @@ -332,10 +333,10 @@ contract('WeiFund', (accounts) => { assert.equal(0.2, (await web3.eth.getBalance(milestone2.address)).toNumber() / 1e18); assert.equal(0.7, (await web3.eth.getBalance(milestone3.address)).toNumber() / 1e18); - var totalNeed = await splitter.getTotalWeiNeeded(0); - var minNeed = await splitter.getMinWeiNeeded(0);/*minNeedFix*/ + var totalNeed = await splitter.getTotalWeiNeeded(1e18); + var minNeed = await splitter.getMinWeiNeeded(1e18); var isNeed = await splitter.isNeedsMoney(); - assert.equal(totalNeed.toNumber(), 0); + assert.equal(totalNeed.toNumber(), 1e18); assert.equal(minNeed.toNumber(), 0); await splitter.processFunds(0.3e18, { value: 0.3e18 });