created common getter for current rate

This commit is contained in:
Sowmay Jain 2020-08-31 01:47:40 +10:00
parent 3cc84a904a
commit 07c2b67b63

View File

@ -75,11 +75,19 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath {
emit LogDeploy(token, amount); emit LogDeploy(token, amount);
} }
/**
* @dev get pool token rate
* @param tokenAmt total token amount
*/
function getCurrentRate(uint tokenAmt) public {
return wdiv(totalSupply(), tokenAmt);
}
function setExchangeRate() public isChief { function setExchangeRate() public isChief {
uint _previousRate = exchangeRate; uint _previousRate = exchangeRate;
uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken(); uint _totalToken = RateInterface(registry.poolLogic(address(this))).getTotalToken();
_totalToken = sub(_totalToken, insuranceAmt); _totalToken = sub(_totalToken, insuranceAmt);
uint _currentRate = wdiv(totalSupply(), _totalToken); uint _currentRate = getCurrentRate(_totalToken);
require(_currentRate != 0, "current-rate-is-zero"); require(_currentRate != 0, "current-rate-is-zero");
if (_currentRate > _previousRate) { if (_currentRate > _previousRate) {
uint _difTkn = sub(tokenBalance, _totalToken); uint _difTkn = sub(tokenBalance, _totalToken);
@ -89,13 +97,13 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath {
} else { } else {
insuranceAmt = 0; insuranceAmt = 0;
tokenBalance = add(_totalToken, insuranceAmt); tokenBalance = add(_totalToken, insuranceAmt);
_currentRate = wdiv(totalSupply(), tokenBalance); _currentRate = getCurrentRate(tokenBalance);
} }
} else { } else {
uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), registry.insureFee(address(this))); uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), registry.insureFee(address(this)));
insuranceAmt = add(insuranceAmt, insureFeeAmt); insuranceAmt = add(insuranceAmt, insureFeeAmt);
tokenBalance = sub(_totalToken, insureFeeAmt); tokenBalance = sub(_totalToken, insureFeeAmt);
_currentRate = wdiv(totalSupply(), tokenBalance); _currentRate = getCurrentRate(tokenBalance);
} }
exchangeRate = _currentRate; exchangeRate = _currentRate;
emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt); emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt);