文件 1 的 1:XchangeRouterV3.sol
pragma solidity =0.8.25;
abstract contract Ownable {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor(address owner_) {
_transferOwnership(owner_);
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
require(owner() == msg.sender, "Ownable: caller is not the owner");
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
interface IMulticall {
function multicall(bytes[] calldata data) external payable returns (bytes[] memory results);
}
interface IMulticallExtended is IMulticall {
function multicall(uint256 deadline, bytes[] calldata data) external payable returns (bytes[] memory results);
function multicall(
bytes32 previousBlockhash,
bytes[] calldata data
) external payable returns (bytes[] memory results);
}
interface IPeripheryPayments {
function unwrapWETH9(uint256 amountMinimum, address recipient) external payable;
function refundETH() external payable;
function sweepToken(address token, uint256 amountMinimum, address recipient) external payable;
}
interface IPeripheryPaymentsExtended is IPeripheryPayments {
function unwrapWETH9(uint256 amountMinimum) external payable;
function wrapETH(uint256 value) external payable;
function sweepToken(address token, uint256 amountMinimum) external payable;
function pull(address token, uint256 value) external payable;
}
interface IXchangeFactory {
function isFailsafeLiquidator(address) external view returns (bool);
function getPair(address tokenA, address tokenB) external view returns (address pair);
function createPair(address tokenA, address tokenB) external returns (address pair);
}
interface IXchangePair {
function transferFrom(address from, address to, uint value) external returns (bool);
function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function mint(address to) external returns (uint liquidity);
function burn(address to) external returns (uint amount0, uint amount1);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
function mustBurn(address to, uint256 gasAmount) external returns (uint256 amount0, uint256 amount1);
}
interface IXchangeRouter {
function factory() external view returns (address);
function WETH() external view returns (address);
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
function removeLiquidity(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB);
function mustRemoveLiquidity(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline,
uint gasAmount
) external returns (uint amountA, uint amountB);
function removeLiquidityETH(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountToken, uint amountETH);
function removeLiquidityWithPermit(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline,
bool approveMax,
uint8 v,
bytes32 r,
bytes32 s
) external returns (uint amountA, uint amountB);
function removeLiquidityETHWithPermit(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax,
uint8 v,
bytes32 r,
bytes32 s
) external returns (uint amountToken, uint amountETH);
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapExactETHForTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable returns (uint[] memory amounts);
function swapTokensForExactETH(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapExactTokensForETH(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapETHForExactTokens(
uint amountOut,
address[] calldata path,
address to,
uint deadline
) external payable returns (uint[] memory amounts);
function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
function removeLiquidityETHSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountETH);
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax,
uint8 v,
bytes32 r,
bytes32 s
) external returns (uint amountETH);
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable;
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
}
interface IERC20 {
function balanceOf(address owner) external view returns (uint);
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
}
interface IWETH {
function deposit() external payable;
function transfer(address to, uint value) external returns (bool);
function withdraw(uint) external;
}
interface IWETH9 is IERC20 {
function deposit() external payable;
function withdraw(uint256) external;
}
interface IPeripheryImmutableState {
function factory() external view returns (address);
function WETH9() external view returns (address);
}
interface IXchangeDiscountAuthority {
function fee(address) external view returns (uint256);
}
abstract contract Multicall is IMulticall {
function multicall(bytes[] calldata data) public payable override returns (bytes[] memory results) {
results = new bytes[](data.length);
for (uint256 i = 0; i < data.length; i++) {
(bool success, bytes memory result) = address(this).delegatecall(data[i]);
if (!success) {
if (result.length < 68) revert();
assembly {
result := add(result, 0x04)
}
revert(abi.decode(result, (string)));
}
results[i] = result;
}
}
}
abstract contract BlockTimestamp {
function _blockTimestamp() internal view virtual returns (uint256) {
return block.timestamp;
}
}
abstract contract PeripheryValidation is BlockTimestamp {
modifier checkDeadline(uint256 deadline) {
require(_blockTimestamp() <= deadline, "Transaction too old");
_;
}
}
abstract contract PeripheryValidationExtended is PeripheryValidation {
modifier checkPreviousBlockhash(bytes32 previousBlockhash) {
require(blockhash(block.number - 1) == previousBlockhash, "Blockhash");
_;
}
}
abstract contract MulticallExtended is IMulticallExtended, Multicall, PeripheryValidationExtended {
function multicall(
uint256 deadline,
bytes[] calldata data
) external payable override checkDeadline(deadline) returns (bytes[] memory) {
return multicall(data);
}
function multicall(
bytes32 previousBlockhash,
bytes[] calldata data
) external payable override checkPreviousBlockhash(previousBlockhash) returns (bytes[] memory) {
return multicall(data);
}
}
abstract contract PeripheryImmutableState is IPeripheryImmutableState {
address public immutable override factory;
address public immutable override WETH9;
constructor(address _factory, address _WETH9) {
factory = _factory;
WETH9 = _WETH9;
}
}
abstract contract PeripheryPayments is IPeripheryPayments, PeripheryImmutableState {
receive() external payable {
require(msg.sender == WETH9, "Not WETH9");
}
function unwrapWETH9(uint256 amountMinimum, address recipient) public payable override {
uint256 balanceWETH9 = IWETH9(WETH9).balanceOf(address(this));
require(balanceWETH9 >= amountMinimum, "Insufficient WETH9");
if (balanceWETH9 > 0) {
IWETH9(WETH9).withdraw(balanceWETH9);
TransferHelper.safeTransferETH(recipient, balanceWETH9);
}
}
function sweepToken(address token, uint256 amountMinimum, address recipient) public payable override {
uint256 balanceToken = IERC20(token).balanceOf(address(this));
require(balanceToken >= amountMinimum, "Insufficient token");
if (balanceToken > 0) {
TransferHelper.safeTransfer(token, recipient, balanceToken);
}
}
function refundETH() external payable override {
if (address(this).balance > 0) TransferHelper.safeTransferETH(msg.sender, address(this).balance);
}
function pay(address token, address payer, address recipient, uint256 value) internal {
if (token == WETH9 && address(this).balance >= value) {
IWETH9(WETH9).deposit{ value: value }();
IWETH9(WETH9).transfer(recipient, value);
} else if (payer == address(this)) {
TransferHelper.safeTransfer(token, recipient, value);
} else {
TransferHelper.safeTransferFrom(token, payer, recipient, value);
}
}
}
contract XchangeRouterV3 is IXchangeRouter, Ownable, MulticallExtended {
address public immutable override factory;
address public immutable override WETH;
modifier ensure(uint deadline) {
require(deadline >= block.timestamp, "Xchange: EXPIRED");
_;
}
constructor(address _factory, address _WETH, address _owner) Ownable(_owner) {
factory = _factory;
WETH = _WETH;
}
receive() external payable {
require(msg.sender == WETH);
}
function quote(uint amountA, uint reserveA, uint reserveB) public pure virtual override returns (uint amountB) {
return XchangeLibrary.quote(amountA, reserveA, reserveB);
}
function getAmountOut(
uint amountIn,
uint reserveIn,
uint reserveOut
) public pure virtual override returns (uint amountOut) {
return XchangeLibrary.getAmountOut(amountIn, reserveIn, reserveOut, 20);
}
function getAmountIn(
uint amountOut,
uint reserveIn,
uint reserveOut
) public pure virtual override returns (uint amountIn) {
return XchangeLibrary.getAmountIn(amountOut, reserveIn, reserveOut, 20);
}
function getAmountsOut(
uint amountIn,
address[] memory path
) public view virtual override returns (uint[] memory amounts) {
return XchangeLibrary.getAmountsOut(factory, amountIn, 20, path);
}
function getAmountsIn(
uint amountOut,
address[] memory path
) public view virtual override returns (uint[] memory amounts) {
return XchangeLibrary.getAmountsIn(factory, amountOut, 20, path);
}
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external virtual override ensure(deadline) returns (uint amountA, uint amountB, uint liquidity) {
(amountA, amountB) = _addLiquidity(tokenA, tokenB, amountADesired, amountBDesired, amountAMin, amountBMin);
address pair = XchangeLibrary.pairFor(factory, tokenA, tokenB);
TransferHelper.safeTransferFrom(tokenA, msg.sender, pair, amountA);
TransferHelper.safeTransferFrom(tokenB, msg.sender, pair, amountB);
liquidity = IXchangePair(pair).mint(to);
}
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable virtual override ensure(deadline) returns (uint amountToken, uint amountETH, uint liquidity) {
(amountToken, amountETH) = _addLiquidity(
token,
WETH,
amountTokenDesired,
msg.value,
amountTokenMin,
amountETHMin
);
address pair = XchangeLibrary.pairFor(factory, token, WETH);
TransferHelper.safeTransferFrom(token, msg.sender, pair, amountToken);
IWETH(WETH).deposit{ value: amountETH }();
assert(IWETH(WETH).transfer(pair, amountETH));
liquidity = IXchangePair(pair).mint(to);
if (msg.value > amountETH) TransferHelper.safeTransferETH(msg.sender, msg.value - amountETH);
}
function removeLiquidity(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) public virtual override ensure(deadline) returns (uint amountA, uint amountB) {
address pair = XchangeLibrary.pairFor(factory, tokenA, tokenB);
IXchangePair(pair).transferFrom(msg.sender, pair, liquidity);
(uint amount0, uint amount1) = IXchangePair(pair).burn(to);
(address token0, ) = XchangeLibrary.sortTokens(tokenA, tokenB);
(amountA, amountB) = tokenA == token0 ? (amount0, amount1) : (amount1, amount0);
require(amountA >= amountAMin, "Xchange: INSUFFICIENT_A_AMOUNT");
require(amountB >= amountBMin, "Xchange: INSUFFICIENT_B_AMOUNT");
}
function mustRemoveLiquidity(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline,
uint gasAmount
) public virtual override ensure(deadline) returns (uint amountA, uint amountB) {
require(IXchangeFactory(factory).isFailsafeLiquidator(msg.sender));
address pair = XchangeLibrary.pairFor(factory, tokenA, tokenB);
IXchangePair(pair).transferFrom(msg.sender, pair, liquidity);
(uint amount0, uint amount1) = IXchangePair(pair).mustBurn(to, gasAmount);
(address token0, ) = XchangeLibrary.sortTokens(tokenA, tokenB);
(amountA, amountB) = tokenA == token0 ? (amount0, amount1) : (amount1, amount0);
require(amountA >= amountAMin, "Xchange: INSUFFICIENT_A_AMOUNT");
require(amountB >= amountBMin, "Xchange: INSUFFICIENT_B_AMOUNT");
}
function removeLiquidityETH(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) public virtual override ensure(deadline) returns (uint amountToken, uint amountETH) {
(amountToken, amountETH) = removeLiquidity(
token,
WETH,
liquidity,
amountTokenMin,
amountETHMin,
address(this),
deadline
);
TransferHelper.safeTransfer(token, to, amountToken);
IWETH(WETH).withdraw(amountETH);
TransferHelper.safeTransferETH(to, amountETH);
}
function removeLiquidityWithPermit(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline,
bool approveMax,
uint8 v,
bytes32 r,
bytes32 s
) external virtual override returns (uint amountA, uint amountB) {
address pair = XchangeLibrary.pairFor(factory, tokenA, tokenB);
uint value = approveMax ? type(uint256).max : liquidity;
IXchangePair(pair).permit(msg.sender, address(this), value, deadline, v, r, s);
(amountA, amountB) = removeLiquidity(tokenA, tokenB, liquidity, amountAMin, amountBMin, to, deadline);
}
function removeLiquidityETHWithPermit(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax,
uint8 v,
bytes32 r,
bytes32 s
) external virtual override returns (uint amountToken, uint amountETH) {
address pair = XchangeLibrary.pairFor(factory, token, WETH);
uint value = approveMax ? type(uint256).max : liquidity;
IXchangePair(pair).permit(msg.sender, address(this), value, deadline, v, r, s);
(amountToken, amountETH) = removeLiquidityETH(token, liquidity, amountTokenMin, amountETHMin, to, deadline);
}
function removeLiquidityETHSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) public virtual override ensure(deadline) returns (uint amountETH) {
(, amountETH) = removeLiquidity(token, WETH, liquidity, amountTokenMin, amountETHMin, address(this), deadline);
TransferHelper.safeTransfer(token, to, IERC20(token).balanceOf(address(this)));
IWETH(WETH).withdraw(amountETH);
TransferHelper.safeTransferETH(to, amountETH);
}
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax,
uint8 v,
bytes32 r,
bytes32 s
) external virtual override returns (uint amountETH) {
address pair = XchangeLibrary.pairFor(factory, token, WETH);
uint value = approveMax ? type(uint256).max : liquidity;
IXchangePair(pair).permit(msg.sender, address(this), value, deadline, v, r, s);
amountETH = removeLiquidityETHSupportingFeeOnTransferTokens(
token,
liquidity,
amountTokenMin,
amountETHMin,
to,
deadline
);
}
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external virtual override ensure(deadline) returns (uint[] memory amounts) {
amounts = XchangeLibrary.getAmountsOut(factory, amountIn, 20, path);
require(amounts[amounts.length - 1] >= amountOutMin, "Xchange: INSUFFICIENT_OUTPUT_AMOUNT");
TransferHelper.safeTransferFrom(
path[0],
msg.sender,
XchangeLibrary.pairFor(factory, path[0], path[1]),
amounts[0]
);
_swap(amounts, path, to);
}
function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external virtual override ensure(deadline) returns (uint[] memory amounts) {
amounts = XchangeLibrary.getAmountsIn(factory, amountOut, 20, path);
require(amounts[0] <= amountInMax, "Xchange: EXCESSIVE_INPUT_AMOUNT");
TransferHelper.safeTransferFrom(
path[0],
msg.sender,
XchangeLibrary.pairFor(factory, path[0], path[1]),
amounts[0]
);
_swap(amounts, path, to);
}
function swapExactETHForTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable virtual override ensure(deadline) returns (uint[] memory amounts) {
require(path[0] == WETH, "Xchange: INVALID_PATH");
amounts = XchangeLibrary.getAmountsOut(factory, msg.value, 20, path);
require(amounts[amounts.length - 1] >= amountOutMin, "Xchange: INSUFFICIENT_OUTPUT_AMOUNT");
IWETH(WETH).deposit{ value: amounts[0] }();
assert(IWETH(WETH).transfer(XchangeLibrary.pairFor(factory, path[0], path[1]), amounts[0]));
_swap(amounts, path, to);
}
function swapTokensForExactETH(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external virtual override ensure(deadline) returns (uint[] memory amounts) {
require(path[path.length - 1] == WETH, "Xchange: INVALID_PATH");
amounts = XchangeLibrary.getAmountsIn(factory, amountOut, 20, path);
require(amounts[0] <= amountInMax, "Xchange: EXCESSIVE_INPUT_AMOUNT");
TransferHelper.safeTransferFrom(
path[0],
msg.sender,
XchangeLibrary.pairFor(factory, path[0], path[1]),
amounts[0]
);
_swap(amounts, path, address(this));
IWETH(WETH).withdraw(amounts[amounts.length - 1]);
TransferHelper.safeTransferETH(to, amounts[amounts.length - 1]);
}
function swapExactTokensForETH(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external virtual override ensure(deadline) returns (uint[] memory amounts) {
require(path[path.length - 1] == WETH, "Xchange: INVALID_PATH");
amounts = XchangeLibrary.getAmountsOut(factory, amountIn, 20, path);
require(amounts[amounts.length - 1] >= amountOutMin, "Xchange: INSUFFICIENT_OUTPUT_AMOUNT");
TransferHelper.safeTransferFrom(
path[0],
msg.sender,
XchangeLibrary.pairFor(factory, path[0], path[1]),
amounts[0]
);
_swap(amounts, path, address(this));
IWETH(WETH).withdraw(amounts[amounts.length - 1]);
TransferHelper.safeTransferETH(to, amounts[amounts.length - 1]);
}
function swapETHForExactTokens(
uint amountOut,
address[] calldata path,
address to,
uint deadline
) external payable virtual override ensure(deadline) returns (uint[] memory amounts) {
require(path[0] == WETH, "Xchange: INVALID_PATH");
amounts = XchangeLibrary.getAmountsIn(factory, amountOut, 20, path);
require(amounts[0] <= msg.value, "Xchange: EXCESSIVE_INPUT_AMOUNT");
IWETH(WETH).deposit{ value: amounts[0] }();
assert(IWETH(WETH).transfer(XchangeLibrary.pairFor(factory, path[0], path[1]), amounts[0]));
_swap(amounts, path, to);
if (msg.value > amounts[0]) TransferHelper.safeTransferETH(msg.sender, msg.value - amounts[0]);
}
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external virtual override ensure(deadline) {
TransferHelper.safeTransferFrom(
path[0],
msg.sender,
XchangeLibrary.pairFor(factory, path[0], path[1]),
amountIn
);
uint balanceBefore = IERC20(path[path.length - 1]).balanceOf(to);
_swapSupportingFeeOnTransferTokens(path, to);
require(
IERC20(path[path.length - 1]).balanceOf(to) - balanceBefore >= amountOutMin,
"Xchange: INSUFFICIENT_OUTPUT_AMOUNT"
);
}
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable virtual override ensure(deadline) {
require(path[0] == WETH, "Xchange: INVALID_PATH");
uint amountIn = msg.value;
IWETH(WETH).deposit{ value: amountIn }();
assert(IWETH(WETH).transfer(XchangeLibrary.pairFor(factory, path[0], path[1]), amountIn));
uint balanceBefore = IERC20(path[path.length - 1]).balanceOf(to);
_swapSupportingFeeOnTransferTokens(path, to);
require(
IERC20(path[path.length - 1]).balanceOf(to) - balanceBefore >= amountOutMin,
"Xchange: INSUFFICIENT_OUTPUT_AMOUNT"
);
}
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external virtual override ensure(deadline) {
require(path[path.length - 1] == WETH, "Xchange: INVALID_PATH");
TransferHelper.safeTransferFrom(
path[0],
msg.sender,
XchangeLibrary.pairFor(factory, path[0], path[1]),
amountIn
);
_swapSupportingFeeOnTransferTokens(path, address(this));
uint amountOut = IERC20(WETH).balanceOf(address(this));
require(amountOut >= amountOutMin, "Xchange: INSUFFICIENT_OUTPUT_AMOUNT");
IWETH(WETH).withdraw(amountOut);
TransferHelper.safeTransferETH(to, amountOut);
}
function _addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin
) internal virtual returns (uint amountA, uint amountB) {
if (IXchangeFactory(factory).getPair(tokenA, tokenB) == address(0)) {
IXchangeFactory(factory).createPair(tokenA, tokenB);
}
(uint reserveA, uint reserveB) = XchangeLibrary.getReserves(factory, tokenA, tokenB);
if (reserveA == 0 && reserveB == 0) {
(amountA, amountB) = (amountADesired, amountBDesired);
} else {
uint amountBOptimal = XchangeLibrary.quote(amountADesired, reserveA, reserveB);
if (amountBOptimal <= amountBDesired) {
require(amountBOptimal >= amountBMin, "Xchange: INSUFFICIENT_B_AMOUNT");
(amountA, amountB) = (amountADesired, amountBOptimal);
} else {
uint amountAOptimal = XchangeLibrary.quote(amountBDesired, reserveB, reserveA);
assert(amountAOptimal <= amountADesired);
require(amountAOptimal >= amountAMin, "Xchange: INSUFFICIENT_A_AMOUNT");
(amountA, amountB) = (amountAOptimal, amountBDesired);
}
}
}
function _swap(uint[] memory amounts, address[] memory path, address _to) internal virtual {
for (uint i; i < path.length - 1; i++) {
(address input, address output) = (path[i], path[i + 1]);
(address token0, ) = XchangeLibrary.sortTokens(input, output);
uint amountOut = amounts[i + 1];
(uint amount0Out, uint amount1Out) = input == token0 ? (uint(0), amountOut) : (amountOut, uint(0));
address to = i < path.length - 2 ? XchangeLibrary.pairFor(factory, output, path[i + 2]) : _to;
IXchangePair(XchangeLibrary.pairFor(factory, input, output)).swap(amount0Out, amount1Out, to, new bytes(0));
}
}
function _swapSupportingFeeOnTransferTokens(address[] memory path, address _to) internal virtual {
for (uint i; i < path.length - 1; i++) {
(address input, address output) = (path[i], path[i + 1]);
(address token0, ) = XchangeLibrary.sortTokens(input, output);
IXchangePair pair = IXchangePair(XchangeLibrary.pairFor(factory, input, output));
uint amountInput;
uint amountOutput;
{
(uint reserve0, uint reserve1, ) = pair.getReserves();
(uint reserveInput, uint reserveOutput) = input == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
amountInput = IERC20(input).balanceOf(address(pair)) - reserveInput;
amountOutput = XchangeLibrary.getAmountOut(amountInput, reserveInput, reserveOutput, 20);
}
(uint amount0Out, uint amount1Out) = input == token0 ? (uint(0), amountOutput) : (amountOutput, uint(0));
address to = i < path.length - 2 ? XchangeLibrary.pairFor(factory, output, path[i + 2]) : _to;
pair.swap(amount0Out, amount1Out, to, new bytes(0));
}
}
}
library XchangeLibrary {
function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1) {
require(tokenA != tokenB, "XchangeLibrary: IDENTICAL_ADDRESSES");
(token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), "XchangeLibrary: ZERO_ADDRESS");
}
function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
(address token0, address token1) = sortTokens(tokenA, tokenB);
pair = address(
uint160(
uint(
keccak256(
abi.encodePacked(
hex"ff",
factory,
keccak256(abi.encodePacked(token0, token1)),
hex"d75846df8bac2f946ea9ee78caa53b6812e7514197698275b8322d75e1543193"
)
)
)
)
);
}
function getReserves(
address factory,
address tokenA,
address tokenB
) internal view returns (uint reserveA, uint reserveB) {
(address token0, ) = sortTokens(tokenA, tokenB);
(uint reserve0, uint reserve1, ) = IXchangePair(pairFor(factory, tokenA, tokenB)).getReserves();
(reserveA, reserveB) = tokenA == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
}
function quote(uint amountA, uint reserveA, uint reserveB) internal pure returns (uint amountB) {
require(amountA > 0, "XchangeLibrary: INSUFFICIENT_AMOUNT");
require(reserveA > 0 && reserveB > 0, "XchangeLibrary: INSUFFICIENT_LIQUIDITY");
amountB = (amountA * reserveB) / reserveA;
}
function getAmountOut(
uint amountIn,
uint reserveIn,
uint reserveOut,
uint feeAmount
) internal pure returns (uint amountOut) {
require(amountIn > 0, "XchangeLibrary: INSUFFICIENT_INPUT_AMOUNT");
require(reserveIn > 0 && reserveOut > 0, "XchangeLibrary: INSUFFICIENT_LIQUIDITY");
require(feeAmount <= 20, "XchangeLibrary: EXCESSIVE_FEE");
uint amountInWithFee = amountIn * (10000 - feeAmount);
uint numerator = amountInWithFee * reserveOut;
uint denominator = (reserveIn * 10000) + amountInWithFee;
amountOut = numerator / denominator;
}
function getAmountIn(
uint amountOut,
uint reserveIn,
uint reserveOut,
uint feeAmount
) internal pure returns (uint amountIn) {
require(amountOut > 0, "XchangeLibrary: INSUFFICIENT_OUTPUT_AMOUNT");
require(reserveIn > 0 && reserveOut > 0, "XchangeLibrary: INSUFFICIENT_LIQUIDITY");
require(feeAmount <= 20, "XchangeLibrary: EXCESSIVE_FEE");
uint numerator = reserveIn * amountOut * 10000;
uint denominator = (reserveOut - amountOut) * (10000 - feeAmount);
amountIn = (numerator / denominator) + 1;
}
function getAmountsOut(
address factory,
uint amountIn,
uint feeAmount,
address[] memory path
) internal view returns (uint[] memory amounts) {
require(path.length >= 2, "XchangeLibrary: INVALID_PATH");
amounts = new uint[](path.length);
amounts[0] = amountIn;
for (uint i; i < path.length - 1; i++) {
(uint reserveIn, uint reserveOut) = getReserves(factory, path[i], path[i + 1]);
amounts[i + 1] = getAmountOut(amounts[i], reserveIn, reserveOut, feeAmount);
}
}
function getAmountsIn(
address factory,
uint amountOut,
uint feeAmount,
address[] memory path
) internal view returns (uint[] memory amounts) {
require(path.length >= 2, "XchangeLibrary: INVALID_PATH");
amounts = new uint[](path.length);
amounts[amounts.length - 1] = amountOut;
for (uint i = path.length - 1; i > 0; i--) {
(uint reserveIn, uint reserveOut) = getReserves(factory, path[i - 1], path[i]);
amounts[i - 1] = getAmountIn(amounts[i], reserveIn, reserveOut, feeAmount);
}
}
}
library TransferHelper {
function safeTransfer(address token, address to, uint value) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), "TransferHelper: TRANSFER_FAILED");
}
function safeTransferFrom(address token, address from, address to, uint value) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), "TransferHelper: TRANSFER_FROM_FAILED");
}
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, uint value) internal {
(bool success, ) = to.call{ value: value }(new bytes(0));
require(success, "TransferHelper: ETH_TRANSFER_FAILED");
}
}