From bba39bc963f39572a10d61f8806026ab165d7728 Mon Sep 17 00:00:00 2001 From: onibakuchi Date: Mon, 3 Jun 2024 22:56:04 +0400 Subject: [PATCH] snapshot --- .gas-snapshot | 24 ++++-------------------- README.md | 16 ++++++++-------- test/GrimReaper.t.sol | 23 ++++++++++++++--------- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index b3c186d..78bb465 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,20 +1,4 @@ -GrimReaperHuffTest:testFuzz_Liquidate(uint256,address) (runs: 257, μ: 390235, ~: 390503) -GrimReaperHuffTest:testLiquidate() (gas: 390412) -GrimReaperHuffTest:testOnlyOwner(address) (runs: 257, μ: 20235, ~: 20235) -GrimReaperHuffTest:testRecoverERC20() (gas: 330912) -GrimReaperHuffTest:testRevertIfLiquidationFail() (gas: 52407) -GrimReaperSolTest:testFuzz_Liquidate(uint256,address) (runs: 257, μ: 390396, ~: 390912) -GrimReaperSolTest:testLiquidate() (gas: 390820) -GrimReaperSolTest:testOnlyOwner(address) (runs: 257, μ: 18922, ~: 18922) -GrimReaperSolTest:testRecoverERC20() (gas: 331740) -GrimReaperSolTest:testRevertIfLiquidationFail() (gas: 51145) -OptimizedGrimReaperSolTest:testFuzz_Liquidate(uint256,address) (runs: 257, μ: 390301, ~: 390568) -OptimizedGrimReaperSolTest:testLiquidate() (gas: 390477) -OptimizedGrimReaperSolTest:testOnlyOwner(address) (runs: 257, μ: 20388, ~: 20388) -OptimizedGrimReaperSolTest:testRecoverERC20() (gas: 330873) -OptimizedGrimReaperSolTest:testRevertIfLiquidationFail() (gas: 52467) -OptimizedGrimReaperSolV2Test:testFuzz_Liquidate(uint256,address) (runs: 257, μ: 390417, ~: 390685) -OptimizedGrimReaperSolV2Test:testLiquidate() (gas: 390594) -OptimizedGrimReaperSolV2Test:testOnlyOwner(address) (runs: 257, μ: 20498, ~: 20498) -OptimizedGrimReaperSolV2Test:testRecoverERC20() (gas: 330873) -OptimizedGrimReaperSolV2Test:testRevertIfLiquidationFail() (gas: 52613) \ No newline at end of file +GrimReaperHuffTest:testLiquidate() (gas: 390453) +GrimReaperSolTest:testLiquidate() (gas: 391254) +OptimizedGrimReaperSolTest:testLiquidate() (gas: 390518) +OptimizedGrimReaperSolV2Test:testLiquidate() (gas: 390635) \ No newline at end of file diff --git a/README.md b/README.md index 14efe45..4001531 100644 --- a/README.md +++ b/README.md @@ -38,17 +38,17 @@ For more information on how to use Foundry, check out the [Foundry Github Reposi | Single Liquidation (Optimizer runs: 200) | Gas Used | Bytecode Size (kB) | | ---------------------------------------- | -------- | ------------------ | -| Solidity Contract | 97933 | 1.005 | -| Assembly | 97295 | 0.303 | -| Assembly (GrimReaper V2) | 97264 | 0.317 | -| Huff Contract | 97234 | 0.245 | +| Solidity Contract | 98783 | 1.005 | +| Assembly | 97688 | 0.303 | +| Assembly (GrimReaper V2) | 97657 | 0.317 | +| Huff Contract | 97627 | 0.245 | | Single Liquidation (Optimizer runs: 10_000_000) | Gas Used | Bytecode Size (kB) | | ----------------------------------------------- | -------- | ------------------ | -| Solidity Contract | 97339 | 1.308 | -| Assembly | 96755 | 0.330 | -| Assembly (GrimReaper V2) | 96724 | 0.344 | -| Huff Contract | 96706 | 0.245 | +| Solidity Contract | 98127 | 1.308 | +| Assembly | 97098 | 0.330 | +| Assembly (GrimReaper V2) | 97067 | 0.344 | +| Huff Contract | 97049 | 0.245 | > `66270` gas is used for the liquidation logic itself on mock Aave v3 pool. diff --git a/test/GrimReaper.t.sol b/test/GrimReaper.t.sol index efe5734..73f164c 100644 --- a/test/GrimReaper.t.sol +++ b/test/GrimReaper.t.sol @@ -58,16 +58,19 @@ abstract contract GrimReaperBaseTest is Test { vm.expectCall( POOL, abi.encodeWithSelector(MockPool.liquidationCall.selector, collateral, debt, user, amount, false) ); - vm.prank(owner); - _callLiquidate(address(collateral), address(debt), user, amount); + _callLiquidate(owner, address(collateral), address(debt), user, amount); assertEq(debt.balanceOf(address(reaper)), 0); assertEq(collateral.balanceOf(address(reaper)), liquidationBonus); } - function _callLiquidate(address _col, address _debt, address _user, uint256 _debtToCover) public virtual { + function _callLiquidate(address caller, address _col, address _debt, address _user, uint256 _debtToCover) + public + virtual + { uint256 _before = gasleft(); - (bool s, ) = address(reaper).call(abi.encodeCall(reaper.execute,(_col, _debt, _user, _debtToCover))); + vm.prank(caller); + (bool s,) = address(reaper).call(abi.encodeCall(reaper.execute, (_col, _debt, _user, _debtToCover))); uint256 _after = gasleft(); console2.log("Gas used: ", (_before - _after)); require(s, "ExpectRevert: liquidation failed"); @@ -76,8 +79,7 @@ abstract contract GrimReaperBaseTest is Test { function testRevertIfLiquidationFail() public { pool.setLiquidation(false); vm.expectRevert("ExpectRevert: liquidation failed"); - vm.prank(owner); - this._callLiquidate(address(collateral), address(debt), address(0xcafe), 1000); + this._callLiquidate(owner, address(collateral), address(debt), address(0xcafe), 1000); } function testRecoverERC20() public virtual { @@ -97,8 +99,7 @@ abstract contract GrimReaperBaseTest is Test { vm.assume(non_user != owner); vm.expectRevert(); - vm.prank(non_user); - _callLiquidate(address(collateral), address(debt), address(0xcafe), 1000); + _callLiquidate(non_user, address(collateral), address(debt), address(0xcafe), 1000); vm.expectRevert(); vm.prank(non_user); @@ -117,10 +118,14 @@ contract OptimizedGrimReaperSolTest is GrimReaperBaseTest { reaper = GrimReaper(address(new OptimizedGrimReaper())); } - function _callLiquidate(address _col, address _debt, address _user, uint256 _debtToCover) public override { + function _callLiquidate(address caller, address _col, address _debt, address _user, uint256 _debtToCover) + public + override + { bytes memory payload = getLiquidationPayload(_col, _debt, _user, _debtToCover); uint256 _before = gasleft(); + vm.prank(caller); (bool success,) = address(reaper).call(payload); uint256 _after = gasleft(); console2.log("Gas used: ", (_before - _after));