Verified contractExactMIT
BalancerFlashArbitrage
Address0x3f30b7d747fdb0addb4d7d253654be27cd13d556
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.24
- Same code
Only this contract
Details
- Compiler
- solc 0.8.24+commit.e11b9ed9
- Language
- solidity
- Optimization
- No
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,329,836
- Creation transaction
- 0xe105aaab5f…4e4c34cb6c
contracts/BalancerArbitrage.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
// ======================================================
// Minimal ERC20 Interface
// ======================================================
interface IERC20Minimal {
function balanceOf(address account)
external view returns (uint256);
function approve(address spender, uint256 value)
external returns (bool);
function transfer(address recipient, uint256 value)
external returns (bool);
}
// ======================================================
// Balancer V2 Flash Loan Interfaces
// ======================================================
interface IFlashLoanRecipient {
function receiveFlashLoan(
IERC20Minimal[] calldata tokens,
uint256[] calldata amounts,
uint256[] calldata feeAmounts,
bytes calldata userData
) external;
}
interface IBalancerVault {
function flashLoan(
IFlashLoanRecipient recipient,
IERC20Minimal[] memory tokens,
uint256[] memory amounts,
bytes memory userData
) external;
}
// ======================================================
// Uniswap V2 / SushiSwap V2 Router Interface
// ======================================================
interface IUniswapV2Router {
function swapExactTokensForTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
}
// ======================================================
// Main Contract
// ======================================================
contract BalancerFlashArbitrage is IFlashLoanRecipient {
// Ethereum Mainnet Addresses
address public constant BALANCER_VAULT =
0xBA12222222228d8Ba445958a75a0704d566BF2C8;
address public constant USDC =
0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address public constant WETH =
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address public constant UNISWAP_V2_ROUTER =
0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address public constant SUSHISWAP_V2_ROUTER =
0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F;
// 10,000,000 USDC (6 decimals)
uint256 public constant LOAN_AMOUNT =
10_000_000 * 1_000_000;
address public immutable owner;
bool private loanActive;
// Events
event ArbitrageExecuted(
address indexed owner,
uint256 loanAmount,
uint256 profitUSDC
);
// Constructor
constructor() {
owner = msg.sender;
}
// Owner restriction
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
// ==================================================
// Start Flash Loan
// ==================================================
function startArbitrage(
uint256 minProfitUSDC
) external onlyOwner {
require(!loanActive, "Loan already active");
require(minProfitUSDC > 0, "Min profit required");
uint256 startingBalance =
IERC20Minimal(USDC).balanceOf(address(this));
loanActive = true;
IERC20Minimal[] memory tokens =
new IERC20Minimal[](1);
uint256[] memory amounts =
new uint256[](1);
tokens[0] = IERC20Minimal(USDC);
amounts[0] = LOAN_AMOUNT;
IBalancerVault(BALANCER_VAULT).flashLoan(
IFlashLoanRecipient(address(this)),
tokens,
amounts,
abi.encode(minProfitUSDC, startingBalance)
);
loanActive = false;
// Clear remaining Vault allowance
_forceApprove(
IERC20Minimal(USDC),
BALANCER_VAULT,
0
);
// Calculate actual profit
uint256 endingBalance =
IERC20Minimal(USDC).balanceOf(address(this));
require(
endingBalance >= startingBalance,
"Balance check failed"
);
uint256 profitUSDC =
endingBalance - startingBalance;
// Send profit to deployer's wallet
if (profitUSDC > 0) {
require(
IERC20Minimal(USDC).transfer(
owner,
profitUSDC
),
"Profit transfer failed"
);
}
emit ArbitrageExecuted(
owner,
LOAN_AMOUNT,
profitUSDC
);
}
// ==================================================
// Balancer Callback
// ==================================================
function receiveFlashLoan(
IERC20Minimal[] calldata tokens,
uint256[] calldata amounts,
uint256[] calldata feeAmounts,
bytes calldata userData
) external override {
require(
msg.sender == BALANCER_VAULT,
"Only Balancer"
);
require(loanActive, "No active loan");
require(
tokens.length == 1 &&
amounts.length == 1 &&
feeAmounts.length == 1,
"Invalid loan arrays"
);
require(
address(tokens[0]) == USDC,
"Wrong loan token"
);
require(
amounts[0] == LOAN_AMOUNT,
"Wrong loan amount"
);
(
uint256 minProfitUSDC,
uint256 startingBalance
) = abi.decode(
userData,
(uint256, uint256)
);
// ----------------------------------------------
// STEP 1: Buy WETH on Uniswap V2
// ----------------------------------------------
uint256 wethBefore =
IERC20Minimal(WETH).balanceOf(address(this));
_forceApprove(
IERC20Minimal(USDC),
UNISWAP_V2_ROUTER,
amounts[0]
);
address[] memory buyPath =
new address[](2);
buyPath[0] = USDC;
buyPath[1] = WETH;
IUniswapV2Router(UNISWAP_V2_ROUTER)
.swapExactTokensForTokens(
amounts[0],
1,
buyPath,
address(this),
block.timestamp
);
uint256 wethAfter =
IERC20Minimal(WETH).balanceOf(address(this));
require(wethAfter > wethBefore, "No WETH received");
uint256 wethBought =
wethAfter - wethBefore;
// ----------------------------------------------
// STEP 2: Sell WETH on SushiSwap V2
// ----------------------------------------------
_forceApprove(
IERC20Minimal(WETH),
SUSHISWAP_V2_ROUTER,
wethBought
);
address[] memory sellPath =
new address[](2);
sellPath[0] = WETH;
sellPath[1] = USDC;
IUniswapV2Router(SUSHISWAP_V2_ROUTER)
.swapExactTokensForTokens(
wethBought,
1,
sellPath,
address(this),
block.timestamp
);
// Clear DEX allowances
_forceApprove(
IERC20Minimal(USDC),
UNISWAP_V2_ROUTER,
0
);
_forceApprove(
IERC20Minimal(WETH),
SUSHISWAP_V2_ROUTER,
0
);
// ----------------------------------------------
// STEP 3: Check profit and repay Balancer
// ----------------------------------------------
uint256 repayment =
amounts[0] + feeAmounts[0];
uint256 requiredBalance =
startingBalance +
repayment +
minProfitUSDC;
require(
IERC20Minimal(USDC).balanceOf(address(this))
>= requiredBalance,
"Trade not profitable"
);
// Approve exact repayment
_forceApprove(
IERC20Minimal(USDC),
BALANCER_VAULT,
repayment
);
}
// ==================================================
// Safe Approval Helper
// ==================================================
function _forceApprove(
IERC20Minimal token,
address spender,
uint256 amount
) internal {
require(
token.approve(spender, 0),
"Approve reset failed"
);
if (amount > 0) {
require(
token.approve(spender, amount),
"Approve failed"
);
}
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": false
},
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.