2020-10-27 12:18:30 +00:00
|
|
|
// SPDX-License-Identifier: agpl-3.0
|
2020-11-25 14:07:33 +00:00
|
|
|
pragma solidity 0.6.12;
|
2020-10-27 12:18:30 +00:00
|
|
|
|
2021-01-13 14:11:39 +00:00
|
|
|
import {IUniswapV2Router02} from '../../interfaces/IUniswapV2Router02.sol';
|
2020-10-27 12:18:30 +00:00
|
|
|
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
|
|
|
|
import {MintableERC20} from '../tokens/MintableERC20.sol';
|
|
|
|
|
|
|
|
contract MockUniswapV2Router02 is IUniswapV2Router02 {
|
2020-11-03 18:37:06 +00:00
|
|
|
mapping(address => uint256) internal _amountToReturn;
|
|
|
|
mapping(address => uint256) internal _amountToSwap;
|
2020-10-27 19:32:09 +00:00
|
|
|
mapping(address => mapping(address => mapping(uint256 => uint256))) internal _amountsIn;
|
|
|
|
mapping(address => mapping(address => mapping(uint256 => uint256))) internal _amountsOut;
|
2020-11-25 13:44:50 +00:00
|
|
|
uint256 internal defaultMockValue;
|
2020-10-27 12:18:30 +00:00
|
|
|
|
2020-11-03 18:37:06 +00:00
|
|
|
function setAmountToReturn(address reserve, uint256 amount) public {
|
|
|
|
_amountToReturn[reserve] = amount;
|
2020-10-27 12:18:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 18:37:06 +00:00
|
|
|
function setAmountToSwap(address reserve, uint256 amount) public {
|
|
|
|
_amountToSwap[reserve] = amount;
|
2020-10-27 12:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function swapExactTokensForTokens(
|
|
|
|
uint256 amountIn,
|
2021-01-13 14:11:39 +00:00
|
|
|
uint256, /* amountOutMin */
|
2020-10-27 12:18:30 +00:00
|
|
|
address[] calldata path,
|
|
|
|
address to,
|
|
|
|
uint256 /* deadline */
|
|
|
|
) external override returns (uint256[] memory amounts) {
|
|
|
|
IERC20(path[0]).transferFrom(msg.sender, address(this), amountIn);
|
|
|
|
|
2020-11-03 18:37:06 +00:00
|
|
|
MintableERC20(path[1]).mint(_amountToReturn[path[0]]);
|
|
|
|
IERC20(path[1]).transfer(to, _amountToReturn[path[0]]);
|
2020-10-27 12:18:30 +00:00
|
|
|
|
2021-01-13 14:11:39 +00:00
|
|
|
amounts = new uint256[](path.length);
|
2020-10-27 12:18:30 +00:00
|
|
|
amounts[0] = amountIn;
|
2020-11-03 18:37:06 +00:00
|
|
|
amounts[1] = _amountToReturn[path[0]];
|
2020-10-27 12:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function swapTokensForExactTokens(
|
2021-01-13 14:11:39 +00:00
|
|
|
uint256 amountOut,
|
|
|
|
uint256, /* amountInMax */
|
2020-10-27 12:18:30 +00:00
|
|
|
address[] calldata path,
|
|
|
|
address to,
|
2021-01-13 14:11:39 +00:00
|
|
|
uint256 /* deadline */
|
2020-10-27 12:18:30 +00:00
|
|
|
) external override returns (uint256[] memory amounts) {
|
2020-11-03 18:37:06 +00:00
|
|
|
IERC20(path[0]).transferFrom(msg.sender, address(this), _amountToSwap[path[0]]);
|
2020-10-27 12:18:30 +00:00
|
|
|
|
2020-11-03 18:37:06 +00:00
|
|
|
MintableERC20(path[1]).mint(amountOut);
|
|
|
|
IERC20(path[1]).transfer(to, amountOut);
|
2020-10-27 12:18:30 +00:00
|
|
|
|
2021-01-13 14:11:39 +00:00
|
|
|
amounts = new uint256[](path.length);
|
2020-11-03 18:37:06 +00:00
|
|
|
amounts[0] = _amountToSwap[path[0]];
|
|
|
|
amounts[1] = amountOut;
|
2020-10-27 12:18:30 +00:00
|
|
|
}
|
2020-10-27 19:32:09 +00:00
|
|
|
|
2021-01-13 14:11:39 +00:00
|
|
|
function setAmountOut(
|
|
|
|
uint256 amountIn,
|
|
|
|
address reserveIn,
|
|
|
|
address reserveOut,
|
|
|
|
uint256 amountOut
|
|
|
|
) public {
|
2020-10-27 19:32:09 +00:00
|
|
|
_amountsOut[reserveIn][reserveOut][amountIn] = amountOut;
|
|
|
|
}
|
|
|
|
|
2021-01-13 14:11:39 +00:00
|
|
|
function setAmountIn(
|
|
|
|
uint256 amountOut,
|
|
|
|
address reserveIn,
|
|
|
|
address reserveOut,
|
|
|
|
uint256 amountIn
|
|
|
|
) public {
|
2020-10-27 19:32:09 +00:00
|
|
|
_amountsIn[reserveIn][reserveOut][amountOut] = amountIn;
|
|
|
|
}
|
|
|
|
|
2021-01-13 14:11:39 +00:00
|
|
|
function setDefaultMockValue(uint256 value) public {
|
2020-11-25 13:44:50 +00:00
|
|
|
defaultMockValue = value;
|
|
|
|
}
|
|
|
|
|
2021-01-13 14:11:39 +00:00
|
|
|
function getAmountsOut(uint256 amountIn, address[] calldata path)
|
|
|
|
external
|
|
|
|
view
|
|
|
|
override
|
|
|
|
returns (uint256[] memory)
|
|
|
|
{
|
|
|
|
uint256[] memory amounts = new uint256[](path.length);
|
2020-10-27 19:32:09 +00:00
|
|
|
amounts[0] = amountIn;
|
2021-01-13 14:11:39 +00:00
|
|
|
amounts[1] = _amountsOut[path[0]][path[1]][amountIn] > 0
|
|
|
|
? _amountsOut[path[0]][path[1]][amountIn]
|
|
|
|
: defaultMockValue;
|
2020-10-27 19:32:09 +00:00
|
|
|
return amounts;
|
|
|
|
}
|
|
|
|
|
2021-01-13 14:11:39 +00:00
|
|
|
function getAmountsIn(uint256 amountOut, address[] calldata path)
|
|
|
|
external
|
|
|
|
view
|
|
|
|
override
|
|
|
|
returns (uint256[] memory)
|
|
|
|
{
|
|
|
|
uint256[] memory amounts = new uint256[](path.length);
|
|
|
|
amounts[0] = _amountsIn[path[0]][path[1]][amountOut] > 0
|
|
|
|
? _amountsIn[path[0]][path[1]][amountOut]
|
|
|
|
: defaultMockValue;
|
2020-10-27 19:32:09 +00:00
|
|
|
amounts[1] = amountOut;
|
|
|
|
return amounts;
|
|
|
|
}
|
2020-10-27 12:18:30 +00:00
|
|
|
}
|