yield-contract/contracts/pools/eth.sol

189 lines
6.2 KiB
Solidity
Raw Normal View History

2020-08-22 03:07:55 +00:00
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.8;
2020-08-24 00:40:01 +00:00
pragma experimental ABIEncoderV2;
2020-08-22 03:07:55 +00:00
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Pausable.sol";
2020-08-25 15:54:42 +00:00
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
2020-08-24 00:40:01 +00:00
2020-08-28 17:12:59 +00:00
import { DSMath } from "../libs/safeMath.sol";
2020-08-24 00:40:01 +00:00
interface IndexInterface {
function master() external view returns (address);
function build(address _owner, uint accountVersion, address _origin) external returns (address _account);
}
interface RegistryInterface {
function chief(address) external view returns (bool);
function poolLogic(address) external returns (address);
2020-09-06 12:08:29 +00:00
function fee(address) external view returns (uint);
2020-09-06 16:01:27 +00:00
function poolCap(address) external view returns (uint);
2020-09-06 12:08:29 +00:00
function checkSettleLogics(address, address[] calldata) external view returns (bool);
2020-08-24 00:40:01 +00:00
}
interface RateInterface {
function getTotalToken() external returns (uint totalUnderlyingTkn);
}
2020-08-30 18:03:16 +00:00
contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath {
using SafeERC20 for IERC20;
event LogExchangeRate(uint exchangeRate, uint tokenBalance, uint insuranceAmt);
2020-08-30 17:08:40 +00:00
event LogSettle(uint settleBlock);
event LogDeposit(address indexed user, uint depositAmt, uint poolMintAmt);
2020-09-06 14:26:08 +00:00
event LogWithdraw(address indexed user, uint withdrawAmt, uint poolBurnAmt);
2020-09-06 12:08:29 +00:00
event LogWithdrawFee(uint amount);
event LogPausePool(bool);
2020-09-06 12:08:29 +00:00
IERC20 public immutable baseToken; // Base token.
RegistryInterface public immutable registry; // Pool Registry
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723);
uint public exchangeRate = 10 ** 18; // initial 1 token = 1
2020-09-06 12:08:29 +00:00
uint public feeAmt; // fee collected on profits
constructor(
address _registry,
string memory _name,
string memory _symbol,
2020-08-27 11:02:09 +00:00
address _baseToken
) public ERC20(_name, _symbol) {
baseToken = IERC20(_baseToken);
registry = RegistryInterface(_registry);
}
modifier isChief() {
require(registry.chief(msg.sender) || msg.sender == instaIndex.master(), "not-chief");
_;
}
2020-08-30 15:47:40 +00:00
/**
2020-08-30 16:33:07 +00:00
* @dev get pool token rate
* @param tokenAmt total token amount
*/
2020-09-06 16:01:27 +00:00
function getCurrentRate(uint tokenAmt) internal view returns (uint) {
2020-08-30 15:47:40 +00:00
return wdiv(totalSupply(), tokenAmt);
}
2020-08-30 16:33:07 +00:00
/**
* @dev sets exchange rates
2020-09-06 12:08:29 +00:00
*/
2020-09-06 16:01:27 +00:00
function setExchangeRate() public {
require(msg.sender == address(this), "not-pool-address");
2020-09-06 15:19:39 +00:00
uint _prevRate = exchangeRate;
uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken();
_totalToken = sub(_totalToken, feeAmt);
uint _newRate = getCurrentRate(_totalToken);
2020-09-06 16:01:27 +00:00
uint _tokenBal;
2020-09-06 15:19:39 +00:00
require(_newRate != 0, "current-rate-is-zero");
if (_newRate > _prevRate) {
_newRate = _prevRate;
2020-09-06 13:38:13 +00:00
} else {
2020-09-06 16:01:27 +00:00
_tokenBal = wdiv(totalSupply(), _prevRate);
2020-09-06 15:19:39 +00:00
uint _newFee = wmul(sub(_totalToken, _tokenBal), registry.fee(address(this)));
feeAmt = add(feeAmt, _newFee);
_tokenBal = sub(_totalToken, _newFee);
_newRate = getCurrentRate(_tokenBal);
2020-08-24 00:40:01 +00:00
}
2020-09-06 15:19:39 +00:00
exchangeRate = _newRate;
emit LogExchangeRate(exchangeRate, _tokenBal, feeAmt);
2020-09-06 12:08:29 +00:00
}
/**
2020-09-06 15:19:39 +00:00
* @dev delegate the calls to connector and this function is ran by settle()
2020-09-06 12:08:29 +00:00
* @param _target Target to of Connector.
* @param _data CallData of function in Connector.
*/
function spell(address _target, bytes memory _data) internal {
require(_target != address(0), "target-invalid");
assembly {
let succeeded := delegatecall(gas(), _target, add(_data, 0x20), mload(_data), 0, 0)
switch iszero(succeeded)
case 1 {
// throw if delegatecall failed
let size := returndatasize()
returndatacopy(0x00, 0x00, size)
revert(0x00, size)
}
}
}
2020-08-24 00:40:01 +00:00
/**
* @dev Settle the assets on dsa and update exchange rate
* @param _targets array of connector's address
2020-09-06 12:08:29 +00:00
* @param _data array of connector's function calldata
*/
2020-09-06 12:08:29 +00:00
function settle(address[] calldata _targets, bytes[] calldata _data) external isChief {
require(_targets.length == _data.length , "array-length-invalid");
require(registry.checkSettleLogics(address(this), _targets), "not-logic");
for (uint i = 0; i < _targets.length; i++) {
spell(_targets[i], _data[i]);
2020-08-24 00:40:01 +00:00
}
2020-08-30 17:08:40 +00:00
emit LogSettle(block.number);
}
/**
* @dev Deposit token.
* @param tknAmt token amount
2020-09-06 14:26:08 +00:00
* @return mintAmt amount of wrap token minted
*/
2020-09-06 14:26:08 +00:00
function deposit(uint tknAmt) public whenNotPaused payable returns (uint mintAmt) {
require(tknAmt == msg.value, "unmatched-amount");
2020-09-06 16:01:27 +00:00
uint _tokenBal = wdiv(totalSupply(), exchangeRate);
uint _newTknBal = add(_tokenBal, tknAmt);
require(_newTknBal < registry.poolCap(address(this)), "unmatched-amount");
2020-09-06 14:26:08 +00:00
mintAmt = wmul(msg.value, exchangeRate);
_mint(msg.sender, mintAmt);
emit LogDeposit(msg.sender, tknAmt, mintAmt);
}
/**
* @dev Withdraw tokens.
* @param tknAmt token amount
2020-09-06 14:26:08 +00:00
* @param target withdraw tokens to address
2020-09-06 16:01:27 +00:00
* @return wdAmt amount of token withdrawn
*/
2020-09-06 14:26:08 +00:00
function withdraw(uint tknAmt, address target) external nonReentrant whenNotPaused returns (uint wdAmt) {
require(target != address(0), "invalid-target-address");
2020-09-06 15:19:39 +00:00
uint _userBal = wdiv(balanceOf(msg.sender), exchangeRate);
uint _burnAmt;
if (tknAmt >= _userBal) {
_burnAmt = balanceOf(msg.sender);
wdAmt = _userBal;
} else {
2020-09-06 15:19:39 +00:00
_burnAmt = wmul(tknAmt, exchangeRate);
2020-09-06 14:26:08 +00:00
wdAmt = tknAmt;
2020-08-22 03:07:55 +00:00
}
2020-09-06 14:26:08 +00:00
require(wdAmt <= address(this).balance, "not-enough-liquidity-available");
2020-08-24 00:40:01 +00:00
2020-09-06 15:19:39 +00:00
_burn(msg.sender, _burnAmt);
2020-09-06 14:26:08 +00:00
payable(target).transfer(wdAmt);
2020-08-24 00:40:01 +00:00
2020-09-06 15:19:39 +00:00
emit LogWithdraw(msg.sender, wdAmt, _burnAmt);
}
2020-08-24 00:40:01 +00:00
/**
2020-09-06 14:26:08 +00:00
* @dev withdraw fee from the pool
* @notice only master can call this function
* @param wdAmt fee amount to withdraw
*/
2020-09-06 14:26:08 +00:00
function withdrawFee(uint wdAmt) external {
2020-08-29 15:24:21 +00:00
require(msg.sender == instaIndex.master(), "not-master");
2020-09-06 14:26:08 +00:00
if (wdAmt > feeAmt) wdAmt = feeAmt;
msg.sender.transfer(wdAmt);
feeAmt = sub(feeAmt, wdAmt);
emit LogWithdrawFee(wdAmt);
2020-08-29 15:24:21 +00:00
}
/**
* @dev Shut the pool.
* @notice only master can call this function.
*/
function shutdown() external {
require(msg.sender == instaIndex.master(), "not-master");
paused() ? _unpause() : _pause();
}
2020-08-24 00:40:01 +00:00
receive() external payable {}
2020-09-06 15:19:39 +00:00
}