Lots of refactoring

This commit is contained in:
Thrilok Kumar 2020-05-28 04:31:01 +05:30
parent 8bfc8f6ec7
commit 1e7ef19452

View File

@ -200,32 +200,37 @@ contract LiquidityHelpers is UniswapHelpers {
} }
function _removeLiquidity( function _removeLiquidity(
address[] memory tokens, address tokenA,
address tokenB,
uint _amt, uint _amt,
uint[] memory slippages, uint unitAmtA,
uint unitAmtB,
uint deadline uint deadline
) internal returns (uint _amtA, uint _amtB, uint _uniAmt) { ) internal returns (uint _amtA, uint _amtB, uint _uniAmt) {
IUniswapV2Router01 router = IUniswapV2Router01(getUniswapAddr()); IUniswapV2Router01 router = IUniswapV2Router01(getUniswapAddr());
TokenInterface[] memory _tokens = changeEthToWeth(tokens); (TokenInterface _tokenA, TokenInterface _tokenB) = changeEthAddress(tokenA, tokenB);
address exchangeAddr = IUniswapV2Factory(router.factory()).getPair(address(_tokens[0]), address(_tokens[1])); address exchangeAddr = IUniswapV2Factory(router.factory()).getPair(address(_tokenA), address(_tokenB));
require(exchangeAddr != address(0), "pair-not-found."); require(exchangeAddr != address(0), "pair-not-found.");
TokenInterface uniToken = TokenInterface(exchangeAddr); TokenInterface uniToken = TokenInterface(exchangeAddr);
_uniAmt = _amt == uint(-1) ? uniToken.balanceOf(address(this)) : _amt; _uniAmt = _amt == uint(-1) ? uniToken.balanceOf(address(this)) : _amt;
uniToken.approve(address(router), _uniAmt); uniToken.approve(address(router), _uniAmt);
uint minAmtA = convert18ToDec(_tokenA.decimals(), wmul(unitAmtA, _amt));
uint minAmtB = convert18ToDec(_tokenB.decimals(), wmul(unitAmtB, _amt));
(_amtA, _amtB) = router.removeLiquidity( (_amtA, _amtB) = router.removeLiquidity(
address(_tokens[0]), address(_tokenA),
address(_tokens[1]), address(_tokenB),
_amt, _amt,
slippages[0], minAmtA,
slippages[1], minAmtB,
address(this), address(this),
now + deadline // TODO - deadline? now + deadline // TODO - deadline?
); );
convertWethToEth(_tokens[0], _amtA); convertWethToEth(_tokenA, _amtA);
convertWethToEth(_tokens[1], _amtB); convertWethToEth(_tokenB, _amtB);
} }
} }
@ -269,7 +274,7 @@ contract UniswapLiquidity is LiquidityHelpers {
setId setId
); );
bytes32 _eventCode = keccak256("LogDepositLiquidity(address,address,uint256,uint256,uint256,uint256[],uint256)"); bytes32 _eventCode = keccak256("LogDepositLiquidity(address,address,uint256,uint256,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode( bytes memory _eventParam = abi.encode(
tokenA, tokenA,
tokenB, tokenB,
@ -283,7 +288,8 @@ contract UniswapLiquidity is LiquidityHelpers {
} }
function emitWithdraw( function emitWithdraw(
address[] memory tokens, address tokenA,
address tokenB,
uint _amtA, uint _amtA,
uint _amtB, uint _amtB,
uint _uniAmt, uint _uniAmt,
@ -291,18 +297,18 @@ contract UniswapLiquidity is LiquidityHelpers {
uint[] memory setIds uint[] memory setIds
) internal { ) internal {
emit LogWithdrawLiquidity( emit LogWithdrawLiquidity(
tokens[0], tokenA,
tokens[1], tokenB,
_amtA, _amtA,
_amtB, _amtB,
_uniAmt, _uniAmt,
getId, getId,
setIds setIds
); );
bytes32 _eventCode = keccak256("LogWithdrawLiquidity(address,address,uint256,uint256,uint256,uint256[],uint256)"); bytes32 _eventCode = keccak256("LogWithdrawLiquidity(address,address,uint256,uint256,uint256,uint256,uint256[])");
bytes memory _eventParam = abi.encode( bytes memory _eventParam = abi.encode(
tokens[0], tokenA,
tokens[1], tokenB,
_amtA, _amtA,
_amtB, _amtB,
_uniAmt, _uniAmt,
@ -337,21 +343,29 @@ contract UniswapLiquidity is LiquidityHelpers {
} }
function withdraw( function withdraw(
address[] calldata tokens, address tokenA,
uint amt, address tokenB,
uint[] calldata slippages, uint uinAmt,
uint unitAmtA,
uint unitAmtB,
uint deadline, uint deadline,
uint getId, uint getId,
uint[] calldata setIds uint[] calldata setIds
) external payable { ) external payable {
require(tokens.length == 2, "length-is-not-two"); uint _amt = getUint(getId, uinAmt);
uint _amt = getUint(getId, amt);
(uint _amtA, uint _amtB, uint _uniAmt) = _removeLiquidity(tokens, _amt, slippages, deadline); (uint _amtA, uint _amtB, uint _uniAmt) = _removeLiquidity(
tokenA,
tokenB,
_amt,
unitAmtA,
unitAmtB,
deadline
);
setUint(setIds[0], _amtA); setUint(setIds[0], _amtA);
setUint(setIds[1], _amtB); setUint(setIds[1], _amtB);
emitWithdraw(tokens, _amtA, _amtB, _uniAmt, getId, setIds); emitWithdraw(tokenA, tokenB, _amtA, _amtB, _uniAmt, getId, setIds);
} }
} }