编译器
0.8.17+commit.8df45f5f
文件 1 的 17:Address.sol
pragma solidity ^0.8.1;
library Address {
function isContract(address account) internal view returns (bool) {
return account.code.length > 0;
}
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, "Address: low-level call failed");
}
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value
) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function _revert(bytes memory returndata, string memory errorMessage) private pure {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
文件 2 的 17:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
文件 3 的 17:IERC20.sol
pragma solidity ^0.8.0;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, 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 from,
address to,
uint256 amount
) external returns (bool);
}
文件 4 的 17:ISwapRouter.sol
pragma solidity >=0.7.5;
pragma abicoder v2;
import '@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol';
interface ISwapRouter is IUniswapV3SwapCallback {
struct ExactInputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
uint160 sqrtPriceLimitX96;
}
function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);
struct ExactInputParams {
bytes path;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
}
function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut);
struct ExactOutputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 deadline;
uint256 amountOut;
uint256 amountInMaximum;
uint160 sqrtPriceLimitX96;
}
function exactOutputSingle(ExactOutputSingleParams calldata params) external payable returns (uint256 amountIn);
struct ExactOutputParams {
bytes path;
address recipient;
uint256 deadline;
uint256 amountOut;
uint256 amountInMaximum;
}
function exactOutput(ExactOutputParams calldata params) external payable returns (uint256 amountIn);
}
文件 5 的 17:IUniswapV2Factory.sol
pragma solidity >=0.5.0;
interface IUniswapV2Factory {
event PairCreated(address indexed token0, address indexed token1, address pair, uint);
function feeTo() external view returns (address);
function feeToSetter() external view returns (address);
function getPair(address tokenA, address tokenB) external view returns (address pair);
function allPairs(uint) external view returns (address pair);
function allPairsLength() external view returns (uint);
function createPair(address tokenA, address tokenB) external returns (address pair);
function setFeeTo(address) external;
function setFeeToSetter(address) external;
}
文件 6 的 17:IUniswapV2Pair.sol
pragma solidity >=0.5.0;
interface IUniswapV2Pair {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
function name() external pure returns (string memory);
function symbol() external pure returns (string memory);
function decimals() external pure returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);
function DOMAIN_SEPARATOR() external view returns (bytes32);
function PERMIT_TYPEHASH() external pure returns (bytes32);
function nonces(address owner) external view returns (uint);
function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;
event Mint(address indexed sender, uint amount0, uint amount1);
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
event Swap(
address indexed sender,
uint amount0In,
uint amount1In,
uint amount0Out,
uint amount1Out,
address indexed to
);
event Sync(uint112 reserve0, uint112 reserve1);
function MINIMUM_LIQUIDITY() external pure returns (uint);
function factory() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function price0CumulativeLast() external view returns (uint);
function price1CumulativeLast() external view returns (uint);
function kLast() external view returns (uint);
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 skim(address to) external;
function sync() external;
function initialize(address, address) external;
}
文件 7 的 17:IUniswapV2Router01.sol
pragma solidity >=0.6.2;
interface IUniswapV2Router01 {
function factory() external pure returns (address);
function WETH() external pure 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 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);
}
文件 8 的 17:IUniswapV2Router02.sol
pragma solidity >=0.6.2;
import './IUniswapV2Router01.sol';
interface IUniswapV2Router02 is IUniswapV2Router01 {
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;
}
文件 9 的 17:IUniswapV3SwapCallback.sol
pragma solidity >=0.5.0;
interface IUniswapV3SwapCallback {
function uniswapV3SwapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata data
) external;
}
文件 10 的 17:IWETH.sol
pragma solidity 0.8.17;
interface IWETH {
function name() external view returns (string memory);
function approve(address guy, uint256 wad) external returns (bool);
function totalSupply() external view returns (uint256);
function transferFrom(
address src,
address dst,
uint256 wad
) external returns (bool);
function withdraw(uint256 wad) external;
function decimals() external view returns (uint8);
function balanceOf(address) external view returns (uint256);
function symbol() external view returns (string memory);
function transfer(address dst, uint256 wad) external returns (bool);
function deposit() external payable;
function allowance(address, address) external view returns (uint256);
receive() external payable;
event Approval(address indexed src, address indexed guy, uint256 wad);
event Transfer(address indexed src, address indexed dst, uint256 wad);
event Deposit(address indexed dst, uint256 wad);
event Withdrawal(address indexed src, uint256 wad);
}
文件 11 的 17:InternetMoneySwapRouter.sol
pragma solidity ^0.8.17;
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "./OracleReader.sol";
import "./interfaces/IWETH.sol";
import "./Migratable.sol";
import "./Utils.sol";
contract InternetMoneySwapRouter is Ownable, Migratable, Utils, OracleReader {
using Address for address payable;
using SafeERC20 for IERC20;
struct Dex {
uint64 id;
address router;
bool disabled;
string name;
}
Dex[] public dexInfo;
error DexConflict(uint256 dexIndex);
error DexDisabled();
error NativeMissing(uint256 pathIndex);
error FeeMissing(uint256 expected, uint256 provided, string message);
error DestinationMissing();
function allDexInfo() public view returns(
Dex[] memory _dexInfo,
address _wNative,
address _destination,
uint256 _fee,
uint256 _feeDenominator
) {
_dexInfo = dexInfo;
_wNative = wNative;
_destination = destination;
_fee = fee;
_feeDenominator = feeDenominator;
}
address payable public immutable destination;
mapping(address => uint256) public routerToDex;
event AddDex(address indexed executor, uint256 indexed dexId);
event UpdateDex(address indexed executor, uint256 indexed dexId);
constructor(
address payable _destination,
address payable _wNative,
uint96 _fee
) OracleReader(_wNative, _destination == address(0) ? 0 : _fee) {
destination = _destination;
}
receive() external payable {
if (destination == address(0)) {
revert DestinationMissing();
}
}
function addDex(
string calldata _dexName,
address _router
) external payable onlyOwner {
uint256 id = dexInfo.length;
dexInfo.push(Dex({
name: _dexName,
router: _router,
id: uint64(id),
disabled: false
}));
if (routerToDex[_router] != 0) {
revert DexConflict(routerToDex[_router]);
}
routerToDex[_router] = dexInfo.length;
emit AddDex(msg.sender, id);
}
function updateDex(
uint256 id,
string memory _name
) external payable onlyOwner {
if (bytes(_name).length == 0) {
return;
}
dexInfo[id].name = _name;
emit UpdateDex(msg.sender, id);
}
function disableDex(uint256 id, bool disabled) external payable onlyOwner {
if (dexInfo[id].disabled == disabled) {
return;
}
dexInfo[id].disabled = disabled;
emit UpdateDex(msg.sender, id);
}
function distributeAll(uint256 amount) external payable {
(uint256 nativeBalance, uint256 wNativeBalance) = pendingDistributionSegmented();
if ((amount == 0 || nativeBalance < amount) && wNativeBalance > 0) {
IWETH(wNative).withdraw(wNativeBalance);
}
amount = clamp(amount, nativeBalance + wNativeBalance);
if (amount == 0) {
return;
}
destination.sendValue(amount);
}
function distribute(uint256 amount) external payable {
uint256 balance = address(this).balance;
if (balance == 0) {
return;
}
destination.sendValue(clamp(amount, balance));
}
function pendingDistributionSegmented() public view returns(uint256, uint256) {
return (address(this).balance, IWETH(wNative).balanceOf(address(this)));
}
function pendingDistribution() public view returns(uint256) {
(uint256 nativeBalance, uint256 wNativeBalance) = pendingDistributionSegmented();
return nativeBalance + wNativeBalance;
}
function collectFunds(address sourceTokenId, uint256 amountIn) internal returns(uint256) {
address self = address(this);
uint256 balanceBefore = IERC20(sourceTokenId).balanceOf(self);
IERC20(sourceTokenId).safeTransferFrom(_msgSender(), self, amountIn);
return IERC20(sourceTokenId).balanceOf(self) - balanceBefore;
}
function swapTokenV2(
uint256 _dexId,
address recipient,
address[] calldata _path,
uint256 _amountIn,
uint256 _minAmountOut,
uint256 _deadline
) external payable {
address first = _path[0];
address last = _path[_path.length - 1];
address _wNative = wNative;
uint256 nativeFee = 0;
if (first == _wNative) {
nativeFee = (_amountIn * fee) / feeDenominator;
if (msg.value != 0) {
revert FeeMissing(0, msg.value, "fees paid from input");
}
} else if (last != _wNative && fee > 0) {
(, uint256 minimum) = getFeeMinimum(
IUniswapV2Router02(dexInfo[_dexId].router).factory(),
_amountIn,
_path
);
minimum = (minimum * 9) / 10;
if (minimum == 0) {
revert FeeMissing(0, msg.value, "unable to compute fees");
}
if (msg.value < minimum) {
revert FeeMissing(minimum, msg.value, "not enough fee value");
}
}
uint256 actualAmountIn = collectFunds(first, _amountIn) - nativeFee;
uint256 actualAmountOut = swapExactTokenForTokenV2(
_dexId,
_path,
actualAmountIn,
_minAmountOut,
_deadline
);
uint256 actualAmountOutAfterFees = actualAmountOut;
if (last == _wNative) {
actualAmountOutAfterFees -= (actualAmountOut * fee) / feeDenominator;
if (fee != 0 && msg.value != 0) {
revert FeeMissing(0, msg.value, "fees paid from output");
}
}
IERC20(last).safeTransfer(recipient, actualAmountOutAfterFees);
}
function swapNativeToV2(
uint256 _dexId,
address recipient,
address[] calldata _path,
uint256 _amountIn,
uint256 _minAmountOut,
uint256 _deadline
) external payable {
uint256 minimal = (msg.value * fee) / feeDenominator;
address payable _wNative = wNative;
if (msg.value != _amountIn + minimal) {
revert FeeMissing(_amountIn + minimal, msg.value, "amount + fees must = total");
}
if (_path[0] != _wNative) {
revert NativeMissing(0);
}
IWETH(_wNative).deposit{value: _amountIn}();
uint256 actualAmountOut = swapExactTokenForTokenV2(_dexId, _path, _amountIn, _minAmountOut, _deadline);
IERC20(_path[_path.length - 1]).safeTransfer(recipient, actualAmountOut);
}
function swapToNativeV2(
uint256 _dexId,
address payable recipient,
address[] calldata _path,
uint256 _amountIn,
uint256 _minAmountOut,
uint256 _deadline
) external payable {
address payable _wNative = wNative;
if (_path[_path.length - 1] != _wNative) {
revert NativeMissing(_path.length - 1);
}
uint256 actualAmountIn = collectFunds(_path[0], _amountIn);
uint256 actualAmountOut = swapExactTokenForTokenV2(_dexId, _path, actualAmountIn, _minAmountOut, _deadline);
uint256 minimal = (actualAmountOut * fee) / feeDenominator;
uint256 actualAmountOutAfterFee = actualAmountOut - minimal;
if (actualAmountOut > 0) {
IWETH(_wNative).withdraw(actualAmountOut);
}
recipient.sendValue(actualAmountOutAfterFee);
}
function swapExactTokenForTokenV2(
uint256 dexId,
address[] calldata _path,
uint256 _amountIn,
uint256 _minAmountOut,
uint256 _deadline
) internal returns (uint256) {
Dex memory dex = dexInfo[dexId];
if (dex.disabled) {
revert DexDisabled();
}
address last = _path[_path.length - 1];
IERC20(_path[0]).approve(dex.router, _amountIn);
uint256 balanceBefore = IERC20(last).balanceOf(address(this));
IUniswapV2Router02(dex.router).swapExactTokensForTokensSupportingFeeOnTransferTokens(
_amountIn,
_minAmountOut,
_path,
address(this),
_deadline
);
return IERC20(last).balanceOf(address(this)) - balanceBefore;
}
}
文件 12 的 17:Migratable.sol
pragma solidity ^0.8.17;
import "@openzeppelin/contracts/access/Ownable.sol";
abstract contract Migratable is Ownable {
address[] public migrationAddresses;
event UpdateMigrationAddresses(address indexed executor, address[] addresses);
function setMigrationAddress(address[] calldata _migrationAddresses) external onlyOwner {
migrationAddresses = _migrationAddresses;
emit UpdateMigrationAddresses(_msgSender(), _migrationAddresses);
}
}
文件 13 的 17:OracleReader.sol
pragma solidity ^0.8.17;
import "@openzeppelin/contracts/utils/Address.sol";
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol";
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol";
abstract contract OracleReader {
using Address for address;
uint256 public constant feeDenominator = 100_000;
uint256 public immutable fee;
address payable public immutable wNative;
constructor(address payable _wNative, uint96 _fee) {
wNative = _wNative;
fee = _fee;
}
function getFeeMinimum(
address factory,
uint256 _amountIn,
address[] calldata path
) public view returns(address, uint256) {
for (uint256 i = 0; i < path.length; ++i) {
address target = path[i];
uint256 minimum = getSingleFeeMinimum(factory, _amountIn, target);
if (minimum > 0) {
return (target, minimum);
}
if (i != path.length - 1) {
_amountIn = amountOutFrom(factory, target, path[i + 1], _amountIn);
}
}
return (address(0), 0);
}
function amountOutFrom(address factory, address tokenIn, address tokenOut, uint256 amountIn) public view returns(uint256) {
address pair = IUniswapV2Factory(factory).getPair(tokenIn, tokenOut);
bool inIs0 = IUniswapV2Pair(pair).token0() == tokenIn;
(uint256 reserveA, uint256 reserveB, ) = IUniswapV2Pair(pair).getReserves();
if (inIs0) {
return (reserveB * amountIn) / reserveA;
}
return (reserveA * amountIn) / reserveB;
}
function getSingleFeeMinimum(
address factory,
uint256 _amountIn,
address source
) public view returns(uint256 minimum) {
(uint256 tokenAmount, uint256 wethAmount) = checkPairForValidPrice(factory, source);
if (wethAmount > 1 ether) {
return (_amountIn * wethAmount * fee) / (feeDenominator * tokenAmount);
}
}
function checkPairForValidPrice(address factory, address token) public view returns(uint256 tokenReserve, uint256 wethReserve) {
IUniswapV2Factory uniFactory = IUniswapV2Factory(factory);
address _wNative = wNative;
address pair = uniFactory.getPair(token, _wNative);
if (pair.isContract()) {
(uint256 _reserveA, uint256 _reserveB, ) = IUniswapV2Pair(pair).getReserves();
address token0 = IUniswapV2Pair(pair).token0();
bool zeroIsWeth = token0 == _wNative;
tokenReserve = zeroIsWeth ? _reserveB : _reserveA;
wethReserve = zeroIsWeth ? _reserveA : _reserveB;
}
}
}
文件 14 的 17:Ownable.sol
pragma solidity ^0.8.0;
import "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() {
_transferOwnership(_msgSender());
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "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);
}
}
文件 15 的 17:SafeERC20.sol
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../extensions/draft-IERC20Permit.sol";
import "../../../utils/Address.sol";
library SafeERC20 {
using Address for address;
function safeTransfer(
IERC20 token,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(
IERC20 token,
address from,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(
IERC20 token,
address spender,
uint256 value
) internal {
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
function safePermit(
IERC20Permit token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
文件 16 的 17:Utils.sol
pragma solidity ^0.8.17;
abstract contract Utils {
function clamp(uint256 amount, uint256 max) pure public returns(uint256) {
uint256 min = amount < max ? amount : max;
return min == 0 ? max : min;
}
}
文件 17 的 17:draft-IERC20Permit.sol
pragma solidity ^0.8.0;
interface IERC20Permit {
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
function nonces(address owner) external view returns (uint256);
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
{
"compilationTarget": {
"contracts/InternetMoneySwapRouter.sol": "InternetMoneySwapRouter"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 1000
},
"remappings": []
}
[{"inputs":[{"internalType":"address payable","name":"_destination","type":"address"},{"internalType":"address payable","name":"_wNative","type":"address"},{"internalType":"uint96","name":"_fee","type":"uint96"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"DestinationMissing","type":"error"},{"inputs":[{"internalType":"uint256","name":"dexIndex","type":"uint256"}],"name":"DexConflict","type":"error"},{"inputs":[],"name":"DexDisabled","type":"error"},{"inputs":[{"internalType":"uint256","name":"expected","type":"uint256"},{"internalType":"uint256","name":"provided","type":"uint256"},{"internalType":"string","name":"message","type":"string"}],"name":"FeeMissing","type":"error"},{"inputs":[{"internalType":"uint256","name":"pathIndex","type":"uint256"}],"name":"NativeMissing","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"executor","type":"address"},{"indexed":true,"internalType":"uint256","name":"dexId","type":"uint256"}],"name":"AddDex","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"executor","type":"address"},{"indexed":true,"internalType":"uint256","name":"dexId","type":"uint256"}],"name":"UpdateDex","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"executor","type":"address"},{"indexed":false,"internalType":"address[]","name":"addresses","type":"address[]"}],"name":"UpdateMigrationAddresses","type":"event"},{"inputs":[{"internalType":"string","name":"_dexName","type":"string"},{"internalType":"address","name":"_router","type":"address"}],"name":"addDex","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"allDexInfo","outputs":[{"components":[{"internalType":"uint64","name":"id","type":"uint64"},{"internalType":"address","name":"router","type":"address"},{"internalType":"bool","name":"disabled","type":"bool"},{"internalType":"string","name":"name","type":"string"}],"internalType":"struct InternetMoneySwapRouter.Dex[]","name":"_dexInfo","type":"tuple[]"},{"internalType":"address","name":"_wNative","type":"address"},{"internalType":"address","name":"_destination","type":"address"},{"internalType":"uint256","name":"_fee","type":"uint256"},{"internalType":"uint256","name":"_feeDenominator","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"factory","type":"address"},{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"uint256","name":"amountIn","type":"uint256"}],"name":"amountOutFrom","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"factory","type":"address"},{"internalType":"address","name":"token","type":"address"}],"name":"checkPairForValidPrice","outputs":[{"internalType":"uint256","name":"tokenReserve","type":"uint256"},{"internalType":"uint256","name":"wethReserve","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"max","type":"uint256"}],"name":"clamp","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"destination","outputs":[{"internalType":"address payable","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"dexInfo","outputs":[{"internalType":"uint64","name":"id","type":"uint64"},{"internalType":"address","name":"router","type":"address"},{"internalType":"bool","name":"disabled","type":"bool"},{"internalType":"string","name":"name","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"bool","name":"disabled","type":"bool"}],"name":"disableDex","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"distribute","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"distributeAll","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"fee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"feeDenominator","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"factory","type":"address"},{"internalType":"uint256","name":"_amountIn","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getFeeMinimum","outputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"factory","type":"address"},{"internalType":"uint256","name":"_amountIn","type":"uint256"},{"internalType":"address","name":"source","type":"address"}],"name":"getSingleFeeMinimum","outputs":[{"internalType":"uint256","name":"minimum","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"migrationAddresses","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingDistribution","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingDistributionSegmented","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"routerToDex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"_migrationAddresses","type":"address[]"}],"name":"setMigrationAddress","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_dexId","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"address[]","name":"_path","type":"address[]"},{"internalType":"uint256","name":"_amountIn","type":"uint256"},{"internalType":"uint256","name":"_minAmountOut","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"}],"name":"swapNativeToV2","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_dexId","type":"uint256"},{"internalType":"address payable","name":"recipient","type":"address"},{"internalType":"address[]","name":"_path","type":"address[]"},{"internalType":"uint256","name":"_amountIn","type":"uint256"},{"internalType":"uint256","name":"_minAmountOut","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"}],"name":"swapToNativeV2","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_dexId","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"address[]","name":"_path","type":"address[]"},{"internalType":"uint256","name":"_amountIn","type":"uint256"},{"internalType":"uint256","name":"_minAmountOut","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"}],"name":"swapTokenV2","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"string","name":"_name","type":"string"}],"name":"updateDex","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"wNative","outputs":[{"internalType":"address payable","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]