mirror of
				https://github.com/Instadapp/yield-contract.git
				synced 2024-07-29 21:47:29 +00:00 
			
		
		
		
	Basic contracts. WIP
This commit is contained in:
		
							parent
							
								
									1c0d4bfae6
								
							
						
					
					
						commit
						82584a71ed
					
				
							
								
								
									
										13
									
								
								contracts/ethPool.sol
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								contracts/ethPool.sol
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,13 @@ | |||
| // SPDX-License-Identifier: MIT | ||||
| pragma solidity ^0.6.8; | ||||
| 
 | ||||
| import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||||
| 
 | ||||
| contract PoolToken is ERC20 { | ||||
|     constructor( | ||||
|         string memory _name, | ||||
|         string memory _symbol | ||||
|     ) public ERC20(_name, _symbol) { | ||||
|         // TODO - 0 | ||||
|     } | ||||
| } | ||||
							
								
								
									
										42
									
								
								contracts/libs/safeMath.sol
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								contracts/libs/safeMath.sol
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,42 @@ | |||
| // SPDX-License-Identifier: MIT | ||||
| 
 | ||||
| pragma solidity ^0.6.8; | ||||
| 
 | ||||
| import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol"; | ||||
| 
 | ||||
| contract DSMath { | ||||
|   uint constant WAD = 10 ** 18; | ||||
|   uint constant RAY = 10 ** 27; | ||||
| 
 | ||||
|   function add(uint x, uint y) internal pure returns (uint z) { | ||||
|     z = SafeMath.add(x, y); | ||||
|   } | ||||
| 
 | ||||
|   function sub(uint x, uint y) internal virtual pure returns (uint z) { | ||||
|     z = SafeMath.sub(x, y); | ||||
|   } | ||||
| 
 | ||||
|   function mul(uint x, uint y) internal pure returns (uint z) { | ||||
|     z = SafeMath.mul(x, y); | ||||
|   } | ||||
| 
 | ||||
|   function div(uint x, uint y) internal pure returns (uint z) { | ||||
|     z = SafeMath.div(x, y); | ||||
|   } | ||||
| 
 | ||||
|   function wmul(uint x, uint y) internal pure returns (uint z) { | ||||
|     z = SafeMath.add(SafeMath.mul(x, y), WAD / 2) / WAD; | ||||
|   } | ||||
| 
 | ||||
|   function wdiv(uint x, uint y) internal pure returns (uint z) { | ||||
|     z = SafeMath.add(SafeMath.mul(x, WAD), y / 2) / y; | ||||
|   } | ||||
| 
 | ||||
|   function rdiv(uint x, uint y) internal pure returns (uint z) { | ||||
|     z = SafeMath.add(SafeMath.mul(x, RAY), y / 2) / y; | ||||
|   } | ||||
| 
 | ||||
|   function rmul(uint x, uint y) internal pure returns (uint z) { | ||||
|     z = SafeMath.add(SafeMath.mul(x, y), RAY / 2) / RAY; | ||||
|   } | ||||
| } | ||||
							
								
								
									
										54
									
								
								contracts/rateLogics/dai.sol
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								contracts/rateLogics/dai.sol
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,54 @@ | |||
| // SPDX-License-Identifier: MIT | ||||
| pragma solidity ^0.6.8; | ||||
| 
 | ||||
| import { DSMath } from "../libs/safeMath.sol"; | ||||
| 
 | ||||
| interface PoolTokenInterface { | ||||
|   function totalBalance() external view returns (uint); | ||||
|   function dsaAmount() external view returns (uint); | ||||
|   function totalSupply() external view returns (uint); | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| interface ATokenInterface { | ||||
|   function balanceOf(address) external view returns (uint); | ||||
| } | ||||
| 
 | ||||
| interface CTokenInterface { | ||||
|   function getExchangeRate() external view returns (uint); | ||||
|   function balanceOf(address) external view returns (uint); | ||||
| } | ||||
| 
 | ||||
| contract RateLogic is DSMath { | ||||
|     PoolTokenInterface poolToken = PoolTokenInterface(address(0)); | ||||
|     ATokenInterface atoken = ATokenInterface(address(0)); | ||||
|     CTokenInterface ctoken = CTokenInterface(address(0)); | ||||
|     CTokenInterface token = CTokenInterface(address(0)); | ||||
| 
 | ||||
|     uint fee = 1e17; // 10% | ||||
|     function totalBalanceDSA() public view returns (uint) { | ||||
|         address _dsa; | ||||
|         uint abal = atoken.balanceOf(_dsa); | ||||
|         uint cbal = wmul(ctoken.balanceOf(_dsa), ctoken.getExchangeRate()); | ||||
|         uint bal = token.balanceOf(_dsa); | ||||
|         return add(abal, add(cbal, bal)); | ||||
|     } | ||||
| 
 | ||||
|     function totalBalance() public view returns (uint) { | ||||
|         address _dsa; | ||||
|         uint abal = atoken.balanceOf(_dsa); | ||||
|         uint cbal = wmul(ctoken.balanceOf(_dsa), ctoken.getExchangeRate()); | ||||
|         uint dsaBal = token.balanceOf(_dsa); | ||||
|         uint poolBal = token.balanceOf(address(poolToken)); | ||||
|         return add(add(abal, poolBal) , add(cbal, dsaBal)); | ||||
|     } | ||||
| 
 | ||||
|     function pricePerToken() public view returns(uint256) { | ||||
|         // TODO - add security logic | ||||
|         uint _totalBalance = totalBalanceDSA(); | ||||
|         uint profit = sub(_totalBalance, poolToken.dsaAmount()); | ||||
|         uint leftProfit = wmul(profit, fee); | ||||
|         uint leftTotalBalance = add(leftProfit, poolToken.dsaAmount()); | ||||
|         return wdiv(leftTotalBalance, poolToken.totalSupply()); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										62
									
								
								contracts/registry.sol
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								contracts/registry.sol
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,62 @@ | |||
| // SPDX-License-Identifier: MIT | ||||
| 
 | ||||
| pragma solidity ^0.6.8; | ||||
| pragma experimental ABIEncoderV2; | ||||
| 
 | ||||
| // TODO | ||||
| // function for adding and removing rate logics. | ||||
| // Added fee variable and required function to change it. | ||||
| // Link all the contract more properly. | ||||
| // Have to think more on pricePerToken function. | ||||
| 
 | ||||
| interface IndexInterface { | ||||
|   function master() external view returns (address); | ||||
| } | ||||
| 
 | ||||
| contract Registry { | ||||
| 
 | ||||
|   event LogAddChief(address indexed chief); | ||||
|   event LogRemoveChief(address indexed chief); | ||||
| 
 | ||||
|   IndexInterface public instaIndex; | ||||
| 
 | ||||
|   mapping (address => bool) public chief; | ||||
|   mapping (address => address) public dsa; | ||||
|   mapping (address => address) public rateLogic; | ||||
| 
 | ||||
|   modifier isMaster() { | ||||
|     require(msg.sender == instaIndex.master(), "not-master"); | ||||
|     _; | ||||
|   } | ||||
| 
 | ||||
|   modifier isController() { | ||||
|     require(chief[msg.sender] || msg.sender == instaIndex.master(), "not-chief"); | ||||
|     _; | ||||
|   } | ||||
| 
 | ||||
|   constructor(address _index) public { | ||||
|     instaIndex = IndexInterface(_index); | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|     * @dev Enable New Chief. | ||||
|     * @param _chief Address of the new chief. | ||||
|   */ | ||||
|   function enableChief(address _chief) external isMaster { | ||||
|       require(_chief != address(0), "address-not-valid"); | ||||
|       require(!chief[_chief], "chief-already-enabled"); | ||||
|       chief[_chief] = true; | ||||
|       emit LogAddChief(_chief); | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|     * @dev Disable Chief. | ||||
|     * @param _chief Address of the existing chief. | ||||
|   */ | ||||
|   function disableChief(address _chief) external isMaster { | ||||
|       require(_chief != address(0), "address-not-valid"); | ||||
|       require(chief[_chief], "chief-already-disabled"); | ||||
|       delete chief[_chief]; | ||||
|       emit LogRemoveChief(_chief); | ||||
|   } | ||||
| } | ||||
							
								
								
									
										113
									
								
								contracts/tokenPool.sol
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										113
									
								
								contracts/tokenPool.sol
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,113 @@ | |||
| // SPDX-License-Identifier: MIT | ||||
| pragma solidity ^0.6.8; | ||||
| pragma experimental ABIEncoderV2; | ||||
| 
 | ||||
| import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||||
| import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||||
| import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; | ||||
| 
 | ||||
| import { DSMath } from "./libs/safeMath.sol"; | ||||
| 
 | ||||
| // TODO - Add ReentrancyGuard lib | ||||
| 
 | ||||
| interface AccountInterface { | ||||
|   function enable(address authority) external; | ||||
|   function cast(address[] calldata _targets, bytes[] calldata _datas, address _origin) external payable; | ||||
| } | ||||
| 
 | ||||
| interface IndexInterface { | ||||
|   function master() external view returns (address); | ||||
| } | ||||
| 
 | ||||
| interface RegistryInterface { | ||||
|   function chief(address) external view returns (bool); | ||||
|   function dsa(address) external view returns (address); | ||||
|   function rateLogic(address) external view returns (address); | ||||
| } | ||||
| 
 | ||||
| interface RateInterface { | ||||
|   function pricePerToken() external view returns (uint); | ||||
|   function totalBalance() external view returns (uint); | ||||
| } | ||||
| 
 | ||||
| contract PoolToken is ERC20, DSMath { | ||||
|     using SafeERC20 for IERC20; | ||||
| 
 | ||||
|     IERC20 public immutable baseToken; | ||||
|     RegistryInterface public immutable registry; | ||||
|     IndexInterface public immutable instaIndex; | ||||
| 
 | ||||
|     uint private tokenBalance; | ||||
|     uint private tokenProfit; | ||||
|     uint private tokenCap; | ||||
|      | ||||
|      | ||||
| 
 | ||||
|     constructor( | ||||
|         address _registry, | ||||
|         address _index, | ||||
|         string memory _name, | ||||
|         string memory _symbol, | ||||
|         address _baseToken | ||||
|     ) public ERC20(_name, _symbol) { | ||||
|         // TODO - 0 | ||||
|         baseToken = IERC20(_baseToken); | ||||
|         registry = RegistryInterface(_registry); | ||||
|         instaIndex = IndexInterface(_index); | ||||
|     } | ||||
| 
 | ||||
|     modifier isMaster() { | ||||
|         require(msg.sender == instaIndex.master(), "not-master"); | ||||
|         _; | ||||
|     } | ||||
| 
 | ||||
|     modifier isChief() { | ||||
|         require(registry.chief(msg.sender) || msg.sender == instaIndex.master(), "not-chief"); | ||||
|         _; | ||||
|     } | ||||
|      | ||||
|     uint dsaAmount; | ||||
|     function depositDSA(uint amount) public isChief { | ||||
|         address _dsa = registry.dsa(address(this)); | ||||
|         baseToken.safeTransfer(_dsa, amount); | ||||
|         dsaAmount = add(dsaAmount, amount); | ||||
|     } | ||||
| 
 | ||||
|     function withdrawDSA(uint amount) public isChief { | ||||
|         // address _dsa = registry.dsa(address(this)); | ||||
|         baseToken.safeTransferFrom(msg.sender, address(this), amount); | ||||
|         uint totalAmountWithProfit = RateInterface(address(0)).totalBalance(); // TODO - change to => totalBalanceDSA | ||||
|         uint _amount = wdiv(wmul(amount, dsaAmount), totalAmountWithProfit); | ||||
|         uint profit = sub(amount, _amount); | ||||
|         tokenProfit = add(tokenProfit, profit); | ||||
|         dsaAmount = sub(dsaAmount, _amount); | ||||
|     } | ||||
| 
 | ||||
|     function getBalance() public view returns(uint) { | ||||
|         return sub(add(dsaAmount, baseToken.balanceOf(address(this))), tokenProfit); | ||||
|     } | ||||
| 
 | ||||
|     function pricePerToken() public view returns(uint) { | ||||
|         return 1e18; // TODO - still need to work on it. | ||||
|     } | ||||
| 
 | ||||
|     function deposit(uint amount) public returns(uint) { | ||||
|         uint _newTokenBal = add(tokenBalance, amount); | ||||
|         require(_newTokenBal <= getBalance(), "deposit-cap-reached"); | ||||
| 
 | ||||
|         baseToken.safeTransferFrom(msg.sender, address(this), amount); | ||||
|         uint iAmt = wdiv(amount, pricePerToken()); | ||||
|         _mint(msg.sender, iAmt); | ||||
|     } | ||||
| 
 | ||||
|     function withdraw(address owner, uint iAmount) public returns (uint) { | ||||
|         uint amount = wmul(iAmount, pricePerToken()); | ||||
|         _burn(msg.sender, iAmount); | ||||
| 
 | ||||
|         baseToken.safeTransfer(owner, amount); | ||||
|     } | ||||
| 
 | ||||
|     function withdraw(uint amount) public returns (uint) { | ||||
|         return withdraw(msg.sender, amount); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										7
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										7
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							|  | @ -531,10 +531,9 @@ | |||
|       } | ||||
|     }, | ||||
|     "@openzeppelin/contracts": { | ||||
|       "version": "2.5.1", | ||||
|       "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-2.5.1.tgz", | ||||
|       "integrity": "sha512-qIy6tLx8rtybEsIOAlrM4J/85s2q2nPkDqj/Rx46VakBZ0LwtFhXIVub96LXHczQX0vaqmAueDqNPXtbSXSaYQ==", | ||||
|       "dev": true | ||||
|       "version": "3.1.0", | ||||
|       "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-3.1.0.tgz", | ||||
|       "integrity": "sha512-dVXDnUKxrAKLzPdCRkz+N8qsVkK1XxJ6kk3zuI6zaQmcKxN7CkizoDP7lXxcs/Mi2I0mxceTRjJBqlzFffLJrQ==" | ||||
|     }, | ||||
|     "@openzeppelin/test-helpers": { | ||||
|       "version": "0.5.6", | ||||
|  |  | |||
|  | @ -17,6 +17,7 @@ | |||
|   }, | ||||
|   "homepage": "https://github.com/InstaDApp/dsa-yield-contract#readme", | ||||
|   "dependencies": { | ||||
|     "@openzeppelin/contracts": "^3.1.0", | ||||
|     "solc": "^0.6.8", | ||||
|     "truffle-assertions": "^0.9.2", | ||||
|     "truffle-hdwallet-provider": "^1.0.17", | ||||
|  | @ -24,7 +25,6 @@ | |||
|     "web3": "^1.2.11" | ||||
|   }, | ||||
|   "devDependencies": { | ||||
|     "@openzeppelin/contracts": "^2.4.0", | ||||
|     "@nomiclabs/buidler": "^1.4.3", | ||||
|     "@nomiclabs/buidler-truffle5": "^1.3.4", | ||||
|     "@nomiclabs/buidler-web3": "^1.3.4", | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Thrilok Kumar
						Thrilok Kumar