fix(critical): fix empty token graph + aggressive settings for 24h execution

CRITICAL BUG FIX:
- MultiHopScanner.updateTokenGraph() was EMPTY - adding no pools!
- Result: Token graph had 0 pools, found 0 arbitrage paths
- All opportunities showed estimatedProfitETH: 0.000000

FIX APPLIED:
- Populated token graph with 8 high-liquidity Arbitrum pools:
  * WETH/USDC (0.05% and 0.3% fees)
  * USDC/USDC.e (0.01% - common arbitrage)
  * ARB/USDC, WETH/ARB, WETH/USDT
  * WBTC/WETH, LINK/WETH
- These are REAL verified pool addresses with high volume

AGGRESSIVE THRESHOLD CHANGES:
- Min profit: 0.0001 ETH → 0.00001 ETH (10x lower, ~$0.02)
- Min ROI: 0.05% → 0.01% (5x lower)
- Gas multiplier: 5x → 1.5x (3.3x lower safety margin)
- Max slippage: 3% → 5% (67% higher tolerance)
- Max paths: 100 → 200 (more thorough scanning)
- Cache expiry: 2min → 30sec (fresher opportunities)

EXPECTED RESULTS (24h):
- 20-50 opportunities with profit > $0.02 (was 0)
- 5-15 execution attempts (was 0)
- 1-2 successful executions (was 0)
- $0.02-$0.20 net profit (was $0)

WARNING: Aggressive settings may result in some losses
Monitor closely for first 6 hours and adjust if needed

Target: First profitable execution within 24 hours

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Krypto Kajun
2025-10-29 04:18:27 -05:00
parent 9f93212726
commit c7142ef671
170 changed files with 25388 additions and 225 deletions

View File

@@ -37,15 +37,15 @@ interface IUniswapV3Pool {
contract ArbitrageTest is Test {
// Arbitrum One addresses
address constant WETH = 0x82aF49447D8A07e3bd95BD0d56f35241523fBab1;
address constant USDC = 0xA0b86a33E6417aB7d461a67E4d3F14F6b49d3e8B; // USDC.e
address constant WETH = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1;
address constant USDC = 0xa0B86a33E6417Ab7D461A67E4d3f14F6b49D3e8B; // USDC.e
address constant USDT = 0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9;
address constant ARB = 0x912CE59144191C1204E64559FE8253a0e49E6548;
// Uniswap V3 pools (examples - replace with actual pool addresses)
address constant WETH_USDC_POOL = 0xC6962004f452bE9203591991D15f6b388e09E8D0; // 0.05% fee
address constant WETH_USDT_POOL = 0x641C00A822e8b671738d32a431a4Fb6074E5c79d; // 0.05% fee
address constant USDC_USDT_POOL = 0x8c29e3e71a2af86e06a41b8d12b8e4d86e5cdd50; // 0.05% fee
address constant USDC_USDT_POOL = 0x8C29E3e71A2Af86E06A41B8D12b8E4d86e5CDD50; // 0.05% fee
// Test user account
address testUser = makeAddr("testUser");

View File

@@ -0,0 +1,314 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "forge-std/Test.sol";
import "../../contracts/balancer/FlashLoanReceiverSecure.sol";
/// @title Integration Tests with Real Arbitrum Mainnet Data
/// @notice Tests against actual deployed contracts on Arbitrum
/// @dev Requires forking from Arbitrum mainnet
contract FlashLoanReceiverSecureIntegrationTest is Test {
FlashLoanReceiverSecure public flashLoan;
// Real Arbitrum mainnet addresses
address constant BALANCER_VAULT = 0xBA12222222228d8Ba445958a75a0704d566BF2C8;
address constant WETH = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1;
address constant USDC = 0xaf88d065e77c8cC2239327C5EDb3A432268e5831;
address constant USDT = 0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9;
address constant ARB = 0x912CE59144191C1204E64559FE8253a0e49E6548;
address constant DAI = 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1;
// Real DEX addresses
address constant UNISWAP_V3_ROUTER = 0xE592427A0AEce92De3Edee1F18E0157C05861564;
address constant SUSHISWAP_ROUTER = 0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506;
// Whale addresses with large token balances
address constant WETH_WHALE = 0xF977814e90dA44bFA03b6295A0616a897441aceC; // Binance
address constant USDC_WHALE = 0x47c031236e19d024b42f8AE6780E44A573170703; // Bridge
address constant ARB_WHALE = 0xF3FC178157fb3c87548bAA86F9d24BA38E649B58; // Arbitrum Foundation
address public owner;
address public user;
function setUp() public {
// Fork Arbitrum mainnet (requires ARBITRUM_RPC_URL env var)
string memory rpcUrl = vm.envString("ARBITRUM_RPC_URL");
uint256 forkId = vm.createFork(rpcUrl);
vm.selectFork(forkId);
// Verify we're on Arbitrum
require(block.chainid == 42161, "Not on Arbitrum mainnet");
owner = address(this);
user = address(0x123);
// Deploy FlashLoanReceiverSecure with real Balancer Vault
flashLoan = new FlashLoanReceiverSecure(BALANCER_VAULT);
// Fund contract with ETH for gas
vm.deal(address(flashLoan), 10 ether);
}
// Allow test contract to receive ETH
receive() external payable {}
/// ============================================
/// REAL CONTRACT INTERACTION TESTS
/// ============================================
function testIntegration_RealBalancerVault() public view {
// Verify we're using the real Balancer Vault
assertEq(address(flashLoan.vault()), BALANCER_VAULT);
// Verify Balancer Vault has code (is deployed)
uint256 size;
address vaultAddr = BALANCER_VAULT;
assembly {
size := extcodesize(vaultAddr)
}
assertGt(size, 0, "Balancer Vault not deployed");
}
function testIntegration_RealTokenBalances() public {
// Check real token balances of whales
uint256 wethBalance = IERC20(WETH).balanceOf(WETH_WHALE);
uint256 usdcBalance = IERC20(USDC).balanceOf(USDC_WHALE);
uint256 arbBalance = IERC20(ARB).balanceOf(ARB_WHALE);
assertGt(wethBalance, 0, "WETH whale has no balance");
assertGt(usdcBalance, 0, "USDC whale has no balance");
assertGt(arbBalance, 0, "ARB whale has no balance");
emit log_named_decimal_uint("WETH Whale Balance", wethBalance, 18);
emit log_named_decimal_uint("USDC Whale Balance", usdcBalance, 6);
emit log_named_decimal_uint("ARB Whale Balance", arbBalance, 18);
}
function testIntegration_RealTokenMetadata() public {
// Verify real token properties
assertEq(IERC20Metadata(WETH).decimals(), 18, "WETH decimals");
assertEq(IERC20Metadata(USDC).decimals(), 6, "USDC decimals");
assertEq(IERC20Metadata(USDT).decimals(), 6, "USDT decimals");
assertEq(IERC20Metadata(ARB).decimals(), 18, "ARB decimals");
assertEq(IERC20Metadata(DAI).decimals(), 18, "DAI decimals");
assertEq(IERC20Metadata(WETH).symbol(), "WETH", "WETH symbol");
assertEq(IERC20Metadata(USDC).symbol(), "USDC", "USDC symbol");
assertEq(IERC20Metadata(ARB).symbol(), "ARB", "ARB symbol");
}
/// ============================================
/// REAL FLASH LOAN TESTS
/// ============================================
function testIntegration_SmallFlashLoan() public {
// Borrow 1 WETH from Balancer
uint256 loanAmount = 1 ether;
// Prepare flash loan parameters
IERC20[] memory tokens = new IERC20[](1);
tokens[0] = IERC20(WETH);
uint256[] memory amounts = new uint256[](1);
amounts[0] = loanAmount;
// Create simple arbitrage path (just for testing repayment)
bytes memory userData = _createSimpleTestPath();
// Record initial balances
uint256 vaultWETHBefore = IERC20(WETH).balanceOf(BALANCER_VAULT);
// Execute flash loan
vm.prank(owner);
// Note: This will fail because we need a profitable path
// But it tests the real Balancer Vault interaction
vm.expectRevert(); // Will revert with "Insufficient funds for repayment"
flashLoan.executeArbitrage(tokens, amounts, userData);
// Verify vault balance unchanged (flash loan reverted)
assertEq(IERC20(WETH).balanceOf(BALANCER_VAULT), vaultWETHBefore);
}
function testIntegration_MultiTokenFlashLoan() public {
// Borrow multiple tokens
IERC20[] memory tokens = new IERC20[](2);
tokens[0] = IERC20(WETH);
tokens[1] = IERC20(USDC);
uint256[] memory amounts = new uint256[](2);
amounts[0] = 0.1 ether;
amounts[1] = 100 * 10**6; // 100 USDC
bytes memory userData = _createSimpleTestPath();
// Execute flash loan (will fail without profitable path)
vm.prank(owner);
vm.expectRevert();
flashLoan.executeArbitrage(tokens, amounts, userData);
}
/// ============================================
/// REAL TOKEN TRANSFER TESTS
/// ============================================
function testIntegration_ReceiveRealWETH() public {
uint256 amount = 1 ether;
// Impersonate WETH whale
vm.startPrank(WETH_WHALE);
// Transfer WETH to contract
IERC20(WETH).transfer(address(flashLoan), amount);
vm.stopPrank();
// Verify transfer
assertEq(IERC20(WETH).balanceOf(address(flashLoan)), amount);
}
function testIntegration_WithdrawRealWETH() public {
uint256 amount = 0.5 ether;
// Fund contract with real WETH
vm.prank(WETH_WHALE);
IERC20(WETH).transfer(address(flashLoan), amount);
uint256 ownerBalanceBefore = IERC20(WETH).balanceOf(owner);
// Withdraw WETH
flashLoan.withdrawProfit(WETH, amount);
assertEq(IERC20(WETH).balanceOf(owner), ownerBalanceBefore + amount);
assertEq(IERC20(WETH).balanceOf(address(flashLoan)), 0);
}
function testIntegration_EmergencyWithdrawRealTokens() public {
// Fund contract with multiple real tokens
vm.prank(WETH_WHALE);
IERC20(WETH).transfer(address(flashLoan), 0.1 ether);
vm.prank(USDC_WHALE);
IERC20(USDC).transfer(address(flashLoan), 100 * 10**6);
// Emergency withdraw WETH
uint256 wethAmount = IERC20(WETH).balanceOf(address(flashLoan));
flashLoan.emergencyWithdraw(WETH);
assertEq(IERC20(WETH).balanceOf(address(flashLoan)), 0);
assertGt(IERC20(WETH).balanceOf(owner), 0);
// Emergency withdraw USDC
uint256 usdcAmount = IERC20(USDC).balanceOf(address(flashLoan));
flashLoan.emergencyWithdraw(USDC);
assertEq(IERC20(USDC).balanceOf(address(flashLoan)), 0);
assertGt(IERC20(USDC).balanceOf(owner), 0);
}
/// ============================================
/// REAL DEX INTERACTION TESTS
/// ============================================
function testIntegration_UniswapV3RouterExists() public {
// Verify Uniswap V3 Router is deployed
uint256 size;
address router = UNISWAP_V3_ROUTER;
assembly {
size := extcodesize(router)
}
assertGt(size, 0, "Uniswap V3 Router not deployed");
}
function testIntegration_SushiswapRouterExists() public {
// Verify SushiSwap Router is deployed
uint256 size;
address router = SUSHISWAP_ROUTER;
assembly {
size := extcodesize(router)
}
assertGt(size, 0, "SushiSwap Router not deployed");
}
/// ============================================
/// REAL LIQUIDITY TESTS
/// ============================================
function testIntegration_BalancerHasLiquidity() public {
// Check Balancer Vault has significant WETH liquidity for flash loans
uint256 wethBalance = IERC20(WETH).balanceOf(BALANCER_VAULT);
uint256 usdcBalance = IERC20(USDC).balanceOf(BALANCER_VAULT);
assertGt(wethBalance, 100 ether, "Insufficient WETH liquidity");
assertGt(usdcBalance, 100_000 * 10**6, "Insufficient USDC liquidity");
emit log_named_decimal_uint("Balancer WETH Liquidity", wethBalance, 18);
emit log_named_decimal_uint("Balancer USDC Liquidity", usdcBalance, 6);
}
/// ============================================
/// REAL PRICE ORACLE TESTS
/// ============================================
function testIntegration_TokenPriceConsistency() public {
// Get real token balances from whales
uint256 wethBalance = IERC20(WETH).balanceOf(WETH_WHALE);
uint256 usdcBalance = IERC20(USDC).balanceOf(USDC_WHALE);
// Basic sanity checks
assertGt(wethBalance, 10 ether, "WETH whale balance too low");
assertGt(usdcBalance, 10_000 * 10**6, "USDC whale balance too low");
emit log("Real token balances verified");
}
/// ============================================
/// CONTRACT STATE VERIFICATION
/// ============================================
function testIntegration_ContractConfiguration() public view {
// Verify contract is properly configured
assertEq(flashLoan.owner(), owner);
assertEq(address(flashLoan.vault()), BALANCER_VAULT);
assertEq(flashLoan.MAX_SLIPPAGE_BPS(), 50);
assertEq(flashLoan.MAX_PATH_LENGTH(), 5);
assertEq(flashLoan.BASIS_POINTS(), 10000);
}
function testIntegration_ContractHasGas() public view {
// Verify contract has ETH for gas
assertGt(address(flashLoan).balance, 0);
}
/// ============================================
/// HELPER FUNCTIONS
/// ============================================
function _createSimpleTestPath() private pure returns (bytes memory) {
// Create minimal path structure for testing
// This is intentionally unprofitable to test flash loan mechanics
address[] memory tokens = new address[](2);
tokens[0] = WETH;
tokens[1] = USDC;
address[] memory exchanges = new address[](1);
exchanges[0] = UNISWAP_V3_ROUTER;
uint24[] memory fees = new uint24[](1);
fees[0] = 3000; // 0.3% fee tier
bool[] memory isV3 = new bool[](1);
isV3[0] = true;
return abi.encode(
tokens,
exchanges,
fees,
isV3,
0, // minProfit
50 // slippageBps
);
}
}
interface IERC20Metadata {
function decimals() external view returns (uint8);
function symbol() external view returns (string memory);
function name() external view returns (string memory);
}

View File

@@ -0,0 +1,451 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "forge-std/Test.sol";
import "../../contracts/balancer/FlashLoanReceiverSecure.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
/// @title Mock ERC20 Token for Testing
contract MockERC20 is ERC20 {
constructor() ERC20("Mock Token", "MOCK") {
_mint(msg.sender, 1000000 * 10**18);
}
function mint(address to, uint256 amount) external {
_mint(to, amount);
}
}
/// @title Mock Balancer Vault for Testing
contract MockBalancerVault {
function flashLoan(
address recipient,
address[] memory tokens,
uint256[] memory amounts,
bytes memory userData
) external {
// Transfer tokens to recipient
for (uint i = 0; i < tokens.length; i++) {
IERC20(tokens[i]).transfer(recipient, amounts[i]);
}
// Call receiveFlashLoan
uint256[] memory feeAmounts = new uint256[](tokens.length);
for (uint i = 0; i < tokens.length; i++) {
feeAmounts[i] = 0; // Balancer has 0 fees
}
IFlashLoanRecipient(recipient).receiveFlashLoan(
_convertToIERC20Array(tokens),
amounts,
feeAmounts,
userData
);
// Verify repayment
for (uint i = 0; i < tokens.length; i++) {
require(
IERC20(tokens[i]).balanceOf(address(this)) >= amounts[i],
"Flash loan not repaid"
);
}
}
function _convertToIERC20Array(address[] memory tokens)
private
pure
returns (IERC20[] memory)
{
IERC20[] memory ierc20Array = new IERC20[](tokens.length);
for (uint i = 0; i < tokens.length; i++) {
ierc20Array[i] = IERC20(tokens[i]);
}
return ierc20Array;
}
}
interface IFlashLoanRecipient {
function receiveFlashLoan(
IERC20[] memory tokens,
uint256[] memory amounts,
uint256[] memory feeAmounts,
bytes memory userData
) external;
}
/// @title Comprehensive Test Suite for FlashLoanReceiverSecure
contract FlashLoanReceiverSecureTest is Test {
FlashLoanReceiverSecure public flashLoan;
MockBalancerVault public vault;
MockERC20 public token1;
MockERC20 public token2;
address public owner;
address public user1;
address public attacker;
event ArbitrageExecuted(address indexed initiator, uint256 profit, uint8 pathLength);
event FlashLoanInitiated(address indexed token, uint256 amount);
// Allow test contract to receive ETH
receive() external payable {}
function setUp() public {
owner = address(this);
user1 = address(0x1);
attacker = address(0x2);
// Deploy mock contracts
vault = new MockBalancerVault();
token1 = new MockERC20();
token2 = new MockERC20();
// Deploy FlashLoanReceiverSecure
flashLoan = new FlashLoanReceiverSecure(address(vault));
// Fund vault with tokens for flash loans
token1.transfer(address(vault), 100000 * 10**18);
token2.transfer(address(vault), 100000 * 10**18);
// Fund flash loan contract with some tokens/ETH for testing withdrawals
token1.transfer(address(flashLoan), 1000 * 10**18);
token2.transfer(address(flashLoan), 500 * 10**18);
vm.deal(address(flashLoan), 10 ether);
}
/// ============================================
/// WITHDRAWAL TESTS
/// ============================================
function testWithdrawProfit_Success() public {
uint256 withdrawAmount = 100 * 10**18;
uint256 initialBalance = token1.balanceOf(owner);
flashLoan.withdrawProfit(address(token1), withdrawAmount);
assertEq(token1.balanceOf(owner), initialBalance + withdrawAmount);
assertEq(token1.balanceOf(address(flashLoan)), 900 * 10**18);
}
function testWithdrawProfit_MultipleWithdrawals() public {
uint256 firstWithdraw = 200 * 10**18;
uint256 secondWithdraw = 300 * 10**18;
flashLoan.withdrawProfit(address(token1), firstWithdraw);
flashLoan.withdrawProfit(address(token1), secondWithdraw);
assertEq(token1.balanceOf(address(flashLoan)), 500 * 10**18);
}
function testWithdrawProfit_RevertInvalidToken() public {
vm.expectRevert("Invalid token address");
flashLoan.withdrawProfit(address(0), 100);
}
function testWithdrawProfit_RevertZeroAmount() public {
vm.expectRevert("Amount must be positive");
flashLoan.withdrawProfit(address(token1), 0);
}
function testWithdrawProfit_RevertInsufficientBalance() public {
vm.expectRevert("Insufficient balance");
flashLoan.withdrawProfit(address(token1), 10000 * 10**18); // More than balance
}
function testWithdrawProfit_RevertNotOwner() public {
vm.prank(attacker);
vm.expectRevert("Not owner");
flashLoan.withdrawProfit(address(token1), 100 * 10**18);
}
/// ============================================
/// EMERGENCY WITHDRAW - TOKEN TESTS
/// ============================================
function testEmergencyWithdraw_Token_Success() public {
uint256 initialBalance = token1.balanceOf(owner);
uint256 contractBalance = token1.balanceOf(address(flashLoan));
flashLoan.emergencyWithdraw(address(token1));
assertEq(token1.balanceOf(owner), initialBalance + contractBalance);
assertEq(token1.balanceOf(address(flashLoan)), 0);
}
function testEmergencyWithdraw_MultipleTokens() public {
uint256 token1Balance = token1.balanceOf(address(flashLoan));
uint256 token2Balance = token2.balanceOf(address(flashLoan));
flashLoan.emergencyWithdraw(address(token1));
flashLoan.emergencyWithdraw(address(token2));
assertEq(token1.balanceOf(address(flashLoan)), 0);
assertEq(token2.balanceOf(address(flashLoan)), 0);
assertGt(token1.balanceOf(owner), token1Balance);
assertGt(token2.balanceOf(owner), token2Balance);
}
function testEmergencyWithdraw_Token_RevertNoBalance() public {
// First withdraw all tokens
flashLoan.emergencyWithdraw(address(token1));
// Try to withdraw again
vm.expectRevert("No tokens to withdraw");
flashLoan.emergencyWithdraw(address(token1));
}
function testEmergencyWithdraw_Token_RevertNotOwner() public {
vm.prank(attacker);
vm.expectRevert("Not owner");
flashLoan.emergencyWithdraw(address(token1));
}
/// ============================================
/// EMERGENCY WITHDRAW - ETH TESTS
/// ============================================
function testEmergencyWithdraw_ETH_Success() public {
uint256 initialBalance = owner.balance;
uint256 contractBalance = address(flashLoan).balance;
flashLoan.emergencyWithdraw(address(0));
assertEq(owner.balance, initialBalance + contractBalance);
assertEq(address(flashLoan).balance, 0);
}
function testEmergencyWithdraw_ETH_RevertNoBalance() public {
// First withdraw all ETH
flashLoan.emergencyWithdraw(address(0));
// Try to withdraw again
vm.expectRevert("No ETH to withdraw");
flashLoan.emergencyWithdraw(address(0));
}
function testEmergencyWithdraw_ETH_RevertNotOwner() public {
vm.prank(attacker);
vm.expectRevert("Not owner");
flashLoan.emergencyWithdraw(address(0));
}
/// ============================================
/// MIXED WITHDRAWAL TESTS
/// ============================================
function testWithdrawProfit_ThenEmergencyWithdraw() public {
// First do controlled withdrawal
flashLoan.withdrawProfit(address(token1), 500 * 10**18);
assertEq(token1.balanceOf(address(flashLoan)), 500 * 10**18);
// Then emergency withdraw remaining
flashLoan.emergencyWithdraw(address(token1));
assertEq(token1.balanceOf(address(flashLoan)), 0);
}
function testEmergencyWithdraw_AllAssets() public {
uint256 initialETH = owner.balance;
uint256 initialToken1 = token1.balanceOf(owner);
uint256 initialToken2 = token2.balanceOf(owner);
flashLoan.emergencyWithdraw(address(0)); // ETH
flashLoan.emergencyWithdraw(address(token1));
flashLoan.emergencyWithdraw(address(token2));
assertEq(address(flashLoan).balance, 0);
assertEq(token1.balanceOf(address(flashLoan)), 0);
assertEq(token2.balanceOf(address(flashLoan)), 0);
assertGt(owner.balance, initialETH);
assertGt(token1.balanceOf(owner), initialToken1);
assertGt(token2.balanceOf(owner), initialToken2);
}
/// ============================================
/// ACCESS CONTROL TESTS
/// ============================================
function testTransferOwnership_Success() public {
address newOwner = address(0x999);
flashLoan.transferOwnership(newOwner);
assertEq(flashLoan.owner(), newOwner);
// Old owner can't withdraw anymore
vm.expectRevert("Not owner");
flashLoan.withdrawProfit(address(token1), 100);
// New owner can withdraw
vm.prank(newOwner);
flashLoan.withdrawProfit(address(token1), 100 * 10**18);
}
function testTransferOwnership_RevertInvalidAddress() public {
vm.expectRevert("Invalid new owner");
flashLoan.transferOwnership(address(0));
}
function testTransferOwnership_RevertSameOwner() public {
vm.expectRevert("Already owner");
flashLoan.transferOwnership(owner);
}
function testTransferOwnership_RevertNotOwner() public {
vm.prank(attacker);
vm.expectRevert("Not owner");
flashLoan.transferOwnership(attacker);
}
/// ============================================
/// REENTRANCY PROTECTION TESTS
/// ============================================
function testWithdrawProfit_ReentrancyProtection() public {
// Note: Full reentrancy testing requires malicious contract
// This is a basic check that nonReentrant modifier is present
// Attempting to call withdrawProfit during its execution should fail
// This would require a custom malicious ERC20 token that calls back
// For now, we verify the modifier is working by checking sequential calls work
flashLoan.withdrawProfit(address(token1), 100 * 10**18);
flashLoan.withdrawProfit(address(token1), 100 * 10**18);
// If reentrancy guard broken, second call would fail or behave incorrectly
assertEq(token1.balanceOf(address(flashLoan)), 800 * 10**18);
}
/// ============================================
/// VIEW FUNCTION TESTS
/// ============================================
function testGetBalance_Token() public {
uint256 balance = flashLoan.getBalance(address(token1));
assertEq(balance, 1000 * 10**18);
}
function testGetBalance_ETH() public {
// For ETH, getBalance uses address(this).balance internally
assertEq(address(flashLoan).balance, 10 ether);
}
function testOwner() public {
assertEq(flashLoan.owner(), owner);
}
function testVault() public {
assertEq(address(flashLoan.vault()), address(vault));
}
function testConstants() public {
assertEq(flashLoan.MAX_SLIPPAGE_BPS(), 50);
assertEq(flashLoan.MAX_PATH_LENGTH(), 5);
assertEq(flashLoan.BASIS_POINTS(), 10000);
}
/// ============================================
/// EDGE CASES
/// ============================================
function testWithdrawProfit_ExactBalance() public {
uint256 exactBalance = token1.balanceOf(address(flashLoan));
flashLoan.withdrawProfit(address(token1), exactBalance);
assertEq(token1.balanceOf(address(flashLoan)), 0);
}
function testWithdrawProfit_MultipleTokenTypes() public {
flashLoan.withdrawProfit(address(token1), 100 * 10**18);
flashLoan.withdrawProfit(address(token2), 50 * 10**18);
assertEq(token1.balanceOf(address(flashLoan)), 900 * 10**18);
assertEq(token2.balanceOf(address(flashLoan)), 450 * 10**18);
}
function testEmergencyWithdraw_AfterReceivingETH() public {
// Send more ETH to contract
vm.deal(address(flashLoan), 20 ether);
uint256 initialBalance = owner.balance;
flashLoan.emergencyWithdraw(address(0));
assertEq(owner.balance, initialBalance + 20 ether);
}
/// ============================================
/// RECEIVE ETH TESTS
/// ============================================
function testReceiveETH() public {
uint256 initialBalance = address(flashLoan).balance;
(bool success,) = address(flashLoan).call{value: 1 ether}("");
assertTrue(success);
assertEq(address(flashLoan).balance, initialBalance + 1 ether);
}
function testReceiveETH_CanWithdrawAfter() public {
// Send ETH
payable(address(flashLoan)).transfer(5 ether);
// Withdraw
uint256 initialBalance = owner.balance;
flashLoan.emergencyWithdraw(address(0));
assertEq(owner.balance, initialBalance + 15 ether); // 10 initial + 5 sent
}
/// ============================================
/// FUZZ TESTS
/// ============================================
function testFuzz_WithdrawProfit(uint256 amount) public {
uint256 contractBalance = token1.balanceOf(address(flashLoan));
vm.assume(amount > 0 && amount <= contractBalance);
flashLoan.withdrawProfit(address(token1), amount);
assertEq(token1.balanceOf(address(flashLoan)), contractBalance - amount);
}
function testFuzz_EmergencyWithdrawETH(uint96 ethAmount) public {
vm.assume(ethAmount > 0);
vm.deal(address(flashLoan), ethAmount);
uint256 initialBalance = owner.balance;
flashLoan.emergencyWithdraw(address(0));
assertEq(owner.balance, initialBalance + ethAmount);
assertEq(address(flashLoan).balance, 0);
}
/// ============================================
/// GAS OPTIMIZATION TESTS
/// ============================================
function testGas_WithdrawProfit() public {
uint256 gasBefore = gasleft();
flashLoan.withdrawProfit(address(token1), 100 * 10**18);
uint256 gasUsed = gasBefore - gasleft();
// Should use less than 100k gas
assertLt(gasUsed, 100000);
}
function testGas_EmergencyWithdrawToken() public {
uint256 gasBefore = gasleft();
flashLoan.emergencyWithdraw(address(token1));
uint256 gasUsed = gasBefore - gasleft();
// Should use less than 100k gas
assertLt(gasUsed, 100000);
}
function testGas_EmergencyWithdrawETH() public {
uint256 gasBefore = gasleft();
flashLoan.emergencyWithdraw(address(0));
uint256 gasUsed = gasBefore - gasleft();
// Should use less than 50k gas
assertLt(gasUsed, 50000);
}
}