chnaged exchange rate modal

This commit is contained in:
Samyak Jain 2020-08-27 20:23:04 +10:00
parent 73492a3450
commit 8e4cbd8863

View File

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