From de4232c233a9ff083c7ab95d6ff935dbfdb356f3 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Thu, 27 Aug 2020 00:49:48 +0530 Subject: [PATCH 1/8] fixed calulation error --- contracts/tokenPool.sol | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/contracts/tokenPool.sol b/contracts/tokenPool.sol index 5faac78..2424b1c 100644 --- a/contracts/tokenPool.sol +++ b/contracts/tokenPool.sol @@ -80,9 +80,10 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { insuranceAmt = sub(insuranceAmt, difTkn); _currentRate = _previousRate; } else { + uint fee = registry.insureFee(address(this)); uint difRate = _currentRate - _previousRate; - uint insureFee = wmul(difRate, registry.insureFee(address(this))); - uint insureFeeAmt = wmul(_totalToken, insureFee); + uint insureFee = wmul(difRate, fee); + uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), fee); insuranceAmt = add(insuranceAmt, insureFeeAmt); _currentRate = sub(_currentRate, insureFee); tokenBalance = sub(_totalToken, insuranceAmt); From abeeb6386599e98c9d472a824ccec80f2ae18337 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Thu, 27 Aug 2020 12:23:28 +0530 Subject: [PATCH 2/8] Added variable exchange rate --- contracts/tokenPool.sol | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/contracts/tokenPool.sol b/contracts/tokenPool.sol index 2424b1c..be488c1 100644 --- a/contracts/tokenPool.sol +++ b/contracts/tokenPool.sol @@ -31,6 +31,20 @@ interface RateInterface { } contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { + function convert36To18(uint _dec, uint256 _amt) public pure returns (uint256 amt) { + amt = (_amt / 10 ** (18 - _dec)); + } + + function convert18To36(uint _dec, uint256 _amt) public pure returns (uint256 amt) { + amt = mul(_amt, 10 ** (18 - _dec)); + } + + function convertDecTo18(uint _dec, uint256 _amt) internal pure returns (uint256 amt) { + amt = mul(_amt, 10 ** (18 - _dec)); + } + + + using SafeERC20 for IERC20; event LogDeploy(uint amount); @@ -42,6 +56,7 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { event LogPausePool(bool); IERC20 public immutable baseToken; // Base token. Eg:- DAI, USDC, etc. + uint256 public immutable baseDecimal; // Base token. Eg:- DAI, USDC, etc. RegistryInterface public immutable registry; // Pool Registry IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); // Main Index @@ -56,6 +71,7 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { address _baseToken ) public ERC20(_name, _symbol) { baseToken = IERC20(_baseToken); + baseDecimal = ERC20(_baseToken).decimals(); registry = RegistryInterface(_registry); } @@ -71,12 +87,11 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { } function setExchangeRate() public isChief { - uint _previousRate = exchangeRate; + uint _previousRate = convert36To18(baseDecimal, exchangeRate); uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken(); - uint _currentRate = wdiv(_totalToken, totalSupply()); + uint _currentRate = convertDecTo18(baseDecimal, wdiv(_totalToken, totalSupply())); if (_currentRate < _previousRate) { - uint difRate = _previousRate - _currentRate; - uint difTkn = wmul(_totalToken, difRate); + uint difTkn = sub(tokenBalance, _totalToken); insuranceAmt = sub(insuranceAmt, difTkn); _currentRate = _previousRate; } else { @@ -88,7 +103,7 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { _currentRate = sub(_currentRate, insureFee); tokenBalance = sub(_totalToken, insuranceAmt); } - exchangeRate = _currentRate; + exchangeRate = convert18To36(baseDecimal, _currentRate); emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt); } From 73492a3450b2f3893d5d33a7a406a27940d1d890 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Thu, 27 Aug 2020 12:26:00 +0530 Subject: [PATCH 3/8] setting exchange rate in constructor --- contracts/tokenPool.sol | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contracts/tokenPool.sol b/contracts/tokenPool.sol index be488c1..698839f 100644 --- a/contracts/tokenPool.sol +++ b/contracts/tokenPool.sol @@ -61,7 +61,7 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); // Main Index uint private tokenBalance; // total token balance since last rebalancing - uint public exchangeRate = 10 ** 18; // initial 1 token = 1 + uint public exchangeRate; // initial 1 token = 1 uint public insuranceAmt; // insurance amount to keep pool safe constructor( @@ -73,6 +73,7 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { baseToken = IERC20(_baseToken); baseDecimal = ERC20(_baseToken).decimals(); registry = RegistryInterface(_registry); + exchangeRate = 10 ** uint(36 - ERC20(_baseToken).decimals()); } modifier isChief() { From 8e4cbd88633f84847ad41a22bc69a12cca091f11 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Thu, 27 Aug 2020 20:23:04 +1000 Subject: [PATCH 4/8] chnaged exchange rate modal --- contracts/tokenPool.sol | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/contracts/tokenPool.sol b/contracts/tokenPool.sol index 698839f..5cda409 100644 --- a/contracts/tokenPool.sol +++ b/contracts/tokenPool.sol @@ -31,20 +31,6 @@ interface RateInterface { } contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { - function convert36To18(uint _dec, uint256 _amt) public pure returns (uint256 amt) { - amt = (_amt / 10 ** (18 - _dec)); - } - - function convert18To36(uint _dec, uint256 _amt) public pure returns (uint256 amt) { - amt = mul(_amt, 10 ** (18 - _dec)); - } - - function convertDecTo18(uint _dec, uint256 _amt) internal pure returns (uint256 amt) { - amt = mul(_amt, 10 ** (18 - _dec)); - } - - - using SafeERC20 for IERC20; event LogDeploy(uint amount); @@ -56,7 +42,6 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { event LogPausePool(bool); IERC20 public immutable baseToken; // Base token. Eg:- DAI, USDC, etc. - uint256 public immutable baseDecimal; // Base token. Eg:- DAI, USDC, etc. RegistryInterface public immutable registry; // Pool Registry IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); // Main Index @@ -71,7 +56,6 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { address _baseToken ) public ERC20(_name, _symbol) { baseToken = IERC20(_baseToken); - baseDecimal = ERC20(_baseToken).decimals(); registry = RegistryInterface(_registry); exchangeRate = 10 ** uint(36 - ERC20(_baseToken).decimals()); } @@ -88,23 +72,22 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { } function setExchangeRate() public isChief { - uint _previousRate = convert36To18(baseDecimal, exchangeRate); + uint _previousRate = exchangeRate; uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken(); - uint _currentRate = convertDecTo18(baseDecimal, wdiv(_totalToken, totalSupply())); - if (_currentRate < _previousRate) { + // should we add totalToken - insurance here or keep it on logic contract? + uint _currentRate = wdiv(totalSupply(), _totalToken); + if (_currentRate > _previousRate) { uint difTkn = sub(tokenBalance, _totalToken); insuranceAmt = sub(insuranceAmt, difTkn); _currentRate = _previousRate; } else { uint fee = registry.insureFee(address(this)); - uint difRate = _currentRate - _previousRate; - uint insureFee = wmul(difRate, fee); uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), fee); insuranceAmt = add(insuranceAmt, insureFeeAmt); - _currentRate = sub(_currentRate, insureFee); - tokenBalance = sub(_totalToken, insuranceAmt); + tokenBalance = sub(_totalToken, insureFeeAmt); + _currentRate = wdiv(totalSupply(), tokenBalance); } - exchangeRate = convert18To36(baseDecimal, _currentRate); + exchangeRate = _currentRate; emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt); } @@ -122,7 +105,7 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { require(_newTokenBal <= registry.poolCap(address(this)), "deposit-cap-reached"); baseToken.safeTransferFrom(msg.sender, address(this), tknAmt); - uint _mintAmt = wdiv(tknAmt, exchangeRate); + uint _mintAmt = wmul(tknAmt, exchangeRate); _mint(msg.sender, _mintAmt); emit LogDeposit(tknAmt, _mintAmt); @@ -132,14 +115,14 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { uint poolBal = baseToken.balanceOf(address(this)); require(tknAmt <= poolBal, "not-enough-liquidity-available"); uint _bal = balanceOf(msg.sender); - uint _tknBal = wmul(_bal, exchangeRate); + uint _tknBal = wdiv(_bal, exchangeRate); uint _burnAmt; if (tknAmt == uint(-1)) { _burnAmt = _bal; - _tknAmt = wmul(_burnAmt, exchangeRate); + _tknAmt = wdiv(_burnAmt, exchangeRate); } else { require(tknAmt <= _tknBal, "balance-exceeded"); - _burnAmt = wdiv(tknAmt, exchangeRate); + _burnAmt = wmul(tknAmt, exchangeRate); _tknAmt = tknAmt; } From 27be4fbfd162bb7c69a0dc3bfa652e82dfe501a1 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Thu, 27 Aug 2020 21:02:09 +1000 Subject: [PATCH 5/8] ethPool exchange rate changes --- contracts/ethPool.sol | 29 +++++++++++++---------------- contracts/tokenPool.sol | 4 +++- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/contracts/ethPool.sol b/contracts/ethPool.sol index 0066551..716aaac 100644 --- a/contracts/ethPool.sol +++ b/contracts/ethPool.sol @@ -51,12 +51,10 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { address _registry, string memory _name, string memory _symbol, - address _baseToken, - address _origin + address _baseToken ) public ERC20(_name, _symbol) { baseToken = IERC20(_baseToken); registry = RegistryInterface(_registry); - address _dsa = instaIndex.build(address(this), 1, _origin); } modifier isChief() { @@ -73,19 +71,18 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { function setExchangeRate() public isChief { uint _previousRate = exchangeRate; uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken(); - uint _currentRate = wdiv(_totalToken, totalSupply()); - if (_currentRate < _previousRate) { - uint difRate = _previousRate - _currentRate; - uint difTkn = wmul(_totalToken, difRate); + _totalToken = sub(_totalToken, insuranceAmt); + uint _currentRate = wdiv(totalSupply(), _totalToken); + if (_currentRate > _previousRate) { + uint difTkn = sub(tokenBalance, _totalToken); insuranceAmt = sub(insuranceAmt, difTkn); _currentRate = _previousRate; } else { - uint difRate = _currentRate - _previousRate; - uint insureFee = wmul(difRate, registry.insureFee(address(this))); - uint insureFeeAmt = wmul(_totalToken, insureFee); + uint fee = registry.insureFee(address(this)); + uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), fee); insuranceAmt = add(insuranceAmt, insureFeeAmt); - _currentRate = sub(_currentRate, insureFee); - tokenBalance = sub(_totalToken, insuranceAmt); + tokenBalance = sub(_totalToken, insureFeeAmt); + _currentRate = wdiv(totalSupply(), tokenBalance); } exchangeRate = _currentRate; emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt); @@ -106,7 +103,7 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { uint _newTokenBal = add(tokenBalance, msg.value); require(_newTokenBal <= registry.poolCap(address(this)), "deposit-cap-reached"); - uint _mintAmt = wdiv(msg.value, exchangeRate); + uint _mintAmt = wmul(msg.value, exchangeRate); _mint(msg.sender, _mintAmt); emit LogDeposit(tknAmt, _mintAmt); @@ -116,14 +113,14 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { uint poolBal = address(this).balance; require(tknAmt <= poolBal, "not-enough-liquidity-available"); uint _bal = balanceOf(msg.sender); - uint _tknBal = wmul(_bal, exchangeRate); + uint _tknBal = wdiv(_bal, exchangeRate); uint _burnAmt; if (tknAmt == uint(-1)) { _burnAmt = _bal; - _tknAmt = wmul(_burnAmt, exchangeRate); + _tknAmt = wdiv(_burnAmt, exchangeRate); } else { require(tknAmt <= _tknBal, "balance-exceeded"); - _burnAmt = wdiv(tknAmt, exchangeRate); + _burnAmt = wmul(tknAmt, exchangeRate); _tknAmt = tknAmt; } diff --git a/contracts/tokenPool.sol b/contracts/tokenPool.sol index 5cda409..0707651 100644 --- a/contracts/tokenPool.sol +++ b/contracts/tokenPool.sol @@ -74,7 +74,7 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { function setExchangeRate() public isChief { uint _previousRate = exchangeRate; uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken(); - // should we add totalToken - insurance here or keep it on logic contract? + _totalToken = sub(_totalToken, insuranceAmt); uint _currentRate = wdiv(totalSupply(), _totalToken); if (_currentRate > _previousRate) { uint difTkn = sub(tokenBalance, _totalToken); @@ -143,4 +143,6 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { require(msg.sender == instaIndex.master(), "not-master"); paused() ? _unpause() : _pause(); } + + receive() external payable {} } From f63a3fc04ded9891085a6371f7e11a92a28e38da Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Thu, 27 Aug 2020 17:26:31 +0530 Subject: [PATCH 6/8] Added token paramater in deploy function --- contracts/ethPool.sol | 17 ++++++++++++----- contracts/tokenPool.sol | 18 ++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/contracts/ethPool.sol b/contracts/ethPool.sol index 716aaac..4581e67 100644 --- a/contracts/ethPool.sol +++ b/contracts/ethPool.sol @@ -2,7 +2,7 @@ pragma solidity ^0.6.8; pragma experimental ABIEncoderV2; -import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; import "@openzeppelin/contracts/token/ERC20/ERC20Pausable.sol"; import "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; @@ -31,7 +31,9 @@ interface RateInterface { } contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { - event LogDeploy(uint amount); + using SafeERC20 for IERC20; + + event LogDeploy(address indexed token, uint amount); event LogExchangeRate(uint exchangeRate, uint tokenBalance, uint insuranceAmt); event LogSettle(uint settleTime); event LogDeposit(uint depositAmt, uint poolMintAmt); @@ -62,10 +64,14 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { _; } - function deploy(address _dsa, uint amount) external isChief { + function deploy(address _dsa, address token, uint amount) external isChief { require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa"); - payable(_dsa).transfer(amount); - emit LogDeploy(amount); + if (token == address(0)) { + payable(_dsa).transfer(amount); + } else { + IERC20(token).safeTransfer(_dsa, amount); + } + emit LogDeploy(token, amount); } function setExchangeRate() public isChief { @@ -73,6 +79,7 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { 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); insuranceAmt = sub(insuranceAmt, difTkn); diff --git a/contracts/tokenPool.sol b/contracts/tokenPool.sol index 0707651..07c4254 100644 --- a/contracts/tokenPool.sol +++ b/contracts/tokenPool.sol @@ -33,7 +33,7 @@ interface RateInterface { contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { using SafeERC20 for IERC20; - event LogDeploy(uint amount); + event LogDeploy(address token, uint amount); event LogExchangeRate(uint exchangeRate, uint tokenBalance, uint insuranceAmt); event LogSettle(uint settleTime); event LogDeposit(uint depositAmt, uint poolMintAmt); @@ -65,10 +65,16 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { _; } - function deploy(address _dsa, uint amount) public isChief { + function deploy(address _dsa, address token, uint amount) public isChief { require(registry.isDsa(address(this), _dsa), "not-autheticated-dsa"); - baseToken.safeTransfer(_dsa, amount); - emit LogDeploy(amount); + if (token == address(0)) { + baseToken.safeTransfer(_dsa, amount); + } else if (token == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE){ + payable(_dsa).transfer(amount); + } else { + IERC20(token).safeTransfer(_dsa, amount); + } + emit LogDeploy(token, amount); } function setExchangeRate() public isChief { @@ -76,13 +82,13 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable { 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); insuranceAmt = sub(insuranceAmt, difTkn); _currentRate = _previousRate; } else { - uint fee = registry.insureFee(address(this)); - uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), fee); + uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), registry.insureFee(address(this))); insuranceAmt = add(insuranceAmt, insureFeeAmt); tokenBalance = sub(_totalToken, insureFeeAmt); _currentRate = wdiv(totalSupply(), tokenBalance); From 1cf3106273dd09eb0280edb2e1043d242223d0d1 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Thu, 27 Aug 2020 17:30:48 +0530 Subject: [PATCH 7/8] minor edit --- contracts/ethPool.sol | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/contracts/ethPool.sol b/contracts/ethPool.sol index 4581e67..8b2a4ad 100644 --- a/contracts/ethPool.sol +++ b/contracts/ethPool.sol @@ -67,11 +67,11 @@ 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"); if (token == address(0)) { - payable(_dsa).transfer(amount); - } else { - IERC20(token).safeTransfer(_dsa, amount); - } - emit LogDeploy(token, amount); + payable(_dsa).transfer(amount); + } else { + IERC20(token).safeTransfer(_dsa, amount); + } + emit LogDeploy(token, amount); } function setExchangeRate() public isChief { From 581de54d230dc5b249df477251d06546dd683886 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Thu, 27 Aug 2020 17:32:23 +0530 Subject: [PATCH 8/8] code refactor --- contracts/ethPool.sol | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contracts/ethPool.sol b/contracts/ethPool.sol index 8b2a4ad..e396cd1 100644 --- a/contracts/ethPool.sol +++ b/contracts/ethPool.sol @@ -85,8 +85,7 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath { insuranceAmt = sub(insuranceAmt, difTkn); _currentRate = _previousRate; } else { - uint fee = registry.insureFee(address(this)); - uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), fee); + uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), registry.insureFee(address(this))); insuranceAmt = add(insuranceAmt, insureFeeAmt); tokenBalance = sub(_totalToken, insureFeeAmt); _currentRate = wdiv(totalSupply(), tokenBalance);