Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: assume trust of protocol fee controller #223

Merged
merged 6 commits into from
Jan 22, 2025

Conversation

ChefMist
Copy link
Collaborator

@ChefMist ChefMist commented Jan 21, 2025

Context

Currently pool initialization requires a buffer of 1% of block.gasLimit. This means that for BNB chain (with 100m gas), it require user to allocate > 1mil in gas.

Problem: This result in integrator asking why do they need so much gas to initialize a pool.

Also:

  1. protocolFeeController is built by PCS (trusted)
  2. no incentive for PCS to block pool initialization with our protocolFeeController

Description

This PR assume the trust of protocol fee controller. When integrator try to initialize a pool, they will not need to specify the 1 million buffer.

Now

  • If protocolFeeController calls failed or it returns non 32 bytes data, we'll revert
  • if protocolFeeController return a value > max protocol fee, we'll revert
  • if protocolFeeController consume infinite gas, we'll revert

Previously

  • If protocolFeeController calls failed or it returns non 32 bytes data, we'll set protocolFee = 0
  • if protocolFeeController return a value > max protocol fee, we'll set protocolFee = 0
  • if protocolFeeController consume infinite gas, we'll set protocolFee = 0 as we set a cap of 1% of block.gasLimit

Copy link

| File | % Lines | % Statements | % Branches | % Funcs |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/Extsload.sol | 11.76% (2/17) | 0.00% (0/16) | 0.00% (0/1) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/Owner.sol | 50.00% (2/4) | 0.00% (0/2) | 100.00% (0/0) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/ProtocolFeeController.sol | 93.94% (31/33) | 86.11% (31/36) | 72.73% (8/11) | 100.00% (7/7) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/ProtocolFees.sol | 86.67% (26/30) | 87.50% (28/32) | 85.71% (6/7) | 100.00% (5/5) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/Vault.sol | 93.83% (76/81) | 94.05% (79/84) | 91.67% (11/12) | 90.48% (19/21) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/VaultToken.sol | 85.71% (30/35) | 82.86% (29/35) | 100.00% (4/4) | 87.50% (7/8) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/base/Ownable.sol | 88.24% (15/17) | 83.33% (10/12) | 50.00% (1/2) | 100.00% (6/6) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/base/Pausable.sol | 84.62% (11/13) | 75.00% (6/8) | 100.00% (1/1) | 100.00% (5/5) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/base/PausableRole.sol | 100.00% (8/8) | 100.00% (9/9) | 100.00% (1/1) | 100.00% (3/3) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/base/PoolManagerOwnable2Step.sol | 100.00% (7/7) | 100.00% (4/4) | 100.00% (1/1) | 100.00% (3/3) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/BipsLibrary.sol | 100.00% (3/3) | 100.00% (4/4) | 100.00% (1/1) | 100.00% (1/1) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/CustomRevert.sol | 14.29% (2/14) | 7.69% (1/13) | 100.00% (0/0) | 100.00% (1/1) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/Hooks.sol | 81.48% (22/27) | 84.21% (32/38) | 83.33% (5/6) | 100.00% (5/5) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/LPFeeLibrary.sol | 86.67% (13/15) | 86.67% (13/15) | 100.00% (1/1) | 100.00% (6/6) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/ParseBytes.sol | 50.00% (3/6) | 0.00% (0/3) | 100.00% (0/0) | 100.00% (3/3) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/ProtocolFeeLibrary.sol | 46.15% (6/13) | 22.22% (2/9) | 100.00% (0/0) | 100.00% (4/4) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/SafeCast.sol | 88.89% (16/18) | 61.11% (11/18) | 0.00% (0/5) | 100.00% (6/6) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/SettlementGuard.sol | 66.67% (14/21) | 68.18% (15/22) | 100.00% (4/4) | 100.00% (5/5) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/VaultAppDeltaSettlement.sol | 100.00% (4/4) | 100.00% (3/3) | 50.00% (1/2) | 100.00% (1/1) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/VaultReserve.sol | 33.33% (2/6) | 0.00% (0/4) | 100.00% (0/0) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/math/Encoded.sol | 40.00% (6/15) | 0.00% (0/9) | 100.00% (0/0) | 85.71% (6/7) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/math/ParametersHelper.sol | 100.00% (5/5) | 100.00% (3/3) | 100.00% (1/1) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/libraries/math/UnsafeMath.sol | 50.00% (2/4) | 0.00% (0/2) | 100.00% (0/0) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/BinPoolManager.sol | 99.01% (100/101) | 98.11% (104/106) | 80.00% (8/10) | 100.00% (14/14) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/BinPoolManagerOwner.sol | 100.00% (20/20) | 100.00% (13/13) | 100.00% (1/1) | 100.00% (8/8) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/BinHelper.sol | 90.62% (87/96) | 91.60% (109/119) | 75.00% (12/16) | 100.00% (9/9) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/BinHooks.sol | 91.76% (78/85) | 91.67% (77/84) | 72.73% (16/22) | 100.00% (11/11) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/BinPool.sol | 97.09% (167/172) | 96.88% (186/192) | 93.94% (31/33) | 100.00% (16/16) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/BinPoolParametersHelper.sol | 100.00% (4/4) | 100.00% (3/3) | 100.00% (0/0) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/BinPosition.sol | 72.73% (8/11) | 62.50% (5/8) | 100.00% (0/0) | 100.00% (4/4) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/FeeHelper.sol | 100.00% (11/11) | 100.00% (12/12) | 100.00% (0/0) | 100.00% (3/3) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/PriceHelper.sol | 100.00% (16/16) | 100.00% (21/21) | 100.00% (0/0) | 100.00% (6/6) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/math/BitMath.sol | 14.75% (9/61) | 12.90% (8/62) | 0.00% (0/16) | 100.00% (4/4) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/math/LiquidityConfigurations.sol | 87.50% (14/16) | 89.47% (17/19) | 100.00% (1/1) | 100.00% (3/3) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/math/PackedUint128Math.sol | 76.36% (42/55) | 79.69% (51/64) | 100.00% (10/10) | 86.67% (13/15) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/math/SafeCast.sol | 97.06% (66/68) | 49.25% (33/67) | 0.00% (0/32) | 100.00% (33/33) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/math/TreeMath.sol | 100.00% (89/89) | 100.00% (101/101) | 100.00% (16/16) | 100.00% (7/7) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/math/Uint128x128Math.sol | 36.49% (27/74) | 32.00% (32/100) | 20.69% (6/29) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/libraries/math/Uint256x256Math.sol | 81.63% (40/49) | 84.21% (48/57) | 100.00% (9/9) | 100.00% (8/8) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-bin/types/BinSlot0.sol | 50.00% (6/12) | 0.00% (0/6) | 100.00% (0/0) | 100.00% (6/6) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/CLPoolManager.sol | 94.94% (75/79) | 93.10% (81/87) | 75.00% (6/8) | 85.71% (12/14) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/CLPoolManagerOwner.sol | 100.00% (15/15) | 100.00% (9/9) | 100.00% (0/0) | 100.00% (6/6) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/BitMath.sol | 30.77% (4/13) | 18.18% (2/11) | 0.00% (0/4) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/CLHooks.sol | 92.31% (72/78) | 93.75% (75/80) | 78.26% (18/23) | 100.00% (9/9) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/CLPool.sol | 94.02% (110/117) | 94.02% (110/117) | 91.67% (33/36) | 100.00% (8/8) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/CLPoolGetters.sol | 100.00% (6/6) | 100.00% (3/3) | 100.00% (0/0) | 100.00% (3/3) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/CLPoolParametersHelper.sol | 100.00% (4/4) | 100.00% (3/3) | 100.00% (0/0) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/CLPosition.sol | 84.21% (16/19) | 84.21% (16/19) | 100.00% (4/4) | 100.00% (3/3) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/FullMath.sol | 70.97% (22/31) | 72.73% (24/33) | 33.33% (2/6) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/LiquidityMath.sol | 20.00% (1/5) | 0.00% (0/4) | 0.00% (0/1) | 100.00% (1/1) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/SqrtPriceMath.sol | 66.67% (38/57) | 69.84% (44/63) | 58.33% (7/12) | 100.00% (9/9) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/SwapMath.sol | 82.14% (23/28) | 81.48% (22/27) | 100.00% (6/6) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/Tick.sol | 81.48% (44/54) | 81.48% (44/54) | 100.00% (10/10) | 100.00% (6/6) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/TickBitmap.sol | 47.06% (16/34) | 50.00% (18/36) | 66.67% (2/3) | 100.00% (4/4) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/libraries/TickMath.sol | 32.26% (40/124) | 48.80% (81/166) | 95.83% (23/24) | 100.00% (4/4) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/pool-cl/types/CLSlot0.sol | 50.00% (8/16) | 0.00% (0/8) | 100.00% (0/0) | 100.00% (8/8) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/types/BalanceDelta.sol | 50.00% (2/4) | 0.00% (0/2) | 100.00% (0/0) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/types/BeforeSwapDelta.sol | 50.00% (2/4) | 0.00% (0/2) | 100.00% (0/0) | 100.00% (2/2) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/types/Currency.sol | 68.97% (20/29) | 70.00% (21/30) | 75.00% (6/8) | 100.00% (6/6) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
| src/types/PoolId.sol | 50.00% (1/2) | 0.00% (0/1) | 100.00% (0/0) | 100.00% (1/1) |
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|
|---------------------------------------------------------+--------------------+--------------------+------------------+------------------|

@@ -11,6 +11,7 @@ import {PoolKey} from "./types/PoolKey.sol";
import {PoolId} from "./types/PoolId.sol";
import {IVault} from "./interfaces/IVault.sol";
import {BipsLibrary} from "./libraries/BipsLibrary.sol";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove BipsLibrary and "using BipsLibrary for uint256;" now

address targetProtocolFeeController = address(protocolFeeController);
bytes memory data = abi.encodeCall(IProtocolFeeController.protocolFeeForPool, (key));

bool success;
uint256 returnData;
assembly ("memory-safe") {
// only load the first 32 bytes of the return data to prevent gas griefing
success := call(controllerGasLimit, targetProtocolFeeController, 0, add(data, 0x20), mload(data), 0, 32)
success := call(gas(), targetProtocolFeeController, 0, add(data, 0x20), mload(data), 0, 32)
// if success is false this wont actually be returned, instead 0 will be returned
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to edit this comments , will revert with "ProtocolFeeCannotBeFetched" , not 0

ChefSnoopy
ChefSnoopy previously approved these changes Jan 21, 2025
Copy link
Collaborator

@ChefSnoopy ChefSnoopy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@chef-omelette chef-omelette left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

src/ProtocolFees.sol Show resolved Hide resolved
chef-omelette
chef-omelette previously approved these changes Jan 22, 2025
@ChefMist ChefMist merged commit fc9c125 into main Jan 22, 2025
3 checks passed
@ChefMist ChefMist deleted the ux/trust-protocol-fee-controller branch January 22, 2025 02:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants