Verified contractExactMIT
UniversalArb
Address0xcd837040f18d3cbb1fd49fd748c83017dbc735e7
- Balance
0.015781 ETH
- Transactions
2
- Compiler
solc 0.8.24
- Same code
Only this contract
Details
- Compiler
- solc 0.8.24+commit.e11b9ed9
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,329,984
- Creation transaction
- 0x0e8fb79be2…fa83cefed1
src/contracts/UniversalArb.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
interface IERC20 {
function balanceOf(address account) external view returns (uint256);
}
interface IWETH is IERC20 {
function withdraw(uint256 wad) external;
}
interface IUniswapV2Pool {
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function swap(uint256 amount0Out, uint256 amount1Out, address to, bytes calldata data) external;
}
interface IUniswapV3Pool {
function token0() external view returns (address);
function token1() external view returns (address);
function swap(
address recipient,
bool zeroForOne,
int256 amountSpecified,
uint160 sqrtPriceLimitX96,
bytes calldata data
) external returns (int256 amount0Delta, int256 amount1Delta);
}
interface IBalancerVault {
function flashLoan(
address receiver,
address[] memory tokens,
uint256[] memory amounts,
bytes memory userData
) external;
}
contract UniversalArb {
// Non-immutable for slot 0x0 stateOverride testing compatibility
address public immutable owner;
address public constant BALANCER_VAULT = 0xBA12222222228d8Ba445958a75a0704d566BF2C8;
address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
// Uniswap V3 Math Constants
uint112 private constant MIN_SQRT_RATIO = 4295128739;
uint160 private constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;
// Transient slot tracking current active target pool to secure V3 callbacks
address private currentActivePool;
bool public constant DEBUG_MODE = true;
event ArbExecuted(
address indexed tokenBase,
uint256 amountIn,
uint256 grossProfit,
uint256 netProfit,
uint256 builderTip,
uint256 minTokenProfit
);
struct ArbParams {
address pool1;
address pool2;
address pool3;
address tokenBase;
address tokenInt1;
address tokenInt2;
bool isV2_1;
bool isV2_2;
bool isV2_3;
uint256 amountIn;
uint256 builderTip;
uint256 minTokenProfit; // NEW: Token-equivalent profit required to cover the bribe
}
// =========================================================================
// GAS-OPTIMIZED CUSTOM ERRORS
// =========================================================================
error NotOwner();
error UnauthorizedCallback();
error InsufficientBalanceDelta(int256 realizedProfit, uint256 requiredTip);
error TransferFailed();
error InsufficientETHForBribe();
modifier onlyOwner() {
if (msg.sender != owner) revert NotOwner();
_;
}
constructor() {
owner = msg.sender;
}
receive() external payable {}
/**
* @notice Primary execution gateway utilizing off-chain derived calldata
*/
function executeArbTriangle(ArbParams calldata params) external onlyOwner {
address[] memory tokens = new address[](1);
tokens[0] = params.tokenBase;
uint256[] memory amounts = new uint256[](1);
amounts[0] = params.amountIn;
bytes memory userData = abi.encode(params);
IBalancerVault(BALANCER_VAULT).flashLoan(address(this), tokens, amounts, userData);
}
/**
* @notice Balancer V2 Vault Callback execution sequence with Multi-Asset Profit Verification
*/
function receiveFlashLoan(
address[] memory tokens,
uint256[] memory /* amounts */,
uint256[] memory feeAmounts,
bytes memory userData
) external {
if (msg.sender != BALANCER_VAULT) revert UnauthorizedCallback();
ArbParams memory params = abi.decode(userData, (ArbParams));
address tokenBase = tokens[0];
//ADDED: Basic sanity checks improves robustness and prevents accidental mis‑configuration
require(params.amountIn > 0, "amountIn zero");
require(params.pool1 != address(0) && params.pool2 != address(0) && params.pool3 != address(0), "invalid pool");
require(params.tokenBase != address(0) && params.tokenInt1 != address(0) && params.tokenInt2 != address(0), "invalid token");
// 1. SNAPSHOT BALANCE BEFORE SWAPS
uint256 balanceBefore = IERC20(tokenBase).balanceOf(address(this)); //What is in the contract + amountIn
// --- HOP 1 ---
uint256 out1 = _swap(params.pool1, params.tokenBase, params.tokenInt1, params.amountIn, params.isV2_1);
// --- HOP 2 ---
uint256 out2 = _swap(params.pool2, params.tokenInt1, params.tokenInt2, out1, params.isV2_2);
// --- HOP 3 ---
_swap(params.pool3, params.tokenInt2, params.tokenBase, out2, params.isV2_3);
// 2. SNAPSHOT BALANCE AFTER SWAPS
uint256 balanceAfter = IERC20(tokenBase).balanceOf(address(this));
// 3. CALCULATE ON-CHAIN BALANCE DELTA
// balanceBefore is measured after the flashloan has already been received
if (balanceBefore < params.amountIn) {
revert InsufficientBalanceDelta(int256(0), params.builderTip);
}
// This is the balance that the contract had right after receiving the
// flash‑loan but before the input amount (params.amountIn) was taken out for the trade.
uint256 preLoanBalance = balanceBefore - params.amountIn;
uint256 totalOwed = params.amountIn + feeAmounts[0];
// Cast to int256 so the custom error can output negative values for losses
int256 realizedProfit = (int256(balanceAfter) - int256(preLoanBalance)) - int256(totalOwed);
// 4. MULTI-ASSET PROFIT & TIP VERIFICATION
uint256 requiredProfitOffset = (tokenBase == WETH) ? params.builderTip : params.minTokenProfit;
//Revert if grossProfit is less than or equal to zero, meaning the trade was unprofitable
if (balanceAfter <= preLoanBalance) {
revert InsufficientBalanceDelta(realizedProfit, requiredProfitOffset); //????????????
}
// Compute gross profit safely and enforce the correct profit offset.
//Prevents under‑flow by using a ternary operator.
uint256 grossProfit = balanceAfter > preLoanBalance ? balanceAfter - preLoanBalance : 0;
if (grossProfit < totalOwed + requiredProfitOffset) {
revert InsufficientBalanceDelta(realizedProfit, requiredProfitOffset);
}
// If using a non-WETH base asset, verify the contract holds enough native ETH for the bribe
if (tokenBase != WETH && params.builderTip > 0 && address(this).balance < params.builderTip) {
revert InsufficientETHForBribe();
}
// 5. REPAY LOAN PRINCIPAL + FEES TO BALANCER
_safeTransfer(tokenBase, BALANCER_VAULT, totalOwed);
// 6. DISBURSE BLOCK MINER BRIBE NATIVELY
// The builder tip is always paid in native ETH, regardless of the base asset.
// - WETH case: unwrap WETH into ETH, then send ETH
// - non-WETH case: verify ETH exists, then send ETH directly
if (params.builderTip > 0) {
if (tokenBase == WETH) { //converts the WETH balance into ETH
IWETH(WETH).withdraw(params.builderTip);
}
address targetCoinbase = block.coinbase == address(0) ? owner : block.coinbase;
(bool success, ) = targetCoinbase.call{value: params.builderTip, gas: 5_000}("");
if (!success) revert TransferFailed();
}
uint256 netProfit = grossProfit > totalOwed + requiredProfitOffset
? grossProfit - totalOwed - requiredProfitOffset
: 0;
emit ArbExecuted(tokenBase, params.amountIn, grossProfit, netProfit, params.builderTip, params.minTokenProfit);
}
/**
* @notice Internal abstraction handling structural execution routing variants
*/
function _swap(
address pool,
address tokenIn,
address tokenOut,
uint256 amountIn,
bool isV2
) internal returns (uint256) {
bool zeroForOne = tokenIn < tokenOut;
if (isV2) {
uint256 balanceBefore = IERC20(tokenOut).balanceOf(address(this));
uint256 estimatedOut = _getV2AmountOut(pool, zeroForOne, amountIn);
_safeTransfer(tokenIn, pool, amountIn);
IUniswapV2Pool(pool).swap(
zeroForOne ? 0 : estimatedOut,
zeroForOne ? estimatedOut : 0,
address(this),
""
);
uint256 balanceAfter = IERC20(tokenOut).balanceOf(address(this));
return balanceAfter - balanceBefore;
} else {
uint160 sqrtPriceLimitX96 = zeroForOne ? MIN_SQRT_RATIO + 1 : MAX_SQRT_RATIO - 1;
currentActivePool = pool;
(int256 amount0, int256 amount1) = IUniswapV3Pool(pool).swap(
address(this),
zeroForOne,
int256(amountIn),
sqrtPriceLimitX96,
""
);
currentActivePool = address(0);
return zeroForOne ? uint256(-amount1) : uint256(-amount0);
}
}
function _getV2AmountOut(address pool, bool zeroForOne, uint256 amountIn) internal view returns (uint256) {
(uint112 reserve0, uint112 reserve1, ) = IUniswapV2Pool(pool).getReserves();
(uint256 reserveIn, uint256 reserveOut) = zeroForOne
? (uint256(reserve0), uint256(reserve1))
: (uint256(reserve1), uint256(reserve0));
uint256 amountInWithFee = amountIn * 997;
uint256 numerator = amountInWithFee * reserveOut;
uint256 denominator = (reserveIn * 1000) + amountInWithFee;
return numerator / denominator;
}
// =========================================================================
// V3 CALLBACK WRAPPERS
// =========================================================================
function uniswapV3SwapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata
) external {
_handleV3Callback(amount0Delta, amount1Delta);
}
function pancakeV3SwapCallback(
int256 amount0Delta,
int256 amount1Delta,
bytes calldata
) external {
_handleV3Callback(amount0Delta, amount1Delta);
}
function algebraSwapCallback(int256 a0, int256 a1, bytes calldata) external {
_handleV3Callback(a0, a1);
}
function _handleV3Callback(int256 amount0Delta, int256 amount1Delta) internal {
if (msg.sender != currentActivePool) revert UnauthorizedCallback();
if (amount0Delta > 0) {
_safeTransfer(IUniswapV3Pool(msg.sender).token0(), msg.sender, uint256(amount0Delta));
} else if (amount1Delta > 0) {
_safeTransfer(IUniswapV3Pool(msg.sender).token1(), msg.sender, uint256(amount1Delta));
}
}
// =========================================================================
// NON-STANDARD ERC-20 COMPATIBILITY HELPERS
// =========================================================================
function _safeTransfer(address token, address to, uint256 value) internal {
(bool success, bytes memory data) = token.call(
abi.encodeWithSelector(0xa9059cbb, to, value)
);
if (!success || (data.length != 0 && !abi.decode(data, (bool)))) {
revert TransferFailed();
}
}
function withdrawToken(address token) external onlyOwner {
uint256 balance = IERC20(token).balanceOf(address(this));
_safeTransfer(token, owner, balance);
}
function withdrawETH() external onlyOwner {
(bool success, ) = owner.call{value: address(this).balance}("");
if (!success) revert TransferFailed();
}
/*
function transferOwnership(address newOwner) external onlyOwner {
require(newOwner != address(0), "Zero address");
owner = newOwner;
}
*/
}Compiler settingsShow
{
"viaIR": true,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"forge-std/=lib/forge-std/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.