Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP:Feature/2 fund #4

Merged
merged 18 commits into from
Apr 21, 2019
Prev Previous commit
Next Next commit
Fund testing finished
  • Loading branch information
Nicca42 committed Apr 20, 2019
commit ac54330fe04d27e743a95a13c061af78163c11a9
31 changes: 19 additions & 12 deletions contracts/Fund.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ contract Fund is IFund {
{
return _rebalancePeriod;
}
//TODO make all arrays 100 length

function getLastRebalance()
public
view
Expand All @@ -107,6 +107,14 @@ contract Fund is IFund {
return _lastRebalance;
}

function getNextRebalance()
public
view
returns(uint256)
{
return _lastRebalance + _rebalancePeriod;
}

function getBalanceOfToken(uint256 _tokenPosition)
public
view
Expand Down Expand Up @@ -138,15 +146,12 @@ contract Fund is IFund {
uint8[] memory _percentages
)
public
// notDisabled()
notDisabled()
isAdmin()
{
_tokens = _tokenAddresses;
_distribution = _percentages;
for(uint8 i = 0; i < _tokens.length - 1; i++) {
uint256 balance = IERC20(_tokens[i]).balanceOf(address(this));
// IERC20(_tokens[i]).approve(FundFactory(_factory).getRebabalncer(), balance);
}
manualRebalance();
//TODO call the rebalance function and forward all eth
_lastRebalance = now;
}
Expand All @@ -158,20 +163,22 @@ contract Fund is IFund {
require(_lastRebalance + _rebalancePeriod < now, "Rebalance period has not passed");
for(uint i = 0; i < _tokens.length; i++) {
uint256 balance = IERC20(_tokens[i]).balanceOf(address(this));
IERC20(_tokens[i]).approve(FundFactory(_factory).getRebabalncer(), balance);
IERC20(_tokens[i]).transfer(FundFactory(_factory).getRebabalncer(), balance);
}
//TODO make all arrays 100 length
//TODO: Call rebalancer with all eth
}

function manualRebalance()
public
notDisabled()
isOwner()
isAdmin()
{
for(uint i = 0; i < _tokens.length; i++) {
uint256 balance = IERC20(_tokens[i]).balanceOf(address(this));
// IERC20(_tokens[i]).approve(FundFactory(_factory).getRebabalncer(), balance);
IERC20(_tokens[i]).transfer(FundFactory(_factory).getRebabalncer(), balance);
}
//TODO make all arrays 100 length
//TODO: Call rebalancer with all eth
}

Expand All @@ -182,9 +189,9 @@ contract Fund is IFund {
//TODO: send all tokens to owner
for(uint i = 0; i < _tokens.length; i++) {
uint256 balance = IERC20(_tokens[i]).balanceOf(address(this));
// IERC20(_tokens[i]).transfer(_owner, balance);
// uint256 balanceAfter = IERC20(_tokens[i]).balanceOf(address(this));
// require(balanceAfter == 0, "Sending funds failed");
IERC20(_tokens[i]).transfer(_owner, balance);
uint256 balanceAfter = IERC20(_tokens[i]).balanceOf(address(this));
require(balanceAfter == 0, "Sending funds failed");
}
//TODO: send remaining eth
emit FundDeath(_owner);
Expand Down
52 changes: 44 additions & 8 deletions test/Fund.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
increaseTimeTo,
duration
} = require('./helpers/increaseTime');
const latestTime = require("./helpers/latestTime");
const _ = require("lodash");
const BigNumber = web3.BigNumber;

Expand Down Expand Up @@ -89,8 +90,8 @@ contract("Fund", (accounts) => {
await erc20Two.mint(fund.address, 1000);
await erc20Three.mint(fund.address, 10000);
await erc20Four.mint(fund.address, 3000);
//TODO: create tokens to interact with
//TODO: check buying and selling works? maybe in a sep test
let balance = await erc20One.balanceOf(fund.address);
assert.equal(balance.toNumber(), 100, "Balance not set");
});

it("Fund created correctly", async () => {
Expand Down Expand Up @@ -149,10 +150,29 @@ contract("Fund", (accounts) => {


it("Rebalance check", async () => {
await assertRevert(fund.rebalance( { from: fundOwner } ), EVMRevert);
// await increaseTimeTo(fundDetails.rebalancePeriod + 100);
// let receipt = await fund.rebalance( { from: fundOwner } );
// console.log(receipt);
let receipt = await fundFactory.createFund(
fundDetails.newTokenAddresses,
fundDetails.newTokenPercentages,
fundDetails.rebalancePeriod,
{ from: fundOwner }
)
let receivedFundAddress = receipt.receipt.logs['0'].args['1'];
fund = await Fund.at(receivedFundAddress);
let ethReserve = web3.utils.toWei("10", 'ether')
await fund.init( { from: fundOwner, value: ethReserve } );
let lastRebalance = await fund.getLastRebalance();
let nextRebalance = await fund.getNextRebalance();
let result = nextRebalance - lastRebalance
assert.notEqual(
lastRebalance.toNumber(),
nextRebalance.toNumber(),
"Rebalance amounts not correct"
);
assert.equal(
result,
fundDetails.rebalancePeriod,
"Rebalance period does not match"
);
});


Expand All @@ -165,8 +185,24 @@ contract("Fund", (accounts) => {
});

it("When fund is created rebalance happens", async () => {
//TODO: check that the rebalance time is set to now
//TODO: check rebalance happens

//TODO: check tokens get bought
});

it("Check kill fund moves tokens", async () => {
await fund.killFund( { from: fundOwner } );
let balance1 = await erc20One.balanceOf(fund.address);
let balance2 = await erc20Two.balanceOf(fund.address);
let balance3 = await erc20Three.balanceOf(fund.address);
let balance4 = await erc20Four.balanceOf(fund.address);
assert.equal(balance1.toNumber(), 0, "Fund is not drained");
assert.equal(balance2.toNumber(), 0, "Fund is not drained");
assert.equal(balance3.toNumber(), 0, "Fund is not drained");
assert.equal(balance4.toNumber(), 0, "Fund is not drained");
await assertRevert(fund.addTokens(
fundDetails.tokenAddresses,
fundDetails.tokenPercentages,
{ from: fundOwner }
), EVMRevert);
});
});