contracts: split swapInput into two functions

This commit is contained in:
Dan Robinson
2019-10-01 10:35:36 -07:00
parent 9ea8b3a5d4
commit 52e2971afc

View File

@ -49,32 +49,28 @@ contract UniswapERC20 is ERC20 {
}
//TO: DO msg.sender is wrapper
function swapInput(address inputToken, uint256 amountSold, address recipient) public nonReentrant returns (uint256) {
address _tokenA = address(tokenA);
address _tokenB = address(tokenB);
bool inputIsA = inputToken == _tokenA;
require(inputIsA || inputToken == _tokenB);
address outputToken = _tokenA;
if(inputIsA) {
outputToken = _tokenB;
}
function swapAForB(uint256 amountSold, address recipient) public nonReentrant returns (uint256) {
amountBought = swap(tokenA, tokenB, amountSold);
emit SwapAForB(msg.sender, amountSold, amountBought);
return amountBought;
}
//TO: DO msg.sender is wrapper
function swapBForA(uint256 amountSold, address recipient) public nonReentrant returns (uint256) {
amountBought = swap(tokenB, tokenA, amountSold);
emit SwapBForA(msg.sender, amountSold, amountBought);
return amountBought;
}
function swap(address inputToken, address outputToken, uint256 amountSold) private returns (uint256) {
uint256 inputReserve = IERC20(inputToken).balanceOf(address(this));
uint256 outputReserve = IERC20(outputToken).balanceOf(address(this));
uint256 amountBought = getInputPrice(amountSold, inputReserve, outputReserve);
require(IERC20(inputToken).transferFrom(msg.sender, address(this), amountSold));
require(IERC20(outputToken).transfer(recipient, amountBought));
if(inputIsA) {
emit SwapAForB(msg.sender, amountSold, amountBought);
} else {
emit SwapBForA(msg.sender, amountSold, amountBought);
}
return amountBought;
return amountBought
}
function getInputPrice(address inputToken, uint256 amountSold) public view returns (uint256) {
require(amountSold > 0);
address _tokenA = address(tokenA);