ethPool exchange rate changes

This commit is contained in:
Samyak Jain 2020-08-27 21:02:09 +10:00
parent 8e4cbd8863
commit 27be4fbfd1
2 changed files with 16 additions and 17 deletions

View File

@ -51,12 +51,10 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath {
address _registry, address _registry,
string memory _name, string memory _name,
string memory _symbol, string memory _symbol,
address _baseToken, address _baseToken
address _origin
) public ERC20(_name, _symbol) { ) public ERC20(_name, _symbol) {
baseToken = IERC20(_baseToken); baseToken = IERC20(_baseToken);
registry = RegistryInterface(_registry); registry = RegistryInterface(_registry);
address _dsa = instaIndex.build(address(this), 1, _origin);
} }
modifier isChief() { modifier isChief() {
@ -73,19 +71,18 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath {
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();
uint _currentRate = wdiv(_totalToken, totalSupply()); _totalToken = sub(_totalToken, insuranceAmt);
if (_currentRate < _previousRate) { uint _currentRate = wdiv(totalSupply(), _totalToken);
uint difRate = _previousRate - _currentRate; if (_currentRate > _previousRate) {
uint difTkn = wmul(_totalToken, difRate); uint difTkn = sub(tokenBalance, _totalToken);
insuranceAmt = sub(insuranceAmt, difTkn); insuranceAmt = sub(insuranceAmt, difTkn);
_currentRate = _previousRate; _currentRate = _previousRate;
} else { } else {
uint difRate = _currentRate - _previousRate; uint fee = registry.insureFee(address(this));
uint insureFee = wmul(difRate, registry.insureFee(address(this))); uint insureFeeAmt = wmul(sub(_totalToken, tokenBalance), fee);
uint insureFeeAmt = wmul(_totalToken, insureFee);
insuranceAmt = add(insuranceAmt, insureFeeAmt); insuranceAmt = add(insuranceAmt, insureFeeAmt);
_currentRate = sub(_currentRate, insureFee); tokenBalance = sub(_totalToken, insureFeeAmt);
tokenBalance = sub(_totalToken, insuranceAmt); _currentRate = wdiv(totalSupply(), tokenBalance);
} }
exchangeRate = _currentRate; exchangeRate = _currentRate;
emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt); emit LogExchangeRate(exchangeRate, tokenBalance, insuranceAmt);
@ -106,7 +103,7 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath {
uint _newTokenBal = add(tokenBalance, msg.value); uint _newTokenBal = add(tokenBalance, msg.value);
require(_newTokenBal <= registry.poolCap(address(this)), "deposit-cap-reached"); 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); _mint(msg.sender, _mintAmt);
emit LogDeposit(tknAmt, _mintAmt); emit LogDeposit(tknAmt, _mintAmt);
@ -116,14 +113,14 @@ contract PoolToken is ReentrancyGuard, ERC20Pausable, DSMath {
uint poolBal = address(this).balance; uint poolBal = address(this).balance;
require(tknAmt <= poolBal, "not-enough-liquidity-available"); require(tknAmt <= poolBal, "not-enough-liquidity-available");
uint _bal = balanceOf(msg.sender); uint _bal = balanceOf(msg.sender);
uint _tknBal = wmul(_bal, exchangeRate); uint _tknBal = wdiv(_bal, exchangeRate);
uint _burnAmt; uint _burnAmt;
if (tknAmt == uint(-1)) { if (tknAmt == uint(-1)) {
_burnAmt = _bal; _burnAmt = _bal;
_tknAmt = wmul(_burnAmt, exchangeRate); _tknAmt = wdiv(_burnAmt, exchangeRate);
} else { } else {
require(tknAmt <= _tknBal, "balance-exceeded"); require(tknAmt <= _tknBal, "balance-exceeded");
_burnAmt = wdiv(tknAmt, exchangeRate); _burnAmt = wmul(tknAmt, exchangeRate);
_tknAmt = tknAmt; _tknAmt = tknAmt;
} }

View File

@ -74,7 +74,7 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable {
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();
// should we add totalToken - insurance here or keep it on logic contract? _totalToken = sub(_totalToken, insuranceAmt);
uint _currentRate = wdiv(totalSupply(), _totalToken); uint _currentRate = wdiv(totalSupply(), _totalToken);
if (_currentRate > _previousRate) { if (_currentRate > _previousRate) {
uint difTkn = sub(tokenBalance, _totalToken); uint difTkn = sub(tokenBalance, _totalToken);
@ -143,4 +143,6 @@ contract PoolToken is ReentrancyGuard, DSMath, ERC20Pausable {
require(msg.sender == instaIndex.master(), "not-master"); require(msg.sender == instaIndex.master(), "not-master");
paused() ? _unpause() : _pause(); paused() ? _unpause() : _pause();
} }
receive() external payable {}
} }