Skip to content

Commit

Permalink
Optimization/suggestions from audit part2 (#115)
Browse files Browse the repository at this point in the history
* chore: [internal-s11] pragma version

* docs: [internal-s12] docsstring

* optimization: [internal-s15] add error parameter for better debugging

* optimization: [internal-s13] a collection of modifications according audit feedback

* optimization: [internal-s16] wrap external call errors inside our custom error

* feat: Revert if contract size exceeds limit (#112)

* feat: Remove duplicate import (#114)

* Internal Audit (#113)

* feat: add indexed keyword for ProtocolFeeControllerUpdated

* optimization: add name for parameters in mapping

* optimization: add visibility modifier for constant variables

* optimization: readability

* optimization: remove unused code

* docs: better naming for IProtocolFees

* docs: better docstring for IVaultToken

* fix: tag missing memory-safe for assembly block

* docs: aligns IVaultToken interfaces with actually implementation

* chore: resolving issues

* docs

* test: added test cases to make sure revert reason bubble up when doing external hook call

---------

Co-authored-by: Chef Snoopy <[email protected]>
  • Loading branch information
chefburger and ChefSnoopy authored Jul 25, 2024
1 parent be21978 commit 870eb15
Show file tree
Hide file tree
Showing 124 changed files with 723 additions and 455 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
247817
247837
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23506
23620
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133718
133834
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32345
32347
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142521
142745
Original file line number Diff line number Diff line change
@@ -1 +1 @@
288323
288502
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126826
127005
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114436
114439
Original file line number Diff line number Diff line change
@@ -1 +1 @@
171727
171842
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177608
177723
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132594
132709
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162332
162352
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22555
22712
Original file line number Diff line number Diff line change
@@ -1 +1 @@
352369
352371
Original file line number Diff line number Diff line change
@@ -1 +1 @@
167810
167812
Original file line number Diff line number Diff line change
@@ -1 +1 @@
239189
239219
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
164628
164624
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
109453
109451
Original file line number Diff line number Diff line change
@@ -1 +1 @@
150070
150114
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115732
115846
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132492
132270
Original file line number Diff line number Diff line change
@@ -1 +1 @@
165173
164836
Original file line number Diff line number Diff line change
@@ -1 +1 @@
152527
152376
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
72726
72718
Original file line number Diff line number Diff line change
@@ -1 +1 @@
144720
144569
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
88821
88813
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
72729
72721
Original file line number Diff line number Diff line change
@@ -1 +1 @@
32081
32083
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7208
7403
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#collectFee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25351
25466
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenFlashloan.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118602
119050
Original file line number Diff line number Diff line change
@@ -1 +1 @@
42803
42915
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultTest#lockSettledWhenSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
42799
42911
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2273
2267
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2614
2246
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Pancake v4 Core

1. `0.8.24` in `foundry.toml` as we need `tload/tstore` support
1. `0.8.26` in `foundry.toml` as it provides better gas optimization
2. `.prettierrc` explictly set `singleQuote: false` to overwrite any local dev's settings

## Running test
Expand Down
2 changes: 1 addition & 1 deletion src/Extsload.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2024 PancakeSwap
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

/// @notice This code is adapted from
// https://github.com/RageTrade/core/blob/main/contracts/utils/Extsload.sol
Expand Down
2 changes: 1 addition & 1 deletion src/PausableRole.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2024 PancakeSwap
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

import {IPausableRole} from "./interfaces/IPausableRole.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
Expand Down
8 changes: 6 additions & 2 deletions src/ProtocolFees.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2024 PancakeSwap
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

import {PausableRole} from "./PausableRole.sol";
import {Currency} from "./types/Currency.sol";
Expand All @@ -15,9 +15,13 @@ abstract contract ProtocolFees is IProtocolFees, PausableRole {
using PoolIdLibrary for PoolKey;
using ProtocolFeeLibrary for uint24;

/// @inheritdoc IProtocolFees
mapping(Currency currency => uint256 amount) public protocolFeesAccrued;

/// @inheritdoc IProtocolFees
IProtocolFeeController public protocolFeeController;

/// @inheritdoc IProtocolFees
IVault public immutable vault;

uint256 private immutable controllerGasLimit;
Expand All @@ -32,7 +36,7 @@ abstract contract ProtocolFees is IProtocolFees, PausableRole {
/// @inheritdoc IProtocolFees
function setProtocolFee(PoolKey memory key, uint24 newProtocolFee) external virtual {
if (msg.sender != address(protocolFeeController)) revert InvalidCaller();
if (!newProtocolFee.validate()) revert FeeTooLarge();
if (!newProtocolFee.validate()) revert ProtocolFeeTooLarge(newProtocolFee);
PoolId id = key.toId();
_setProtocolFee(id, newProtocolFee);
emit ProtocolFeeUpdated(id, newProtocolFee);
Expand Down
2 changes: 1 addition & 1 deletion src/Vault.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2024 PancakeSwap
pragma solidity ^0.8.24;
pragma solidity 0.8.26;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IVault, IVaultToken} from "./interfaces/IVault.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/VaultToken.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2024 PancakeSwap
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

import {Currency} from "./types/Currency.sol";
import {IVaultToken} from "./interfaces/IVaultToken.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IERC20Minimal.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

/// @title Minimal ERC20 interface for PancakeSwap
/// @notice Contains a subset of the full ERC20 interface that is used in PancakeSwap V3
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IExtsload.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

interface IExtsload {
/// @notice Called by external contracts to access granular pool state
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IHooks.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

interface IHooks {
function getHooksRegistrationBitmap() external view returns (uint16);
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/ILockCallback.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

/// @notice Interface for the callback executed when an address locks the vault
interface ILockCallback {
/// @notice Called by the pool manager on `msg.sender` when a lock is acquired
/// @param data The data that was passed to the call to lock
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IPausableRole.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

interface IPausableRole {
/// @notice Thrown when the caller does not have the pausable role or is not owner
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/IPoolManager.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

import {IHooks} from "./IHooks.sol";
import {PoolKey} from "../types/PoolKey.sol";
Expand All @@ -11,7 +11,7 @@ interface IPoolManager {
error PoolNotInitialized();

/// @notice PoolKey must have currencies where address(currency0) < address(currency1)
error CurrenciesInitializedOutOfOrder();
error CurrenciesInitializedOutOfOrder(address currency0, address currency1);

/// @notice Thrown when a call to updateDynamicLPFee is made by an address that is not the hook,
/// or on a pool is not a dynamic fee pool.
Expand Down
9 changes: 6 additions & 3 deletions src/interfaces/IProtocolFeeController.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

import {PoolKey} from "../types/PoolKey.sol";

interface IProtocolFeeController {
/// @notice Returns the protocol fee for a pool given the conditions of this contract
/// @notice Get the protocol fee for a pool given the conditions of this contract
/// @param poolKey The pool key to identify the pool. The controller may want to use attributes on the pool
/// to determine the protocol fee, hence the entire key is needed.
function protocolFeeForPool(PoolKey memory poolKey) external view returns (uint24);
/// @return protocolFee The pool's protocol fee, expressed in hundredths of a bip. The upper 12 bits are for 1->0
/// and the lower 12 are for 0->1. The maximum is 1000 - meaning the maximum protocol fee is 0.1%.
/// the protocolFee is taken from the input first, then the lpFee is taken from the remaining input
function protocolFeeForPool(PoolKey memory poolKey) external view returns (uint24 protocolFee);
}
13 changes: 11 additions & 2 deletions src/interfaces/IProtocolFees.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

import {Currency} from "../types/Currency.sol";
import {IProtocolFeeController} from "./IProtocolFeeController.sol";
import {PoolId} from "../types/PoolId.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {IVault} from "./IVault.sol";

interface IProtocolFees {
/// @notice Thrown when the protocol fee exceeds the upper limit.
error FeeTooLarge();
error ProtocolFeeTooLarge(uint24 fee);
/// @notice Thrown when not enough gas is provided to look up the protocol fee
error ProtocolFeeCannotBeFetched();
/// @notice Thrown when user not authorized to set or collect protocol fee
Expand All @@ -29,6 +30,10 @@ interface IProtocolFees {
/// @return amount The amount of protocol fees accrued in the given currency
function protocolFeesAccrued(Currency currency) external view returns (uint256 amount);

/// @notice Returns the current protocol fee controller address
/// @return IProtocolFeeController The currency protocol fee controller
function protocolFeeController() external view returns (IProtocolFeeController);

/// @notice Sets the protocol's swap fee for the given pool
/// @param key The pool key for which to set the protocol fee
/// @param newProtocolFee The new protocol fee to set
Expand All @@ -46,4 +51,8 @@ interface IProtocolFees {
function collectProtocolFees(address recipient, Currency currency, uint256 amount)
external
returns (uint256 amountCollected);

/// @notice Returns the vault where the protocol fees are safely stored
/// @return IVault The address of the vault
function vault() external view returns (IVault);
}
2 changes: 1 addition & 1 deletion src/interfaces/IVault.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

import {Currency} from "../types/Currency.sol";
import {PoolId} from "../types/PoolId.sol";
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IVaultToken.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
pragma solidity ^0.8.0;

import {PoolId} from "../types/PoolId.sol";
import {PoolKey} from "../types/PoolKey.sol";
Expand Down
Loading

0 comments on commit 870eb15

Please sign in to comment.