Skip to content

Commit

Permalink
deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
ZumZoom committed Nov 22, 2021
1 parent 0993657 commit a35978f
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions contracts/FixedRateSwap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,7 @@ contract FixedRateSwap is ERC20 {
token0Amount = token0.balanceOf(address(this)) * amount / _totalSupply;
token1Amount = token1.balanceOf(address(this)) * amount / _totalSupply;

_burn(msg.sender, amount);
emit Withdrawal(msg.sender, token0Amount, token1Amount, amount);
if (token0Amount > 0) {
token0.safeTransfer(to, token0Amount);
}
if (token1Amount > 0) {
token1.safeTransfer(to, token1Amount);
}
_withdraw(to, amount, token0Amount, token1Amount);
}

/**
Expand Down Expand Up @@ -208,16 +201,9 @@ contract FixedRateSwap is ERC20 {
require(firstTokenShare <= _ONE, "Ratio should be in [0, 1]");

uint256 _totalSupply = totalSupply();
_burn(msg.sender, amount);
(token0Amount, token1Amount) = _getRealAmountsForWithdraw(amount, firstTokenShare, _totalSupply);
emit Withdrawal(msg.sender, token0Amount, token1Amount, amount);

if (token0Amount > 0) {
token0.safeTransfer(to, token0Amount);
}
if (token1Amount > 0) {
token1.safeTransfer(to, token1Amount);
}
_withdraw(to, amount, token0Amount, token1Amount);
}

/**
Expand Down Expand Up @@ -313,6 +299,17 @@ contract FixedRateSwap is ERC20 {
}
}

function _withdraw(address to, uint256 amount, uint256 token0Amount, uint256 token1Amount) private {
_burn(msg.sender, amount);
emit Withdrawal(msg.sender, token0Amount, token1Amount, amount);
if (token0Amount > 0) {
token0.safeTransfer(to, token0Amount);
}
if (token1Amount > 0) {
token1.safeTransfer(to, token1Amount);
}
}

function _swap(IERC20 tokenFrom, IERC20 tokenTo, uint256 inputAmount, address to) private returns(uint256 outputAmount) {
require(inputAmount > 0, "Input amount should be > 0");
outputAmount = getReturn(tokenFrom, tokenTo, inputAmount);
Expand Down

0 comments on commit a35978f

Please sign in to comment.