yield-contract/contracts/pools/erc20.sol

160 lines
6.0 KiB
Solidity
Raw Normal View History

2020-08-22 03:07:55 +00:00
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.8;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
2020-08-25 14:32:59 +00:00
import "@openzeppelin/contracts/token/ERC20/ERC20Pausable.sol";
2020-08-25 15:54:42 +00:00
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
2020-08-22 03:07:55 +00:00
import { DSMath } from "./libs/safeMath.sol";
interface AccountInterface {
function enable(address authority) external;
2020-08-26 16:46:39 +00:00
function isAuth(address) external view returns(bool);
2020-08-22 03:07:55 +00:00
function cast(address[] calldata _targets, bytes[] calldata _datas, address _origin) external payable;
}
interface IndexInterface {
function master() external view returns (address);
2020-08-24 00:40:01 +00:00
function build(address _owner, uint accountVersion, address _origin) external returns (address _account);
2020-08-22 03:07:55 +00:00
}
interface RegistryInterface {
function chief(address) external view returns (bool);
2020-08-24 00:40:01 +00:00
function poolLogic(address) external returns (address);
function poolCap(address) external view returns (uint);
function insureFee(address) external view returns (uint);
2020-08-26 13:35:10 +00:00
function isDsa(address, address) external view returns (bool);
2020-08-22 03:07:55 +00:00
}
interface RateInterface {
2020-08-24 00:40:01 +00:00
function getTotalToken() external returns (uint totalUnderlyingTkn);
2020-08-22 03:07:55 +00:00
}
2020-08-25 15:54:42 +00:00
contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable {
2020-08-22 03:07:55 +00:00
using SafeERC20 for IERC20;
event LogDeploy(address token, uint amount);
2020-08-24 00:40:01 +00:00
event LogExchangeRate(uint exchangeRate, uint tokenBalance, uint insuranceAmt);
event LogSettle(uint settleTime);
event LogDeposit(uint depositAmt, uint poolMintAmt);
event LogWithdraw(uint withdrawAmt, uint poolBurnAmt);
event LogAddInsurance(uint amount);
2020-08-24 18:01:28 +00:00
event LogPausePool(bool);
2020-08-24 00:40:01 +00:00
IERC20 public immutable baseToken; // Base token. Eg:- DAI, USDC, etc.
RegistryInterface public immutable registry; // Pool Registry
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); // Main Index
2020-08-22 03:07:55 +00:00
2020-08-24 00:40:01 +00:00
uint private tokenBalance; // total token balance since last rebalancing
2020-08-27 06:56:00 +00:00
uint public exchangeRate; // initial 1 token = 1
2020-08-24 00:40:01 +00:00
uint public insuranceAmt; // insurance amount to keep pool safe
2020-08-22 03:07:55 +00:00
constructor(
address _registry,
string memory _name,
string memory _symbol,
2020-08-26 13:35:10 +00:00
address _baseToken
2020-08-22 03:07:55 +00:00
) public ERC20(_name, _symbol) {
baseToken = IERC20(_baseToken);
registry = RegistryInterface(_registry);
2020-08-27 06:56:00 +00:00
exchangeRate = 10 ** uint(36 - ERC20(_baseToken).decimals());
2020-08-22 03:07:55 +00:00
}
modifier isChief() {
require(registry.chief(msg.sender) || msg.sender == instaIndex.master(), "not-chief");
_;
}
2020-08-24 00:40:01 +00:00
function deploy(address _dsa, address token, uint amount) public isChief {
2020-08-26 13:35:10 +00:00
require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa");
2020-08-26 16:46:39 +00:00
require(AccountInterface(_dsa).isAuth(address(this)), "token-pool-not-auth");
if (token == address(0)) {
baseToken.safeTransfer(_dsa, amount);
} else if (token == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE){
payable(_dsa).transfer(amount);
} else {
IERC20(token).safeTransfer(_dsa, amount);
}
emit LogDeploy(token, amount);
2020-08-22 03:07:55 +00:00
}
2020-08-24 00:40:01 +00:00
function setExchangeRate() public isChief {
2020-08-27 10:23:04 +00:00
uint _previousRate = exchangeRate;
uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken();
2020-08-27 11:02:09 +00:00
_totalToken = sub(_totalToken, insuranceAmt);
2020-08-27 10:23:04 +00:00
uint _currentRate = wdiv(totalSupply(), _totalToken);
require(_currentRate != 0, "currentRate-is-0");
2020-08-27 10:23:04 +00:00
if (_currentRate > _previousRate) {
2020-08-27 06:53:28 +00:00
uint difTkn = sub(tokenBalance, _totalToken);
insuranceAmt = sub(insuranceAmt, difTkn);
_currentRate = _previousRate;
} else {
uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), registry.insureFee(address(this)));
insuranceAmt = add(insuranceAmt, insureFeeAmt);
2020-08-27 10:23:04 +00:00
tokenBalance = sub(_totalToken, insureFeeAmt);
_currentRate = wdiv(totalSupply(), tokenBalance);
}
2020-08-27 10:23:04 +00:00
exchangeRate = _currentRate;
emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt);
2020-08-22 03:07:55 +00:00
}
2020-08-26 13:35:10 +00:00
function settle(address _dsa, address[] calldata _targets, bytes[] calldata _datas, address _origin) external isChief {
require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa");
2020-08-26 16:46:39 +00:00
AccountInterface dsaWallet = AccountInterface(_dsa);
if (_targets.length > 0 && _datas.length > 0) {
2020-08-26 16:46:39 +00:00
dsaWallet.cast(_targets, _datas, _origin);
}
2020-08-26 16:46:39 +00:00
require(dsaWallet.isAuth(address(this)), "token-pool-not-auth");
setExchangeRate();
emit LogSettle(block.timestamp);
2020-08-22 03:07:55 +00:00
}
function deposit(uint tknAmt) external whenNotPaused payable returns(uint) {
uint _newTokenBal = add(tokenBalance, tknAmt);
require(_newTokenBal <= registry.poolCap(address(this)), "deposit-cap-reached");
2020-08-22 03:07:55 +00:00
baseToken.safeTransferFrom(msg.sender, address(this), tknAmt);
2020-08-27 10:23:04 +00:00
uint _mintAmt = wmul(tknAmt, exchangeRate);
_mint(msg.sender, _mintAmt);
2020-08-22 03:07:55 +00:00
emit LogDeposit(tknAmt, _mintAmt);
2020-08-22 03:07:55 +00:00
}
2020-08-25 15:54:42 +00:00
function withdraw(uint tknAmt, address to) external nonReentrant whenNotPaused returns (uint _tknAmt) {
uint poolBal = baseToken.balanceOf(address(this));
require(tknAmt <= poolBal, "not-enough-liquidity-available");
uint _bal = balanceOf(msg.sender);
2020-08-27 10:23:04 +00:00
uint _tknBal = wdiv(_bal, exchangeRate);
uint _burnAmt;
if (tknAmt == uint(-1)) {
_burnAmt = _bal;
2020-08-27 10:23:04 +00:00
_tknAmt = wdiv(_burnAmt, exchangeRate);
} else {
require(tknAmt <= _tknBal, "balance-exceeded");
2020-08-27 10:23:04 +00:00
_burnAmt = wmul(tknAmt, exchangeRate);
_tknAmt = tknAmt;
}
_burn(msg.sender, _burnAmt);
baseToken.safeTransfer(to, _tknAmt);
emit LogWithdraw(tknAmt, _burnAmt);
2020-08-24 00:40:01 +00:00
}
2020-08-22 03:07:55 +00:00
2020-08-24 00:40:01 +00:00
function addInsurance(uint tknAmt) external payable {
baseToken.safeTransferFrom(msg.sender, address(this), tknAmt);
insuranceAmt += tknAmt;
emit LogAddInsurance(tknAmt);
2020-08-22 03:07:55 +00:00
}
2020-08-24 00:40:01 +00:00
function shutdown() external {
require(msg.sender == instaIndex.master(), "not-master");
paused() ? _unpause() : _pause();
2020-08-22 03:07:55 +00:00
}
2020-08-27 11:02:09 +00:00
receive() external payable {}
2020-08-22 03:07:55 +00:00
}