Skip to content

Commit

Permalink
Tag Fuse2 borrowers in Permit2Manager and fix slither (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenshively authored Nov 30, 2023
1 parent da971c6 commit e543bb4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
18 changes: 16 additions & 2 deletions .github/workflows/slither.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,28 @@ jobs:

steps:
- uses: actions/checkout@v3
with:
# This is a workaround for the fact that the latest version of crytic-compile
# does not respect the `target` argument for foundry compiles. Instead of
# telling slither to look inside `core`, we need to copy `core` to the root.
#
# Should be fixed after: https://github.com/crytic/crytic-compile/pull/515
submodules: recursive
# workaround (continued)
- name: Copy core to root
run: |
rm -rf docs periphery foundry.toml package.json remappings.txt yarn.lock
git mv core/lib lib
cp -r ./core/* ./
rm -rf core
- name: Run Slither
uses: crytic/slither-action@v0.3.0
id: run-slither
with:
target: 'core/'
# target: './core/'
fail-on: none
slither-args: --config-file core/slither.config.json
# slither-args: --config-file core/slither.config.json
sarif: slither.results.sarif

- name: Upload SARIF file
Expand Down
1 change: 1 addition & 0 deletions core/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ libs = ['lib']
out = 'build'
cache_path = 'cache'

solc_version = '0.8.17'
via_ir = true
optimizer = true
optimizer_runs = 4294967295
Expand Down
3 changes: 3 additions & 0 deletions periphery/script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {BorrowerNFT, IBorrowerURISource} from "src/borrower-nft/BorrowerNFT.sol"
import {IUniswapPositionNFT} from "src/interfaces/IUniswapPositionNFT.sol";
import {BoostManager} from "src/managers/BoostManager.sol";
import {FrontendManager} from "src/managers/FrontendManager.sol";
import {Permit2Manager} from "src/managers/Permit2Manager.sol";
import {SimpleManager} from "src/managers/SimpleManager.sol";
import {UniswapNFTManager} from "src/managers/UniswapNFTManager.sol";

Expand Down Expand Up @@ -63,6 +64,8 @@ contract DeployScript is Script {
BorrowerNFT borrowerNft = new BorrowerNFT{salt: TAG}(factory, uriSource);

new BoostManager{salt: TAG}(factory, address(borrowerNft), uniswapNft);
new Permit2Manager{salt: TAG}(permit2, factory, address(borrowerNft));

new FrontendManager{salt: TAG}(factory);
new SimpleManager{salt: TAG}();
new UniswapNFTManager{salt: TAG}(factory, uniswapNft);
Expand Down
19 changes: 19 additions & 0 deletions periphery/src/BorrowerLens.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.17;

import {ClonesWithImmutableArgs} from "clones-with-immutable-args/ClonesWithImmutableArgs.sol";
import {FixedPointMathLib as SoladyMath} from "solady/utils/FixedPointMathLib.sol";
import {IUniswapV3Pool} from "v3-core/contracts/interfaces/IUniswapV3Pool.sol";

Expand All @@ -10,13 +11,31 @@ import {LiquidityAmounts} from "aloe-ii-core/libraries/LiquidityAmounts.sol";
import {square, mulDiv128, mulDiv128Up} from "aloe-ii-core/libraries/MulDiv.sol";
import {TickMath} from "aloe-ii-core/libraries/TickMath.sol";
import {Borrower} from "aloe-ii-core/Borrower.sol";
import {Factory} from "aloe-ii-core/Factory.sol";

import {Uniswap} from "./libraries/Uniswap.sol";

contract BorrowerLens {
using SoladyMath for uint256;
using Uniswap for Uniswap.Position;

function predictBorrowerAddress(
IUniswapV3Pool pool,
address owner,
bytes12 salt,
address caller,
Factory factory
) external view returns (address borrower) {
(, , Borrower implementation) = factory.getMarket(pool);

borrower = ClonesWithImmutableArgs.predictDeterministicAddress(
address(implementation),
bytes32(bytes.concat(bytes20(caller), salt)),
address(factory),
abi.encodePacked(owner)
);
}

/// @dev Mirrors the logic in `BalanceSheet.isHealthy`, but returns numbers instead of a boolean
function getHealth(
Borrower account,
Expand Down
12 changes: 6 additions & 6 deletions periphery/src/managers/Permit2Manager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ contract Permit2Manager is IManager {
// | value | start | end | length |
// | owner | 0 | 20 | 20 |
// | permit2 selector | 20 | 24 | 4 |
// | permit2 args | 24 | 248 | 224 |
// | permit2 sig | 248 | 313 | 65 |
// | borrower call | 313 | ? | ? |
// | permit2 args | 24 | 408 | 384 |
// | borrower call | 408 | ? | ? |
// -------------------------------------------

// Get references to the calldata for the 2 calls we're going to make
bytes calldata dataPermit2 = data[20:313];
bytes calldata dataBorrower = data[313:];
bytes calldata dataPermit2 = data[20:408];
bytes calldata dataBorrower = data[408:];

// Before calling `PERMIT2`, verify
// (a) correct function selector
Expand All @@ -68,6 +67,7 @@ contract Permit2Manager is IManager {
(success, ) = msg.sender.call(dataBorrower); // solhint-disable-line avoid-low-level-calls
if (!success) revert BorrowerCallFailed();

return 0;
// Tag Borrower with `Fuse2Borrower()` function selector so we can identify it on the frontend
return 0x83ee755b << 144;
}
}
8 changes: 8 additions & 0 deletions periphery/test/borrower-nft/ERC721Z.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.17;

import "forge-std/Test.sol";

import {LibRLP} from "solady/utils/LibRLP.sol";
import {ERC721} from "solmate/tokens/ERC721.sol";

import {ERC721Z, SafeSSTORE2, BytesLib} from "src/borrower-nft/ERC721Z.sol";
Expand Down Expand Up @@ -267,6 +268,13 @@ contract ERC721ZTest is Test {
excludeSender(address(mock));
excludeSender(address(baseline));
excludeSender(address(harness));

for (uint256 i = 0; i < 256; i++) {
// Workaround for a bug in forge where if an address has been used as a sender,
// deploying to that address will fail. We need to make sure that ERC721Z's SSTORE2
// deployments will work, so we exclude all those addresses.
excludeSender(LibRLP.computeAddress(address(mock), i));
}
}

function invariant_totalSupply() public {
Expand Down

0 comments on commit e543bb4

Please sign in to comment.