Skip to content

Commit

Permalink
feat: add pausable (#10)
Browse files Browse the repository at this point in the history
* feat: add pausable

* refactor: Remove unused imports in IPausableRole

* refactor: Tweak to !hasPausableRole[msg.sender]
  • Loading branch information
ChefMist authored Apr 8, 2024
1 parent ae7b6c3 commit 02e4b30
Show file tree
Hide file tree
Showing 56 changed files with 345 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testBurnSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143677
143699
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134868
134890
Original file line number Diff line number Diff line change
@@ -1 +1 @@
109021
109043
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testMintSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
298905
298927
2 changes: 1 addition & 1 deletion .forge-snapshots/BinHookTest#testSwapSucceedsWithHook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
141089
141133
Original file line number Diff line number Diff line change
@@ -1 +1 @@
92245
92267
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9201
9179
Original file line number Diff line number Diff line change
@@ -1 +1 @@
67437
67459
Original file line number Diff line number Diff line change
@@ -1 +1 @@
150992
151014
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasBurnOneBin.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
68616
68638
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
53952
53974
Original file line number Diff line number Diff line change
@@ -1 +1 @@
968047
968069
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121561
121583
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337237
337259
Original file line number Diff line number Diff line change
@@ -1 +1 @@
56298
56320
Original file line number Diff line number Diff line change
@@ -1 +1 @@
93040
93084
Original file line number Diff line number Diff line change
@@ -1 +1 @@
95025
95069
Original file line number Diff line number Diff line change
@@ -1 +1 @@
74469
74513
Original file line number Diff line number Diff line change
@@ -1 +1 @@
318945
318967
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testNoOpGas_Burn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
41876
41898
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19493
19515
Original file line number Diff line number Diff line change
@@ -1 +1 @@
37221
37243
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testNoOpGas_Mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
37345
37367
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testNoOpGas_Swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22699
22743
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testSetMasterChef.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24243
24265
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8126
8148
Original file line number Diff line number Diff line change
@@ -1 +1 @@
348452
350592
Original file line number Diff line number Diff line change
@@ -1 +1 @@
61000
61140
Original file line number Diff line number Diff line change
@@ -1 +1 @@
241390
243530
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
84138
84302
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
53445
53609
Original file line number Diff line number Diff line change
@@ -1 +1 @@
36477
36544
Original file line number Diff line number Diff line change
@@ -1 +1 @@
43322
43462
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#setLmPool.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
26294
26272
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#setMasterChef.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30760
30715
Original file line number Diff line number Diff line change
@@ -1 +1 @@
56445
56609
Original file line number Diff line number Diff line change
@@ -1 +1 @@
104543
104707
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25044269
25044433
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
36336
36500
Original file line number Diff line number Diff line change
@@ -1 +1 @@
103041
103205
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
41986
42150
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
36339
36503
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19448
19612
Original file line number Diff line number Diff line change
@@ -1 +1 @@
37427
37494
Original file line number Diff line number Diff line change
@@ -1 +1 @@
27615
29755
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#testNoOp_gas_Swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21897
22061
2 changes: 1 addition & 1 deletion .forge-snapshots/ExtsloadTest#extsloadInBatch.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11418
11352
5 changes: 2 additions & 3 deletions src/Fees.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// Copyright (C) 2024 PancakeSwap
pragma solidity ^0.8.24;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

import {PausableRole} from "./PausableRole.sol";
import {Currency} from "./types/Currency.sol";
import {IProtocolFeeController} from "./interfaces/IProtocolFeeController.sol";
import {IFees} from "./interfaces/IFees.sol";
import {PoolKey} from "./types/PoolKey.sol";
import {IVault} from "./interfaces/IVault.sol";

abstract contract Fees is IFees, Ownable {
abstract contract Fees is IFees, PausableRole {
uint8 public constant MIN_PROTOCOL_FEE_DENOMINATOR = 4;

mapping(Currency currency => uint256) public protocolFeesAccrued;
Expand Down
36 changes: 36 additions & 0 deletions src/PausableRole.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2024 PancakeSwap
pragma solidity ^0.8.24;

import {IPausableRole} from "./interfaces/IPausableRole.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Pausable} from "@openzeppelin/contracts/security/Pausable.sol";

/// @notice Allow owner and multiple accounts to pause but only owner can unpause
/// @dev Potentially allow security partners to programatically pause()
abstract contract PausableRole is IPausableRole, Ownable, Pausable {
mapping(address => bool) public hasPausableRole;

modifier onlyPausableRoleOrOwner() {
if (msg.sender != owner() && !hasPausableRole[msg.sender]) revert NoPausableRole();
_;
}

function pause() public override onlyPausableRoleOrOwner {
_pause();
}

function unpause() public override onlyOwner {
_unpause();
}

function grantPausableRole(address account) public override onlyOwner {
hasPausableRole[account] = true;
emit PausableRoleGranted(account);
}

function revokePausableRole(address account) public override onlyOwner {
hasPausableRole[account] = false;
emit PausableRoleRevoked(account);
}
}
22 changes: 22 additions & 0 deletions src/interfaces/IPausableRole.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

interface IPausableRole {
/// @notice Thrown when the caller does not have the pausable role or is not owner
error NoPausableRole();

event PausableRoleGranted(address indexed account);
event PausableRoleRevoked(address indexed account);

/// @notice Pause the contract, called by the owner or an account with the pausable role
function pause() external;

/// @notice Unpause the contract, called by the owner
function unpause() external;

/// @notice Grant the pausable role to an account, called by the owner
function grantPausableRole(address account) external;

/// @notice Revoke the pausable role to an account, called by the owner
function revokePausableRole(address account) external;
}
5 changes: 5 additions & 0 deletions src/pool-cl/CLPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ contract CLPoolManager is ICLPoolManager, Fees, Extsload {
ICLPoolManager.ModifyLiquidityParams memory params,
bytes calldata hookData
) external override poolManagerMatch(address(key.poolManager)) returns (BalanceDelta delta) {
// Do not allow add liquidity when paused()
if (paused() && params.liquidityDelta > 0) revert PoolPaused();

PoolId id = key.toId();
_checkPoolInitialized(id);

Expand Down Expand Up @@ -196,6 +199,7 @@ contract CLPoolManager is ICLPoolManager, Fees, Extsload {
external
override
poolManagerMatch(address(key.poolManager))
whenNotPaused
returns (BalanceDelta delta)
{
PoolId id = key.toId();
Expand Down Expand Up @@ -258,6 +262,7 @@ contract CLPoolManager is ICLPoolManager, Fees, Extsload {
external
override
poolManagerMatch(address(key.poolManager))
whenNotPaused
returns (BalanceDelta delta)
{
PoolId id = key.toId();
Expand Down
2 changes: 2 additions & 0 deletions src/pool-cl/interfaces/ICLPoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ interface ICLPoolManager is IFees, IPoolManager, IExtsload {
error TickSpacingTooSmall();
/// @notice Error thrown when Unauthorized caller
error UnauthorizedCaller();
/// @notice Error thrown when add liquidity is called when paused()
error PoolPaused();

/// @notice Emitted when a new pool is initialized
/// @param id The abi encoded hash of the pool key struct for the new pool
Expand Down
17 changes: 10 additions & 7 deletions test/Extsload.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import {IProtocolFeeController} from "../src/interfaces/IProtocolFeeController.s

contract ExtsloadTest is Test, GasSnapshot {
// Slot
// 0 PoolManager#Ownable#_owner
// 1 PooAlManager#Fees#protocolFeesAccrued
// 2 PooAlManager#Fees#protocolFeeController
// 3 PooAlManager#pools
// 0 PoolManager#PausableRole#Pausable#_paused and PooAlManager#PausableRole#Ownable#_owner
// 1 PoolManager#PausableRole#hasPausableRole
// 2 PooAlManager#Fees#protocolFeesAccrued
// 3 PooAlManager#Fees#protocolFeeController
// 4 PooAlManager#pools
ICLPoolManager poolManager;

function setUp() public {
Expand All @@ -25,19 +26,21 @@ contract ExtsloadTest is Test, GasSnapshot {
}

function testExtsload() public {
// as contract is not paused, slot0 is 0x0...0_owner_address,
// if paused, slot0 is 0x0...1_owner_address
snapStart("ExtsloadTest#extsload");
bytes32 slot0 = poolManager.extsload(0x00);
snapEnd();
assertEq(abi.encode(slot0), abi.encode(address(this)));

bytes32 slot2 = poolManager.extsload(bytes32(uint256(0x02)));
assertEq(abi.encode(slot2), abi.encode(address(0xabcd)));
bytes32 slot3 = poolManager.extsload(bytes32(uint256(0x03)));
assertEq(abi.encode(slot3), abi.encode(address(0xabcd)));
}

function testExtsloadInBatch() public {
bytes32[] memory slots = new bytes32[](2);
slots[0] = 0x00;
slots[1] = bytes32(uint256(0x02));
slots[1] = bytes32(uint256(0x03));
snapStart("ExtsloadTest#extsloadInBatch");
slots = poolManager.extsload(slots);
snapEnd();
Expand Down
Loading

0 comments on commit 02e4b30

Please sign in to comment.