Skip to content

Commit

Permalink
optimize(huff): remove redundant swap1
Browse files Browse the repository at this point in the history
  • Loading branch information
massun-onibakuchi committed Jun 3, 2024
1 parent 213755a commit 6e0081e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ For more information on how to use Foundry, check out the [Foundry Github Reposi
| Solidity Contract | 97933 | 1.005 |
| Assembly | 97295 | 0.303 |
| Assembly (GrimReaper V2) | 97264 | 0.317 |
| Huff Contract | 97234 | 0.254 |
| Huff Contract | 97234 | 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.254 |
| Huff Contract | 96706 | 0.245 |

> `66270` gas is used for the liquidation logic itself on mock Aave v3 pool.
Expand Down
10 changes: 5 additions & 5 deletions src/GrimReaper.huff
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
staticcall // [success, token]

// Validate successful call
iszero error jumpi // [token]
0x00 mload // [balance, token]
0x01 swap1 sub // [(balance - 1), token]
dup1 // [success, success, token]
iszero error jumpi // [success=1, token]
// At this point, `success` is 1, so we can think this boolean as number 1
0x00 mload // [balance, 1, token]
sub // [(balance - 1), token]
}

/// @notice Receive profits from contract
Expand Down Expand Up @@ -166,6 +168,4 @@
RECOVER_ERC20()
error:
WAGMI()

WAGMI()
}
13 changes: 7 additions & 6 deletions test/GrimReaper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,19 @@ abstract contract GrimReaperBaseTest is Test {
assertEq(collateral.balanceOf(address(reaper)), liquidationBonus);
}

function _callLiquidate(address _col, address _debt, address _user, uint256 _debtToCover) internal virtual {
function _callLiquidate(address _col, address _debt, address _user, uint256 _debtToCover) public virtual {
uint256 _before = gasleft();
reaper.execute(_col, _debt, _user, _debtToCover);
(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");
}

function testRevertIfLiquidationFail() public {
pool.setLiquidation(false);
vm.expectRevert();
vm.expectRevert("ExpectRevert: liquidation failed");
vm.prank(owner);
_callLiquidate(address(collateral), address(debt), address(0xcafe), 1000);
this._callLiquidate(address(collateral), address(debt), address(0xcafe), 1000);
}

function testRecoverERC20() public virtual {
Expand Down Expand Up @@ -116,14 +117,14 @@ contract OptimizedGrimReaperSolTest is GrimReaperBaseTest {
reaper = GrimReaper(address(new OptimizedGrimReaper()));
}

function _callLiquidate(address _col, address _debt, address _user, uint256 _debtToCover) internal override {
function _callLiquidate(address _col, address _debt, address _user, uint256 _debtToCover) public override {
bytes memory payload = getLiquidationPayload(_col, _debt, _user, _debtToCover);

uint256 _before = gasleft();
(bool success,) = address(reaper).call(payload);
uint256 _after = gasleft();
console2.log("Gas used: ", (_before - _after));
require(success, "liquidation failed");
require(success, "ExpectRevert: liquidation failed");
}

function getLiquidationPayload(address _col, address _debt, address _user, uint256 _debtToCover)
Expand Down

0 comments on commit 6e0081e

Please sign in to comment.