Skip to content

Commit

Permalink
all tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
enkogu committed Nov 8, 2018
1 parent 1bd8a23 commit 1153adc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 53 deletions.
19 changes: 7 additions & 12 deletions contracts/ether/WeiExpense.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
83 changes: 42 additions & 41 deletions test/wei_fund.tests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var MoneyFlow = artifacts.require('./MoneyFlow');
var IWeiReceiver = artifacts.require('./IWeiReceiver');

var WeiSplitter = artifacts.require('./WeiSplitter');
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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 });
Expand Down

0 comments on commit 1153adc

Please sign in to comment.