mirror of
https://github.com/Instadapp/yield-contract.git
synced 2024-07-29 21:47:29 +00:00
Merge pull request #8 from InstaDApp/last-edits
Merging last-edits in testcases
This commit is contained in:
commit
a5c45ce761
|
@ -33,19 +33,20 @@ interface RateInterface {
|
||||||
contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable {
|
contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable {
|
||||||
using SafeERC20 for IERC20;
|
using SafeERC20 for IERC20;
|
||||||
|
|
||||||
event LogDeploy(address token, uint amount);
|
event LogDeploy(address indexed dsa, address token, uint amount);
|
||||||
event LogExchangeRate(uint exchangeRate, uint tokenBalance, uint insuranceAmt);
|
event LogExchangeRate(uint exchangeRate, uint tokenBalance, uint insuranceAmt);
|
||||||
event LogSettle(uint settleTime);
|
event LogSettle(uint settleBlock);
|
||||||
event LogDeposit(uint depositAmt, uint poolMintAmt);
|
event LogDeposit(address indexed user, uint depositAmt, uint poolMintAmt);
|
||||||
event LogWithdraw(uint withdrawAmt, uint poolBurnAmt, uint feeAmt);
|
event LogWithdraw(address indexed user, uint withdrawAmt, uint poolBurnAmt, uint feeAmt);
|
||||||
event LogAddInsurance(uint amount);
|
event LogAddInsurance(uint amount);
|
||||||
|
event LogWithdrawInsurance(uint amount);
|
||||||
event LogPausePool(bool);
|
event LogPausePool(bool);
|
||||||
|
|
||||||
IERC20 public immutable baseToken; // Base token. Eg:- DAI, USDC, etc.
|
IERC20 public immutable baseToken; // Base token. Eg:- DAI, USDC, etc.
|
||||||
RegistryInterface public immutable registry; // Pool Registry
|
RegistryInterface public immutable registry; // Pool Registry
|
||||||
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); // Main Index
|
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); // Main Index
|
||||||
|
|
||||||
uint private tokenBalance; // total token balance since last rebalancing
|
uint private tokenBalance; // total token balance
|
||||||
uint public exchangeRate; // initial 1 token = 1
|
uint public exchangeRate; // initial 1 token = 1
|
||||||
uint public insuranceAmt; // insurance amount to keep pool safe
|
uint public insuranceAmt; // insurance amount to keep pool safe
|
||||||
|
|
||||||
|
@ -65,44 +66,69 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable {
|
||||||
_;
|
_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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) public isChief {
|
function deploy(address _dsa, address token, uint amount) public isChief {
|
||||||
require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa");
|
require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa");
|
||||||
require(AccountInterface(_dsa).isAuth(address(this)), "token-pool-not-auth");
|
require(AccountInterface(_dsa).isAuth(address(this)), "token-pool-not-auth");
|
||||||
if (token == address(0)) {
|
if (token == address(0)) { // pool base token
|
||||||
baseToken.safeTransfer(_dsa, amount);
|
baseToken.safeTransfer(_dsa, amount);
|
||||||
} else if (token == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE){
|
} else if (token == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE){ // non-pool ethereum
|
||||||
payable(_dsa).transfer(amount);
|
payable(_dsa).transfer(amount);
|
||||||
} else {
|
} else { // non-pool other tokens
|
||||||
IERC20(token).safeTransfer(_dsa, amount);
|
IERC20(token).safeTransfer(_dsa, amount);
|
||||||
}
|
}
|
||||||
emit LogDeploy(token, amount);
|
emit LogDeploy(_dsa, token, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev get pool token rate
|
||||||
|
* @param tokenAmt total token amount
|
||||||
|
*/
|
||||||
|
function getCurrentRate(uint tokenAmt) public view returns (uint) {
|
||||||
|
return wdiv(totalSupply(), tokenAmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev sets exchange rates
|
||||||
|
*/
|
||||||
function setExchangeRate() public isChief {
|
function setExchangeRate() public isChief {
|
||||||
uint _previousRate = exchangeRate;
|
uint _previousRate = exchangeRate;
|
||||||
uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken();
|
uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken();
|
||||||
_totalToken = sub(_totalToken, insuranceAmt);
|
_totalToken = sub(_totalToken, insuranceAmt);
|
||||||
uint _currentRate = wdiv(totalSupply(), _totalToken);
|
uint _currentRate = getCurrentRate(_totalToken);
|
||||||
require(_currentRate != 0, "currentRate-is-0");
|
require(_currentRate != 0, "current-rate-is-zero");
|
||||||
if (_currentRate > _previousRate) {
|
if (_currentRate > _previousRate) { // loss => deduct partially/fully from insurance amount
|
||||||
uint difTkn = sub(tokenBalance, _totalToken);
|
uint _loss = sub(tokenBalance, _totalToken);
|
||||||
if (difTkn < insuranceAmt) {
|
if (_loss <= insuranceAmt) {
|
||||||
insuranceAmt = sub(insuranceAmt, difTkn);
|
insuranceAmt = sub(insuranceAmt, _loss);
|
||||||
_currentRate = _previousRate;
|
_currentRate = _previousRate;
|
||||||
} else {
|
} else {
|
||||||
tokenBalance = add(_totalToken, insuranceAmt);
|
tokenBalance = add(_totalToken, insuranceAmt);
|
||||||
_currentRate = wdiv(totalSupply(), tokenBalance);
|
insuranceAmt = 0;
|
||||||
|
_currentRate = getCurrentRate(tokenBalance);
|
||||||
}
|
}
|
||||||
} else {
|
} else { // profit => add to insurance amount
|
||||||
uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), registry.insureFee(address(this)));
|
uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), registry.insureFee(address(this)));
|
||||||
insuranceAmt = add(insuranceAmt, insureFeeAmt);
|
insuranceAmt = add(insuranceAmt, insureFeeAmt);
|
||||||
tokenBalance = sub(_totalToken, insureFeeAmt);
|
tokenBalance = sub(_totalToken, insureFeeAmt);
|
||||||
_currentRate = wdiv(totalSupply(), tokenBalance);
|
_currentRate = getCurrentRate(tokenBalance);
|
||||||
}
|
}
|
||||||
exchangeRate = _currentRate;
|
exchangeRate = _currentRate;
|
||||||
emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt);
|
emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Settle the assets on dsa and update exchange rate
|
||||||
|
* @param _dsa DSA address
|
||||||
|
* @param _targets array of connector's address
|
||||||
|
* @param _datas array of connector's function calldata
|
||||||
|
* @param _origin origin address
|
||||||
|
*/
|
||||||
function settle(address _dsa, address[] calldata _targets, bytes[] calldata _datas, address _origin) external isChief {
|
function settle(address _dsa, address[] calldata _targets, bytes[] calldata _datas, address _origin) external isChief {
|
||||||
require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa");
|
require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa");
|
||||||
AccountInterface dsaWallet = AccountInterface(_dsa);
|
AccountInterface dsaWallet = AccountInterface(_dsa);
|
||||||
|
@ -110,35 +136,48 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable {
|
||||||
dsaWallet.cast(_targets, _datas, _origin);
|
dsaWallet.cast(_targets, _datas, _origin);
|
||||||
}
|
}
|
||||||
require(dsaWallet.isAuth(address(this)), "token-pool-not-auth");
|
require(dsaWallet.isAuth(address(this)), "token-pool-not-auth");
|
||||||
|
|
||||||
setExchangeRate();
|
setExchangeRate();
|
||||||
emit LogSettle(block.timestamp);
|
emit LogSettle(block.number);
|
||||||
}
|
}
|
||||||
|
|
||||||
function deposit(uint tknAmt) external whenNotPaused payable returns(uint) {
|
/**
|
||||||
uint _newTokenBal = add(tokenBalance, tknAmt);
|
* @dev Deposit token.
|
||||||
|
* @param tknAmt token amount
|
||||||
|
* @return _mintAmt amount of wrap token minted
|
||||||
|
*/
|
||||||
|
function deposit(uint tknAmt) external whenNotPaused payable returns (uint _mintAmt) {
|
||||||
|
require(msg.value == 0, "non-eth-pool");
|
||||||
|
tokenBalance = add(tokenBalance, tknAmt);
|
||||||
|
|
||||||
baseToken.safeTransferFrom(msg.sender, address(this), tknAmt);
|
baseToken.safeTransferFrom(msg.sender, address(this), tknAmt);
|
||||||
uint _mintAmt = wmul(tknAmt, exchangeRate);
|
_mintAmt = wmul(tknAmt, exchangeRate);
|
||||||
_mint(msg.sender, _mintAmt);
|
_mint(msg.sender, _mintAmt);
|
||||||
|
|
||||||
emit LogDeposit(tknAmt, _mintAmt);
|
emit LogDeposit(msg.sender, tknAmt, _mintAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Withdraw tokens.
|
||||||
|
* @param tknAmt token amount
|
||||||
|
* @param to withdraw tokens to address
|
||||||
|
* @return _tknAmt amount of token withdrawn
|
||||||
|
*/
|
||||||
function withdraw(uint tknAmt, address to) external nonReentrant whenNotPaused returns (uint _tknAmt) {
|
function withdraw(uint tknAmt, address to) external nonReentrant whenNotPaused returns (uint _tknAmt) {
|
||||||
uint poolBal = baseToken.balanceOf(address(this));
|
uint poolBal = baseToken.balanceOf(address(this));
|
||||||
require(tknAmt <= poolBal, "not-enough-liquidity-available");
|
require(to != address(0), "to-address-not-vaild");
|
||||||
uint _bal = balanceOf(msg.sender);
|
uint _bal = balanceOf(msg.sender);
|
||||||
uint _tknBal = wdiv(_bal, exchangeRate);
|
uint _tknBal = wdiv(_bal, exchangeRate);
|
||||||
uint _burnAmt;
|
uint _burnAmt;
|
||||||
if (tknAmt == uint(-1)) {
|
if (tknAmt >= _tknBal) {
|
||||||
_burnAmt = _bal;
|
_burnAmt = _bal;
|
||||||
_tknAmt = _tknBal;
|
_tknAmt = _tknBal;
|
||||||
} else {
|
} else {
|
||||||
require(tknAmt <= _tknBal, "balance-exceeded");
|
|
||||||
_burnAmt = wmul(tknAmt, exchangeRate);
|
_burnAmt = wmul(tknAmt, exchangeRate);
|
||||||
_tknAmt = tknAmt;
|
_tknAmt = tknAmt;
|
||||||
}
|
}
|
||||||
|
require(tknAmt <= poolBal, "not-enough-liquidity-available");
|
||||||
|
|
||||||
|
tokenBalance = sub(tokenBalance, _tknAmt);
|
||||||
|
|
||||||
_burn(msg.sender, _burnAmt);
|
_burn(msg.sender, _burnAmt);
|
||||||
|
|
||||||
|
@ -152,28 +191,36 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable {
|
||||||
|
|
||||||
baseToken.safeTransfer(to, _tknAmt);
|
baseToken.safeTransfer(to, _tknAmt);
|
||||||
|
|
||||||
emit LogWithdraw(tknAmt, _burnAmt, _feeAmt);
|
emit LogWithdraw(msg.sender, tknAmt, _burnAmt, _feeAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Add Insurance to the pool.
|
||||||
|
* @param tknAmt insurance token amount to add
|
||||||
|
*/
|
||||||
function addInsurance(uint tknAmt) external {
|
function addInsurance(uint tknAmt) external {
|
||||||
baseToken.safeTransferFrom(msg.sender, address(this), tknAmt);
|
baseToken.safeTransferFrom(msg.sender, address(this), tknAmt);
|
||||||
insuranceAmt = add(insuranceAmt, tknAmt);
|
insuranceAmt = add(insuranceAmt, tknAmt);
|
||||||
emit LogAddInsurance(tknAmt);
|
emit LogAddInsurance(tknAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Withdraw Insurance from the pool.
|
||||||
|
* @notice only master can call this function.
|
||||||
|
* @param tknAmt insurance token amount to remove
|
||||||
|
*/
|
||||||
function withdrawInsurance(uint tknAmt) external {
|
function withdrawInsurance(uint tknAmt) external {
|
||||||
require(msg.sender == instaIndex.master(), "not-master");
|
require(msg.sender == instaIndex.master(), "not-master");
|
||||||
require(tknAmt <= insuranceAmt || tknAmt == uint(-1), "not-enough-insurance");
|
require(tknAmt <= insuranceAmt, "not-enough-insurance");
|
||||||
if (tknAmt == uint(-1)) {
|
baseToken.safeTransfer(msg.sender, tknAmt);
|
||||||
baseToken.safeTransfer(msg.sender, insuranceAmt);
|
insuranceAmt = sub(insuranceAmt, tknAmt);
|
||||||
insuranceAmt = 0;
|
emit LogWithdrawInsurance(tknAmt);
|
||||||
} else {
|
|
||||||
baseToken.safeTransfer(msg.sender, tknAmt);
|
|
||||||
insuranceAmt = sub(insuranceAmt, tknAmt);
|
|
||||||
}
|
|
||||||
emit LogAddInsurance(tknAmt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Shut the pool.
|
||||||
|
* @notice only master can call this function.
|
||||||
|
*/
|
||||||
function shutdown() external {
|
function shutdown() external {
|
||||||
require(msg.sender == instaIndex.master(), "not-master");
|
require(msg.sender == instaIndex.master(), "not-master");
|
||||||
paused() ? _unpause() : _pause();
|
paused() ? _unpause() : _pause();
|
||||||
|
|
|
@ -33,19 +33,20 @@ interface RateInterface {
|
||||||
contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath {
|
contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath {
|
||||||
using SafeERC20 for IERC20;
|
using SafeERC20 for IERC20;
|
||||||
|
|
||||||
event LogDeploy(address indexed token, uint amount);
|
event LogDeploy(address indexed dsa, address indexed token, uint amount);
|
||||||
event LogExchangeRate(uint exchangeRate, uint tokenBalance, uint insuranceAmt);
|
event LogExchangeRate(uint exchangeRate, uint tokenBalance, uint insuranceAmt);
|
||||||
event LogSettle(uint settleTime);
|
event LogSettle(uint settleBlock);
|
||||||
event LogDeposit(uint depositAmt, uint poolMintAmt);
|
event LogDeposit(address indexed user, uint depositAmt, uint poolMintAmt);
|
||||||
event LogWithdraw(uint withdrawAmt, uint poolBurnAmt, uint feeAmt);
|
event LogWithdraw(address indexed user, uint withdrawAmt, uint poolBurnAmt, uint feeAmt);
|
||||||
event LogAddInsurance(uint amount);
|
event LogAddInsurance(uint amount);
|
||||||
|
event LogWithdrawInsurance(uint amount);
|
||||||
event LogPausePool(bool);
|
event LogPausePool(bool);
|
||||||
|
|
||||||
RegistryInterface public immutable registry; // Pool Registry
|
RegistryInterface public immutable registry; // Pool Registry
|
||||||
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723);
|
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723);
|
||||||
|
|
||||||
IERC20 public immutable baseToken; // Base token.
|
IERC20 public immutable baseToken; // Base token.
|
||||||
uint private tokenBalance; // total token balance since last rebalancing
|
uint private tokenBalance; // total token balance
|
||||||
uint public exchangeRate = 10 ** 18; // initial 1 token = 1
|
uint public exchangeRate = 10 ** 18; // initial 1 token = 1
|
||||||
uint public insuranceAmt; // insurance amount to keep pool safe
|
uint public insuranceAmt; // insurance amount to keep pool safe
|
||||||
|
|
||||||
|
@ -64,78 +65,115 @@ 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 {
|
function deploy(address _dsa, address token, uint amount) external isChief {
|
||||||
require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa");
|
require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa");
|
||||||
require(AccountInterface(_dsa).isAuth(address(this)), "token-pool-not-auth");
|
require(AccountInterface(_dsa).isAuth(address(this)), "token-pool-not-auth");
|
||||||
if (token == address(0)) {
|
if (token == address(0)) { // pool base ETH
|
||||||
payable(_dsa).transfer(amount);
|
payable(_dsa).transfer(amount);
|
||||||
} else {
|
} else { // non-pool other tokens
|
||||||
IERC20(token).safeTransfer(_dsa, amount);
|
IERC20(token).safeTransfer(_dsa, amount);
|
||||||
}
|
}
|
||||||
emit LogDeploy(token, amount);
|
emit LogDeploy(_dsa, token, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev get pool token rate
|
||||||
|
* @param tokenAmt total token amount
|
||||||
|
*/
|
||||||
|
function getCurrentRate(uint tokenAmt) public view returns (uint) {
|
||||||
|
return wdiv(totalSupply(), tokenAmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev sets exchange rates
|
||||||
|
*/
|
||||||
function setExchangeRate() public isChief {
|
function setExchangeRate() public isChief {
|
||||||
uint _previousRate = exchangeRate;
|
uint _previousRate = exchangeRate;
|
||||||
uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken();
|
uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken();
|
||||||
_totalToken = sub(_totalToken, insuranceAmt);
|
_totalToken = sub(_totalToken, insuranceAmt);
|
||||||
uint _currentRate = wdiv(totalSupply(), _totalToken);
|
uint _currentRate = getCurrentRate(_totalToken);
|
||||||
require(_currentRate != 0, "currentRate-is-0");
|
require(_currentRate != 0, "current-rate-is-zero");
|
||||||
if (_currentRate > _previousRate) {
|
if (_currentRate > _previousRate) { // loss => deduct partially/fully from insurance amount
|
||||||
uint difTkn = sub(tokenBalance, _totalToken);
|
uint _loss = sub(tokenBalance, _totalToken);
|
||||||
if (difTkn < insuranceAmt) {
|
if (_loss <= insuranceAmt) {
|
||||||
insuranceAmt = sub(insuranceAmt, difTkn);
|
insuranceAmt = sub(insuranceAmt, _loss);
|
||||||
_currentRate = _previousRate;
|
_currentRate = _previousRate;
|
||||||
} else {
|
} else {
|
||||||
tokenBalance = add(_totalToken, insuranceAmt);
|
tokenBalance = add(_totalToken, insuranceAmt);
|
||||||
_currentRate = wdiv(totalSupply(), tokenBalance);
|
insuranceAmt = 0;
|
||||||
|
_currentRate = getCurrentRate(tokenBalance);
|
||||||
}
|
}
|
||||||
} else {
|
} else { // profit => add to insurance amount
|
||||||
uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), registry.insureFee(address(this)));
|
uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), registry.insureFee(address(this)));
|
||||||
insuranceAmt = add(insuranceAmt, insureFeeAmt);
|
insuranceAmt = add(insuranceAmt, insureFeeAmt);
|
||||||
tokenBalance = sub(_totalToken, insureFeeAmt);
|
tokenBalance = sub(_totalToken, insureFeeAmt);
|
||||||
_currentRate = wdiv(totalSupply(), tokenBalance);
|
_currentRate = getCurrentRate(tokenBalance);
|
||||||
}
|
}
|
||||||
exchangeRate = _currentRate;
|
exchangeRate = _currentRate;
|
||||||
emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt);
|
emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Settle the assets on dsa and update exchange rate
|
||||||
|
* @param _dsa DSA address
|
||||||
|
* @param _targets array of connector's address
|
||||||
|
* @param _datas array of connector's function calldata
|
||||||
|
* @param _origin origin address
|
||||||
|
*/
|
||||||
function settle(address _dsa, address[] calldata _targets, bytes[] calldata _datas, address _origin) external isChief {
|
function settle(address _dsa, address[] calldata _targets, bytes[] calldata _datas, address _origin) external isChief {
|
||||||
require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa");
|
require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa");
|
||||||
AccountInterface dsaWallet = AccountInterface(_dsa);
|
AccountInterface dsaWallet = AccountInterface(_dsa);
|
||||||
if (_targets.length > 0 && _datas.length > 0) {
|
if (_targets.length > 0 && _datas.length > 0) {
|
||||||
dsaWallet.cast(_targets, _datas, _origin);
|
dsaWallet.cast(_targets, _datas, _origin);
|
||||||
}
|
}
|
||||||
require(dsaWallet.isAuth(address(this)), "token-pool-not-auth");
|
require(dsaWallet.isAuth(address(this)), "token-pool-not-auth");
|
||||||
setExchangeRate();
|
setExchangeRate();
|
||||||
|
emit LogSettle(block.number);
|
||||||
emit LogSettle(block.timestamp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function deposit(uint tknAmt) public whenNotPaused payable returns(uint) {
|
/**
|
||||||
|
* @dev Deposit token.
|
||||||
|
* @param tknAmt token amount
|
||||||
|
* @return _mintAmt amount of wrap token minted
|
||||||
|
*/
|
||||||
|
function deposit(uint tknAmt) public whenNotPaused payable returns (uint _mintAmt) {
|
||||||
require(tknAmt == msg.value, "unmatched-amount");
|
require(tknAmt == msg.value, "unmatched-amount");
|
||||||
uint _newTokenBal = add(tokenBalance, msg.value);
|
tokenBalance = add(tokenBalance, tknAmt);
|
||||||
|
|
||||||
uint _mintAmt = wmul(msg.value, exchangeRate);
|
_mintAmt = wmul(msg.value, exchangeRate);
|
||||||
_mint(msg.sender, _mintAmt);
|
_mint(msg.sender, _mintAmt);
|
||||||
|
|
||||||
emit LogDeposit(tknAmt, _mintAmt);
|
emit LogDeposit(msg.sender, tknAmt, _mintAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Withdraw tokens.
|
||||||
|
* @param tknAmt token amount
|
||||||
|
* @param to withdraw tokens to address
|
||||||
|
* @return _tknAmt amount of token withdrawn
|
||||||
|
*/
|
||||||
function withdraw(uint tknAmt, address to) external nonReentrant whenNotPaused returns (uint _tknAmt) {
|
function withdraw(uint tknAmt, address to) external nonReentrant whenNotPaused returns (uint _tknAmt) {
|
||||||
uint poolBal = address(this).balance;
|
uint poolBal = address(this).balance;
|
||||||
require(tknAmt <= poolBal, "not-enough-liquidity-available");
|
require(to != address(0), "to-address-not-vaild");
|
||||||
uint _bal = balanceOf(msg.sender);
|
uint _bal = balanceOf(msg.sender);
|
||||||
uint _tknBal = wdiv(_bal, exchangeRate);
|
uint _tknBal = wdiv(_bal, exchangeRate);
|
||||||
uint _burnAmt;
|
uint _burnAmt;
|
||||||
if (tknAmt == uint(-1)) {
|
if (tknAmt >= _tknBal) {
|
||||||
_burnAmt = _bal;
|
_burnAmt = _bal;
|
||||||
_tknAmt = _tknBal;
|
_tknAmt = _tknBal;
|
||||||
} else {
|
} else {
|
||||||
require(tknAmt <= _tknBal, "balance-exceeded");
|
|
||||||
_burnAmt = wmul(tknAmt, exchangeRate);
|
_burnAmt = wmul(tknAmt, exchangeRate);
|
||||||
_tknAmt = tknAmt;
|
_tknAmt = tknAmt;
|
||||||
}
|
}
|
||||||
|
require(tknAmt <= poolBal, "not-enough-liquidity-available");
|
||||||
|
|
||||||
|
tokenBalance = sub(tokenBalance, _tknAmt);
|
||||||
|
|
||||||
_burn(msg.sender, _burnAmt);
|
_burn(msg.sender, _burnAmt);
|
||||||
|
|
||||||
|
@ -149,33 +187,40 @@ contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath {
|
||||||
|
|
||||||
payable(to).transfer(_tknAmt);
|
payable(to).transfer(_tknAmt);
|
||||||
|
|
||||||
emit LogWithdraw(tknAmt, _burnAmt, _feeAmt);
|
emit LogWithdraw(msg.sender, tknAmt, _burnAmt, _feeAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Add Insurance to the pool.
|
||||||
|
* @param tknAmt insurance token amount to add
|
||||||
|
*/
|
||||||
function addInsurance(uint tknAmt) external payable {
|
function addInsurance(uint tknAmt) external payable {
|
||||||
require(tknAmt == msg.value, "unmatched-amount");
|
require(tknAmt == msg.value, "unmatched-amount");
|
||||||
insuranceAmt += tknAmt;
|
insuranceAmt = add(insuranceAmt, tknAmt);
|
||||||
emit LogAddInsurance(tknAmt);
|
emit LogAddInsurance(tknAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Withdraw Insurance from the pool.
|
||||||
|
* @notice only master can call this function.
|
||||||
|
* @param tknAmt insurance token amount to remove
|
||||||
|
*/
|
||||||
function withdrawInsurance(uint tknAmt) external {
|
function withdrawInsurance(uint tknAmt) external {
|
||||||
require(msg.sender == instaIndex.master(), "not-master");
|
require(msg.sender == instaIndex.master(), "not-master");
|
||||||
require(tknAmt <= insuranceAmt || tknAmt == uint(-1), "not-enough-insurance");
|
require(tknAmt <= insuranceAmt, "not-enough-insurance");
|
||||||
if (tknAmt == uint(-1)) {
|
msg.sender.transfer(tknAmt);
|
||||||
msg.sender.transfer(insuranceAmt);
|
insuranceAmt = sub(insuranceAmt, tknAmt);
|
||||||
insuranceAmt = 0;
|
emit LogWithdrawInsurance(tknAmt);
|
||||||
} else {
|
|
||||||
msg.sender.transfer(tknAmt);
|
|
||||||
insuranceAmt = sub(insuranceAmt, tknAmt);
|
|
||||||
}
|
|
||||||
emit LogAddInsurance(tknAmt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Shut the pool.
|
||||||
|
* @notice only master can call this function.
|
||||||
|
*/
|
||||||
function shutdown() external {
|
function shutdown() external {
|
||||||
require(msg.sender == instaIndex.master(), "not-master");
|
require(msg.sender == instaIndex.master(), "not-master");
|
||||||
paused() ? _unpause() : _pause();
|
paused() ? _unpause() : _pause();
|
||||||
}
|
}
|
||||||
|
|
||||||
receive() external payable {}
|
receive() external payable {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,6 +127,11 @@ contract Registry {
|
||||||
emit LogUpdatePool(_pool, isPool[_pool]);
|
emit LogUpdatePool(_pool, isPool[_pool]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev update pool rate logic
|
||||||
|
* @param _pool pool address
|
||||||
|
* @param _newLogic new rate logic address
|
||||||
|
*/
|
||||||
function updatePoolLogic(address _pool, address _newLogic) external isMaster {
|
function updatePoolLogic(address _pool, address _newLogic) external isMaster {
|
||||||
require(isPool[_pool], "not-pool");
|
require(isPool[_pool], "not-pool");
|
||||||
require(_newLogic != address(0), "invalid-address");
|
require(_newLogic != address(0), "invalid-address");
|
||||||
|
@ -135,6 +140,11 @@ contract Registry {
|
||||||
emit LogUpdatePoolLogic(_pool, _newLogic);
|
emit LogUpdatePoolLogic(_pool, _newLogic);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev update pool insure fee
|
||||||
|
* @param _pool pool address
|
||||||
|
* @param _newFee new fee amount
|
||||||
|
*/
|
||||||
function updateInsureFee(address _pool, uint _newFee) external isMaster {
|
function updateInsureFee(address _pool, uint _newFee) external isMaster {
|
||||||
require(isPool[_pool], "not-pool");
|
require(isPool[_pool], "not-pool");
|
||||||
require(_newFee < 10 ** 18, "insure-fee-limit-reached");
|
require(_newFee < 10 ** 18, "insure-fee-limit-reached");
|
||||||
|
@ -143,6 +153,11 @@ contract Registry {
|
||||||
emit LogUpdateInsureFee(_pool, _newFee);
|
emit LogUpdateInsureFee(_pool, _newFee);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev update pool withdrawal fee
|
||||||
|
* @param _pool pool address
|
||||||
|
* @param _newFee new withdrawal fee amount
|
||||||
|
*/
|
||||||
function updateWithdrawalFee(address _pool, uint _newFee) external isMaster {
|
function updateWithdrawalFee(address _pool, uint _newFee) external isMaster {
|
||||||
require(isPool[_pool], "not-pool");
|
require(isPool[_pool], "not-pool");
|
||||||
require(_newFee < 1 * 10 ** 16, "withdrawal-fee-limit-reached");
|
require(_newFee < 1 * 10 ** 16, "withdrawal-fee-limit-reached");
|
||||||
|
@ -151,6 +166,11 @@ contract Registry {
|
||||||
emit LogUpdateWithdrawalFee(_pool, _newFee);
|
emit LogUpdateWithdrawalFee(_pool, _newFee);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev add dsa for a pool
|
||||||
|
* @param _pool pool address
|
||||||
|
* @param _dsa DSA address
|
||||||
|
*/
|
||||||
function addDsa(address _pool, address _dsa) external isMaster {
|
function addDsa(address _pool, address _dsa) external isMaster {
|
||||||
require(isPool[_pool], "not-pool");
|
require(isPool[_pool], "not-pool");
|
||||||
if (_dsa == address(0)) _dsa = instaIndex.build(_pool, 1, address(this));
|
if (_dsa == address(0)) _dsa = instaIndex.build(_pool, 1, address(this));
|
||||||
|
@ -159,6 +179,11 @@ contract Registry {
|
||||||
emit LogNewDSA(_pool, _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 {
|
function removeDsa(address _pool, address _dsa) external isMaster {
|
||||||
require(isPool[_pool], "not-pool");
|
require(isPool[_pool], "not-pool");
|
||||||
require(isDsa[_pool][_dsa], "not-dsa-for-pool");
|
require(isDsa[_pool][_dsa], "not-dsa-for-pool");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user