文件 1 的 7:IERC20.sol
pragma solidity >=0.6.0 <0.8.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
文件 2 的 7:IUniswapV2Pair.sol
pragma solidity 0.7.5;
interface IUniswapV2Pair {
function getReserves()
external
view
returns (
uint112 reserve0,
uint112 reserve1,
uint32 blockTimestampLast
);
function swap(
uint256 amount0Out,
uint256 amount1Out,
address to,
bytes calldata data
) external;
}
文件 3 的 7:IWETH.sol
pragma solidity 0.7.5;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
abstract contract IWETH is IERC20 {
function deposit() external payable virtual;
function withdraw(uint256 amount) external virtual;
}
文件 4 的 7:NewUniswapV2ExchangeRouter.sol
pragma solidity 0.7.5;
import "../weth/IWETH.sol";
import "./NewUniswapV2Lib.sol";
import "@uniswap/v3-periphery/contracts/libraries/TransferHelper.sol";
contract NewUniswapV2ExchangeRouter {
using SafeMath for uint256;
address private constant ETH_IDENTIFIER = address(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
uint256 private constant FEE_OFFSET = 161;
uint256 private constant DIRECTION_FLAG = 0x0000000000000000000000010000000000000000000000000000000000000000;
receive() external payable {}
function swap(
address tokenIn,
uint256 amountIn,
uint256 amountOutMin,
address weth,
uint256[] calldata pools
) external payable returns (uint256 tokensBought) {
return _swap(tokenIn, amountIn, amountOutMin, weth, pools);
}
function buy(
address tokenIn,
uint256 amountInMax,
uint256 amountOut,
address weth,
uint256[] memory pools
) external payable returns (uint256 tokensSold) {
return _buy(tokenIn, amountInMax, amountOut, weth, pools);
}
function _swap(
address tokenIn,
uint256 amountIn,
uint256 amountOutMin,
address weth,
uint256[] memory pools
) private returns (uint256 tokensBought) {
uint256 pairs = pools.length;
require(pairs != 0, "At least one pool required");
bool tokensBoughtEth;
if (tokenIn == ETH_IDENTIFIER) {
require(amountIn == msg.value, "Incorrect amount of ETH sent");
IWETH(weth).deposit{ value: msg.value }();
require(IWETH(weth).transfer(address(pools[0]), msg.value));
} else {
TransferHelper.safeTransferFrom(tokenIn, msg.sender, address(pools[0]), amountIn);
tokensBoughtEth = weth != address(0);
}
tokensBought = amountIn;
for (uint256 i = 0; i < pairs; ++i) {
uint256 p = pools[i];
address pool = address(p);
bool direction = p & DIRECTION_FLAG == 0;
tokensBought = NewUniswapV2Lib.getAmountOut(tokensBought, pool, direction, p >> FEE_OFFSET);
(uint256 amount0Out, uint256 amount1Out) = direction
? (uint256(0), tokensBought)
: (tokensBought, uint256(0));
IUniswapV2Pair(pool).swap(
amount0Out,
amount1Out,
i + 1 == pairs ? (tokensBoughtEth ? address(this) : msg.sender) : address(pools[i + 1]),
""
);
}
if (tokensBoughtEth) {
IWETH(weth).withdraw(tokensBought);
TransferHelper.safeTransferETH(msg.sender, tokensBought);
}
require(tokensBought >= amountOutMin, "UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT");
}
function _buy(
address tokenIn,
uint256 amountInMax,
uint256 amountOut,
address weth,
uint256[] memory pools
) private returns (uint256 tokensSold) {
uint256 pairs = pools.length;
require(pairs != 0, "At least one pool required");
uint256[] memory amounts = new uint256[](pairs + 1);
amounts[pairs] = amountOut;
for (uint256 i = pairs; i != 0; --i) {
uint256 p = pools[i - 1];
amounts[i - 1] = NewUniswapV2Lib.getAmountIn(
amounts[i],
address(p),
p & DIRECTION_FLAG == 0,
p >> FEE_OFFSET
);
}
tokensSold = amounts[0];
require(tokensSold <= amountInMax, "UniswapV2Router: INSUFFICIENT_INPUT_AMOUNT");
bool tokensBoughtEth;
if (tokenIn == ETH_IDENTIFIER) {
TransferHelper.safeTransferETH(msg.sender, msg.value.sub(tokensSold));
IWETH(weth).deposit{ value: tokensSold }();
require(IWETH(weth).transfer(address(pools[0]), tokensSold));
} else {
TransferHelper.safeTransferFrom(tokenIn, msg.sender, address(pools[0]), tokensSold);
tokensBoughtEth = weth != address(0);
}
for (uint256 i = 0; i < pairs; ++i) {
uint256 p = pools[i];
(uint256 amount0Out, uint256 amount1Out) = p & DIRECTION_FLAG == 0
? (uint256(0), amounts[i + 1])
: (amounts[i + 1], uint256(0));
IUniswapV2Pair(address(p)).swap(
amount0Out,
amount1Out,
i + 1 == pairs ? (tokensBoughtEth ? address(this) : msg.sender) : address(pools[i + 1]),
""
);
}
if (tokensBoughtEth) {
IWETH(weth).withdraw(amountOut);
TransferHelper.safeTransferETH(msg.sender, amountOut);
}
}
}
文件 5 的 7:NewUniswapV2Lib.sol
pragma solidity 0.7.5;
import "./IUniswapV2Pair.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
library NewUniswapV2Lib {
using SafeMath for uint256;
function getReservesByPair(address pair, bool direction)
internal
view
returns (uint256 reserveIn, uint256 reserveOut)
{
(uint256 reserve0, uint256 reserve1, ) = IUniswapV2Pair(pair).getReserves();
(reserveIn, reserveOut) = direction ? (reserve0, reserve1) : (reserve1, reserve0);
}
function getAmountOut(
uint256 amountIn,
address pair,
bool direction,
uint256 fee
) internal view returns (uint256 amountOut) {
require(amountIn > 0, "UniswapV2Lib: INSUFFICIENT_INPUT_AMOUNT");
(uint256 reserveIn, uint256 reserveOut) = getReservesByPair(pair, direction);
uint256 amountInWithFee = amountIn.mul(fee);
uint256 numerator = amountInWithFee.mul(reserveOut);
uint256 denominator = reserveIn.mul(10000).add(amountInWithFee);
amountOut = uint256(numerator / denominator);
}
function getAmountIn(
uint256 amountOut,
address pair,
bool direction,
uint256 fee
) internal view returns (uint256 amountIn) {
require(amountOut > 0, "UniswapV2Lib: INSUFFICIENT_OUTPUT_AMOUNT");
(uint256 reserveIn, uint256 reserveOut) = getReservesByPair(pair, direction);
require(reserveOut > amountOut, "UniswapV2Lib: reserveOut should be greater than amountOut");
uint256 numerator = reserveIn.mul(amountOut).mul(10000);
uint256 denominator = reserveOut.sub(amountOut).mul(fee);
amountIn = (numerator / denominator).add(1);
}
}
文件 6 的 7:SafeMath.sol
pragma solidity >=0.6.0 <0.8.0;
library SafeMath {
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b > a) return (false, 0);
return (true, a - b);
}
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a / b);
}
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a % b);
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
return a - b;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) return 0;
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: division by zero");
return a / b;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: modulo by zero");
return a % b;
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
return a - b;
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a / b;
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a % b;
}
}
文件 7 的 7:TransferHelper.sol
pragma solidity >=0.6.0;
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
library TransferHelper {
function safeTransferFrom(
address token,
address from,
address to,
uint256 value
) internal {
(bool success, bytes memory data) =
token.call(abi.encodeWithSelector(IERC20.transferFrom.selector, from, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'STF');
}
function safeTransfer(
address token,
address to,
uint256 value
) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(IERC20.transfer.selector, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'ST');
}
function safeApprove(
address token,
address to,
uint256 value
) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(IERC20.approve.selector, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'SA');
}
function safeTransferETH(address to, uint256 value) internal {
(bool success, ) = to.call{value: value}(new bytes(0));
require(success, 'STE');
}
}
{
"compilationTarget": {
"contracts/lib/uniswapv2/NewUniswapV2ExchangeRouter.sol": "NewUniswapV2ExchangeRouter"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "none",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 1000000
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address","name":"weth","type":"address"},{"internalType":"uint256[]","name":"pools","type":"uint256[]"}],"name":"buy","outputs":[{"internalType":"uint256","name":"tokensSold","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address","name":"weth","type":"address"},{"internalType":"uint256[]","name":"pools","type":"uint256[]"}],"name":"swap","outputs":[{"internalType":"uint256","name":"tokensBought","type":"uint256"}],"stateMutability":"payable","type":"function"},{"stateMutability":"payable","type":"receive"}]