Skip to content

Commit

Permalink
WIP add part complete fund manager for the OVM #8
Browse files Browse the repository at this point in the history
  • Loading branch information
bitcoinwarrior1 committed May 2, 2021
1 parent fe02c41 commit 5aba3cf
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 24 deletions.
74 changes: 74 additions & 0 deletions contracts/DCAManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
pragma solidity ^0.8.0;

import "./interfaces/IDCAManager.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

/*
* @dev this contract is responsible for dollar cost averaging a users position from DAI to ETH at a specific time (weekly) and percentage(10%) on the OVM
*/
contract DCAManager is IDCAManager {

uint public percentage; //= 0.1 ether; // since both DAI and WETH are decimals 18
uint public timeInterval; //= 604800; // 1 week in seconds
uint public lastTimeStamp;
string public fundPosition; // e.g. DAI to WETH 10% Weekly
address public baseToken;
address public destinationToken;

constructor(
address _baseToken,
address _destinationToken,
uint _timeInterval,
uint _percentage,
string _fundPosition
) public {
lastTimeStamp = block.timestamp;
baseToken = _baseToken;
destinationToken = _destinationToken;
timeInterval = _timeInterval;
percentage = _percentage;
fundPosition = _fundPosition;
}

mapping(address => mapping(address => uint)) balances; // user => token => balance

modifier isATokenPair(address token) {
require(token == baseToken || token == destinationToken);
_;
}

// you should only deposit the base token i.e. DAI
function deposit(uint amount) public returns(bool) {
require(ERC20(baseToken).transferFrom(msg.sender, address(this), amount));
baseTokenBalances[msg.sender] += amount;

return true;
}

function getContractBalances() public view returns(address, uint, address, uint) {
uint baseTokenBalance = ERC20(baseToken).balanceOf(address(this));
uint destinationTokenBalance = ERC20(destinationToken).balanceOf(address(this));
// base & destination token
return (baseToken, baseTokenBalance, destinationToken, destinationTokenBalance);
}

// you should be able to withdraw either the base token or the destination token
function withdraw(uint amount, address token) public isATokenPair(token) returns(bool) {
require(balances[msg.sender][token] >= amount);
require(ERC20(token).transferFrom(address(this), msg.sender, amount));
balances[msg.sender][token] -= amount;

return true;
}

function performSwap() public returns(bool) {
require(lastTimeStamp + timeInterval <= block.timestamp);
(address dai, uint daiBalance, address weth, uint wethBalance) = getContractBalances();
uint amountOfDaiToSwap = daiBalance * percentage;
//TODO do uniswap swap here with default values
lastTimeStamp = block.timestamp;

return true;
}

}
23 changes: 0 additions & 23 deletions contracts/Greeter.sol

This file was deleted.

26 changes: 26 additions & 0 deletions contracts/interfaces/IDCAManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pragma solidity ^0.8.0;

interface IDCAManager {
/*
* @dev allow the user to deposit the base pair into the contract for DCA
* @param amount - the amount of the token they wish to deposit
*/
function deposit(uint amount) external returns(bool);
/*
* @dev allow the user to withdraw the either token from the pool
* @param amount - the amount of the token they wish to deposit
* @param token - the address of the token contract
* @returns boolean - true if successful else revert
*/
function withdraw(uint amount, address token) external returns(bool);
/*
* @dev perform the swap of the base token into the destination token
* @returns boolean - true if successful else revert
*/
function performSwap() external returns(bool);
/*
* @dev get the balances of both the base and destination token that the contract holds
* @returns address base token, uint balance, address destination token, uint balance
*/
function getContractBalances() external returns(address, uint, address, uint);
}
2 changes: 1 addition & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ task("accounts", "Prints the list of accounts", async () => {
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: "0.7.3",
solidity: "0.8.0",
defaultNetwork: "hardhat",
networks: {
hardhat: {
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"ethereum-waffle": "^3.3.0",
"ethers": "^5.1.4",
"hardhat": "^2.2.1"
},
"dependencies": {
"@openzeppelin/contracts": "^4.1.0"
}
}

0 comments on commit 5aba3cf

Please sign in to comment.