mirror of
https://github.com/Instadapp/yield-contract.git
synced 2024-07-29 21:47:29 +00:00
mike drop
This commit is contained in:
parent
cba07b8599
commit
1576a2bfc8
|
@ -8,11 +8,6 @@ import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
|
|||
|
||||
import { DSMath } from "../libs/safeMath.sol";
|
||||
|
||||
interface AccountInterface {
|
||||
function isAuth(address) external view returns(bool);
|
||||
function cast(address[] calldata _targets, bytes[] calldata _datas, address _origin) external payable;
|
||||
}
|
||||
|
||||
interface IndexInterface {
|
||||
function master() external view returns (address);
|
||||
function build(address _owner, uint accountVersion, address _origin) external returns (address _account);
|
||||
|
@ -22,7 +17,6 @@ interface RegistryInterface {
|
|||
function chief(address) external view returns (bool);
|
||||
function poolLogic(address) external returns (address);
|
||||
function fee(address) external view returns (uint);
|
||||
function isDsa(address, address) external view returns (bool);
|
||||
function checkSettleLogics(address, address[] calldata) external view returns (bool);
|
||||
}
|
||||
|
||||
|
@ -33,7 +27,6 @@ interface RateInterface {
|
|||
contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
event LogDeploy(address indexed dsa, address indexed token, uint amount);
|
||||
event LogExchangeRate(uint exchangeRate, uint tokenBalance, uint insuranceAmt);
|
||||
event LogSettle(uint settleBlock);
|
||||
event LogDeposit(address indexed user, uint depositAmt, uint poolMintAmt);
|
||||
|
@ -45,7 +38,6 @@ contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath {
|
|||
RegistryInterface public immutable registry; // Pool Registry
|
||||
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723);
|
||||
|
||||
uint private tokenBalance; // total token balance
|
||||
uint public exchangeRate = 10 ** 18; // initial 1 token = 1
|
||||
uint public feeAmt; // fee collected on profits
|
||||
|
||||
|
@ -64,28 +56,11 @@ contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath {
|
|||
_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Deploy assets to DSA.
|
||||
* @param _dsa DSA address
|
||||
* @param token token address
|
||||
* @param amount token amount
|
||||
*/
|
||||
function deploy(address _dsa, address token, uint amount) external isChief {
|
||||
require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa");
|
||||
require(AccountInterface(_dsa).isAuth(address(this)), "token-pool-not-auth");
|
||||
if (token == address(0)) { // pool ETH
|
||||
payable(_dsa).transfer(amount);
|
||||
} else { // non-pool other tokens
|
||||
IERC20(token).safeTransfer(_dsa, amount);
|
||||
}
|
||||
emit LogDeploy(_dsa, token, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev get pool token rate
|
||||
* @param tokenAmt total token amount
|
||||
*/
|
||||
function getCurrentRate(uint tokenAmt) public view returns (uint) {
|
||||
function getCurrentRate(uint tokenAmt) internal returns (uint) {
|
||||
return wdiv(totalSupply(), tokenAmt);
|
||||
}
|
||||
|
||||
|
@ -93,25 +68,26 @@ contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath {
|
|||
* @dev sets exchange rates
|
||||
*/
|
||||
function setExchangeRate() public isChief {
|
||||
uint _previousRate = exchangeRate;
|
||||
uint totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken();
|
||||
totalToken = sub(totalToken, feeAmt);
|
||||
uint _currentRate = getCurrentRate(totalToken);
|
||||
require(_currentRate != 0, "current-rate-is-zero");
|
||||
if (_currentRate > _previousRate) {
|
||||
_currentRate = _previousRate;
|
||||
uint _prevRate = exchangeRate;
|
||||
uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken();
|
||||
_totalToken = sub(_totalToken, feeAmt);
|
||||
uint _newRate = getCurrentRate(_totalToken);
|
||||
require(_newRate != 0, "current-rate-is-zero");
|
||||
if (_newRate > _prevRate) {
|
||||
_newRate = _prevRate;
|
||||
} else {
|
||||
uint newFee = wmul(sub(totalToken, tokenBalance), registry.fee(address(this)));
|
||||
feeAmt = add(feeAmt, newFee);
|
||||
tokenBalance = sub(totalToken, newFee);
|
||||
_currentRate = getCurrentRate(tokenBalance);
|
||||
uint _tokenBal = wdiv(totalSupply(), _prevRate);
|
||||
uint _newFee = wmul(sub(_totalToken, _tokenBal), registry.fee(address(this)));
|
||||
feeAmt = add(feeAmt, _newFee);
|
||||
_tokenBal = sub(_totalToken, _newFee);
|
||||
_newRate = getCurrentRate(_tokenBal);
|
||||
}
|
||||
exchangeRate = _currentRate;
|
||||
emit LogExchangeRate(exchangeRate, tokenBalance, feeAmt);
|
||||
exchangeRate = _newRate;
|
||||
emit LogExchangeRate(exchangeRate, _tokenBal, feeAmt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Delegate the calls to Connector And this function is ran by cast().
|
||||
* @dev delegate the calls to connector and this function is ran by settle()
|
||||
* @param _target Target to of Connector.
|
||||
* @param _data CallData of function in Connector.
|
||||
*/
|
||||
|
@ -141,7 +117,6 @@ contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath {
|
|||
for (uint i = 0; i < _targets.length; i++) {
|
||||
spell(_targets[i], _data[i]);
|
||||
}
|
||||
setExchangeRate();
|
||||
emit LogSettle(block.number);
|
||||
}
|
||||
|
||||
|
@ -152,11 +127,8 @@ contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath {
|
|||
*/
|
||||
function deposit(uint tknAmt) public whenNotPaused payable returns (uint mintAmt) {
|
||||
require(tknAmt == msg.value, "unmatched-amount");
|
||||
tokenBalance = add(tokenBalance, tknAmt);
|
||||
|
||||
mintAmt = wmul(msg.value, exchangeRate);
|
||||
_mint(msg.sender, mintAmt);
|
||||
|
||||
emit LogDeposit(msg.sender, tknAmt, mintAmt);
|
||||
}
|
||||
|
||||
|
@ -168,22 +140,21 @@ contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath {
|
|||
*/
|
||||
function withdraw(uint tknAmt, address target) external nonReentrant whenNotPaused returns (uint wdAmt) {
|
||||
require(target != address(0), "invalid-target-address");
|
||||
uint userBal = wdiv(balanceOf(msg.sender), exchangeRate);
|
||||
uint burnAmt;
|
||||
if (tknAmt >= userBal) {
|
||||
burnAmt = balanceOf(msg.sender);
|
||||
wdAmt = userBal;
|
||||
uint _userBal = wdiv(balanceOf(msg.sender), exchangeRate);
|
||||
uint _burnAmt;
|
||||
if (tknAmt >= _userBal) {
|
||||
_burnAmt = balanceOf(msg.sender);
|
||||
wdAmt = _userBal;
|
||||
} else {
|
||||
burnAmt = wmul(tknAmt, exchangeRate);
|
||||
_burnAmt = wmul(tknAmt, exchangeRate);
|
||||
wdAmt = tknAmt;
|
||||
}
|
||||
require(wdAmt <= address(this).balance, "not-enough-liquidity-available");
|
||||
|
||||
_burn(msg.sender, burnAmt);
|
||||
tokenBalance = sub(tokenBalance, wdAmt);
|
||||
_burn(msg.sender, _burnAmt);
|
||||
payable(target).transfer(wdAmt);
|
||||
|
||||
emit LogWithdraw(msg.sender, wdAmt, burnAmt);
|
||||
emit LogWithdraw(msg.sender, wdAmt, _burnAmt);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -209,4 +180,4 @@ contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath {
|
|||
}
|
||||
|
||||
receive() external payable {}
|
||||
}
|
||||
}
|
|
@ -14,28 +14,22 @@ contract Registry {
|
|||
event LogRemoveChief(address indexed chief);
|
||||
event LogAddSigner(address indexed signer);
|
||||
event LogRemoveSigner(address indexed signer);
|
||||
event LogUpdatePool(address pool, bool poolState);
|
||||
event LogUpdatePoolLogic(address pool, address newLogic);
|
||||
event LogUpdateTariff(address pool, uint newTariff);
|
||||
event LogUpdatePoolLogic(address token, address newLogic);
|
||||
event LogUpdateFee(address token, uint newFee);
|
||||
event LogAddPool(address indexed token, address indexed pool);
|
||||
event LogRemovePool(address indexed token, address indexed pool);
|
||||
event LogNewDSA(address indexed pool, address indexed dsa);
|
||||
event LogRemoveDSA(address indexed pool, address indexed dsa);
|
||||
event LogAddSettleLogic(address indexed pool, address indexed logic);
|
||||
event LogRemoveSettleLogic(address indexed pool, address indexed logic);
|
||||
event LogAddSettleLogic(address indexed token, address indexed logic);
|
||||
event LogRemoveSettleLogic(address indexed token, address indexed logic);
|
||||
|
||||
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723);
|
||||
|
||||
mapping (address => bool) public chief;
|
||||
mapping (address => bool) public signer;
|
||||
mapping (address => bool) public isPool;
|
||||
mapping (address => address) public poolToken;
|
||||
mapping (address => address) public poolLogic;
|
||||
mapping (address => uint) public poolCap;
|
||||
mapping (address => uint) public tariff;
|
||||
mapping (address => uint) public fee;
|
||||
mapping (address => mapping(address => bool)) public settleLogic;
|
||||
mapping (address => mapping(address => bool)) public isDsa; // pool => dsa => bool
|
||||
mapping (address => address[]) public dsaArr; // pool => all dsa in array
|
||||
|
||||
modifier isMaster() {
|
||||
require(msg.sender == instaIndex.master(), "not-master");
|
||||
|
@ -47,10 +41,6 @@ contract Registry {
|
|||
_;
|
||||
}
|
||||
|
||||
function getDsaLength(address _pool) external view returns(uint) {
|
||||
return dsaArr[_pool].length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Enable New Chief.
|
||||
* @param _chief Address of the new chief.
|
||||
|
@ -97,44 +87,36 @@ contract Registry {
|
|||
|
||||
/**
|
||||
* @dev Add New Pool
|
||||
* @param token ERC20 token address
|
||||
* @param _token ERC20 token address
|
||||
* @param pool pool address
|
||||
*/
|
||||
function addPool(address token, address pool) external isMaster {
|
||||
require(token != address(0) && pool != address(0), "invalid-address");
|
||||
require(poolToken[token] == address(0), "pool-already-added");
|
||||
poolToken[token] = pool;
|
||||
emit LogAddPool(token, pool);
|
||||
function addPool(address _token, address pool) external isMaster {
|
||||
require(_token != address(0) && pool != address(0), "invalid-token-address");
|
||||
require(poolToken[_token] == address(0), "pool-already-added");
|
||||
poolToken[_token] = pool;
|
||||
emit LogAddPool(_token, pool);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Remove Pool
|
||||
* @param token ERC20 token address
|
||||
* @param _token ERC20 token address
|
||||
*/
|
||||
function removePool(address token) external isMaster {
|
||||
require(token != address(0), "invalid-address");
|
||||
require(poolToken[token] != address(0), "pool-already-removed");
|
||||
address poolAddr = poolToken[token];
|
||||
delete poolToken[token];
|
||||
emit LogRemovePool(token, poolAddr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev enable / disable pool
|
||||
* @param _pool pool address
|
||||
*/
|
||||
function updatePool(address _pool) external isMaster {
|
||||
isPool[_pool] = !isPool[_pool];
|
||||
emit LogUpdatePool(_pool, isPool[_pool]);
|
||||
function removePool(address _token) external isMaster {
|
||||
require(_token != address(0), "invalid-token-address");
|
||||
require(poolToken[_token] != address(0), "pool-already-removed");
|
||||
address poolAddr = poolToken[_token];
|
||||
delete poolToken[_token];
|
||||
emit LogRemovePool(_token, poolAddr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev update pool rate logic
|
||||
* @param _pool pool address
|
||||
* @param _token pool address
|
||||
* @param _newLogic new rate logic address
|
||||
*/
|
||||
function updatePoolLogic(address _pool, address _newLogic) external isMaster {
|
||||
require(isPool[_pool], "not-pool");
|
||||
function updatePoolLogic(address _token, address _newLogic) external isMaster {
|
||||
address _pool = poolToken[_token];
|
||||
require(_pool != address(0), "invalid-pool");
|
||||
require(_newLogic != address(0), "invalid-address");
|
||||
require( poolLogic[_pool] != _newLogic, "same-pool-logic");
|
||||
poolLogic[_pool] = _newLogic;
|
||||
|
@ -142,26 +124,29 @@ contract Registry {
|
|||
}
|
||||
|
||||
/**
|
||||
* @dev update pool tariff
|
||||
* @dev update pool fee
|
||||
* @param _pool pool address
|
||||
* @param _newTariff new tariff amount
|
||||
* @param _newFee new fee amount
|
||||
*/
|
||||
function updateTariff(address _pool, uint _newTariff) external isMaster {
|
||||
require(isPool[_pool], "not-pool");
|
||||
require(_newTariff < 3 * 10 ** 17, "insure-tariff-limit-reached");
|
||||
require(tariff[_pool] != _newTariff, "same-pool-tariff");
|
||||
tariff[_pool] = _newTariff;
|
||||
emit LogUpdateTariff(_pool, _newTariff);
|
||||
function updateFee(address _token, uint _newFee) external isMaster {
|
||||
address _pool = poolToken[_token];
|
||||
require(_pool != address(0), "invalid-pool");
|
||||
require(_newFee < 3 * 10 ** 17, "insure-fee-limit-reached");
|
||||
require(fee[_pool] != _newFee, "same-pool-fee");
|
||||
fee[_pool] = _newFee;
|
||||
emit LogUpdateFee(_pool, _newFee);
|
||||
}
|
||||
|
||||
function addSettleLogic(address _pool, address _logic) external isMaster {
|
||||
require(isPool[_pool], "not-pool");
|
||||
function addSettleLogic(address _token, address _logic) external isMaster {
|
||||
address _pool = poolToken[_token];
|
||||
require(_pool != address(0), "invalid-pool");
|
||||
settleLogic[_pool][_logic] = true;
|
||||
emit LogAddSettleLogic(_pool, _logic);
|
||||
}
|
||||
|
||||
function removeSettleLogic(address _pool, address _logic) external isMaster {
|
||||
require(isPool[_pool], "not-pool");
|
||||
function removeSettleLogic(address _token, address _logic) external isMaster {
|
||||
address _pool = poolToken[_token];
|
||||
require(_pool != address(0), "invalid-pool");
|
||||
delete settleLogic[_pool][_logic];
|
||||
emit LogRemoveSettleLogic(_pool, _logic);
|
||||
}
|
||||
|
@ -175,31 +160,6 @@ contract Registry {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev add dsa for a pool
|
||||
* @param _pool pool address
|
||||
* @param _dsa DSA address
|
||||
*/
|
||||
function addDsa(address _pool, address _dsa) external isMaster {
|
||||
require(isPool[_pool], "not-pool");
|
||||
if (_dsa == address(0)) _dsa = instaIndex.build(_pool, 1, address(this));
|
||||
isDsa[_pool][_dsa] = true;
|
||||
dsaArr[_pool].push(_dsa);
|
||||
emit LogNewDSA(_pool, _dsa);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev remove dsa from a pool
|
||||
* @param _pool pool address
|
||||
* @param _dsa DSA address
|
||||
*/
|
||||
function removeDsa(address _pool, address _dsa) external isMaster {
|
||||
require(isPool[_pool], "not-pool");
|
||||
require(isDsa[_pool][_dsa], "not-dsa-for-pool");
|
||||
delete isDsa[_pool][_dsa];
|
||||
emit LogRemoveDSA(_pool, _dsa);
|
||||
}
|
||||
|
||||
constructor(address _chief) public {
|
||||
chief[_chief] = true;
|
||||
emit LogAddChief(_chief);
|
||||
|
|
Loading…
Reference in New Issue
Block a user