Verified contractExactUNLICENSED
UniswapFlashCalculate
Address0xebbf9d920ca38ec90181d40aa7e623b5a2807887
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.6.12
- Same code
Only this contract
Details
- Compiler
- solc 0.6.12+commit.27d51765
- Language
- solidity
- Optimization
- No
- EVM version
- istanbul
- License
- UNLICENSED
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 13,287,449
- Creation transaction
- 0x6f4be67d11…6a27880fd4
contracts/UniswapV2FlashCalculate.sol
//SPDX-License-Identifier: UNLICENSED
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
interface IERC20 {
function balanceOf(address owner) external view returns (uint);
function transfer(address to, uint value) external returns (bool);
}
interface IUniswapV2Pair {
function token0() external view returns (address);
function token1() external view returns (address);
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
}
interface IUniswapV2Router {
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);
}
interface IUniswapV2Factory {
function getPair(address tokenA, address tokenB) external view returns (address pair);
}
// In order to quickly load up data from Uniswap-like market, this contract allows easy iteration with a single eth_call
contract UniswapFlashCalculate {
address private owner;
IUniswapV2Router public immutable UniswapV2Router;
IUniswapV2Factory public immutable UniswapV2Factory;
address private bundleExecutorAddress;
uint16 private exact = 1000;
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
constructor(address _bundleExecutorAddress) public payable {
owner = msg.sender;
bundleExecutorAddress = _bundleExecutorAddress;
UniswapV2Router = IUniswapV2Router(address(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D));
UniswapV2Factory = IUniswapV2Factory(address(0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f));
}
// 设置打包合约的地址
function setBundleExecutorAddress(address _address) internal onlyOwner {
bundleExecutorAddress = _address;
}
// 设置精确度,默认为100,建议不要超过1000
function setExact(uint16 _num) internal onlyOwner {
exact = _num;
}
// V2判断Token0、Token1
function V2GetPairToken(address[] memory path) internal view returns (address pair, bool isToken1, address path0, address path1){
path0 = path[path.length - 2];
path1 = path[path.length - 1];
pair = UniswapV2Factory.getPair(path0, path1);
isToken1 = IUniswapV2Pair(pair).token1() == path1 ? true : false;
}
// V2
function V2GetAmountsOutByStepPath(uint256 amountIn, address[] memory path) internal view returns (uint256[] memory amounts){
amounts = UniswapV2Router.getAmountsOut(amountIn, path);
}
function V2GetAmountOut(uint256 amountIn, uint256 reserveA, uint256 reserveB) internal view returns (uint256 amountOut){
amountOut = UniswapV2Router.getAmountOut(amountIn, reserveB, reserveA);
}
// 路径反转,计算利润
function reversePathAndRemoveLast(address[] memory path) internal pure returns (address[] memory){
address[] memory newPath;
uint16 n = 0;
for (uint i = path.length; i > 1; i--) {
newPath[n] = path[i - 2];
n++;
}
return newPath;
}
// 计算利润
// @returns:
// @maxAmountBuy 最大购买额度
// @finallyAmountOutOfBot 最终可获得的Token数量
// @finallyAmountOutOfUser 对手最终可获得Token数量
// @profit 利润
function V2CalculateMaxAmount(uint256 amountInOfUser, uint256 amountOutMin, address[] memory userPath, address[] memory minePath) external view returns (
uint256 maxAmountBuy,
uint256 finallyAmountOutOfBot,
uint256 finallyAmountOutOfUser,
int256 profit,
uint256 step
) {
require(userPath.length > 1, "Opponent path length error");
// uint256 ETHBalance = WETH.balanceOf(address(this));
// 判断Pair里面的 Token0 是否为需要购买的Token;主要用来区别 reserves 位置
(address pair,bool isToken1, ,) = V2GetPairToken(userPath);
// 将合约的WETH余额分成100等份,然后按照各个梯度进行模拟购买;从而判断最大购买值
// uint256 step = uint256(1000e18 / 1000);
step = uint256(IERC20(minePath[0]).balanceOf(bundleExecutorAddress) / exact);
require(step > 0, "Path Insufficient balance");
// 如果对方是复合路径,则需要重新计算amountInOfUser;新的amountInOfUser为复合路径的倒数第二个值;
if (userPath.length > 2) {
uint256[] memory opponentAmounts = V2GetAmountsOutByStepPath(amountInOfUser, userPath);
amountInOfUser = opponentAmounts[opponentAmounts.length - 2];
}
// 交易对库存
(uint256 reserveA, uint256 reserveB,) = IUniswapV2Pair(pair).getReserves();
// ============计算最终购买数量、用户最终购买数量、机器人最终获得Token数量=================
(maxAmountBuy, finallyAmountOutOfBot, finallyAmountOutOfUser) = _V2CalculateMaxAmount(amountInOfUser, amountOutMin, reserveA, reserveB, step, minePath, isToken1);
// 用户实际可买到的Token数量 amountOut[i-1],用户实际支付的WETH数量:amountInOfUser
// 机器人干预的买入数量:maxAmountBuy,机器人干预后实际买到的Token数量:
// 计算毛利 profit ,此处不考虑机器人复合path的情况
profit = 0;
if (maxAmountBuy > 0) {
uint256 amountOutOfIn = _V2CalculateProfit(amountInOfUser, maxAmountBuy, finallyAmountOutOfBot, finallyAmountOutOfUser, reserveA, reserveB, isToken1);
// 处理机器人购买路径为复合路径的情况下计算有效利润。
if (amountOutOfIn > 0 && amountOutOfIn > maxAmountBuy && minePath.length > 2) {
profit = int256(V2GetAmountsOutByStepPath(amountOutOfIn - maxAmountBuy, reversePathAndRemoveLast(minePath))[0]);
} else {
profit = int256(amountOutOfIn) - int256(maxAmountBuy);
}
}
}
// 如果用户按照精确购买Token数量购买
function V2CalculateMaxAmountByAmountInMax(uint256 amountInMax, address[] calldata userPath, address[] calldata minePath) external view returns (
uint256 maxAmountBuy,
uint256 finallyAmountOutOfBot,
uint256 finallyAmountOutOfUser,
int256 profit,
uint256 step
) {
uint256[] memory amounts = UniswapV2Router.getAmountsOut(amountInMax, userPath);
(maxAmountBuy, finallyAmountOutOfBot, finallyAmountOutOfUser, profit, step) = this.V2CalculateMaxAmount(amountInMax, amounts[amounts.length - 1], userPath, minePath);
}
// 利润计算器核心
function _V2CalculateMaxAmount(uint256 amountInOfUser, uint256 amountOutMin, uint256 reserveA, uint256 reserveB, uint256 step, address[] memory minePath, bool isToken1) internal view returns (
uint256 maxAmountBuy,
uint256 finallyAmountOutOfBot,
uint256 finallyAmountOutOfUser
){
uint16 i = 1;
uint256 amountOut;
finallyAmountOutOfUser = 0;
finallyAmountOutOfBot = 0;
maxAmountBuy = 0;
for (; i <= exact; i++) {
uint256[] memory amounts = V2GetAmountsOutByStepPath(step * i, minePath);
if (isToken1) {
// 如果Token1对应流出池子的代币,那么reserveB则为代币的库存(Reserve),reserveA 则为主流货币的库存(reserve)
// amounts 倒数第二个为机器人干预后买入的主流代币数量;倒数第一个为机器人干预后得到的Token数量;
if (reserveB <= amounts[amounts.length - 1]) {
continue;
}
// 参数1:流入池子的主流货币数量
// 参数2:对应流出池子的代币(reserveB)
// 参数3:对应流入池子的主流货币(reserveA)
amountOut = V2GetAmountOut(
amountInOfUser,
reserveB - amounts[amounts.length - 1],
reserveA + amounts[amounts.length - 2]
);
} else {
// 如果Token0对应流出池子的代币,那么reserveA 则为代币的库存(Reserve),reserveB 则为主流货币的库存(reserve)
// amounts 倒数第二个为机器人干预后买入的主流代币数量;倒数第一个为机器人干预后得到的Token数量;
if (reserveA <= amounts[amounts.length - 1]) {
continue;
}
// 参数1:流入池子的主流货币数量
// 参数2:对应流出池子的代币(reserveA)
// 参数3:对应流入池子的主流货币(reserveB)
amountOut = V2GetAmountOut(
amountInOfUser,
reserveA - amounts[amounts.length - 1],
reserveB + amounts[amounts.length - 2]
);
}
// 从小量购买不断网上增加购买额度,如果机器人干预后的实际 amountOut < 用户能接受的最小值(amountOutMin),那么 i-1 则为最大购买量
if (amountOut < amountOutMin) {
break;
} else {
// 如果 amountOut 小于 amountOutMin
// 用户最终能购买的Token数量
finallyAmountOutOfUser = amountOut;
// 机器人最大购买量:WETH
maxAmountBuy = step * i;
// 机器人可获得最大Token数量
finallyAmountOutOfBot = amounts[amounts.length - 1];
}
}
}
function _V2CalculateProfit(uint256 amountInOfUser, uint256 maxAmountBuy, uint256 finallyAmountOutOfBot, uint256 finallyAmountOutOfUser, uint256 reserveA, uint256 reserveB, bool isToken1) internal view returns (uint256 amountOutOfIn){
amountOutOfIn = 0;
if (isToken1) {
if (reserveB > finallyAmountOutOfBot + finallyAmountOutOfUser) {
amountOutOfIn = V2GetAmountOut(
finallyAmountOutOfBot,
reserveA + maxAmountBuy + amountInOfUser,
reserveB - finallyAmountOutOfBot - finallyAmountOutOfUser
);
}
} else {
if (reserveA > finallyAmountOutOfBot + finallyAmountOutOfUser) {
amountOutOfIn = V2GetAmountOut(
finallyAmountOutOfBot,
reserveB + maxAmountBuy + amountInOfUser,
reserveA - finallyAmountOutOfBot - finallyAmountOutOfUser
);
}
}
}
// 提取合约内部的Token
function WithdrawToken(IERC20 token, uint256 amount) public onlyOwner {
require(token.balanceOf(address(this)) >= amount, "Insufficient balance");
token.transfer(msg.sender, amount);
}
// 转移所有者
function transferOwnership(address newOwner) onlyOwner public {
if (newOwner != address(0)) {
owner = newOwner;
}
}
// 提取ETH
function withdraw(uint256 amount) onlyOwner public {
require(address(this).balance >= amount, "Insufficient balance");
msg.sender.transfer(amount);
amount = 0;
}
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs"
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": false
},
"evmVersion": "istanbul",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.