From eeb2c28375d123ef22d4c3ad586c356cdbeabf28 Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Date: Fri, 8 Dec 2023 23:47:22 +0500 Subject: [PATCH] Update EngineFuzzTest.t.sol --- test/EngineFuzzTest.t.sol | 356 +++++++++++++++++++++++--------------- 1 file changed, 216 insertions(+), 140 deletions(-) diff --git a/test/EngineFuzzTest.t.sol b/test/EngineFuzzTest.t.sol index 2a4fab3..65e4336 100644 --- a/test/EngineFuzzTest.t.sol +++ b/test/EngineFuzzTest.t.sol @@ -28,29 +28,36 @@ contract EngineFuzzTest is Test { IERC20 usdc = IERC20(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48); IWETH9 weth = IWETH9(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); IERC20 wbtc = IERC20(0x9Ee1Aa18F3FEB435f811d6AE2F71B7D2a4Adce0B); - ISwapRouter02 swapRouter02 = ISwapRouter02(0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45); - IPriceFeed usdc_eth = IPriceFeed(0x986b5E1e1755e3C2440e960477f25201B0a8bbD4); + ISwapRouter02 swapRouter02 = + ISwapRouter02(0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45); + IPriceFeed usdc_eth = + IPriceFeed(0x986b5E1e1755e3C2440e960477f25201B0a8bbD4); IQuoter qouter = IQuoter(0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6); IQuoter positions = IQuoter(0xC36442b4a4522E871399CD717aBDD847Ab11FE88); address zeroAddress = address(0); address feeCollector = address(147578); address admin = address(3); - uint256 maker1PrivateKey = 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80; + uint256 maker1PrivateKey = + 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80; address maker1 = vm.addr(maker1PrivateKey); - uint256 maker2PrivateKey = 0xac0974bec39a17e36ba4a6b4d233ff944bacb478cbed5efcae784d7bf4f2ff80; + uint256 maker2PrivateKey = + 0xac0974bec39a17e36ba4a6b4d233ff944bacb478cbed5efcae784d7bf4f2ff80; address maker2 = vm.addr(maker2PrivateKey); - uint256 maker3PrivateKey = 0xac0974bec38a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80; + uint256 maker3PrivateKey = + 0xac0974bec38a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80; address maker3 = vm.addr(maker3PrivateKey); - uint256 operatorPrivateKey = 0xB0B; + uint256 operatorPrivateKey = 0xB0B; address operator = vm.addr(operatorPrivateKey); - function setUp() public { vm.startPrank(admin); predicates = new Predicates(); generateCalldata = new GenerateCalldata(address(predicates)); - advancedOrderEngine = new AdvancedOrderEngine(IPredicates(address(predicates)), feeCollector); + advancedOrderEngine = new AdvancedOrderEngine( + IPredicates(address(predicates)), + feeCollector + ); swapper = address(new Swapper()); helper = address(new Helper()); @@ -74,10 +81,9 @@ contract EngineFuzzTest is Test { vm.deal(maker3, 20000000 ether); vm.deal(operator, 20000000 ether); vm.deal(admin, 20000000 ether); - + vm.stopPrank(); - vm.startPrank(maker1); weth.approve(address(swapRouter02), UINT256_MAX); @@ -91,7 +97,7 @@ contract EngineFuzzTest is Test { // get usdc swapRouter02.exactInputSingle{value: 1000000 ether}( - ISwapRouter02.ExactInputSingleParams ( + ISwapRouter02.ExactInputSingleParams( address(weth), address(usdc), 500, @@ -102,7 +108,10 @@ contract EngineFuzzTest is Test { ) ); - console.log("Maker 1 - USDC Balance:",usdc.balanceOf(address(maker1)) / 1000000); + console.log( + "Maker 1 - USDC Balance:", + usdc.balanceOf(address(maker1)) / 1000000 + ); vm.stopPrank(); @@ -119,7 +128,7 @@ contract EngineFuzzTest is Test { // get usdc swapRouter02.exactInputSingle{value: 1000000 ether}( - ISwapRouter02.ExactInputSingleParams ( + ISwapRouter02.ExactInputSingleParams( address(weth), address(usdc), 500, @@ -130,11 +139,13 @@ contract EngineFuzzTest is Test { ) ); - console.log("Maker 2 Balance - USDC:",usdc.balanceOf(maker2)/1000000); + console.log( + "Maker 2 Balance - USDC:", + usdc.balanceOf(maker2) / 1000000 + ); vm.stopPrank(); - vm.startPrank(maker3); weth.approve(address(swapRouter02), UINT256_MAX); @@ -148,7 +159,7 @@ contract EngineFuzzTest is Test { // get usdc swapRouter02.exactInputSingle{value: 20 ether}( - ISwapRouter02.ExactInputSingleParams ( + ISwapRouter02.ExactInputSingleParams( address(weth), address(usdc), 500, @@ -158,15 +169,17 @@ contract EngineFuzzTest is Test { 0 ) ); - console.log("Maker 23 Balance - USDC:",usdc.balanceOf(maker3)/1000000); - + console.log( + "Maker 23 Balance - USDC:", + usdc.balanceOf(maker3) / 1000000 + ); wmatic.approve(address(swapRouter02), UINT256_MAX); wmatic.approve(address(advancedOrderEngine), UINT256_MAX); // get matic swapRouter02.exactInputSingle{value: 20 ether}( - ISwapRouter02.ExactInputSingleParams ( + ISwapRouter02.ExactInputSingleParams( address(weth), address(wmatic), 3000, @@ -187,13 +200,12 @@ contract EngineFuzzTest is Test { // get weth weth.deposit{value: 20 ether}(); - usdc.approve(address(swapRouter02), UINT256_MAX); usdc.approve(address(advancedOrderEngine), UINT256_MAX); // get usdc swapRouter02.exactInputSingle{value: 20 ether}( - ISwapRouter02.ExactInputSingleParams ( + ISwapRouter02.ExactInputSingleParams( address(weth), address(usdc), 500, @@ -207,13 +219,9 @@ contract EngineFuzzTest is Test { // usdc.transfer(address(advancedOrderEngine), 100000000); vm.stopPrank(); - - - } - - function testFuzz(uint256 buy_qty,uint256 sell_qty) public { + function testFuzz(uint256 buy_qty, uint256 sell_qty) public { vm.assume(buy_qty > 0 && sell_qty > 0); uint256 maker1WETHBalance = weth.balanceOf(address(maker1)); uint256 maker1USDCBalance = usdc.balanceOf(address(maker1)); @@ -221,31 +229,30 @@ contract EngineFuzzTest is Test { uint256 maker2WETHBalance = weth.balanceOf(address(maker2)); uint256 maker2USDCBalance = usdc.balanceOf(address(maker2)); - console.log("make1 WETH Balance:",maker1WETHBalance); - console.log("make2 WETH Balance:",maker2WETHBalance); - - console.log("make1 USDC Balance:",maker1USDCBalance); - console.log("make2 USDC Balance:",maker2USDCBalance); - - console.log("buy_qty:",buy_qty); - console.log("sell_qty:",sell_qty); - - - + console.log("make1 WETH Balance:", maker1WETHBalance); + console.log("make2 WETH Balance:", maker2WETHBalance); + console.log("make1 USDC Balance:", maker1USDCBalance); + console.log("make2 USDC Balance:", maker2USDCBalance); + console.log("buy_qty:", buy_qty); + console.log("sell_qty:", sell_qty); // require(buy_qty <= maker1USDCBalance && buy_qty <= maker2USDCBalance && buy_qty <= maker1WETHBalance && buy_qty <= maker2WETHBalance, "Buy quantity exceeds balance"); // require(sell_qty <= maker1USDCBalance && sell_qty <= maker2USDCBalance && sell_qty <= maker1WETHBalance && sell_qty <= maker2WETHBalance, "Sell quantity exceeds balance"); - - require(buy_qty <= maker1WETHBalance && buy_qty <= maker2USDCBalance, "Buy quantity exceeds WETH or USDC balance"); - require(sell_qty <= maker1USDCBalance && sell_qty <= maker2WETHBalance, "Sell quantity exceeds USDC or WETH balance"); - - console.log("Maker1 Balance on 218:",maker1WETHBalance); - vm.startPrank(operator); + require( + buy_qty <= maker1WETHBalance && buy_qty <= maker2USDCBalance, + "Buy quantity exceeds WETH or USDC balance" + ); + require( + sell_qty <= maker1USDCBalance && sell_qty <= maker2WETHBalance, + "Sell quantity exceeds USDC or WETH balance" + ); + vm.startPrank(operator); + //OrderEngine.Order[] memory orders; ( OrderEngine.Order[] memory orders, uint256[] memory sell, @@ -254,8 +261,9 @@ contract EngineFuzzTest is Test { bytes memory facilitatorInteraction, IERC20[] memory borrowedTokens, uint256[] memory borrowedAmounts - ) = orderInput(buy_qty,sell_qty); + ) = orderInput(buy_qty, sell_qty); + require(orders.length >= 2); advancedOrderEngine.fillOrders( orders, sell, @@ -267,85 +275,124 @@ contract EngineFuzzTest is Test { ); uint256 vaultbal = usdc.balanceOf(address(advancedOrderEngine)); - console.log("Vault Balance:",vaultbal); + console.log("Vault Balance:", vaultbal); assert(vaultbal == 0); vm.stopPrank(); - - } - - function orderInput(uint256 buy_qty,uint256 sell_qty) private returns ( - OrderEngine.Order[] memory orders, - uint256[] memory sell, - uint256[] memory buy, - bytes[] memory signatures, - bytes memory facilitatorInteraction, - IERC20[] memory borrowedTokens, - uint256[] memory borrowedAmounts -) { - uint size = determineOrderSize(); // Function to determine a safe number of orders - borrowedAmounts = new uint256[](0); - borrowedTokens = new IERC20[](0); - facilitatorInteraction = "0x"; - orders = new OrderEngine.Order[](size * 2); // times 2 to accommodate both sell and buy orders - sell = new uint256[](size * 2); - buy = new uint256[](size * 2); - signatures = new bytes[](size * 2); - - for (uint i = 0; i < size; i++) { - // Creating a sell order - (uint256 sellAmount, uint256 buyAmount) = getRandomOrderAmounts(maker1, maker2, weth, usdc,buy_qty,sell_qty); - orders[i] = createOrder(i, sellAmount, buyAmount, maker1, weth, usdc); - sell[i] = sellAmount; - buy[i] = buyAmount; - signatures[i] = signOrder(orders[i], maker1PrivateKey); - - // Creating a corresponding buy order - (sellAmount, buyAmount) = getRandomOrderAmounts(maker2, maker1, usdc, weth,buy_qty,sell_qty); - orders[size + i] = createOrder(size + i, sellAmount, buyAmount, maker2, usdc, weth); - sell[size + i] = sellAmount; - buy[size + i] = buyAmount; - signatures[size + i] = signOrder(orders[size + i], maker2PrivateKey); + function orderInput( + uint256 buy_qty, + uint256 sell_qty) private returns ( + OrderEngine.Order[] memory orders, + uint256[] memory sell, + uint256[] memory buy, + bytes[] memory signatures, + bytes memory facilitatorInteraction, + IERC20[] memory borrowedTokens, + uint256[] memory borrowedAmounts){ + uint size = determineOrderSize(); // Function to determine a safe number of orders + borrowedAmounts = new uint256[](0); + borrowedTokens = new IERC20[](0); + facilitatorInteraction = "0x"; + orders = new OrderEngine.Order[](size * 2); // times 2 to accommodate both sell and buy orders + sell = new uint256[](size * 2); + buy = new uint256[](size * 2); + signatures = new bytes[](size * 2); + + OrderEngine.Order memory order1 = getOrder1(); + orders[0] = order1; + sell[0] = order1.sellTokenAmount; + buy[0] = order1.buyTokenAmount; + signatures[0] = signOrder(orders[0], maker1PrivateKey); + + for (uint i = 1; i < size; i++) { + // Creating a sell order + (uint256 sellAmount, uint256 buyAmount) = getRandomOrderAmounts( + maker1, + maker2, + weth, + usdc, + buy_qty, + sell_qty + ); + orders[i] = createOrder( + i, + sellAmount, + buyAmount, + maker1, + weth, + usdc + ); + sell[i] = sellAmount; + buy[i] = buyAmount; + signatures[i] = signOrder(orders[i], maker1PrivateKey); + + // Creating a corresponding buy order + (sellAmount, buyAmount) = getRandomOrderAmounts( + maker2, + maker1, + usdc, + weth, + buy_qty, + sell_qty + ); + orders[size + i] = createOrder( + size + i, + sellAmount, + buyAmount, + maker2, + usdc, + weth + ); + sell[size + i] = sellAmount; + buy[size + i] = buyAmount; + signatures[size + i] = signOrder( + orders[size + i], + maker2PrivateKey + ); + } } -} function createOrder( - uint nonce, - uint256 sellAmount, - uint256 buyAmount, - address maker, - IERC20 sellToken, - IERC20 buyToken -) private view returns (OrderEngine.Order memory) { - return OrderEngine.Order( - nonce, - block.timestamp + 3600, - sellAmount, - buyAmount, - 0, // fee - maker, - operator, - maker, - sellToken, - buyToken, - true, // is partially fillable - "0x", // facilitator call data - "", // predicate calldata - "0x", // pre-interaction data - "0x" // post-interaction data - ); -} - - function signOrder(OrderEngine.Order memory order, uint256 privateKey) private view returns (bytes memory) { - (uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, _hashTypedDataV4(OrderEngine.hash(order))); - return abi.encodePacked(r, s, v); + uint nonce, + uint256 sellAmount, + uint256 buyAmount, + address maker, + IERC20 sellToken, + IERC20 buyToken + ) private view returns (OrderEngine.Order memory) { + return + OrderEngine.Order( + nonce, + block.timestamp + 9000, + sellAmount, + buyAmount, + 0, // fee + maker, + operator, + maker, + sellToken, + buyToken, + true, // is partially fillable + "0x", // facilitator call data + "", // predicate calldata + "0x", // pre-interaction data + "0x" // post-interaction data + ); } + function signOrder( + OrderEngine.Order memory order, + uint256 privateKey) private view returns (bytes memory) { + (uint8 v, bytes32 r, bytes32 s) = vm.sign( + privateKey, + _hashTypedDataV4(OrderEngine.hash(order)) + ); + return abi.encodePacked(r, s, v);} - + function determineOrderSize() private view returns (uint) { uint256 blockGasLimit = block.gaslimit; // Get the current block gas limit uint256 estimatedGasPerTransaction = 21000; // Estimate gas per transaction (adjust as needed) @@ -353,39 +400,68 @@ contract EngineFuzzTest is Test { } function getRandomOrderAmounts( - address maker1, - address maker2, - IERC20 sellToken, - IERC20 buyToken, - uint256 buy_qty, - uint256 sell_qty -) private view returns (uint256, uint256) { - uint256 sellAmount; - uint256 buyAmount; - - // If the sellToken is WETH, set sellAmount to 1 ether, else to 2,200,000,000 (assumed USDC) - if (address(sellToken) == address(weth)) { - sellAmount = buy_qty * 1 ether; - } else { - sellAmount = buy_qty * 1000000; + address maker1, + address maker2, + IERC20 sellToken, + IERC20 buyToken, + uint256 buy_qty, + uint256 sell_qty + ) private view returns (uint256, uint256) { + uint256 sellAmount; + uint256 buyAmount; + + // If the sellToken is WETH, set sellAmount to 1 ether, else to 2,200,000,000 (assumed USDC) + if (address(sellToken) == address(weth)) { + sellAmount = buy_qty * 1 ether; + } else { + sellAmount = buy_qty * 1000000; + } + + // If the buyToken is WETH, set buyAmount to 1 ether, else to 2,200,000,000 (assumed USDC) + if (address(buyToken) == address(weth)) { + buyAmount = sell_qty * 1 ether; + } else { + buyAmount = sell_qty * 1000000; + } + + return (sellAmount, buyAmount); } - // If the buyToken is WETH, set buyAmount to 1 ether, else to 2,200,000,000 (assumed USDC) - if (address(buyToken) == address(weth)) { - buyAmount = sell_qty* 1 ether; - } else { - buyAmount = sell_qty * 1000000; + function random() private view returns (uint256) { + return + uint256( + keccak256(abi.encodePacked(block.timestamp, block.difficulty)) + ); } - return (sellAmount, buyAmount); -} - - function random() private view returns (uint256) { - return uint256(keccak256(abi.encodePacked(block.timestamp, block.difficulty))); + function _hashTypedDataV4( + bytes32 structHash + ) internal view virtual returns (bytes32) { + return + ECDSA.toTypedDataHash( + advancedOrderEngine.DOMAIN_SEPARATOR(), + structHash + ); } - function _hashTypedDataV4(bytes32 structHash) internal view virtual returns (bytes32) { - return ECDSA.toTypedDataHash(advancedOrderEngine.DOMAIN_SEPARATOR(), structHash); + function getOrder1() private view returns (OrderEngine.Order memory) { + return + OrderEngine.Order( + 0, // nonce value + block.timestamp + 3600, // valid till + 4800000000000000, // 0.0048 weth - sell token + 10000000, // 10 USDC - buy amount + 0, // fee + maker1, // Maker's address + operator, // Taker's Ethereum address (or null for public order) + maker1, // Recipient's Ethereum address + weth, // MATIC token address - sell token + usdc, // USDC token address - buy token + true, // is partially fillable + "0x", // facilitator call data + "", // predicate calldata + "0x", // pre-interaction data + "0x" // post-interaction data + ); } - - } \ No newline at end of file +}