diff --git a/contracts/pools/erc20.sol b/contracts/pools/erc20.sol index 6b1c0a1..d183790 100644 --- a/contracts/pools/erc20.sol +++ b/contracts/pools/erc20.sol @@ -33,19 +33,20 @@ interface RateInterface { contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { 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 LogSettle(uint settleTime); - event LogDeposit(uint depositAmt, uint poolMintAmt); - event LogWithdraw(uint withdrawAmt, uint poolBurnAmt, uint feeAmt); + event LogSettle(uint settleBlock); + event LogDeposit(address indexed user, uint depositAmt, uint poolMintAmt); + event LogWithdraw(address indexed user, uint withdrawAmt, uint poolBurnAmt, uint feeAmt); event LogAddInsurance(uint amount); + event LogWithdrawInsurance(uint amount); event LogPausePool(bool); IERC20 public immutable baseToken; // Base token. Eg:- DAI, USDC, etc. RegistryInterface public immutable registry; // Pool Registry 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 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 { require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa"); 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); - } else if (token == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE){ + } else if (token == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE){ // non-pool ethereum payable(_dsa).transfer(amount); - } else { + } else { // non-pool other tokens 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 { uint _previousRate = exchangeRate; uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken(); _totalToken = sub(_totalToken, insuranceAmt); - uint _currentRate = wdiv(totalSupply(), _totalToken); - require(_currentRate != 0, "currentRate-is-0"); - if (_currentRate > _previousRate) { - uint difTkn = sub(tokenBalance, _totalToken); - if (difTkn < insuranceAmt) { - insuranceAmt = sub(insuranceAmt, difTkn); + uint _currentRate = getCurrentRate(_totalToken); + require(_currentRate != 0, "current-rate-is-zero"); + if (_currentRate > _previousRate) { // loss => deduct partially/fully from insurance amount + uint _loss = sub(tokenBalance, _totalToken); + if (_loss <= insuranceAmt) { + insuranceAmt = sub(insuranceAmt, _loss); _currentRate = _previousRate; } else { 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))); insuranceAmt = add(insuranceAmt, insureFeeAmt); tokenBalance = sub(_totalToken, insureFeeAmt); - _currentRate = wdiv(totalSupply(), tokenBalance); + _currentRate = getCurrentRate(tokenBalance); } exchangeRate = _currentRate; 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 { require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa"); AccountInterface dsaWallet = AccountInterface(_dsa); @@ -110,35 +136,48 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { dsaWallet.cast(_targets, _datas, _origin); } require(dsaWallet.isAuth(address(this)), "token-pool-not-auth"); - 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); - uint _mintAmt = wmul(tknAmt, exchangeRate); + _mintAmt = wmul(tknAmt, exchangeRate); _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) { 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 _tknBal = wdiv(_bal, exchangeRate); uint _burnAmt; - if (tknAmt == uint(-1)) { + if (tknAmt >= _tknBal) { _burnAmt = _bal; _tknAmt = _tknBal; } else { - require(tknAmt <= _tknBal, "balance-exceeded"); _burnAmt = wmul(tknAmt, exchangeRate); _tknAmt = tknAmt; } + require(tknAmt <= poolBal, "not-enough-liquidity-available"); + + tokenBalance = sub(tokenBalance, _tknAmt); _burn(msg.sender, _burnAmt); @@ -152,28 +191,36 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { 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 { baseToken.safeTransferFrom(msg.sender, address(this), tknAmt); insuranceAmt = add(insuranceAmt, 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 { require(msg.sender == instaIndex.master(), "not-master"); - require(tknAmt <= insuranceAmt || tknAmt == uint(-1), "not-enough-insurance"); - if (tknAmt == uint(-1)) { - baseToken.safeTransfer(msg.sender, insuranceAmt); - insuranceAmt = 0; - } else { - baseToken.safeTransfer(msg.sender, tknAmt); - insuranceAmt = sub(insuranceAmt, tknAmt); - } - emit LogAddInsurance(tknAmt); + require(tknAmt <= insuranceAmt, "not-enough-insurance"); + baseToken.safeTransfer(msg.sender, tknAmt); + insuranceAmt = sub(insuranceAmt, tknAmt); + emit LogWithdrawInsurance(tknAmt); } + /** + * @dev Shut the pool. + * @notice only master can call this function. + */ function shutdown() external { require(msg.sender == instaIndex.master(), "not-master"); paused() ? _unpause() : _pause(); diff --git a/contracts/pools/eth.sol b/contracts/pools/eth.sol index 89a07e1..4475331 100644 --- a/contracts/pools/eth.sol +++ b/contracts/pools/eth.sol @@ -33,19 +33,20 @@ interface RateInterface { contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath { 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 LogSettle(uint settleTime); - event LogDeposit(uint depositAmt, uint poolMintAmt); - event LogWithdraw(uint withdrawAmt, uint poolBurnAmt, uint feeAmt); + event LogSettle(uint settleBlock); + event LogDeposit(address indexed user, uint depositAmt, uint poolMintAmt); + event LogWithdraw(address indexed user, uint withdrawAmt, uint poolBurnAmt, uint feeAmt); event LogAddInsurance(uint amount); + event LogWithdrawInsurance(uint amount); event LogPausePool(bool); RegistryInterface public immutable registry; // Pool Registry IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); 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 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 { require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa"); - require(AccountInterface(_dsa).isAuth(address(this)), "token-pool-not-auth"); - if (token == address(0)) { + require(AccountInterface(_dsa).isAuth(address(this)), "token-pool-not-auth"); + if (token == address(0)) { // pool base ETH payable(_dsa).transfer(amount); - } else { + } else { // non-pool other tokens 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 { uint _previousRate = exchangeRate; uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken(); _totalToken = sub(_totalToken, insuranceAmt); - uint _currentRate = wdiv(totalSupply(), _totalToken); - require(_currentRate != 0, "currentRate-is-0"); - if (_currentRate > _previousRate) { - uint difTkn = sub(tokenBalance, _totalToken); - if (difTkn < insuranceAmt) { - insuranceAmt = sub(insuranceAmt, difTkn); + uint _currentRate = getCurrentRate(_totalToken); + require(_currentRate != 0, "current-rate-is-zero"); + if (_currentRate > _previousRate) { // loss => deduct partially/fully from insurance amount + uint _loss = sub(tokenBalance, _totalToken); + if (_loss <= insuranceAmt) { + insuranceAmt = sub(insuranceAmt, _loss); _currentRate = _previousRate; } else { 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))); insuranceAmt = add(insuranceAmt, insureFeeAmt); tokenBalance = sub(_totalToken, insureFeeAmt); - _currentRate = wdiv(totalSupply(), tokenBalance); + _currentRate = getCurrentRate(tokenBalance); } exchangeRate = _currentRate; 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 { require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa"); AccountInterface dsaWallet = AccountInterface(_dsa); if (_targets.length > 0 && _datas.length > 0) { dsaWallet.cast(_targets, _datas, _origin); } - require(dsaWallet.isAuth(address(this)), "token-pool-not-auth"); + require(dsaWallet.isAuth(address(this)), "token-pool-not-auth"); setExchangeRate(); - - emit LogSettle(block.timestamp); + emit LogSettle(block.number); } - 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"); - 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); - 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) { 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 _tknBal = wdiv(_bal, exchangeRate); uint _burnAmt; - if (tknAmt == uint(-1)) { + if (tknAmt >= _tknBal) { _burnAmt = _bal; _tknAmt = _tknBal; } else { - require(tknAmt <= _tknBal, "balance-exceeded"); _burnAmt = wmul(tknAmt, exchangeRate); _tknAmt = tknAmt; } + require(tknAmt <= poolBal, "not-enough-liquidity-available"); + + tokenBalance = sub(tokenBalance, _tknAmt); _burn(msg.sender, _burnAmt); @@ -149,33 +187,40 @@ contract PoolETH is ReentrancyGuard, ERC20Pausable, DSMath { 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 { require(tknAmt == msg.value, "unmatched-amount"); - insuranceAmt += tknAmt; + insuranceAmt = add(insuranceAmt, 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 { require(msg.sender == instaIndex.master(), "not-master"); - require(tknAmt <= insuranceAmt || tknAmt == uint(-1), "not-enough-insurance"); - if (tknAmt == uint(-1)) { - msg.sender.transfer(insuranceAmt); - insuranceAmt = 0; - } else { - msg.sender.transfer(tknAmt); - insuranceAmt = sub(insuranceAmt, tknAmt); - } - emit LogAddInsurance(tknAmt); + require(tknAmt <= insuranceAmt, "not-enough-insurance"); + msg.sender.transfer(tknAmt); + insuranceAmt = sub(insuranceAmt, tknAmt); + emit LogWithdrawInsurance(tknAmt); } + /** + * @dev Shut the pool. + * @notice only master can call this function. + */ function shutdown() external { require(msg.sender == instaIndex.master(), "not-master"); paused() ? _unpause() : _pause(); } receive() external payable {} - } diff --git a/contracts/registry.sol b/contracts/registry.sol index 94fb1b3..eead9fc 100644 --- a/contracts/registry.sol +++ b/contracts/registry.sol @@ -127,6 +127,11 @@ contract Registry { 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 { require(isPool[_pool], "not-pool"); require(_newLogic != address(0), "invalid-address"); @@ -135,6 +140,11 @@ contract Registry { 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 { require(isPool[_pool], "not-pool"); require(_newFee < 10 ** 18, "insure-fee-limit-reached"); @@ -143,6 +153,11 @@ contract Registry { 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 { require(isPool[_pool], "not-pool"); require(_newFee < 1 * 10 ** 16, "withdrawal-fee-limit-reached"); @@ -151,6 +166,11 @@ contract Registry { 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 { require(isPool[_pool], "not-pool"); if (_dsa == address(0)) _dsa = instaIndex.build(_pool, 1, address(this)); @@ -159,6 +179,11 @@ contract Registry { 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");