From e79e2cd970a9f85020d2418ab228d7377abfaf01 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Sun, 30 Aug 2020 22:50:37 +1000 Subject: [PATCH 1/6] added withdrawal fee --- contracts/pools/erc20.sol | 15 ++++++++++++--- contracts/pools/eth.sol | 13 +++++++++++-- contracts/registry.sol | 10 ++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/contracts/pools/erc20.sol b/contracts/pools/erc20.sol index bcd2efa..dc17fc0 100644 --- a/contracts/pools/erc20.sol +++ b/contracts/pools/erc20.sol @@ -24,6 +24,7 @@ interface RegistryInterface { function poolLogic(address) external returns (address); function poolCap(address) external view returns (uint); function insureFee(address) external view returns (uint); + function withdrawalFee(address) external view returns (uint); function isDsa(address, address) external view returns (bool); } @@ -38,7 +39,7 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { 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 LogWithdraw(uint withdrawAmt, uint poolBurnAmt, uint feeAmt); event LogAddInsurance(uint amount); event LogPausePool(bool); @@ -144,14 +145,22 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { _burn(msg.sender, _burnAmt); + uint _withdrawalFee = registry.withdrawalFee(address(this)); + uint _feeAmt; + if (_withdrawalFee > 0) { + _feeAmt = wmul(_tknAmt, _withdrawalFee); + insuranceAmt = add(insuranceAmt, _feeAmt); + _tknAmt = sub(_tknAmt, _feeAmt); + } + baseToken.safeTransfer(to, _tknAmt); - emit LogWithdraw(tknAmt, _burnAmt); + emit LogWithdraw(tknAmt, _burnAmt, _feeAmt); } function addInsurance(uint tknAmt) external payable { baseToken.safeTransferFrom(msg.sender, address(this), tknAmt); - insuranceAmt += tknAmt; + insuranceAmt = add(insuranceAmt, tknAmt); emit LogAddInsurance(tknAmt); } diff --git a/contracts/pools/eth.sol b/contracts/pools/eth.sol index d14bdf5..b4716a0 100644 --- a/contracts/pools/eth.sol +++ b/contracts/pools/eth.sol @@ -23,6 +23,7 @@ interface RegistryInterface { function poolLogic(address) external returns (address); function poolCap(address) external view returns (uint); function insureFee(address) external view returns (uint); + function withdrawalFee(address) external view returns (uint); function isDsa(address, address) external view returns (bool); } @@ -37,7 +38,7 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { 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 LogWithdraw(uint withdrawAmt, uint poolBurnAmt, uint feeAmt); event LogAddInsurance(uint amount); event LogPausePool(bool); @@ -137,9 +138,17 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { _burn(msg.sender, _burnAmt); + uint _withdrawalFee = registry.withdrawalFee(address(this)); + uint _feeAmt; + if (_withdrawalFee > 0) { + _feeAmt = wmul(_tknAmt, _withdrawalFee); + insuranceAmt = add(insuranceAmt, _feeAmt); + _tknAmt = sub(_tknAmt, _feeAmt); + } + payable(to).transfer(_tknAmt); - emit LogWithdraw(tknAmt, _burnAmt); + emit LogWithdraw(tknAmt, _burnAmt, _feeAmt); } function addInsurance(uint tknAmt) external payable { diff --git a/contracts/registry.sol b/contracts/registry.sol index 25d90d5..a823e5b 100644 --- a/contracts/registry.sol +++ b/contracts/registry.sol @@ -18,6 +18,7 @@ contract Registry { event LogUpdatePoolCap(address pool, uint newCap); event LogUpdatePoolLogic(address pool, address newLogic); event LogUpdateInsureFee(address pool, uint newFee); + event LogUpdateWithdrawalFee(address pool, 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); @@ -32,6 +33,7 @@ contract Registry { mapping (address => address) public poolLogic; mapping (address => uint) public poolCap; mapping (address => uint) public insureFee; + mapping (address => uint) public withdrawalFee; mapping (address => mapping(address => bool)) public isDsa; // Pool => DSA address => true/false mapping (address => address[]) public dsaArr; // Pool => all dsa in array @@ -145,6 +147,14 @@ contract Registry { emit LogUpdateInsureFee(_pool, _newFee); } + function updateWithdrawalFee(address _pool, uint _newFee) external isMaster { + require(isPool[_pool], "not-pool"); + require(_newFee < 5 ** 16, "insure-fee-limit-reached"); + require(withdrawalFee[_pool] != _newFee, "same-pool-fee"); + withdrawalFee[_pool] = _newFee; + emit LogUpdateWithdrawalFee(_pool, _newFee); + } + function addDsa(address _pool, address _dsa) external isMaster { require(isPool[_pool], "not-pool"); if (_dsa == address(0)) { From a426722e5723ab514de20da8894947bf75402055 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sun, 30 Aug 2020 18:29:41 +0530 Subject: [PATCH 2/6] Update registry.sol --- contracts/registry.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/registry.sol b/contracts/registry.sol index a823e5b..0bcb3d2 100644 --- a/contracts/registry.sol +++ b/contracts/registry.sol @@ -149,7 +149,7 @@ contract Registry { function updateWithdrawalFee(address _pool, uint _newFee) external isMaster { require(isPool[_pool], "not-pool"); - require(_newFee < 5 ** 16, "insure-fee-limit-reached"); + require(_newFee < 5 * 10 ** 16, "insure-fee-limit-reached"); // max 5% require(withdrawalFee[_pool] != _newFee, "same-pool-fee"); withdrawalFee[_pool] = _newFee; emit LogUpdateWithdrawalFee(_pool, _newFee); From 1b6e03ebd227d5c1ba75db548f2540f145cf0d33 Mon Sep 17 00:00:00 2001 From: Sowmay Jain Date: Sun, 30 Aug 2020 23:27:03 +1000 Subject: [PATCH 3/6] updated comments and removed pool cap --- contracts/pools/erc20.sol | 2 -- contracts/pools/eth.sol | 2 -- contracts/registry.sol | 23 ++++++++++------------- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/contracts/pools/erc20.sol b/contracts/pools/erc20.sol index 54f5693..f99070d 100644 --- a/contracts/pools/erc20.sol +++ b/contracts/pools/erc20.sol @@ -22,7 +22,6 @@ interface IndexInterface { interface RegistryInterface { function chief(address) external view returns (bool); function poolLogic(address) external returns (address); - function poolCap(address) external view returns (uint); function insureFee(address) external view returns (uint); function withdrawalFee(address) external view returns (uint); function isDsa(address, address) external view returns (bool); @@ -119,7 +118,6 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { function deposit(uint tknAmt) external whenNotPaused payable returns(uint) { uint _newTokenBal = add(tokenBalance, tknAmt); - require(_newTokenBal <= registry.poolCap(address(this)), "deposit-cap-reached"); baseToken.safeTransferFrom(msg.sender, address(this), tknAmt); uint _mintAmt = wmul(tknAmt, exchangeRate); diff --git a/contracts/pools/eth.sol b/contracts/pools/eth.sol index f5b54db..3391d6d 100644 --- a/contracts/pools/eth.sol +++ b/contracts/pools/eth.sol @@ -21,7 +21,6 @@ interface IndexInterface { interface RegistryInterface { function chief(address) external view returns (bool); function poolLogic(address) external returns (address); - function poolCap(address) external view returns (uint); function insureFee(address) external view returns (uint); function withdrawalFee(address) external view returns (uint); function isDsa(address, address) external view returns (bool); @@ -113,7 +112,6 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { function deposit(uint tknAmt) public whenNotPaused payable returns(uint) { require(tknAmt == msg.value, "unmatched-amount"); uint _newTokenBal = add(tokenBalance, msg.value); - require(_newTokenBal <= registry.poolCap(address(this)), "deposit-cap-reached"); uint _mintAmt = wmul(msg.value, exchangeRate); _mint(msg.sender, _mintAmt); diff --git a/contracts/registry.sol b/contracts/registry.sol index 0bcb3d2..ec982e1 100644 --- a/contracts/registry.sol +++ b/contracts/registry.sol @@ -14,7 +14,7 @@ contract Registry { event LogRemoveChief(address indexed chief); event LogAddSigner(address indexed signer); event LogRemoveSigner(address indexed signer); - event LogSwitchPool(address pool, bool poolState); + event LogUpdatePool(address pool, bool poolState); event LogUpdatePoolCap(address pool, uint newCap); event LogUpdatePoolLogic(address pool, address newLogic); event LogUpdateInsureFee(address pool, uint newFee); @@ -34,8 +34,8 @@ contract Registry { mapping (address => uint) public poolCap; mapping (address => uint) public insureFee; mapping (address => uint) public withdrawalFee; - mapping (address => mapping(address => bool)) public isDsa; // Pool => DSA address => true/false - mapping (address => address[]) public dsaArr; // Pool => all dsa in array + 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"); @@ -119,16 +119,13 @@ contract Registry { emit LogRemovePool(token, poolAddr); } - function switchPool(address _pool) external isMaster { + /** + * @dev enable / disable pool + * @param _pool pool address + */ + function updatePool(address _pool) external isMaster { isPool[_pool] = !isPool[_pool]; - emit LogSwitchPool(_pool, isPool[_pool]); - } - - function updatePoolCap(address _pool, uint _newCap) external isMaster { - require(isPool[_pool], "not-pool"); - require(poolCap[_pool] != _newCap, "same-pool-cap"); - poolCap[_pool] = _newCap; - emit LogUpdatePoolCap(_pool, _newCap); + emit LogUpdatePool(_pool, isPool[_pool]); } function updatePoolLogic(address _pool, address _newLogic) external isMaster { @@ -149,7 +146,7 @@ contract Registry { function updateWithdrawalFee(address _pool, uint _newFee) external isMaster { require(isPool[_pool], "not-pool"); - require(_newFee < 5 * 10 ** 16, "insure-fee-limit-reached"); // max 5% + require(_newFee < 5 * 10 ** 16, "insure-fee-limit-reached"); require(withdrawalFee[_pool] != _newFee, "same-pool-fee"); withdrawalFee[_pool] = _newFee; emit LogUpdateWithdrawalFee(_pool, _newFee); From 5335fb7189a4110afbf9bc40cef306d668b800b1 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Sun, 30 Aug 2020 19:03:06 +0530 Subject: [PATCH 4/6] added auth condition in eth pool also --- contracts/pools/eth.sol | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/contracts/pools/eth.sol b/contracts/pools/eth.sol index 3391d6d..35fbd53 100644 --- a/contracts/pools/eth.sol +++ b/contracts/pools/eth.sol @@ -9,7 +9,7 @@ import "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; import { DSMath } from "../libs/safeMath.sol"; interface AccountInterface { - function enable(address authority) external; + function isAuth(address) external view returns(bool); function cast(address[] calldata _targets, bytes[] calldata _datas, address _origin) external payable; } @@ -66,6 +66,7 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { 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)) { payable(_dsa).transfer(amount); } else { @@ -101,9 +102,11 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { 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) { - AccountInterface(_dsa).cast(_targets, _datas, _origin); + dsaWallet.cast(_targets, _datas, _origin); } + require(dsaWallet.isAuth(address(this)), "token-pool-not-auth"); setExchangeRate(); emit LogSettle(block.timestamp); From 93e809d852eddfbe96007f4f5b2834377f3e1d3d Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Sun, 30 Aug 2020 19:03:19 +0530 Subject: [PATCH 5/6] Minor edits --- contracts/pools/erc20.sol | 1 - contracts/registry.sol | 1 - 2 files changed, 2 deletions(-) diff --git a/contracts/pools/erc20.sol b/contracts/pools/erc20.sol index f99070d..6b1c0a1 100644 --- a/contracts/pools/erc20.sol +++ b/contracts/pools/erc20.sol @@ -9,7 +9,6 @@ import "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; import { DSMath } from "../libs/safeMath.sol"; interface AccountInterface { - function enable(address authority) external; function isAuth(address) external view returns(bool); function cast(address[] calldata _targets, bytes[] calldata _datas, address _origin) external payable; } diff --git a/contracts/registry.sol b/contracts/registry.sol index ec982e1..ea4f9d0 100644 --- a/contracts/registry.sol +++ b/contracts/registry.sol @@ -15,7 +15,6 @@ contract Registry { event LogAddSigner(address indexed signer); event LogRemoveSigner(address indexed signer); event LogUpdatePool(address pool, bool poolState); - event LogUpdatePoolCap(address pool, uint newCap); event LogUpdatePoolLogic(address pool, address newLogic); event LogUpdateInsureFee(address pool, uint newFee); event LogUpdateWithdrawalFee(address pool, uint newFee); From 18d9f405e31bd685716a54fbdb8981fdfc4db388 Mon Sep 17 00:00:00 2001 From: Sowmay Jain Date: Sun, 30 Aug 2020 23:34:01 +1000 Subject: [PATCH 6/6] edits --- contracts/registry.sol | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/contracts/registry.sol b/contracts/registry.sol index ec982e1..53c8c8e 100644 --- a/contracts/registry.sol +++ b/contracts/registry.sol @@ -146,7 +146,7 @@ contract Registry { function updateWithdrawalFee(address _pool, uint _newFee) external isMaster { require(isPool[_pool], "not-pool"); - require(_newFee < 5 * 10 ** 16, "insure-fee-limit-reached"); + require(_newFee < 1 * 10 ** 16, "withdrawal-fee-limit-reached"); require(withdrawalFee[_pool] != _newFee, "same-pool-fee"); withdrawalFee[_pool] = _newFee; emit LogUpdateWithdrawalFee(_pool, _newFee); @@ -154,11 +154,8 @@ contract Registry { function addDsa(address _pool, address _dsa) external isMaster { 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)); isDsa[_pool][_dsa] = true; - dsaArr[_pool].push(_dsa); emit LogNewDSA(_pool, _dsa); }