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] 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; }